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/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-08 20:11:14 +00:00
|
|
|
// Technically this test belongs to the native package, but it's so deeply tied to
|
|
|
|
// the core internals that it needs to be rewritten to be moved. So let it be
|
|
|
|
// there just to remind us about the imperfect world we live in.
|
2020-10-01 15:17:09 +00:00
|
|
|
func TestDesignate_DesignateAsRole(t *testing.T) {
|
|
|
|
bc := newTestChain(t)
|
|
|
|
|
|
|
|
des := bc.contracts.Designate
|
2021-07-14 12:45:30 +00:00
|
|
|
tx := transaction.New([]byte{byte(opcode.PUSH1)}, 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
|
|
|
}
|