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-06 11:59:08 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
2021-10-19 10:04:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2021-10-06 11:59:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-10-16 09:57:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
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"
|
|
|
|
|
|
|
|
func deployNetmapContract(t *testing.T, bc *core.Blockchain, addrBalance, addrContainer util.Uint160, config ...interface{}) util.Uint160 {
|
|
|
|
args := make([]interface{}, 5)
|
|
|
|
args[0] = false
|
|
|
|
args[1] = addrBalance
|
|
|
|
args[2] = addrContainer
|
|
|
|
args[3] = []interface{}{CommitteeAcc.PrivateKey().PublicKey().Bytes()}
|
|
|
|
args[4] = append([]interface{}{}, config...)
|
|
|
|
return DeployContract(t, bc, netmapPath, args)
|
|
|
|
}
|
2021-10-16 09:57:45 +00:00
|
|
|
|
2021-10-19 10:04:10 +00:00
|
|
|
func prepareNetmapContract(t *testing.T, bc *core.Blockchain, config ...interface{}) util.Uint160 {
|
2021-10-16 09:57:45 +00:00
|
|
|
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)
|
2021-10-19 10:04:10 +00:00
|
|
|
return deployNetmapContract(t, bc, ctrBalance.Hash, ctrContainer.Hash, config...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploySetConfig(t *testing.T) {
|
|
|
|
bc := NewChain(t)
|
|
|
|
h := prepareNetmapContract(t, bc, "SomeKey", "TheValue", container.AliasFeeKey, int64(123))
|
|
|
|
|
|
|
|
tx := PrepareInvoke(t, bc, CommitteeAcc, h, "config", "SomeKey")
|
|
|
|
CheckTestInvoke(t, bc, tx, "TheValue")
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, CommitteeAcc, h, "config", container.AliasFeeKey)
|
|
|
|
CheckTestInvoke(t, bc, tx, bigint.ToBytes(big.NewInt(123)))
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-10-19 07:33:42 +00:00
|
|
|
tx := PrepareInvoke(t, bc, CommitteeAcc, h, "addPeer", dummyInfo)
|
2021-10-16 09:57:45 +00:00
|
|
|
AddBlock(t, bc, tx)
|
2021-10-19 07:33:42 +00:00
|
|
|
CheckFault(t, bc, tx.Hash(), "addPeer: witness check failed")
|
2021-10-16 09:57:45 +00:00
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, acc, h, "addPeer", dummyInfo)
|
|
|
|
AddBlock(t, bc, tx)
|
2021-10-19 07:33:42 +00:00
|
|
|
CheckFault(t, bc, tx.Hash(), "addPeer: alphabet witness check failed")
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h, "addPeer", dummyInfo)
|
|
|
|
AddBlockCheckHalt(t, bc, tx)
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, acc, h, "netmapCandidates")
|
|
|
|
AddBlock(t, bc, tx)
|
|
|
|
|
|
|
|
checkNode := func(t *testing.T, info []byte, state int64, actual stackitem.Item) {
|
|
|
|
// Actual is netmap.netmapNode.
|
|
|
|
require.Equal(t, stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(dummyInfo),
|
|
|
|
}),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(state)),
|
|
|
|
}), actual)
|
|
|
|
}
|
2021-10-16 09:57:45 +00:00
|
|
|
|
|
|
|
aer := CheckHalt(t, bc, tx.Hash())
|
2021-10-19 07:33:42 +00:00
|
|
|
nodes, _ := aer.Stack[0].Value().([]stackitem.Item)
|
|
|
|
require.Equal(t, 1, len(nodes))
|
|
|
|
checkNode(t, dummyInfo, 1, nodes[0])
|
2021-10-16 09:57:45 +00:00
|
|
|
|
|
|
|
dummyInfo[0] ^= 0xFF
|
2021-10-19 07:33:42 +00:00
|
|
|
tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h, "addPeer", dummyInfo)
|
|
|
|
AddBlockCheckHalt(t, bc, tx)
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, acc, h, "netmapCandidates")
|
2021-10-16 09:57:45 +00:00
|
|
|
AddBlock(t, bc, tx)
|
|
|
|
|
|
|
|
aer = CheckHalt(t, bc, tx.Hash())
|
2021-10-19 07:33:42 +00:00
|
|
|
nodes, _ = aer.Stack[0].Value().([]stackitem.Item)
|
|
|
|
require.Equal(t, 1, len(nodes))
|
|
|
|
checkNode(t, dummyInfo, 1, nodes[0])
|
2021-10-16 09:57:45 +00:00
|
|
|
}
|
2021-10-19 07:42:23 +00:00
|
|
|
|
|
|
|
func TestUpdateState(t *testing.T) {
|
|
|
|
bc := NewChain(t)
|
|
|
|
h := prepareNetmapContract(t, bc)
|
|
|
|
|
|
|
|
acc := NewAccount(t, bc)
|
|
|
|
dummyInfo := dummyNodeInfo(acc)
|
|
|
|
|
|
|
|
tx := PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h, "addPeer", dummyInfo)
|
|
|
|
AddBlockCheckHalt(t, bc, tx)
|
|
|
|
|
|
|
|
t.Run("missing witness", func(t *testing.T) {
|
|
|
|
tx = PrepareInvoke(t, bc, acc, h, "updateState", int64(2), acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
AddBlock(t, bc, tx)
|
|
|
|
CheckFault(t, bc, tx.Hash(), "updateState: alphabet witness check failed")
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, CommitteeAcc, h, "updateState", int64(2), acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
AddBlock(t, bc, tx)
|
|
|
|
CheckFault(t, bc, tx.Hash(), "updateState: witness check failed")
|
|
|
|
})
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, []*wallet.Account{CommitteeAcc, acc}, h,
|
|
|
|
"updateState", int64(2), acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
AddBlockCheckHalt(t, bc, tx)
|
|
|
|
|
|
|
|
tx = PrepareInvoke(t, bc, acc, h, "netmapCandidates")
|
|
|
|
AddBlock(t, bc, tx)
|
|
|
|
|
|
|
|
aer := CheckHalt(t, bc, tx.Hash())
|
|
|
|
nodes, ok := aer.Stack[0].Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, len(nodes))
|
|
|
|
}
|