mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-10 03:31:20 +00:00
3edfa8cb88
* Use ValidtorIndex across Prysm. Build ok * First take at fixing tests * Clean up e2e, fuzz... etc * Fix new lines * Update beacon-chain/cache/proposer_indices_test.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/core/helpers/rewards_penalties.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/core/helpers/shuffle.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update validator/graffiti/parse_graffiti_test.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Raul's feedback * Fix downcast int -> uint64 * Victor's feedback Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
79 lines
2.8 KiB
Go
79 lines
2.8 KiB
Go
package state_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestFieldTrie_NewTrie(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 40)
|
|
|
|
// 5 represents the enum value of state roots
|
|
trie, err := state.NewFieldTrie(5, newState.StateRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot))
|
|
require.NoError(t, err)
|
|
root, err := stateutil.RootsArrayHashTreeRoot(newState.StateRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "StateRoots")
|
|
require.NoError(t, err)
|
|
newRoot, err := trie.TrieRoot()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, root, newRoot)
|
|
}
|
|
|
|
func TestFieldTrie_RecomputeTrie(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
// 10 represents the enum value of validators
|
|
trie, err := state.NewFieldTrie(11, newState.Validators(), params.BeaconConfig().ValidatorRegistryLimit)
|
|
require.NoError(t, err)
|
|
|
|
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(types.ValidatorIndex(changedIdx[0]), changedVals[0]))
|
|
require.NoError(t, newState.UpdateValidatorAtIndex(types.ValidatorIndex(changedIdx[1]), changedVals[1]))
|
|
|
|
expectedRoot, err := stateutil.ValidatorRegistryRoot(newState.Validators())
|
|
require.NoError(t, err)
|
|
root, err := trie.RecomputeTrie(changedIdx, newState.Validators())
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedRoot, root)
|
|
}
|
|
|
|
func TestFieldTrie_CopyTrieImmutable(t *testing.T) {
|
|
newState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
// 12 represents the enum value of randao mixes.
|
|
trie, err := state.NewFieldTrie(13, newState.RandaoMixes(), uint64(params.BeaconConfig().EpochsPerHistoricalVector))
|
|
require.NoError(t, err)
|
|
|
|
newTrie := trie.CopyTrie()
|
|
|
|
changedIdx := []uint64{2, 29}
|
|
|
|
changedVals := [][32]byte{{'A', 'B'}, {'C', 'D'}}
|
|
require.NoError(t, newState.UpdateRandaoMixesAtIndex(changedIdx[0], changedVals[0][:]))
|
|
require.NoError(t, newState.UpdateRandaoMixesAtIndex(changedIdx[1], changedVals[1][:]))
|
|
|
|
root, err := trie.RecomputeTrie(changedIdx, newState.RandaoMixes())
|
|
require.NoError(t, err)
|
|
newRoot, err := newTrie.TrieRoot()
|
|
require.NoError(t, err)
|
|
if root == newRoot {
|
|
t.Errorf("Wanted roots to be different, but they are the same: %#x", root)
|
|
}
|
|
}
|