mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 12:40:05 +00:00
a069738c20
* update shared/params * update eth2-types deps * update protobufs * update shared/* * fix testutil/state * update beacon-chain/state * update beacon-chain/db * update tests * fix test * update beacon-chain/core * update beacon-chain/blockchain * update beacon-chain/cache * beacon-chain/forkchoice * update beacon-chain/operations * update beacon-chain/p2p * update beacon-chain/rpc * update sync/initial-sync * update deps * update deps * go fmt * update beacon-chain/sync * update endtoend/ * bazel build //beacon-chain - works w/o issues * update slasher code * udpate tools/ * update validator/ * update fastssz * fix build * fix test building * update tests * update ethereumapis deps * fix tests * update state/stategen * fix build * fix test * add FarFutureSlot * go imports * Radek's suggestions * Ivan's suggestions * type conversions * Nishant's suggestions * add more tests to rpc_send_request * fix test * clean up * fix conflicts Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: nisdas <nishdas93@gmail.com>
112 lines
4.1 KiB
Go
112 lines
4.1 KiB
Go
package stateutil_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
func TestReturnTrieLayer_OK(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
root, err := stateutil.RootsArrayHashTreeRoot(newState.BlockRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
|
|
require.NoError(t, err)
|
|
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]
|
|
assert.Equal(t, root, newRoot)
|
|
}
|
|
|
|
func TestReturnTrieLayerVariable_OK(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
root, err := stateutil.ValidatorRegistryRoot(newState.Validators())
|
|
require.NoError(t, err)
|
|
hasher := hashutil.CustomSHA256Hasher()
|
|
validators := newState.Validators()
|
|
roots := make([][32]byte, 0, len(validators))
|
|
for _, val := range validators {
|
|
rt, err := stateutil.ValidatorRoot(hasher, val)
|
|
require.NoError(t, err)
|
|
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)))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, root, newRoot)
|
|
}
|
|
|
|
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'}}
|
|
require.NoError(t, newState.UpdateBlockRootAtIndex(changedIdx[0], changedRoots[0]))
|
|
require.NoError(t, newState.UpdateBlockRootAtIndex(changedIdx[1], changedRoots[1]))
|
|
|
|
expectedRoot, err := stateutil.RootsArrayHashTreeRoot(newState.BlockRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "BlockRoots")
|
|
require.NoError(t, err)
|
|
root, _, err := stateutil.RecomputeFromLayer(changedRoots, changedIdx, layers)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedRoot, root)
|
|
}
|
|
|
|
func TestRecomputeFromLayer_VariableSizedArray(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
validators := newState.Validators()
|
|
hasher := hashutil.CustomSHA256Hasher()
|
|
roots := make([][32]byte, 0, len(validators))
|
|
for _, val := range validators {
|
|
rt, err := stateutil.ValidatorRoot(hasher, val)
|
|
require.NoError(t, err)
|
|
roots = append(roots, rt)
|
|
}
|
|
layers := stateutil.ReturnTrieLayerVariable(roots, params.BeaconConfig().ValidatorRegistryLimit)
|
|
|
|
changedIdx := []uint64{2, 29}
|
|
val1, err := newState.ValidatorAtIndex(10)
|
|
require.NoError(t, err)
|
|
val2, err := newState.ValidatorAtIndex(11)
|
|
require.NoError(t, err)
|
|
val1.Slashed = true
|
|
val1.ExitEpoch = 20
|
|
|
|
val2.Slashed = true
|
|
val2.ExitEpoch = 40
|
|
|
|
changedVals := []*ethpb.Validator{val1, val2}
|
|
require.NoError(t, newState.UpdateValidatorAtIndex(changedIdx[0], changedVals[0]))
|
|
require.NoError(t, newState.UpdateValidatorAtIndex(changedIdx[1], changedVals[1]))
|
|
|
|
expectedRoot, err := stateutil.ValidatorRegistryRoot(newState.Validators())
|
|
require.NoError(t, err)
|
|
roots = make([][32]byte, 0, len(changedVals))
|
|
for _, val := range changedVals {
|
|
rt, err := stateutil.ValidatorRoot(hasher, val)
|
|
require.NoError(t, err)
|
|
roots = append(roots, rt)
|
|
}
|
|
root, _, err := stateutil.RecomputeFromLayerVariable(roots, changedIdx, layers)
|
|
require.NoError(t, err)
|
|
root, err = stateutil.AddInMixin(root, uint64(len(validators)))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedRoot, root)
|
|
}
|