From a72392f6725f7bc2f4cd539b16d770117a143e9f Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 16 Oct 2021 12:57:45 +0300 Subject: [PATCH] [#146] netmap: allow to update peer info Signed-off-by: Evgenii Stratonikov --- netmap/netmap_contract.go | 6 +--- tests/basic.go | 4 ++- tests/netmap_test.go | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/netmap/netmap_contract.go b/netmap/netmap_contract.go index 7be60c1..fad4872 100644 --- a/netmap/netmap_contract.go +++ b/netmap/netmap_contract.go @@ -191,6 +191,7 @@ func UpdateInnerRing(keys []interop.PublicKey) { // by Alphabet node. If it was invoked by node candidate, it produces AddPeer // notification. Otherwise method throws panic. // +// If the candidate already exists, it's info is updated. // NodeInfo argument contains stable marshaled version of netmap.NodeInfo // structure. func AddPeer(nodeInfo []byte) { @@ -519,11 +520,6 @@ func addToNetmap(ctx storage.Context, n storageNode) { } ) - data := storage.Get(ctx, storageKey) - if data != nil { - return - } - storage.Put(ctx, storageKey, std.Serialize(node)) } diff --git a/tests/basic.go b/tests/basic.go index 6f36fa4..eb5e414 100644 --- a/tests/basic.go +++ b/tests/basic.go @@ -13,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/fee" "github.com/nspcc-dev/neo-go/pkg/core/native" + "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" @@ -138,13 +139,14 @@ func DeployContract(t *testing.T, bc *core.Blockchain, path string, data interfa } // CheckHalt checks that transaction persisted with HALT state. -func CheckHalt(t *testing.T, bc *core.Blockchain, h util.Uint256, stack ...stackitem.Item) { +func CheckHalt(t *testing.T, bc *core.Blockchain, h util.Uint256, stack ...stackitem.Item) *state.AppExecResult { aer, err := bc.GetAppExecResults(h, trigger.Application) require.NoError(t, err) require.Equal(t, vm.HaltState, aer[0].VMState, aer[0].FaultException) if len(stack) != 0 { require.Equal(t, stack, aer[0].Stack) } + return &aer[0] } // CheckFault checks that transaction persisted with FAULT state. diff --git a/tests/netmap_test.go b/tests/netmap_test.go index cf9d472..c93831b 100644 --- a/tests/netmap_test.go +++ b/tests/netmap_test.go @@ -1,10 +1,14 @@ package tests import ( + "math/rand" "testing" "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/stretchr/testify/require" ) const netmapPath = "../netmap" @@ -18,3 +22,59 @@ func deployNetmapContract(t *testing.T, bc *core.Blockchain, addrBalance, addrCo args[4] = append([]interface{}{}, config...) return DeployContract(t, bc, netmapPath, args) } + +func prepareNetmapContract(t *testing.T, bc *core.Blockchain) util.Uint160 { + addrNNS := DeployContract(t, bc, nnsPath, nil) + + ctrNetmap, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), netmapPath) + require.NoError(t, err) + + ctrBalance, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), balancePath) + require.NoError(t, err) + + ctrContainer, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), containerPath) + require.NoError(t, err) + + deployContainerContract(t, bc, ctrNetmap.Hash, ctrBalance.Hash, addrNNS) + deployBalanceContract(t, bc, ctrNetmap.Hash, ctrContainer.Hash) + return deployNetmapContract(t, bc, ctrBalance.Hash, ctrContainer.Hash, "ContainerFee", []byte{}) +} + +func dummyNodeInfo(acc *wallet.Account) []byte { + ni := make([]byte, 66) + rand.Read(ni) + copy(ni[2:], acc.PrivateKey().PublicKey().Bytes()) + return ni +} + +func TestAddPeer(t *testing.T) { + bc := NewChain(t) + h := prepareNetmapContract(t, bc) + + acc := NewAccount(t, bc) + dummyInfo := dummyNodeInfo(acc) + + acc1 := NewAccount(t, bc) + tx := PrepareInvoke(t, bc, acc1, h, "addPeer", dummyInfo) + AddBlock(t, bc, tx) + CheckFault(t, bc, tx.Hash(), "witness check failed") + + tx = PrepareInvoke(t, bc, acc, h, "addPeer", dummyInfo) + AddBlock(t, bc, tx) + + aer := CheckHalt(t, bc, tx.Hash()) + require.Equal(t, 1, len(aer.Events)) + require.Equal(t, "AddPeer", aer.Events[0].Name) + require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo)}), + aer.Events[0].Item) + + dummyInfo[0] ^= 0xFF + tx = PrepareInvoke(t, bc, acc, h, "addPeer", dummyInfo) + AddBlock(t, bc, tx) + + aer = CheckHalt(t, bc, tx.Hash()) + require.Equal(t, 1, len(aer.Events)) + require.Equal(t, "AddPeer", aer.Events[0].Name) + require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo)}), + aer.Events[0].Item) +}