prysm-pulse/beacon-chain/state/field_trie_test.go
Nishant Das 1bb12c3568
Add Field Trie to State (#5118)
* add new helpers

* make zerohash public

* remove unused method

* add more tests

* cleanup

* add in new tests

* fix all tests

* Apply suggestions from code review

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2020-03-18 10:09:31 +08:00

99 lines
2.7 KiB
Go

package state_test
import (
"testing"
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"
)
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(), params.BeaconConfig().SlotsPerHistoricalRoot)
if err != nil {
t.Fatal(err)
}
root, err := stateutil.RootsArrayHashTreeRoot(newState.StateRoots(), params.BeaconConfig().SlotsPerHistoricalRoot, "StateRoots")
if err != nil {
t.Fatal(err)
}
newRoot, err := trie.TrieRoot()
if newRoot != root {
t.Errorf("Wanted root of %#x but got %#x", root, newRoot)
}
}
func TestFieldTrie_RecomputeTrie(t *testing.T) {
newState, _ := testutil.DeterministicGenesisState(t, 32)
// 10 represents the enum value of validators
trie, err := state.NewFieldTrie(10, newState.Validators(), params.BeaconConfig().ValidatorRegistryLimit)
if err != nil {
t.Fatal(err)
}
changedIdx := []uint64{2, 29}
val1, err := newState.ValidatorAtIndex(10)
if err != nil {
t.Fatal(err)
}
val2, err := newState.ValidatorAtIndex(11)
if err != nil {
t.Fatal(err)
}
val1.Slashed = true
val1.ExitEpoch = 20
val2.Slashed = true
val2.ExitEpoch = 40
changedVals := []*ethpb.Validator{val1, val2}
newState.UpdateValidatorAtIndex(changedIdx[0], changedVals[0])
newState.UpdateValidatorAtIndex(changedIdx[1], changedVals[1])
expectedRoot, err := stateutil.ValidatorRegistryRoot(newState.Validators())
if err != nil {
t.Fatal(err)
}
root, err := trie.RecomputeTrie(changedIdx, newState.Validators())
if err != nil {
t.Fatal(err)
}
if root != expectedRoot {
t.Errorf("Wanted root of %#x but got %#x", 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(12, newState.RandaoMixes(), params.BeaconConfig().EpochsPerHistoricalVector)
if err != nil {
t.Fatal(err)
}
newTrie := trie.CopyTrie()
changedIdx := []uint64{2, 29}
changedVals := [][32]byte{{'A', 'B'}, {'C', 'D'}}
newState.UpdateRandaoMixesAtIndex(changedVals[0][:], changedIdx[0])
newState.UpdateRandaoMixesAtIndex(changedVals[1][:], changedIdx[1])
root, err := trie.RecomputeTrie(changedIdx, newState.RandaoMixes())
if err != nil {
t.Fatal(err)
}
newRoot, err := newTrie.TrieRoot()
if err != nil {
t.Fatal(err)
}
if root == newRoot {
t.Errorf("Wanted roots to be different, but they are the same: %#x", root)
}
}