[#146] netmap: allow to update peer info

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-16 12:57:45 +03:00 committed by Alex Vanin
parent 7fe7188e7d
commit a72392f672
3 changed files with 64 additions and 6 deletions

View file

@ -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))
}

View file

@ -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.

View file

@ -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)
}