2022-06-08 19:53:09 +00:00
|
|
|
package stateroot_test
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-11 14:47:59 +00:00
|
|
|
"crypto/elliptic"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2021-02-01 16:00:07 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
2021-03-03 09:37:06 +00:00
|
|
|
"time"
|
2021-02-01 16:00:07 +00:00
|
|
|
|
2022-06-08 12:17:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/basicchain"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2021-02-02 09:34:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2021-03-25 08:53:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-03-23 10:37:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-03-11 14:47:59 +00:00
|
|
|
corestate "github.com/nspcc-dev/neo-go/pkg/core/stateroot"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/roles"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/stateroot"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-02-01 16:00:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-03 09:37:06 +00:00
|
|
|
"go.uber.org/atomic"
|
2021-02-02 09:34:27 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2021-02-01 16:00:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testSignStateRoot(t *testing.T, r *state.MPTRoot, pubs keys.PublicKeys, accs ...*wallet.Account) []byte {
|
|
|
|
n := smartcontract.GetMajorityHonestNodeCount(len(accs))
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := 0; i < n; i++ {
|
2021-03-25 19:11:55 +00:00
|
|
|
sig := accs[i].PrivateKey().SignHashable(uint32(netmode.UnitTestNet), r)
|
2021-02-01 16:00:07 +00:00
|
|
|
emit.Bytes(w.BinWriter, sig)
|
|
|
|
}
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
|
|
|
|
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(pubs.Copy())
|
|
|
|
require.NoError(t, err)
|
2021-03-26 10:39:37 +00:00
|
|
|
r.Witness = []transaction.Witness{{
|
2021-02-01 16:00:07 +00:00
|
|
|
VerificationScript: script,
|
|
|
|
InvocationScript: w.Bytes(),
|
2021-03-26 10:39:37 +00:00
|
|
|
}}
|
2021-03-25 19:11:55 +00:00
|
|
|
data, err := testserdes.EncodeBinary(stateroot.NewMessage(stateroot.RootT, r))
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMajorityMultisigWithGAS(t *testing.T, n int) (util.Uint160, keys.PublicKeys, []*wallet.Account) {
|
|
|
|
accs := make([]*wallet.Account, n)
|
|
|
|
for i := range accs {
|
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
accs[i] = acc
|
|
|
|
}
|
|
|
|
sort.Slice(accs, func(i, j int) bool {
|
2022-09-01 14:52:44 +00:00
|
|
|
pi := accs[i].PublicKey()
|
|
|
|
pj := accs[j].PublicKey()
|
2021-02-01 16:00:07 +00:00
|
|
|
return pi.Cmp(pj) == -1
|
|
|
|
})
|
|
|
|
pubs := make(keys.PublicKeys, n)
|
|
|
|
for i := range pubs {
|
2022-09-01 14:52:44 +00:00
|
|
|
pubs[i] = accs[i].PublicKey()
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
script, err := smartcontract.CreateMajorityMultiSigRedeemScript(pubs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return hash.Hash160(script), pubs, accs
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateRoot(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
bc, validator, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
designationSuperInvoker := e.NewInvoker(e.NativeHash(t, nativenames.Designation), validator, committee)
|
|
|
|
gasValidatorInvoker := e.ValidatorInvoker(e.NativeHash(t, nativenames.Gas))
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
h, pubs, accs := newMajorityMultisigWithGAS(t, 2)
|
2023-04-03 10:34:24 +00:00
|
|
|
validatorNodes := []any{pubs[0].Bytes(), pubs[1].Bytes()}
|
2022-03-11 14:47:59 +00:00
|
|
|
designationSuperInvoker.Invoke(t, stackitem.Null{}, "designateAsRole",
|
|
|
|
int64(roles.StateValidator), validatorNodes)
|
2021-02-01 16:00:07 +00:00
|
|
|
updateIndex := bc.BlockHeight()
|
2022-03-11 14:47:59 +00:00
|
|
|
|
|
|
|
gasValidatorInvoker.Invoke(t, true, "transfer", validator.ScriptHash(), h, 1_0000_0000, nil)
|
2021-02-01 16:00:07 +00:00
|
|
|
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2021-11-17 11:14:22 +00:00
|
|
|
w := createAndWriteWallet(t, accs[0], filepath.Join(tmpDir, "w"), "pass")
|
2021-02-02 09:34:27 +00:00
|
|
|
cfg := createStateRootConfig(w.Path(), "pass")
|
2022-03-11 14:47:59 +00:00
|
|
|
srMod := bc.GetStateModule().(*corestate.Module) // Take full responsibility here.
|
|
|
|
srv, err := stateroot.New(cfg, srMod, zaptest.NewLogger(t), bc, nil)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 0, bc.GetStateModule().CurrentValidatedHeight())
|
|
|
|
r, err := bc.GetStateModule().GetStateRoot(bc.BlockHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:47:59 +00:00
|
|
|
require.Equal(t, r.Root, bc.GetStateModule().CurrentLocalStateRoot())
|
2021-02-01 16:00:07 +00:00
|
|
|
|
|
|
|
t.Run("invalid message", func(t *testing.T) {
|
|
|
|
require.Error(t, srv.OnPayload(&payload.Extensible{Data: []byte{42}}))
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 0, bc.GetStateModule().CurrentValidatedHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
})
|
|
|
|
t.Run("drop zero index", func(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err := bc.GetStateModule().GetStateRoot(0)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-25 19:11:55 +00:00
|
|
|
data, err := testserdes.EncodeBinary(stateroot.NewMessage(stateroot.RootT, r))
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, srv.OnPayload(&payload.Extensible{Data: data}))
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 0, bc.GetStateModule().CurrentValidatedHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid height", func(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err := bc.GetStateModule().GetStateRoot(1)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
r.Index = 10
|
|
|
|
data := testSignStateRoot(t, r, pubs, accs...)
|
|
|
|
require.Error(t, srv.OnPayload(&payload.Extensible{Data: data}))
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 0, bc.GetStateModule().CurrentValidatedHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid signer", func(t *testing.T) {
|
|
|
|
accInv, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
2022-09-01 14:52:44 +00:00
|
|
|
pubs := keys.PublicKeys{accInv.PublicKey()}
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, accInv.ConvertMultisig(1, pubs))
|
2022-03-11 14:47:59 +00:00
|
|
|
gasValidatorInvoker.Invoke(t, true, "transfer", validator.ScriptHash(), accInv.Contract.ScriptHash(), 1_0000_0000, nil)
|
|
|
|
r, err := bc.GetStateModule().GetStateRoot(1)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
data := testSignStateRoot(t, r, pubs, accInv)
|
|
|
|
err = srv.OnPayload(&payload.Extensible{Data: data})
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, err, core.ErrWitnessHashMismatch)
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 0, bc.GetStateModule().CurrentValidatedHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
})
|
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err = bc.GetStateModule().GetStateRoot(updateIndex + 1)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
data := testSignStateRoot(t, r, pubs, accs...)
|
|
|
|
require.NoError(t, srv.OnPayload(&payload.Extensible{Data: data}))
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 2, bc.GetStateModule().CurrentValidatedHeight())
|
2021-02-01 16:00:07 +00:00
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err = bc.GetStateModule().GetStateRoot(updateIndex + 1)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-26 10:39:37 +00:00
|
|
|
require.NotEqual(t, 0, len(r.Witness))
|
|
|
|
require.Equal(t, h, r.Witness[0].ScriptHash())
|
2021-02-01 16:00:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 14:12:04 +00:00
|
|
|
type memoryStore struct {
|
|
|
|
*storage.MemoryStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (memoryStore) Close() error { return nil }
|
|
|
|
|
2021-02-01 16:00:07 +00:00
|
|
|
func TestStateRootInitNonZeroHeight(t *testing.T) {
|
|
|
|
st := memoryStore{storage.NewMemoryStore()}
|
|
|
|
h, pubs, accs := newMajorityMultisigWithGAS(t, 2)
|
|
|
|
|
|
|
|
var root util.Uint256
|
|
|
|
t.Run("init", func(t *testing.T) { // this is in a separate test to do proper cleanup
|
2022-03-11 14:47:59 +00:00
|
|
|
bc, validator, committee := chain.NewMultiWithCustomConfigAndStore(t, nil, st, true)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
designationSuperInvoker := e.NewInvoker(e.NativeHash(t, nativenames.Designation), validator, committee)
|
|
|
|
gasValidatorInvoker := e.ValidatorInvoker(e.NativeHash(t, nativenames.Gas))
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
validatorNodes := []any{pubs[0].Bytes(), pubs[1].Bytes()}
|
2022-03-11 14:47:59 +00:00
|
|
|
designationSuperInvoker.Invoke(t, stackitem.Null{}, "designateAsRole",
|
|
|
|
int64(roles.StateValidator), validatorNodes)
|
|
|
|
gasValidatorInvoker.Invoke(t, true, "transfer", validator.ScriptHash(), h, 1_0000_0000, nil)
|
2021-02-01 16:00:07 +00:00
|
|
|
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2021-11-17 11:14:22 +00:00
|
|
|
w := createAndWriteWallet(t, accs[0], filepath.Join(tmpDir, "w"), "pass")
|
2021-02-02 09:34:27 +00:00
|
|
|
cfg := createStateRootConfig(w.Path(), "pass")
|
2022-03-11 14:47:59 +00:00
|
|
|
srMod := bc.GetStateModule().(*corestate.Module) // Take full responsibility here.
|
|
|
|
srv, err := stateroot.New(cfg, srMod, zaptest.NewLogger(t), bc, nil)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err := bc.GetStateModule().GetStateRoot(2)
|
2021-02-01 16:00:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
data := testSignStateRoot(t, r, pubs, accs...)
|
|
|
|
require.NoError(t, srv.OnPayload(&payload.Extensible{Data: data}))
|
2022-03-11 14:47:59 +00:00
|
|
|
require.EqualValues(t, 2, bc.GetStateModule().CurrentValidatedHeight())
|
|
|
|
root = bc.GetStateModule().CurrentLocalStateRoot()
|
2021-02-01 16:00:07 +00:00
|
|
|
})
|
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
bc2, _, _ := chain.NewMultiWithCustomConfigAndStore(t, nil, st, true)
|
2021-02-01 16:00:07 +00:00
|
|
|
srv := bc2.GetStateModule()
|
|
|
|
require.EqualValues(t, 2, srv.CurrentValidatedHeight())
|
|
|
|
require.Equal(t, root, srv.CurrentLocalStateRoot())
|
|
|
|
}
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
func createAndWriteWallet(t *testing.T, acc *wallet.Account, path, password string) *wallet.Wallet {
|
|
|
|
w, err := wallet.NewWallet(path)
|
|
|
|
require.NoError(t, err)
|
2021-06-04 11:27:22 +00:00
|
|
|
require.NoError(t, acc.Encrypt(password, w.Scrypt))
|
2021-02-02 09:34:27 +00:00
|
|
|
w.AddAccount(acc)
|
|
|
|
require.NoError(t, w.Save())
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func createStateRootConfig(walletPath, password string) config.StateRoot {
|
|
|
|
return config.StateRoot{
|
|
|
|
Enabled: true,
|
|
|
|
UnlockWallet: config.Wallet{
|
|
|
|
Path: walletPath,
|
|
|
|
Password: password,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateRootFull(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2022-03-11 14:47:59 +00:00
|
|
|
bc, validator, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
designationSuperInvoker := e.NewInvoker(e.NativeHash(t, nativenames.Designation), validator, committee)
|
|
|
|
gasValidatorInvoker := e.ValidatorInvoker(e.NativeHash(t, nativenames.Gas))
|
|
|
|
|
|
|
|
getDesignatedByRole := func(t *testing.T, h uint32) keys.PublicKeys {
|
|
|
|
res, err := designationSuperInvoker.TestInvoke(t, "getDesignatedByRole", int64(noderoles.StateValidator), h)
|
|
|
|
require.NoError(t, err)
|
|
|
|
nodes := res.Pop().Value().([]stackitem.Item)
|
|
|
|
pubs := make(keys.PublicKeys, len(nodes))
|
|
|
|
for i, node := range nodes {
|
|
|
|
pubs[i], err = keys.NewPublicKeyFromBytes(node.Value().([]byte), elliptic.P256())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
return pubs
|
|
|
|
}
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
h, pubs, accs := newMajorityMultisigWithGAS(t, 2)
|
2021-11-17 11:14:22 +00:00
|
|
|
w := createAndWriteWallet(t, accs[1], filepath.Join(tmpDir, "wallet2"), "two")
|
2021-02-02 09:34:27 +00:00
|
|
|
cfg := createStateRootConfig(w.Path(), "two")
|
|
|
|
|
2021-03-03 09:37:06 +00:00
|
|
|
var lastValidated atomic.Value
|
|
|
|
var lastHeight atomic.Uint32
|
2022-03-11 14:47:59 +00:00
|
|
|
srMod := bc.GetStateModule().(*corestate.Module) // Take full responsibility here.
|
|
|
|
srv, err := stateroot.New(cfg, srMod, zaptest.NewLogger(t), bc, func(ep *payload.Extensible) {
|
2021-03-03 09:37:06 +00:00
|
|
|
lastHeight.Store(ep.ValidBlockStart)
|
|
|
|
lastValidated.Store(ep)
|
2021-02-02 09:34:27 +00:00
|
|
|
})
|
2021-04-02 09:12:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-12 01:11:21 +00:00
|
|
|
srv.Start()
|
2021-04-02 09:12:36 +00:00
|
|
|
t.Cleanup(srv.Shutdown)
|
2021-02-02 09:34:27 +00:00
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
validatorNodes := []any{pubs[0].Bytes(), pubs[1].Bytes()}
|
2022-03-11 14:47:59 +00:00
|
|
|
designationSuperInvoker.Invoke(t, stackitem.Null{}, "designateAsRole",
|
|
|
|
int64(roles.StateValidator), validatorNodes)
|
|
|
|
gasValidatorInvoker.Invoke(t, true, "transfer", validator.ScriptHash(), h, 1_0000_0000, nil)
|
2021-03-03 09:37:06 +00:00
|
|
|
require.Eventually(t, func() bool { return lastHeight.Load() == 2 }, time.Second, time.Millisecond)
|
2022-03-11 14:47:59 +00:00
|
|
|
checkVoteBroadcasted(t, bc, lastValidated.Load().(*payload.Extensible), 2, 1, getDesignatedByRole)
|
|
|
|
e.AddNewBlock(t)
|
2021-03-03 09:37:06 +00:00
|
|
|
require.Eventually(t, func() bool { return lastHeight.Load() == 3 }, time.Second, time.Millisecond)
|
2022-03-11 14:47:59 +00:00
|
|
|
checkVoteBroadcasted(t, bc, lastValidated.Load().(*payload.Extensible), 3, 1, getDesignatedByRole)
|
2021-02-02 09:34:27 +00:00
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err := bc.GetStateModule().GetStateRoot(2)
|
2021-02-02 09:34:27 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-25 19:11:55 +00:00
|
|
|
require.NoError(t, srv.AddSignature(2, 0, accs[0].PrivateKey().SignHashable(uint32(netmode.UnitTestNet), r)))
|
2021-03-03 09:37:06 +00:00
|
|
|
require.NotNil(t, lastValidated.Load().(*payload.Extensible))
|
2021-02-02 09:34:27 +00:00
|
|
|
|
|
|
|
msg := new(stateroot.Message)
|
2021-03-03 09:37:06 +00:00
|
|
|
require.NoError(t, testserdes.DecodeBinary(lastValidated.Load().(*payload.Extensible).Data, msg))
|
2021-05-07 10:19:40 +00:00
|
|
|
require.NotEqual(t, stateroot.RootT, msg.Type) // not a sender for this root
|
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
r, err = bc.GetStateModule().GetStateRoot(3)
|
2021-05-07 10:19:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Error(t, srv.AddSignature(2, 0, accs[0].PrivateKey().SignHashable(uint32(netmode.UnitTestNet), r)))
|
|
|
|
require.NoError(t, srv.AddSignature(3, 0, accs[0].PrivateKey().SignHashable(uint32(netmode.UnitTestNet), r)))
|
|
|
|
require.NotNil(t, lastValidated.Load().(*payload.Extensible))
|
|
|
|
|
|
|
|
require.NoError(t, testserdes.DecodeBinary(lastValidated.Load().(*payload.Extensible).Data, msg))
|
2021-02-02 09:34:27 +00:00
|
|
|
require.Equal(t, stateroot.RootT, msg.Type)
|
|
|
|
|
|
|
|
actual := msg.Payload.(*state.MPTRoot)
|
|
|
|
require.Equal(t, r.Index, actual.Index)
|
|
|
|
require.Equal(t, r.Version, actual.Version)
|
|
|
|
require.Equal(t, r.Root, actual.Root)
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
func checkVoteBroadcasted(t *testing.T, bc *core.Blockchain, p *payload.Extensible,
|
|
|
|
height uint32, valIndex byte, getDesignatedByRole func(t *testing.T, h uint32) keys.PublicKeys) {
|
2021-02-02 09:34:27 +00:00
|
|
|
require.NotNil(t, p)
|
|
|
|
m := new(stateroot.Message)
|
|
|
|
require.NoError(t, testserdes.DecodeBinary(p.Data, m))
|
|
|
|
require.Equal(t, stateroot.VoteT, m.Type)
|
|
|
|
vote := m.Payload.(*stateroot.Vote)
|
|
|
|
|
|
|
|
srv := bc.GetStateModule()
|
|
|
|
r, err := srv.GetStateRoot(bc.BlockHeight())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, height, vote.Height)
|
|
|
|
require.Equal(t, int32(valIndex), vote.ValidatorIndex)
|
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
pubs := getDesignatedByRole(t, bc.BlockHeight())
|
2021-02-02 09:34:27 +00:00
|
|
|
require.True(t, len(pubs) > int(valIndex))
|
2021-03-25 19:11:55 +00:00
|
|
|
require.True(t, pubs[valIndex].VerifyHashable(vote.Signature, uint32(netmode.UnitTestNet), r))
|
2021-02-02 09:34:27 +00:00
|
|
|
}
|
2022-04-07 15:13:08 +00:00
|
|
|
|
|
|
|
func TestStateroot_GetLatestStateHeight(t *testing.T) {
|
2022-12-06 13:34:38 +00:00
|
|
|
bc, validators, committee := chain.NewMultiWithCustomConfig(t, func(c *config.Blockchain) {
|
2022-04-07 15:13:08 +00:00
|
|
|
c.P2PSigExtensions = true
|
|
|
|
})
|
|
|
|
e := neotest.NewExecutor(t, bc, validators, committee)
|
2022-06-08 19:53:09 +00:00
|
|
|
basicchain.Init(t, "../../../", e)
|
2022-04-07 15:13:08 +00:00
|
|
|
|
|
|
|
m := bc.GetStateModule()
|
|
|
|
for i := uint32(0); i < bc.BlockHeight(); i++ {
|
|
|
|
r, err := m.GetStateRoot(i)
|
|
|
|
require.NoError(t, err)
|
|
|
|
h, err := bc.GetStateModule().GetLatestStateHeight(r.Root)
|
|
|
|
require.NoError(t, err, i)
|
|
|
|
require.Equal(t, i, h)
|
|
|
|
}
|
|
|
|
}
|