2021-08-09 21:54:24 +00:00
|
|
|
package fieldtrie_test
|
2020-03-18 02:09:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-08-09 21:54:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/fieldtrie"
|
|
|
|
stateTypes "github.com/prysmaticlabs/prysm/beacon-chain/state/types"
|
2021-07-23 16:11:21 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-03-18 02:09:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFieldTrie_NewTrie(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
newState, _ := util.DeterministicGenesisState(t, 40)
|
2020-03-18 02:09:31 +00:00
|
|
|
|
|
|
|
// 5 represents the enum value of state roots
|
2021-08-09 21:54:24 +00:00
|
|
|
trie, err := fieldtrie.NewFieldTrie(5, stateTypes.BasicArray, newState.StateRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot))
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-30 15:06:19 +00:00
|
|
|
root, err := v1.RootsArrayHashTreeRoot(newState.StateRoots(), uint64(params.BeaconConfig().SlotsPerHistoricalRoot), "StateRoots")
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
newRoot, err := trie.TrieRoot()
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, root, newRoot)
|
2020-03-18 02:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldTrie_RecomputeTrie(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
newState, _ := util.DeterministicGenesisState(t, 32)
|
2020-03-18 02:09:31 +00:00
|
|
|
// 10 represents the enum value of validators
|
2021-08-09 21:54:24 +00:00
|
|
|
trie, err := fieldtrie.NewFieldTrie(11, stateTypes.CompositeArray, newState.Validators(), params.BeaconConfig().ValidatorRegistryLimit)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
|
|
|
|
changedIdx := []uint64{2, 29}
|
|
|
|
val1, err := newState.ValidatorAtIndex(10)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
val2, err := newState.ValidatorAtIndex(11)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +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-18 02:09:31 +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-18 02:09:31 +00:00
|
|
|
root, err := trie.RecomputeTrie(changedIdx, newState.Validators())
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedRoot, root)
|
2020-03-18 02:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldTrie_CopyTrieImmutable(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
newState, _ := util.DeterministicGenesisState(t, 32)
|
2020-03-18 02:09:31 +00:00
|
|
|
// 12 represents the enum value of randao mixes.
|
2021-08-09 21:54:24 +00:00
|
|
|
trie, err := fieldtrie.NewFieldTrie(13, stateTypes.BasicArray, newState.RandaoMixes(), uint64(params.BeaconConfig().EpochsPerHistoricalVector))
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
|
|
|
|
newTrie := trie.CopyTrie()
|
|
|
|
|
|
|
|
changedIdx := []uint64{2, 29}
|
|
|
|
|
|
|
|
changedVals := [][32]byte{{'A', 'B'}, {'C', 'D'}}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, newState.UpdateRandaoMixesAtIndex(changedIdx[0], changedVals[0][:]))
|
|
|
|
require.NoError(t, newState.UpdateRandaoMixesAtIndex(changedIdx[1], changedVals[1][:]))
|
2020-03-18 02:09:31 +00:00
|
|
|
|
|
|
|
root, err := trie.RecomputeTrie(changedIdx, newState.RandaoMixes())
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
newRoot, err := newTrie.TrieRoot()
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
2020-03-18 02:09:31 +00:00
|
|
|
if root == newRoot {
|
|
|
|
t.Errorf("Wanted roots to be different, but they are the same: %#x", root)
|
|
|
|
}
|
|
|
|
}
|