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"
|
2021-10-06 11:59:08 +00:00
|
|
|
"testing"
|
|
|
|
|
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"
|
2021-11-29 12:21:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2021-10-19 10:04:10 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/container"
|
2021-10-16 09:57:45 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-10-19 13:47:03 +00:00
|
|
|
require.Equal(t, 1, len(aer.Events))
|
|
|
|
require.Equal(t, "AddPeer", aer.Events[0].Name)
|
2022-01-17 10:51:11 +00:00
|
|
|
require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo.raw)}),
|
2021-10-19 13:47:03 +00:00
|
|
|
aer.Events[0].Item)
|
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)
|
2021-10-19 13:47:03 +00:00
|
|
|
require.Equal(t, 1, len(aer.Events))
|
|
|
|
require.Equal(t, "AddPeer", aer.Events[0].Name)
|
2022-01-17 10:51:11 +00:00
|
|
|
require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo.raw)}),
|
2021-10-19 13:47:03 +00:00
|
|
|
aer.Events[0].Item)
|
2021-12-02 15:59:29 +00:00
|
|
|
|
2022-01-17 10:51:11 +00:00
|
|
|
c.InvokeFail(t, common.ErrWitnessFailed, "addPeer", dummyInfo.raw)
|
|
|
|
c.Invoke(t, stackitem.Null{}, "register", dummyInfo.raw)
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
2021-11-15 06:29:08 +00:00
|
|
|
|
|
|
|
func TestUpdateState(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)
|
|
|
|
cAcc := cNm.WithSigners(acc)
|
2021-11-15 06:29:08 +00:00
|
|
|
dummyInfo := dummyNodeInfo(acc)
|
|
|
|
|
2022-03-10 12:01:10 +00:00
|
|
|
cAcc.Invoke(t, stackitem.Null{}, "addPeer", dummyInfo.raw)
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "register", dummyInfo.raw)
|
2021-11-15 06:29:08 +00:00
|
|
|
|
2021-11-17 08:20:13 +00:00
|
|
|
pub, ok := vm.ParseSignatureContract(acc.Script())
|
|
|
|
require.True(t, ok)
|
2021-11-15 06:29:08 +00:00
|
|
|
|
2021-11-17 08:20:13 +00:00
|
|
|
t.Run("missing witness", func(t *testing.T) {
|
2021-11-29 12:21:40 +00:00
|
|
|
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed,
|
2022-03-10 12:01:10 +00:00
|
|
|
"updateStateIR", int64(2), pub)
|
|
|
|
cNm.InvokeFail(t, common.ErrWitnessFailed,
|
2021-11-17 08:20:13 +00:00
|
|
|
"updateState", int64(2), pub)
|
2021-11-15 06:29:08 +00:00
|
|
|
})
|
|
|
|
|
2022-03-10 12:01:10 +00:00
|
|
|
h := cAcc.Invoke(t, stackitem.Null{}, "updateState", int64(2), pub)
|
|
|
|
aer := cAcc.CheckHalt(t, h)
|
|
|
|
require.Equal(t, 1, len(aer.Events))
|
|
|
|
require.Equal(t, "UpdateState", aer.Events[0].Name)
|
|
|
|
require.Equal(t, stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewBigInteger(big.NewInt(2)),
|
|
|
|
stackitem.NewByteArray(pub),
|
|
|
|
}), aer.Events[0].Item)
|
|
|
|
|
|
|
|
// Check that updating happens only after `updateState` is called by the alphabet.
|
|
|
|
s, err := cAcc.TestInvoke(t, "netmapCandidates")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, s.Len())
|
|
|
|
|
|
|
|
arr, ok := s.Pop().Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 1, len(arr))
|
|
|
|
|
|
|
|
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(2), pub)
|
|
|
|
|
2021-11-17 08:20:13 +00:00
|
|
|
cAcc.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "netmapCandidates")
|
2021-11-15 06:29:08 +00:00
|
|
|
}
|