2020-10-01 15:17:09 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testchain"
|
2020-11-05 16:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2021-03-23 10:37:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-29 10:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-11-05 16:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-10-01 15:17:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
func (bc *Blockchain) setNodesByRole(t *testing.T, ok bool, r noderoles.Role, nodes keys.PublicKeys) {
|
2020-10-01 15:17:09 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for _, pub := range nodes {
|
|
|
|
emit.Bytes(w.BinWriter, pub.Bytes())
|
|
|
|
}
|
|
|
|
emit.Int(w.BinWriter, int64(len(nodes)))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
2020-10-01 15:17:09 +00:00
|
|
|
emit.Int(w.BinWriter, int64(r))
|
|
|
|
emit.Int(w.BinWriter, 2)
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.AppCallNoArgs(w.BinWriter, bc.contracts.Designate.Hash, "designateAsRole", callflag.All)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, w.Err)
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New(w.Bytes(), 0)
|
2020-10-01 15:17:09 +00:00
|
|
|
tx.NetworkFee = 10_000_000
|
|
|
|
tx.SystemFee = 10_000_000
|
|
|
|
tx.ValidUntilBlock = 100
|
|
|
|
tx.Signers = []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: testchain.MultisigScriptHash(),
|
2020-10-02 13:23:48 +00:00
|
|
|
Scopes: transaction.None,
|
2020-10-01 15:17:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Account: testchain.CommitteeScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
}
|
2020-11-23 11:09:45 +00:00
|
|
|
require.NoError(t, testchain.SignTx(bc, tx))
|
2020-10-01 15:17:09 +00:00
|
|
|
tx.Scripts = append(tx.Scripts, transaction.Witness{
|
2021-03-25 16:18:01 +00:00
|
|
|
InvocationScript: testchain.SignCommittee(tx),
|
2020-10-01 15:17:09 +00:00
|
|
|
VerificationScript: testchain.CommitteeVerificationScript(),
|
|
|
|
})
|
|
|
|
require.NoError(t, bc.AddBlock(bc.newBlock(tx)))
|
|
|
|
|
2020-11-11 15:43:28 +00:00
|
|
|
aer, err := bc.GetAppExecResults(tx.Hash(), trigger.Application)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-11 15:43:28 +00:00
|
|
|
require.Equal(t, 1, len(aer))
|
2020-10-01 15:17:09 +00:00
|
|
|
if ok {
|
2020-11-11 15:43:28 +00:00
|
|
|
require.Equal(t, vm.HaltState, aer[0].VMState)
|
2021-04-29 11:12:48 +00:00
|
|
|
require.Equal(t, 1, len(aer[0].Events))
|
|
|
|
|
|
|
|
ev := aer[0].Events[0]
|
|
|
|
require.Equal(t, bc.contracts.Designate.Hash, ev.ScriptHash)
|
|
|
|
require.Equal(t, native.DesignationEventName, ev.Name)
|
|
|
|
require.Equal(t, []stackitem.Item{
|
|
|
|
stackitem.Make(int64(r)),
|
|
|
|
stackitem.Make(bc.BlockHeight()),
|
|
|
|
}, ev.Item.Value().([]stackitem.Item))
|
2020-10-01 15:17:09 +00:00
|
|
|
} else {
|
2020-11-11 15:43:28 +00:00
|
|
|
require.Equal(t, vm.FaultState, aer[0].VMState)
|
2021-04-29 11:12:48 +00:00
|
|
|
require.Equal(t, 0, len(aer[0].Events))
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
func (bc *Blockchain) getNodesByRole(t *testing.T, ok bool, r noderoles.Role, index uint32, resLen int) {
|
2020-11-25 15:10:14 +00:00
|
|
|
res, err := invokeContractMethod(bc, 10_000_000, bc.contracts.Designate.Hash, "getDesignatedByRole", int64(r), int64(index))
|
2020-11-05 16:34:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if ok {
|
2020-11-25 15:10:14 +00:00
|
|
|
require.Equal(t, vm.HaltState, res.VMState)
|
|
|
|
require.Equal(t, 1, len(res.Stack))
|
|
|
|
arrItem := res.Stack[0]
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, stackitem.ArrayT, arrItem.Type())
|
|
|
|
arr := arrItem.(*stackitem.Array)
|
|
|
|
require.Equal(t, resLen, arr.Len())
|
|
|
|
} else {
|
2020-11-25 15:10:14 +00:00
|
|
|
checkFAULTState(t, res)
|
2020-11-05 16:34:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
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)
|
2021-03-23 10:37:30 +00:00
|
|
|
bc.setNodesByRole(t, true, noderoles.Oracle, pubs)
|
2020-11-05 16:34:48 +00:00
|
|
|
index := bc.BlockHeight() + 1
|
|
|
|
bc.getNodesByRole(t, false, 0xFF, 0, 0)
|
2021-03-23 10:37:30 +00:00
|
|
|
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
|
2021-01-15 11:05:30 +00:00
|
|
|
|
|
|
|
priv1, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pubs = keys.PublicKeys{priv1.PublicKey()}
|
2021-03-23 10:37:30 +00:00
|
|
|
bc.setNodesByRole(t, true, noderoles.StateValidator, pubs)
|
|
|
|
bc.getNodesByRole(t, true, noderoles.StateValidator, bc.BlockHeight()+1, 1)
|
2021-01-15 11:05:30 +00:00
|
|
|
|
2021-03-05 10:13:31 +00:00
|
|
|
t.Run("neofs", func(t *testing.T) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pubs = keys.PublicKeys{priv.PublicKey()}
|
2021-03-23 10:37:30 +00:00
|
|
|
bc.setNodesByRole(t, true, noderoles.NeoFSAlphabet, pubs)
|
|
|
|
bc.getNodesByRole(t, true, noderoles.NeoFSAlphabet, bc.BlockHeight()+1, 1)
|
2021-03-05 10:13:31 +00:00
|
|
|
})
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDesignate_DesignateAsRole(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
|
|
|
|
des := bc.contracts.Designate
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New([]byte{}, 0)
|
2021-03-25 18:46:52 +00:00
|
|
|
bl := block.New(bc.config.StateRootInHeader)
|
2020-11-05 16:34:48 +00:00
|
|
|
bl.Index = bc.BlockHeight() + 1
|
|
|
|
ic := bc.newInteropContext(trigger.OnPersist, bc.dao, bl, tx)
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.SpawnVM()
|
2020-10-01 15:17:09 +00:00
|
|
|
ic.VM.LoadScript([]byte{byte(opcode.RET)})
|
|
|
|
|
2021-05-12 16:37:14 +00:00
|
|
|
_, _, err := des.GetDesignatedByRole(bc.dao, 0xFF, 255)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.True(t, errors.Is(err, native.ErrInvalidRole), "got: %v", err)
|
|
|
|
|
2021-05-12 16:37:14 +00:00
|
|
|
pubs, index, err := des.GetDesignatedByRole(bc.dao, noderoles.Oracle, 255)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(pubs))
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, uint32(0), index)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.Oracle, keys.PublicKeys{})
|
2020-10-01 15:17:09 +00:00
|
|
|
require.True(t, errors.Is(err, native.ErrEmptyNodeList), "got: %v", err)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.Oracle, make(keys.PublicKeys, 32+1))
|
2020-10-21 09:50:26 +00:00
|
|
|
require.True(t, errors.Is(err, native.ErrLargeNodeList), "got: %v", err)
|
|
|
|
|
2020-10-01 15:17:09 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
|
|
|
|
err = des.DesignateAsRole(ic, 0xFF, keys.PublicKeys{pub})
|
|
|
|
require.True(t, errors.Is(err, native.ErrInvalidRole), "got: %v", err)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.Oracle, keys.PublicKeys{pub})
|
2020-10-01 15:17:09 +00:00
|
|
|
require.True(t, errors.Is(err, native.ErrInvalidWitness), "got: %v", err)
|
|
|
|
|
|
|
|
setSigner(tx, testchain.CommitteeScriptHash())
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.Oracle, keys.PublicKeys{pub})
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.Oracle, bl.Index+1)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, keys.PublicKeys{pub}, pubs)
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, bl.Index+1, index)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.StateValidator, 255)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(pubs))
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, uint32(0), index)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2020-11-18 08:59:34 +00:00
|
|
|
// Set StateValidator role.
|
2020-10-01 15:17:09 +00:00
|
|
|
_, err = keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pub1 := priv.PublicKey()
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.StateValidator, keys.PublicKeys{pub1})
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.Oracle, 255)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, keys.PublicKeys{pub}, pubs)
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, bl.Index+1, index)
|
2020-10-01 15:17:09 +00:00
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.StateValidator, 255)
|
2020-10-01 15:17:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, keys.PublicKeys{pub1}, pubs)
|
2020-11-05 16:34:48 +00:00
|
|
|
require.Equal(t, bl.Index+1, index)
|
2020-11-18 08:59:34 +00:00
|
|
|
|
|
|
|
// Set P2PNotary role.
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.P2PNotary, 255)
|
2020-11-18 08:59:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(pubs))
|
|
|
|
require.Equal(t, uint32(0), index)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
err = des.DesignateAsRole(ic, noderoles.P2PNotary, keys.PublicKeys{pub1})
|
2020-11-18 08:59:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-23 10:37:30 +00:00
|
|
|
pubs, index, err = des.GetDesignatedByRole(ic.DAO, noderoles.P2PNotary, 255)
|
2020-11-18 08:59:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, keys.PublicKeys{pub1}, pubs)
|
|
|
|
require.Equal(t, bl.Index+1, index)
|
2020-10-01 15:17:09 +00:00
|
|
|
}
|