2021-10-06 11:59:08 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2021-10-19 07:33:42 +00:00
|
|
|
"math/big"
|
2021-10-16 09:57:45 +00:00
|
|
|
"math/rand"
|
2021-10-26 11:28:14 +00:00
|
|
|
"path"
|
2022-03-10 11:29:24 +00:00
|
|
|
"strings"
|
2021-10-06 11:59:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-12-12 20:01:38 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-contract/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-contract/container"
|
|
|
|
"github.com/TrueCloudLab/frostfs-contract/netmap"
|
2021-10-19 10:04:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2021-10-26 11:28:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
2021-10-06 11:59:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-10-26 11:28:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2021-10-16 09:57:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-06 11:59:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const netmapPath = "../netmap"
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func deployNetmapContract(t *testing.T, e *neotest.Executor, addrBalance, addrContainer util.Uint160, config ...interface{}) util.Uint160 {
|
|
|
|
_, pubs, ok := vm.ParseMultiSigContract(e.Committee.Script())
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-10-06 11:59:08 +00:00
|
|
|
args := make([]interface{}, 5)
|
|
|
|
args[0] = false
|
|
|
|
args[1] = addrBalance
|
|
|
|
args[2] = addrContainer
|
2021-10-26 11:28:14 +00:00
|
|
|
args[3] = []interface{}{pubs[0]}
|
2021-10-06 11:59:08 +00:00
|
|
|
args[4] = append([]interface{}{}, config...)
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
c := neotest.CompileFile(t, e.CommitteeHash, netmapPath, path.Join(netmapPath, "config.yml"))
|
|
|
|
e.DeployContract(t, c, args)
|
|
|
|
return c.Hash
|
|
|
|
}
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
func newNetmapInvoker(t *testing.T, config ...interface{}) *neotest.ContractInvoker {
|
|
|
|
e := newExecutor(t)
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
ctrNNS := neotest.CompileFile(t, e.CommitteeHash, nnsPath, path.Join(nnsPath, "config.yml"))
|
|
|
|
ctrNetmap := neotest.CompileFile(t, e.CommitteeHash, netmapPath, path.Join(netmapPath, "config.yml"))
|
|
|
|
ctrBalance := neotest.CompileFile(t, e.CommitteeHash, balancePath, path.Join(balancePath, "config.yml"))
|
|
|
|
ctrContainer := neotest.CompileFile(t, e.CommitteeHash, containerPath, path.Join(containerPath, "config.yml"))
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
e.DeployContract(t, ctrNNS, nil)
|
|
|
|
deployContainerContract(t, e, ctrNetmap.Hash, ctrBalance.Hash, ctrNNS.Hash)
|
|
|
|
deployBalanceContract(t, e, ctrNetmap.Hash, ctrContainer.Hash)
|
|
|
|
deployNetmapContract(t, e, ctrBalance.Hash, ctrContainer.Hash, config...)
|
|
|
|
return e.CommitteeInvoker(ctrNetmap.Hash)
|
2021-10-19 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploySetConfig(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNetmapInvoker(t, "SomeKey", "TheValue", container.AliasFeeKey, int64(123))
|
|
|
|
c.Invoke(t, "TheValue", "config", "SomeKey")
|
|
|
|
c.Invoke(t, stackitem.NewByteArray(bigint.ToBytes(big.NewInt(123))),
|
|
|
|
"config", container.AliasFeeKey)
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
type testNodeInfo struct {
|
|
|
|
signer neotest.SingleSigner
|
|
|
|
pub []byte
|
|
|
|
raw []byte
|
2022-09-28 07:41:51 +00:00
|
|
|
state netmap.NodeState
|
2022-01-17 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dummyNodeInfo(acc neotest.Signer) testNodeInfo {
|
2021-10-16 09:57:45 +00:00
|
|
|
ni := make([]byte, 66)
|
|
|
|
rand.Read(ni)
|
2021-10-26 11:28:14 +00:00
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
s := acc.(neotest.SingleSigner)
|
|
|
|
pub := s.Account().PrivateKey().PublicKey().Bytes()
|
2021-10-26 11:28:14 +00:00
|
|
|
copy(ni[2:], pub)
|
2022-01-17 10:51:11 +00:00
|
|
|
return testNodeInfo{
|
|
|
|
signer: s,
|
|
|
|
pub: pub,
|
|
|
|
raw: ni,
|
2022-09-28 07:41:51 +00:00
|
|
|
state: netmap.NodeStateOnline,
|
2022-01-17 10:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStorageNode(t *testing.T, c *neotest.ContractInvoker) testNodeInfo {
|
|
|
|
return dummyNodeInfo(c.NewAccount(t))
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPeer(t *testing.T) {
|
2021-10-26 11:28:14 +00:00
|
|
|
c := newNetmapInvoker(t)
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc := c.NewAccount(t)
|
|
|
|
cAcc := c.WithSigners(acc)
|
2021-10-16 09:57:45 +00:00
|
|
|
dummyInfo := dummyNodeInfo(acc)
|
|
|
|
|
2021-10-26 11:28:14 +00:00
|
|
|
acc1 := c.NewAccount(t)
|
|
|
|
cAcc1 := c.WithSigners(acc1)
|
2022-01-17 10:51:11 +00:00
|
|
|
cAcc1.InvokeFail(t, common.ErrWitnessFailed, "addPeer", dummyInfo.raw)
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
h := cAcc.Invoke(t, stackitem.Null{}, "addPeer", dummyInfo.raw)
|
2021-10-26 11:28:14 +00:00
|
|
|
aer := cAcc.CheckHalt(t, h)
|
2022-03-17 13:02:56 +00:00
|
|
|
require.Equal(t, 0, len(aer.Events))
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
dummyInfo.raw[0] ^= 0xFF
|
|
|
|
h = cAcc.Invoke(t, stackitem.Null{}, "addPeer", dummyInfo.raw)
|
2021-10-26 11:28:14 +00:00
|
|
|
aer = cAcc.CheckHalt(t, h)
|
2022-03-17 13:02:56 +00:00
|
|
|
require.Equal(t, 0, len(aer.Events))
|
2021-12-02 15:59:29 +00:00
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
c.InvokeFail(t, common.ErrWitnessFailed, "addPeer", dummyInfo.raw)
|
2022-03-11 09:19:35 +00:00
|
|
|
c.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo.raw)
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
2021-11-15 06:29:08 +00:00
|
|
|
|
2022-03-10 11:29:24 +00:00
|
|
|
func TestNewEpoch(t *testing.T) {
|
|
|
|
rand.Seed(42)
|
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
const epochCount = netmap.DefaultSnapshotCount * 2
|
2022-03-10 11:29:24 +00:00
|
|
|
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
nodes := make([][]testNodeInfo, epochCount)
|
|
|
|
for i := range nodes {
|
|
|
|
size := rand.Int()%5 + 1
|
|
|
|
arr := make([]testNodeInfo, size)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
arr[j] = newStorageNode(t, cNm)
|
|
|
|
}
|
|
|
|
nodes[i] = arr
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < epochCount; i++ {
|
|
|
|
for _, tn := range nodes[i] {
|
|
|
|
cNm.WithSigners(tn.signer).Invoke(t, stackitem.Null{}, "addPeer", tn.raw)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", tn.raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
// Remove random nodes from the previous netmap.
|
|
|
|
current := make([]testNodeInfo, 0, len(nodes[i])+len(nodes[i-1]))
|
|
|
|
current = append(current, nodes[i]...)
|
|
|
|
|
|
|
|
for j := range nodes[i-1] {
|
|
|
|
if rand.Int()%3 == 0 {
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR",
|
2022-09-28 07:41:51 +00:00
|
|
|
int64(netmap.NodeStateOffline), nodes[i-1][j].pub)
|
2022-03-10 11:29:24 +00:00
|
|
|
} else {
|
|
|
|
current = append(current, nodes[i-1][j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes[i] = current
|
|
|
|
}
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "newEpoch", i+1)
|
|
|
|
|
|
|
|
t.Logf("Epoch: %d, Netmap()", i)
|
|
|
|
s, err := cNm.TestInvoke(t, "netmap")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes[i])
|
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
for j := 0; j <= i && j < netmap.DefaultSnapshotCount; j++ {
|
2022-03-10 11:29:24 +00:00
|
|
|
t.Logf("Epoch: %d, diff: %d", i, j)
|
2022-03-28 06:39:22 +00:00
|
|
|
checkSnapshotAt(t, j, cNm, nodes[i-j])
|
2022-03-10 11:29:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", netmap.DefaultSnapshotCount)
|
2022-03-10 11:29:24 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "incorrect diff"))
|
|
|
|
|
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", -1)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "incorrect diff"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 06:39:22 +00:00
|
|
|
func TestUpdateSnapshotCount(t *testing.T) {
|
|
|
|
rand.Seed(42)
|
|
|
|
|
|
|
|
require.True(t, netmap.DefaultSnapshotCount > 5) // sanity check, adjust tests if false.
|
|
|
|
|
|
|
|
prepare := func(t *testing.T, cNm *neotest.ContractInvoker, epochCount int) [][]testNodeInfo {
|
|
|
|
nodes := make([][]testNodeInfo, epochCount)
|
|
|
|
nodes[0] = []testNodeInfo{newStorageNode(t, cNm)}
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", nodes[0][0].raw)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "newEpoch", 1)
|
|
|
|
for i := 1; i < len(nodes); i++ {
|
|
|
|
sn := newStorageNode(t, cNm)
|
|
|
|
nodes[i] = append(nodes[i-1], sn)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", sn.raw)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "newEpoch", i+1)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("increase size, extend with nil", func(t *testing.T) {
|
|
|
|
// Before: S-old .. S
|
|
|
|
// After : S-old .. S nil nil ...
|
|
|
|
const epochCount = netmap.DefaultSnapshotCount / 2
|
|
|
|
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
nodes := prepare(t, cNm, epochCount)
|
|
|
|
|
|
|
|
const newCount = netmap.DefaultSnapshotCount + 3
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateSnapshotCount", newCount)
|
|
|
|
|
|
|
|
s, err := cNm.TestInvoke(t, "netmap")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes[epochCount-1])
|
|
|
|
for i := 0; i < epochCount; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nodes[epochCount-i-1])
|
|
|
|
}
|
|
|
|
for i := epochCount; i < newCount; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nil)
|
|
|
|
}
|
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", int64(newCount))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("increase size, copy old snapshots", func(t *testing.T) {
|
|
|
|
// Before: S-x .. S S-old ...
|
|
|
|
// After : S-x .. S nil nil S-old ...
|
|
|
|
const epochCount = netmap.DefaultSnapshotCount + netmap.DefaultSnapshotCount/2
|
|
|
|
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
nodes := prepare(t, cNm, epochCount)
|
|
|
|
|
|
|
|
const newCount = netmap.DefaultSnapshotCount + 3
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateSnapshotCount", newCount)
|
|
|
|
|
|
|
|
s, err := cNm.TestInvoke(t, "netmap")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes[epochCount-1])
|
|
|
|
for i := 0; i < newCount-3; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nodes[epochCount-i-1])
|
|
|
|
}
|
|
|
|
for i := newCount - 3; i < newCount; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nil)
|
|
|
|
}
|
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", int64(newCount))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("decrease size, small decrease", func(t *testing.T) {
|
|
|
|
// Before: S-x .. S S-old ... ...
|
|
|
|
// After : S-x .. S S-new ...
|
|
|
|
const epochCount = netmap.DefaultSnapshotCount + netmap.DefaultSnapshotCount/2
|
|
|
|
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
nodes := prepare(t, cNm, epochCount)
|
|
|
|
|
|
|
|
const newCount = netmap.DefaultSnapshotCount/2 + 2
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateSnapshotCount", newCount)
|
|
|
|
|
|
|
|
s, err := cNm.TestInvoke(t, "netmap")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes[epochCount-1])
|
|
|
|
for i := 0; i < newCount; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nodes[epochCount-i-1])
|
|
|
|
}
|
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", int64(newCount))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("decrease size, big decrease", func(t *testing.T) {
|
|
|
|
// Before: S-x ... ... S S-old ... ...
|
|
|
|
// After : S-new ... S
|
|
|
|
const epochCount = netmap.DefaultSnapshotCount + netmap.DefaultSnapshotCount/2
|
|
|
|
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
nodes := prepare(t, cNm, epochCount)
|
|
|
|
|
|
|
|
const newCount = netmap.DefaultSnapshotCount/2 - 2
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateSnapshotCount", newCount)
|
|
|
|
|
|
|
|
s, err := cNm.TestInvoke(t, "netmap")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes[epochCount-1])
|
|
|
|
for i := 0; i < newCount; i++ {
|
|
|
|
checkSnapshotAt(t, i, cNm, nodes[epochCount-i-1])
|
|
|
|
}
|
|
|
|
_, err = cNm.TestInvoke(t, "snapshot", int64(newCount))
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSnapshotAt(t *testing.T, epoch int, cNm *neotest.ContractInvoker, nodes []testNodeInfo) {
|
|
|
|
s, err := cNm.TestInvoke(t, "snapshot", int64(epoch))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
checkSnapshot(t, s, nodes)
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:29:24 +00:00
|
|
|
func checkSnapshot(t *testing.T, s *vm.Stack, nodes []testNodeInfo) {
|
|
|
|
arr, ok := s.Pop().Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok, "expected array")
|
|
|
|
require.Equal(t, len(nodes), len(arr), "expected %d nodes", len(nodes))
|
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
actual := make([]netmap.Node, len(nodes))
|
|
|
|
expected := make([]netmap.Node, len(nodes))
|
2022-03-10 11:29:24 +00:00
|
|
|
for i := range nodes {
|
|
|
|
n, ok := arr[i].Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok, "expected node struct")
|
2022-09-28 07:41:51 +00:00
|
|
|
require.Equalf(t, 2, len(n), "expected %d field(s)", 2)
|
2022-03-10 11:29:24 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
require.IsType(t, []byte{}, n[0].Value())
|
2022-03-10 11:29:24 +00:00
|
|
|
|
2022-09-28 07:41:51 +00:00
|
|
|
state, err := n[1].TryInteger()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual[i].BLOB = n[0].Value().([]byte)
|
|
|
|
actual[i].State = netmap.NodeState(state.Int64())
|
|
|
|
expected[i].BLOB = nodes[i].raw
|
|
|
|
expected[i].State = nodes[i].state
|
2022-03-10 11:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require.ElementsMatch(t, expected, actual, "snapshot is different")
|
|
|
|
}
|
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
func TestUpdateStateIR(t *testing.T) {
|
2022-03-10 12:01:10 +00:00
|
|
|
cNm := newNetmapInvoker(t)
|
2021-11-15 06:29:08 +00:00
|
|
|
|
2022-03-10 12:01:10 +00:00
|
|
|
acc := cNm.NewAccount(t)
|
2022-09-20 09:47:14 +00:00
|
|
|
pub := acc.(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()
|
|
|
|
|
|
|
|
t.Run("can't move online, need addPeerIR", func(t *testing.T) {
|
2022-09-28 07:41:51 +00:00
|
|
|
cNm.InvokeFail(t, "peer is missing", "updateStateIR", int64(netmap.NodeStateOnline), pub)
|
2022-09-20 09:47:14 +00:00
|
|
|
})
|
|
|
|
|
2021-11-15 06:29:08 +00:00
|
|
|
dummyInfo := dummyNodeInfo(acc)
|
2022-03-11 09:19:35 +00:00
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo.raw)
|
2022-03-22 07:50:36 +00:00
|
|
|
|
2022-09-20 07:42:36 +00:00
|
|
|
acc1 := cNm.NewAccount(t)
|
|
|
|
dummyInfo1 := dummyNodeInfo(acc1)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo1.raw)
|
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
t.Run("must be signed by the alphabet", func(t *testing.T) {
|
|
|
|
cAcc := cNm.WithSigners(acc)
|
2022-09-28 07:41:51 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "updateStateIR", int64(netmap.NodeStateOffline), pub)
|
2022-09-20 07:42:36 +00:00
|
|
|
})
|
2022-03-22 07:50:36 +00:00
|
|
|
t.Run("invalid state", func(t *testing.T) {
|
|
|
|
cNm.InvokeFail(t, "unsupported state", "updateStateIR", int64(42), pub)
|
|
|
|
})
|
|
|
|
|
2022-09-20 07:42:36 +00:00
|
|
|
checkNetmapCandidates(t, cNm, 2)
|
|
|
|
|
|
|
|
// Move the first node offline.
|
2022-09-28 07:41:51 +00:00
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.NodeStateOffline), pub)
|
2022-03-22 07:50:36 +00:00
|
|
|
checkNetmapCandidates(t, cNm, 1)
|
2022-09-20 07:42:36 +00:00
|
|
|
|
2022-09-20 09:47:14 +00:00
|
|
|
checkState := func(expected netmap.NodeState) {
|
|
|
|
arr := checkNetmapCandidates(t, cNm, 1)
|
|
|
|
nn := arr[0].Value().([]stackitem.Item)
|
|
|
|
state, err := nn[1].TryInteger()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(expected), state.Int64())
|
|
|
|
}
|
|
|
|
|
2022-09-20 07:42:36 +00:00
|
|
|
// Move the second node in the maintenance state.
|
|
|
|
pub1 := acc1.(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()
|
2022-09-20 09:47:14 +00:00
|
|
|
t.Run("maintenance -> add peer", func(t *testing.T) {
|
2022-09-28 07:41:51 +00:00
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.NodeStateMaintenance), pub1)
|
|
|
|
checkState(netmap.NodeStateMaintenance)
|
2022-09-20 09:47:14 +00:00
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo1.raw)
|
2022-09-28 07:41:51 +00:00
|
|
|
checkState(netmap.NodeStateOnline)
|
2022-09-20 09:47:14 +00:00
|
|
|
})
|
|
|
|
t.Run("maintenance -> online", func(t *testing.T) {
|
2022-09-28 07:41:51 +00:00
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.NodeStateMaintenance), pub1)
|
|
|
|
checkState(netmap.NodeStateMaintenance)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.NodeStateOnline), pub1)
|
|
|
|
checkState(netmap.NodeStateOnline)
|
2022-09-20 09:47:14 +00:00
|
|
|
})
|
2022-03-22 07:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateState(t *testing.T) {
|
|
|
|
cNm := newNetmapInvoker(t)
|
|
|
|
|
|
|
|
accs := []neotest.Signer{cNm.NewAccount(t), cNm.NewAccount(t)}
|
|
|
|
pubs := make([][]byte, len(accs))
|
|
|
|
for i := range accs {
|
|
|
|
dummyInfo := dummyNodeInfo(accs[i])
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo.raw)
|
|
|
|
pubs[i] = accs[i].(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()
|
|
|
|
}
|
2021-11-15 06:29:08 +00:00
|
|
|
|
2021-11-17 08:20:13 +00:00
|
|
|
t.Run("missing witness", func(t *testing.T) {
|
2022-03-22 07:50:36 +00:00
|
|
|
cAcc := cNm.WithSigners(accs[0])
|
|
|
|
cNm.InvokeFail(t, common.ErrWitnessFailed, "updateState", int64(2), pubs[0])
|
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "updateState", int64(2), pubs[0])
|
|
|
|
cAcc.InvokeFail(t, common.ErrWitnessFailed, "updateState", int64(2), pubs[1])
|
2021-11-15 06:29:08 +00:00
|
|
|
})
|
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
checkNetmapCandidates(t, cNm, 2)
|
|
|
|
|
|
|
|
cBoth := cNm.WithSigners(accs[0], cNm.Committee)
|
|
|
|
|
|
|
|
cBoth.Invoke(t, stackitem.Null{}, "updateState", int64(2), pubs[0])
|
|
|
|
checkNetmapCandidates(t, cNm, 1)
|
2022-03-10 12:01:10 +00:00
|
|
|
|
2022-03-22 07:50:36 +00:00
|
|
|
t.Run("remove already removed node", func(t *testing.T) {
|
|
|
|
cBoth.Invoke(t, stackitem.Null{}, "updateState", int64(2), pubs[0])
|
|
|
|
checkNetmapCandidates(t, cNm, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
cBoth = cNm.WithSigners(accs[1], cNm.Committee)
|
|
|
|
cBoth.Invoke(t, stackitem.Null{}, "updateState", int64(2), pubs[1])
|
|
|
|
checkNetmapCandidates(t, cNm, 0)
|
|
|
|
}
|
|
|
|
|
2022-09-20 07:42:36 +00:00
|
|
|
func checkNetmapCandidates(t *testing.T, c *neotest.ContractInvoker, size int) []stackitem.Item {
|
2022-03-22 07:50:36 +00:00
|
|
|
s, err := c.TestInvoke(t, "netmapCandidates")
|
2022-03-10 12:01:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
|
|
|
|
arr, ok := s.Pop().Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
2022-03-22 07:50:36 +00:00
|
|
|
require.Equal(t, size, len(arr))
|
2022-09-20 07:42:36 +00:00
|
|
|
return arr
|
2021-11-15 06:29:08 +00:00
|
|
|
}
|