2020-03-17 19:25:17 +00:00
|
|
|
package stateutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-03-17 19:25:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2021-07-23 16:11:21 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/hash"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-03-17 19:25:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2021-02-23 00:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-03-17 19:25:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReturnTrieLayer_OK(t *testing.T) {
|
|
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
2021-06-30 15:06:19 +00:00
|
|
|
root, err := v1.RootsArrayHashTreeRoot(newState.BlockRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
blockRts := newState.BlockRoots()
|
|
|
|
roots := make([][32]byte, 0, len(blockRts))
|
|
|
|
for _, rt := range blockRts {
|
|
|
|
roots = append(roots, bytesutil.ToBytes32(rt))
|
|
|
|
}
|
|
|
|
layers := stateutil.ReturnTrieLayer(roots, uint64(len(roots)))
|
|
|
|
newRoot := *layers[len(layers)-1][0]
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, root, newRoot)
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReturnTrieLayerVariable_OK(t *testing.T) {
|
|
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
2021-06-30 15:06:19 +00:00
|
|
|
root, err := v1.ValidatorRegistryRoot(newState.Validators())
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-15 22:55:11 +00:00
|
|
|
hasher := hash.CustomSHA256Hasher()
|
2020-03-17 19:25:17 +00:00
|
|
|
validators := newState.Validators()
|
|
|
|
roots := make([][32]byte, 0, len(validators))
|
|
|
|
for _, val := range validators {
|
2021-03-18 23:29:06 +00:00
|
|
|
rt, err := stateutil.ValidatorRootWithHasher(hasher, val)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
roots = append(roots, rt)
|
|
|
|
}
|
|
|
|
layers := stateutil.ReturnTrieLayerVariable(roots, params.BeaconConfig().ValidatorRegistryLimit)
|
|
|
|
newRoot := *layers[len(layers)-1][0]
|
|
|
|
newRoot, err = stateutil.AddInMixin(newRoot, uint64(len(validators)))
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, root, newRoot)
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecomputeFromLayer_FixedSizedArray(t *testing.T) {
|
|
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
blockRts := newState.BlockRoots()
|
|
|
|
roots := make([][32]byte, 0, len(blockRts))
|
|
|
|
for _, rt := range blockRts {
|
|
|
|
roots = append(roots, bytesutil.ToBytes32(rt))
|
|
|
|
}
|
|
|
|
layers := stateutil.ReturnTrieLayer(roots, uint64(len(roots)))
|
|
|
|
|
|
|
|
changedIdx := []uint64{24, 41}
|
|
|
|
changedRoots := [][32]byte{{'A', 'B', 'C'}, {'D', 'E', 'F'}}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, newState.UpdateBlockRootAtIndex(changedIdx[0], changedRoots[0]))
|
|
|
|
require.NoError(t, newState.UpdateBlockRootAtIndex(changedIdx[1], changedRoots[1]))
|
2020-03-17 19:25:17 +00:00
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
expectedRoot, err := v1.RootsArrayHashTreeRoot(newState.BlockRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
root, _, err := stateutil.RecomputeFromLayer(changedRoots, changedIdx, layers)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedRoot, root)
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecomputeFromLayer_VariableSizedArray(t *testing.T) {
|
|
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
validators := newState.Validators()
|
2021-09-15 22:55:11 +00:00
|
|
|
hasher := hash.CustomSHA256Hasher()
|
2020-03-17 19:25:17 +00:00
|
|
|
roots := make([][32]byte, 0, len(validators))
|
|
|
|
for _, val := range validators {
|
2021-03-18 23:29:06 +00:00
|
|
|
rt, err := stateutil.ValidatorRootWithHasher(hasher, val)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
roots = append(roots, rt)
|
|
|
|
}
|
|
|
|
layers := stateutil.ReturnTrieLayerVariable(roots, params.BeaconConfig().ValidatorRegistryLimit)
|
|
|
|
|
|
|
|
changedIdx := []uint64{2, 29}
|
|
|
|
val1, err := newState.ValidatorAtIndex(10)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
val2, err := newState.ValidatorAtIndex(11)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
val1.Slashed = true
|
|
|
|
val1.ExitEpoch = 20
|
|
|
|
|
|
|
|
val2.Slashed = true
|
|
|
|
val2.ExitEpoch = 40
|
|
|
|
|
|
|
|
changedVals := []*ethpb.Validator{val1, val2}
|
2021-02-23 00:14:50 +00:00
|
|
|
require.NoError(t, newState.UpdateValidatorAtIndex(types.ValidatorIndex(changedIdx[0]), changedVals[0]))
|
|
|
|
require.NoError(t, newState.UpdateValidatorAtIndex(types.ValidatorIndex(changedIdx[1]), changedVals[1]))
|
2020-03-17 19:25:17 +00:00
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
expectedRoot, err := v1.ValidatorRegistryRoot(newState.Validators())
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
roots = make([][32]byte, 0, len(changedVals))
|
|
|
|
for _, val := range changedVals {
|
2021-03-18 23:29:06 +00:00
|
|
|
rt, err := stateutil.ValidatorRootWithHasher(hasher, val)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
roots = append(roots, rt)
|
|
|
|
}
|
|
|
|
root, _, err := stateutil.RecomputeFromLayerVariable(roots, changedIdx, layers)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-17 19:25:17 +00:00
|
|
|
root, err = stateutil.AddInMixin(root, uint64(len(validators)))
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedRoot, root)
|
2020-03-17 19:25:17 +00:00
|
|
|
}
|