rpc: use non-native NNS instead of native NNS in testchain

This commit is contained in:
Anna Shaleva 2021-05-14 19:38:04 +03:00
parent 4f1c50373f
commit 7180b2ce06
7 changed files with 93 additions and 61 deletions

View file

@ -227,7 +227,10 @@ func TestCreateBasicChain(t *testing.T) {
}
func initBasicChain(t *testing.T, bc *Blockchain) {
const prefix = "../rpc/server/testdata/"
const (
prefix = "../rpc/server/testdata/"
examplesPrefix = "../../examples/"
)
// Increase in case if you need more blocks
const validUntilBlock = 1200
@ -435,21 +438,36 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
require.NoError(t, addNetworkFee(bc, txDeploy3, acc0))
require.NoError(t, acc0.SignTx(testchain.Network(), txDeploy3))
b = bc.newBlock(txDeploy3)
require.NoError(t, bc.AddBlock(b))
require.NoError(t, bc.AddBlock(b)) // block #10
checkTxHalt(t, bc, txDeploy3.Hash())
// register `neo.com` with A record type and priv0 owner via NNS
transferFundsToCommittee(t, bc) // block #11
// Push NameService contract into the chain.
nsPath := examplesPrefix + "nft-nd-nns/"
nsConfigPath := nsPath + "nns.yml"
txDeploy4, _ := newDeployTx(t, bc, priv0ScriptHash, nsPath, nsPath, &nsConfigPath)
txDeploy4.Nonce = getNextNonce()
txDeploy4.ValidUntilBlock = validUntilBlock
require.NoError(t, addNetworkFee(bc, txDeploy4, acc0))
require.NoError(t, acc0.SignTx(testchain.Network(), txDeploy4))
b = bc.newBlock(txDeploy4)
require.NoError(t, bc.AddBlock(b)) // block #11
checkTxHalt(t, bc, txDeploy4.Hash())
nsHash, err := bc.GetContractScriptHash(4)
require.NoError(t, err)
t.Logf("contract (%s): \n\tHash: %s\n", nsPath, nsHash.StringLE())
// register `neo.com` with A record type and priv0 owner via NS
transferFundsToCommittee(t, bc) // block #12
res, err := invokeContractMethodGeneric(bc, defaultNameServiceSysfee,
bc.contracts.NameService.Hash, "addRoot", true, "com") // block #12
nsHash, "addRoot", true, "com") // block #13
require.NoError(t, err)
checkResult(t, res, stackitem.Null{})
res, err = invokeContractMethodGeneric(bc, native.DefaultDomainPrice+defaultNameServiceSysfee,
bc.contracts.NameService.Hash, "register", acc0, "neo.com", priv0ScriptHash) // block #13
res, err = invokeContractMethodGeneric(bc, defaultNameServiceDomainPrice+defaultNameServiceSysfee+1_0000_000,
nsHash, "register", acc0, "neo.com", priv0ScriptHash) // block #14
require.NoError(t, err)
checkResult(t, res, stackitem.NewBool(true))
res, err = invokeContractMethodGeneric(bc, defaultNameServiceSysfee, bc.contracts.NameService.Hash,
"setRecord", acc0, "neo.com", int64(nnsrecords.A), "1.2.3.4") // block #14
res, err = invokeContractMethodGeneric(bc, defaultNameServiceSysfee, nsHash,
"setRecord", acc0, "neo.com", int64(nnsrecords.A), "1.2.3.4") // block #15
require.NoError(t, err)
checkResult(t, res, stackitem.Null{})
@ -471,8 +489,16 @@ func newNEP17Transfer(sc, from, to util.Uint160, amount int64, additionalArgs ..
func newDeployTx(t *testing.T, bc *Blockchain, sender util.Uint160, name, ctrName string, cfgName *string) (*transaction.Transaction, util.Uint160) {
c, err := ioutil.ReadFile(name)
require.NoError(t, err)
tx, h, avm, err := testchain.NewDeployTx(bc, ctrName, sender, bytes.NewReader(c), cfgName)
var (
tx *transaction.Transaction
h util.Uint160
avm []byte
)
if err == nil {
tx, h, avm, err = testchain.NewDeployTx(bc, ctrName, sender, bytes.NewReader(c), cfgName)
} else {
tx, h, avm, err = testchain.NewDeployTx(bc, ctrName, sender, nil, cfgName)
}
require.NoError(t, err)
t.Logf("contract (%s): \n\tHash: %s\n\tAVM: %s", name, h.StringLE(), base64.StdEncoding.EncodeToString(avm))
return tx, h

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/nnsrecords"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
@ -24,7 +23,7 @@ func TestNameService_Price(t *testing.T) {
bc := newTestChain(t)
testGetSet(t, bc, bc.contracts.NameService.Hash, "Price",
native.DefaultDomainPrice, 1, 10000_00000000)
defaultNameServiceDomainPrice, 1, 10000_00000000)
}
func TestNonfungible(t *testing.T) {
@ -139,7 +138,7 @@ func TestRegisterAndRenew(t *testing.T) {
testNameServiceInvoke(t, bc, "register", nil, "\nneo.com'", testchain.CommitteeScriptHash())
testNameServiceInvoke(t, bc, "register", nil, "neo.com\n", testchain.CommitteeScriptHash())
testNameServiceInvoke(t, bc, "register", nil, "neo.com", testchain.CommitteeScriptHash())
testNameServiceInvokeAux(t, bc, native.DefaultDomainPrice, true, "register",
testNameServiceInvokeAux(t, bc, defaultNameServiceDomainPrice, true, "register",
nil, "neo.com", testchain.CommitteeScriptHash())
testNameServiceInvoke(t, bc, "isAvailable", true, "neo.com")
@ -375,8 +374,9 @@ func TestResolve(t *testing.T) {
}
const (
defaultNameServiceSysfee = 4000_0000
defaultRegisterSysfee = 10_0000_0000 + native.DefaultDomainPrice
defaultNameServiceDomainPrice = 10_0000_0000
defaultNameServiceSysfee = 4000_0000
defaultRegisterSysfee = 10_0000_0000 + defaultNameServiceDomainPrice
)
func testNameServiceInvoke(t *testing.T, bc *Blockchain, method string, result interface{}, args ...interface{}) {

View file

@ -11,6 +11,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// GetOraclePrice invokes `getPrice` method on a native Oracle contract.
@ -22,12 +23,8 @@ func (c *Client) GetOraclePrice() (int64, error) {
return c.invokeNativeGetMethod(oracleHash, "getPrice")
}
// GetNNSPrice invokes `getPrice` method on a native NameService contract.
func (c *Client) GetNNSPrice() (int64, error) {
nnsHash, err := c.GetNativeContractHash(nativenames.NameService)
if err != nil {
return 0, fmt.Errorf("failed to get native NameService hash: %w", err)
}
// GetNNSPrice invokes `getPrice` method on a NeoNameService contract with the specified hash.
func (c *Client) GetNNSPrice(nnsHash util.Uint160) (int64, error) {
return c.invokeNativeGetMethod(nnsHash, "getPrice")
}
@ -66,16 +63,12 @@ func (c *Client) GetDesignatedByRole(role noderoles.Role, index uint32) (keys.Pu
return topPublicKeysFromStack(result.Stack)
}
// NNSResolve invokes `resolve` method on a native NameService contract.
func (c *Client) NNSResolve(name string, typ nnsrecords.Type) (string, error) {
// NNSResolve invokes `resolve` method on a NameService contract with the specified hash.
func (c *Client) NNSResolve(nnsHash util.Uint160, name string, typ nnsrecords.Type) (string, error) {
if typ == nnsrecords.CNAME {
return "", errors.New("can't resolve CNAME record type")
}
rmHash, err := c.GetNativeContractHash(nativenames.NameService)
if err != nil {
return "", fmt.Errorf("failed to get native NameService hash: %w", err)
}
result, err := c.InvokeFunction(rmHash, "resolve", []smartcontract.Parameter{
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: name,
@ -95,13 +88,9 @@ func (c *Client) NNSResolve(name string, typ nnsrecords.Type) (string, error) {
return topStringFromStack(result.Stack)
}
// NNSIsAvailable invokes `isAvailable` method on a native NameService contract.
func (c *Client) NNSIsAvailable(name string) (bool, error) {
rmHash, err := c.GetNativeContractHash(nativenames.NameService)
if err != nil {
return false, fmt.Errorf("failed to get native NameService hash: %w", err)
}
result, err := c.InvokeFunction(rmHash, "isAvailable", []smartcontract.Parameter{
// NNSIsAvailable invokes `isAvailable` method on a NeoNameService contract with the specified hash.
func (c *Client) NNSIsAvailable(nnsHash util.Uint160, name string) (bool, error) {
result, err := c.InvokeFunction(nnsHash, "isAvailable", []smartcontract.Parameter{
{
Type: smartcontract.StringType,
Value: name,

View file

@ -458,7 +458,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetNNSPrice()
return c.GetNNSPrice(util.Uint160{1, 2, 3})
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"EMAMDWdldEZlZVBlckJ5dGUMFJphpG7sl7iTBtfOgfFbRiCR0AkyQWJ9W1I=","stack":[{"type":"Integer","value":"1000000"}],"tx":null}}`,
result: func(c *Client) interface{} {

View file

@ -784,7 +784,7 @@ func TestClient_NEP11(t *testing.T) {
require.NoError(t, err)
require.NoError(t, c.Init())
h, err := chain.GetNativeContractScriptHash(nativenames.NameService)
h, err := util.Uint160DecodeStringLE(nameServiceContractHash)
require.NoError(t, err)
acc := testchain.PrivateKeyByID(0).GetScriptHash()
@ -807,7 +807,7 @@ func TestClient_NEP11(t *testing.T) {
tok, err := c.NEP11TokenInfo(h)
require.NoError(t, err)
require.Equal(t, &wallet.Token{
Name: nativenames.NameService,
Name: "NameService",
Hash: h,
Decimals: 0,
Symbol: "NNS",
@ -827,12 +827,12 @@ func TestClient_NEP11(t *testing.T) {
t.Run("Properties", func(t *testing.T) {
p, err := c.NEP11Properties(h, "neo.com")
require.NoError(t, err)
blockRegisterDomain, err := chain.GetBlock(chain.GetHeaderHash(13)) // `neo.com` domain was registered in 13th block
blockRegisterDomain, err := chain.GetBlock(chain.GetHeaderHash(14)) // `neo.com` domain was registered in 14th block
require.NoError(t, err)
require.Equal(t, 1, len(blockRegisterDomain.Transactions))
expected := stackitem.NewMap()
expected.Add(stackitem.Make([]byte("name")), stackitem.Make([]byte("neo.com")))
expected.Add(stackitem.Make([]byte("expiration")), stackitem.Make(blockRegisterDomain.Timestamp/1000+365*24*3600)) // expiration formula
expected.Add(stackitem.Make([]byte("expiration")), stackitem.Make(blockRegisterDomain.Timestamp+365*24*3600*1000)) // expiration formula
require.EqualValues(t, expected, p)
})
t.Run("Transfer", func(t *testing.T) {
@ -850,27 +850,30 @@ func TestClient_NNS(t *testing.T) {
require.NoError(t, err)
require.NoError(t, c.Init())
nsHash, err := util.Uint160DecodeStringLE(nameServiceContractHash)
require.NoError(t, err)
t.Run("NNSIsAvailable, false", func(t *testing.T) {
b, err := c.NNSIsAvailable("neo.com")
b, err := c.NNSIsAvailable(nsHash, "neo.com")
require.NoError(t, err)
require.Equal(t, false, b)
})
t.Run("NNSIsAvailable, true", func(t *testing.T) {
b, err := c.NNSIsAvailable("neogo.com")
b, err := c.NNSIsAvailable(nsHash, "neogo.com")
require.NoError(t, err)
require.Equal(t, true, b)
})
t.Run("NNSResolve, good", func(t *testing.T) {
b, err := c.NNSResolve("neo.com", nnsrecords.A)
b, err := c.NNSResolve(nsHash, "neo.com", nnsrecords.A)
require.NoError(t, err)
require.Equal(t, "1.2.3.4", b)
})
t.Run("NNSResolve, bad", func(t *testing.T) {
_, err := c.NNSResolve("neogo.com", nnsrecords.A)
_, err := c.NNSResolve(nsHash, "neogo.com", nnsrecords.A)
require.Error(t, err)
})
t.Run("NNSResolve, forbidden", func(t *testing.T) {
_, err := c.NNSResolve("neogo.com", nnsrecords.CNAME)
_, err := c.NNSResolve(nsHash, "neogo.com", nnsrecords.CNAME)
require.Error(t, err)
})
}

View file

@ -55,13 +55,14 @@ type rpcTestCase struct {
}
const testContractHash = "63cc6571e990dd3f345f699fc9c2a6e49edb89af"
const deploymentTxHash = "4450d0047d4b6a20e85176c709df44fae4c63cfa9a698acb11871554b93016df"
const deploymentTxHash = "9b0c586eb07f8c9b6fc46b05c78d87651d50af8e1f44478827848d826f8cd174"
const genesisBlockHash = "73fe50b5564d57118296cbab0a78fe7cb11c97b7699d07a9a21fab60e79bb8fc"
const verifyContractHash = "c50082e0d8364d61ce6933bd24027a3363474dce"
const verifyContractAVM = "VwMAQS1RCDAhcAwU7p6iLCfjS9AUj8QQjgj3To9QSLLbMHFoE87bKGnbKJdA"
const verifyWithArgsContractHash = "8744ffdd07af8e9f18ab90685c8c2ebfd37c6415"
const invokescriptContractAVM = "VwcADBQBDAMOBQYMDQIODw0DDgcJAAAAANswcGhB+CfsjCGqJgQRQAwUDQ8DAgkAAgEDBwMEBQIBAA4GDAnbMHFpQfgn7IwhqiYEEkATQA=="
const nameServiceContractHash = "60d78a0fc048399438c3764f8a67d0fc86d6e0e6"
var rpcTestCases = map[string][]rpcTestCase{
"getapplicationlog": {
@ -648,7 +649,7 @@ var rpcTestCases = map[string][]rpcTestCase{
require.True(t, ok)
expected := result.UnclaimedGas{
Address: testchain.MultisigScriptHash(),
Unclaimed: *big.NewInt(7000),
Unclaimed: *big.NewInt(7500),
}
assert.Equal(t, expected, *actual)
},
@ -1407,7 +1408,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
require.NoErrorf(t, err, "could not parse response: %s", txOut)
assert.Equal(t, *block.Transactions[0], actual.Transaction)
assert.Equal(t, 15, actual.Confirmations)
assert.Equal(t, 16, actual.Confirmations)
assert.Equal(t, TXHash, actual.Transaction.Hash())
})
@ -1525,12 +1526,12 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
require.NoError(t, json.Unmarshal(res, actual))
checkNep17TransfersAux(t, e, actual, sent, rcvd)
}
t.Run("time frame only", func(t *testing.T) { testNEP17T(t, 4, 5, 0, 0, []int{8, 9, 10, 11}, []int{2, 3}) })
t.Run("time frame only", func(t *testing.T) { testNEP17T(t, 4, 5, 0, 0, []int{9, 10, 11, 12}, []int{2, 3}) })
t.Run("no res", func(t *testing.T) { testNEP17T(t, 100, 100, 0, 0, []int{}, []int{}) })
t.Run("limit", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 0, []int{5, 6}, []int{1}) })
t.Run("limit 2", func(t *testing.T) { testNEP17T(t, 4, 5, 2, 0, []int{8}, []int{2}) })
t.Run("limit with page", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 1, []int{7, 8}, []int{2}) })
t.Run("limit with page 2", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 2, []int{9, 10}, []int{3}) })
t.Run("limit", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 0, []int{6, 7}, []int{1}) })
t.Run("limit 2", func(t *testing.T) { testNEP17T(t, 4, 5, 2, 0, []int{9}, []int{2}) })
t.Run("limit with page", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 1, []int{8, 9}, []int{2}) })
t.Run("limit with page 2", func(t *testing.T) { testNEP17T(t, 1, 7, 3, 2, []int{10, 11}, []int{3}) })
})
}
@ -1633,8 +1634,8 @@ func checkNep17Balances(t *testing.T, e *executor, acc interface{}) {
},
{
Asset: e.chain.UtilityTokenHash(),
Amount: "67960000780",
LastUpdated: 14,
Amount: "57941360260",
LastUpdated: 15,
}},
Address: testchain.PrivateKeyByID(0).GetScriptHash().StringLE(),
}
@ -1643,7 +1644,7 @@ func checkNep17Balances(t *testing.T, e *executor, acc interface{}) {
}
func checkNep17Transfers(t *testing.T, e *executor, acc interface{}) {
checkNep17TransfersAux(t, e, acc, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, []int{0, 1, 2, 3, 4, 5, 6, 7})
checkNep17TransfersAux(t, e, acc, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, []int{0, 1, 2, 3, 4, 5, 6, 7})
}
func checkNep17TransfersAux(t *testing.T, e *executor, acc interface{}, sent, rcvd []int) {
@ -1652,12 +1653,12 @@ func checkNep17TransfersAux(t *testing.T, e *executor, acc interface{}, sent, rc
rublesHash, err := util.Uint160DecodeStringLE(testContractHash)
require.NoError(t, err)
blockSetRecord, err := e.chain.GetBlock(e.chain.GetHeaderHash(14)) // add type A record to `neo.com` domain via NNS
blockSetRecord, err := e.chain.GetBlock(e.chain.GetHeaderHash(15)) // add type A record to `neo.com` domain via NNS
require.NoError(t, err)
require.Equal(t, 1, len(blockSetRecord.Transactions))
txSetRecord := blockSetRecord.Transactions[0]
blockRegisterDomain, err := e.chain.GetBlock(e.chain.GetHeaderHash(13)) // register `neo.com` domain via NNS
blockRegisterDomain, err := e.chain.GetBlock(e.chain.GetHeaderHash(14)) // register `neo.com` domain via NNS
require.NoError(t, err)
require.Equal(t, 1, len(blockRegisterDomain.Transactions))
txRegisterDomain := blockRegisterDomain.Transactions[0]
@ -1665,6 +1666,11 @@ func checkNep17TransfersAux(t *testing.T, e *executor, acc interface{}, sent, rc
blockGASBounty2, err := e.chain.GetBlock(e.chain.GetHeaderHash(12)) // size of committee = 6
require.NoError(t, err)
blockDeploy4, err := e.chain.GetBlock(e.chain.GetHeaderHash(11)) // deploy ns.go (non-native neo name service contract)
require.NoError(t, err)
require.Equal(t, 1, len(blockDeploy4.Transactions))
txDeploy4 := blockDeploy4.Transactions[0]
blockDeploy3, err := e.chain.GetBlock(e.chain.GetHeaderHash(10)) // deploy verification_with_args_contract.go
require.NoError(t, err)
require.Equal(t, 1, len(blockDeploy3.Transactions))
@ -1728,7 +1734,7 @@ func checkNep17TransfersAux(t *testing.T, e *executor, acc interface{}, sent, rc
Asset: e.chain.UtilityTokenHash(),
Address: "", // burn
Amount: big.NewInt(txSetRecord.SystemFee + txSetRecord.NetworkFee).String(),
Index: 14,
Index: 15,
TxHash: blockSetRecord.Hash(),
},
{
@ -1736,9 +1742,17 @@ func checkNep17TransfersAux(t *testing.T, e *executor, acc interface{}, sent, rc
Asset: e.chain.UtilityTokenHash(),
Address: "", // burn
Amount: big.NewInt(txRegisterDomain.SystemFee + txRegisterDomain.NetworkFee).String(),
Index: 13,
Index: 14,
TxHash: blockRegisterDomain.Hash(),
},
{
Timestamp: blockDeploy4.Timestamp,
Asset: e.chain.UtilityTokenHash(),
Address: "", // burn
Amount: big.NewInt(txDeploy4.SystemFee + txDeploy4.NetworkFee).String(),
Index: 11,
TxHash: blockDeploy4.Hash(),
},
{
Timestamp: blockDeploy3.Timestamp,
Asset: e.chain.UtilityTokenHash(),

Binary file not shown.