2021-06-11 19:29:50 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-06-11 19:29:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/trieutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateGenesisEth1Data updates eth1 data for genesis state.
|
|
|
|
func UpdateGenesisEth1Data(state iface.BeaconState, deposits []*ethpb.Deposit, eth1Data *ethpb.Eth1Data) (iface.BeaconState, error) {
|
|
|
|
if eth1Data == nil {
|
|
|
|
return nil, errors.New("no eth1data provided for genesis state")
|
|
|
|
}
|
|
|
|
|
|
|
|
var leaves [][]byte
|
|
|
|
for _, deposit := range deposits {
|
|
|
|
if deposit == nil || deposit.Data == nil {
|
|
|
|
return nil, fmt.Errorf("nil deposit or deposit with nil data cannot be processed: %v", deposit)
|
|
|
|
}
|
|
|
|
hash, err := deposit.Data.HashTreeRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
leaves = append(leaves, hash[:])
|
|
|
|
}
|
|
|
|
var trie *trieutil.SparseMerkleTrie
|
|
|
|
var err error
|
|
|
|
if len(leaves) > 0 {
|
|
|
|
trie, err = trieutil.GenerateTrieFromItems(leaves, params.BeaconConfig().DepositContractTreeDepth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trie, err = trieutil.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
depositRoot := trie.Root()
|
|
|
|
eth1Data.DepositRoot = depositRoot[:]
|
|
|
|
err = state.SetEth1Data(eth1Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|