nativetest: migrate Designation contract tests to neotest

This commit is contained in:
Anna Shaleva 2021-12-03 18:51:46 +03:00
parent 3f1af71f52
commit 7927c8d8f5
3 changed files with 88 additions and 45 deletions

View file

@ -4,9 +4,15 @@ import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func newNativeClient(t *testing.T, name string) *neotest.ContractInvoker {
@ -61,3 +67,38 @@ func testGetSet(t *testing.T, c *neotest.ContractInvoker, name string, defaultVa
randomInvoker.Invoke(t, defaultValue+1, getName)
})
}
func setNodesByRole(t *testing.T, designateInvoker *neotest.ContractInvoker, ok bool, r noderoles.Role, nodes keys.PublicKeys) {
pubs := make([]interface{}, len(nodes))
for i := range nodes {
pubs[i] = nodes[i].Bytes()
}
if ok {
h := designateInvoker.Invoke(t, stackitem.Null{}, "designateAsRole", int64(r), pubs)
designateInvoker.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{
ScriptHash: designateInvoker.Hash,
Name: native.DesignationEventName,
Item: stackitem.NewArray([]stackitem.Item{
stackitem.Make(int64(r)),
stackitem.Make(designateInvoker.Chain.BlockHeight()),
}),
})
} else {
designateInvoker.InvokeFail(t, "", "designateAsRole", int64(r), pubs)
}
}
func checkNodeRoles(t *testing.T, designateInvoker *neotest.ContractInvoker, ok bool, r noderoles.Role, index uint32, res keys.PublicKeys) {
if ok {
designateInvoker.InvokeAndCheck(t, func(t *testing.T, stack []stackitem.Item) {
require.Equal(t, 1, len(stack))
arr := stack[0].Value().([]stackitem.Item)
require.Equal(t, len(res), len(arr))
for i := range arr {
require.Equal(t, res[i].Bytes(), arr[i].Value().([]byte), i)
}
}, "getDesignatedByRole", int64(r), int64(index))
} else {
designateInvoker.InvokeFail(t, "", "getDesignatedByRole", int64(r), int64(index))
}
}

View file

@ -0,0 +1,47 @@
package native_test
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/stretchr/testify/require"
)
func newDesignateClient(t *testing.T) *neotest.ContractInvoker {
return newNativeClient(t, nativenames.Designation)
}
func TestDesignate_DesignateAsRole(t *testing.T) {
c := newDesignateClient(t)
e := c.Executor
designateInvoker := c.WithSigners(c.Committee)
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs := keys.PublicKeys{priv.PublicKey()}
setNodesByRole(t, designateInvoker, false, 0xFF, pubs)
setNodesByRole(t, designateInvoker, true, noderoles.Oracle, pubs)
index := e.Chain.BlockHeight() + 1
checkNodeRoles(t, designateInvoker, false, 0xFF, 0, nil)
checkNodeRoles(t, designateInvoker, false, noderoles.Oracle, 100500, nil)
checkNodeRoles(t, designateInvoker, true, noderoles.Oracle, 0, keys.PublicKeys{}) // returns an empty list
checkNodeRoles(t, designateInvoker, true, noderoles.Oracle, index, pubs) // returns pubs
priv1, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs = keys.PublicKeys{priv1.PublicKey()}
setNodesByRole(t, designateInvoker, true, noderoles.StateValidator, pubs)
checkNodeRoles(t, designateInvoker, true, noderoles.StateValidator, e.Chain.BlockHeight()+1, pubs)
t.Run("neofs", func(t *testing.T) {
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs = keys.PublicKeys{priv.PublicKey()}
setNodesByRole(t, designateInvoker, true, noderoles.NeoFSAlphabet, pubs)
checkNodeRoles(t, designateInvoker, true, noderoles.NeoFSAlphabet, e.Chain.BlockHeight()+1, pubs)
})
}

View file

@ -73,51 +73,6 @@ func (bc *Blockchain) setNodesByRole(t *testing.T, ok bool, r noderoles.Role, no
}
}
func (bc *Blockchain) getNodesByRole(t *testing.T, ok bool, r noderoles.Role, index uint32, resLen int) {
res, err := invokeContractMethod(bc, 10_000_000, bc.contracts.Designate.Hash, "getDesignatedByRole", int64(r), int64(index))
require.NoError(t, err)
if ok {
require.Equal(t, vm.HaltState, res.VMState)
require.Equal(t, 1, len(res.Stack))
arrItem := res.Stack[0]
require.Equal(t, stackitem.ArrayT, arrItem.Type())
arr := arrItem.(*stackitem.Array)
require.Equal(t, resLen, arr.Len())
} else {
checkFAULTState(t, res)
}
}
func TestDesignate_DesignateAsRoleTx(t *testing.T) {
bc := newTestChain(t)
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs := keys.PublicKeys{priv.PublicKey()}
bc.setNodesByRole(t, false, 0xFF, pubs)
bc.setNodesByRole(t, true, noderoles.Oracle, pubs)
index := bc.BlockHeight() + 1
bc.getNodesByRole(t, false, 0xFF, 0, 0)
bc.getNodesByRole(t, false, noderoles.Oracle, 100500, 0)
bc.getNodesByRole(t, true, noderoles.Oracle, 0, 0) // returns an empty list
bc.getNodesByRole(t, true, noderoles.Oracle, index, 1) // returns pubs
priv1, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs = keys.PublicKeys{priv1.PublicKey()}
bc.setNodesByRole(t, true, noderoles.StateValidator, pubs)
bc.getNodesByRole(t, true, noderoles.StateValidator, bc.BlockHeight()+1, 1)
t.Run("neofs", func(t *testing.T) {
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
pubs = keys.PublicKeys{priv.PublicKey()}
bc.setNodesByRole(t, true, noderoles.NeoFSAlphabet, pubs)
bc.getNodesByRole(t, true, noderoles.NeoFSAlphabet, bc.BlockHeight()+1, 1)
})
}
func TestDesignate_DesignateAsRole(t *testing.T) {
bc := newTestChain(t)