github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/v1/getters_eth1.go (about)

     1  package v1
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
     6  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     7  	"github.com/prysmaticlabs/prysm/shared/copyutil"
     8  	"github.com/prysmaticlabs/prysm/shared/featureconfig"
     9  	"github.com/prysmaticlabs/prysm/shared/htrutils"
    10  )
    11  
    12  // Eth1Data corresponding to the proof-of-work chain information stored in the beacon state.
    13  func (b *BeaconState) Eth1Data() *ethpb.Eth1Data {
    14  	if !b.hasInnerState() {
    15  		return nil
    16  	}
    17  	if b.state.Eth1Data == nil {
    18  		return nil
    19  	}
    20  
    21  	b.lock.RLock()
    22  	defer b.lock.RUnlock()
    23  
    24  	return b.eth1Data()
    25  }
    26  
    27  // eth1Data corresponding to the proof-of-work chain information stored in the beacon state.
    28  // This assumes that a lock is already held on BeaconState.
    29  func (b *BeaconState) eth1Data() *ethpb.Eth1Data {
    30  	if !b.hasInnerState() {
    31  		return nil
    32  	}
    33  	if b.state.Eth1Data == nil {
    34  		return nil
    35  	}
    36  
    37  	return copyutil.CopyETH1Data(b.state.Eth1Data)
    38  }
    39  
    40  // Eth1DataVotes corresponds to votes from Ethereum on the canonical proof-of-work chain
    41  // data retrieved from eth1.
    42  func (b *BeaconState) Eth1DataVotes() []*ethpb.Eth1Data {
    43  	if !b.hasInnerState() {
    44  		return nil
    45  	}
    46  	if b.state.Eth1DataVotes == nil {
    47  		return nil
    48  	}
    49  
    50  	b.lock.RLock()
    51  	defer b.lock.RUnlock()
    52  
    53  	return b.eth1DataVotes()
    54  }
    55  
    56  // eth1DataVotes corresponds to votes from Ethereum on the canonical proof-of-work chain
    57  // data retrieved from eth1.
    58  // This assumes that a lock is already held on BeaconState.
    59  func (b *BeaconState) eth1DataVotes() []*ethpb.Eth1Data {
    60  	if !b.hasInnerState() {
    61  		return nil
    62  	}
    63  	if b.state.Eth1DataVotes == nil {
    64  		return nil
    65  	}
    66  
    67  	res := make([]*ethpb.Eth1Data, len(b.state.Eth1DataVotes))
    68  	for i := 0; i < len(res); i++ {
    69  		res[i] = copyutil.CopyETH1Data(b.state.Eth1DataVotes[i])
    70  	}
    71  	return res
    72  }
    73  
    74  // Eth1DepositIndex corresponds to the index of the deposit made to the
    75  // validator deposit contract at the time of this state's eth1 data.
    76  func (b *BeaconState) Eth1DepositIndex() uint64 {
    77  	if !b.hasInnerState() {
    78  		return 0
    79  	}
    80  
    81  	b.lock.RLock()
    82  	defer b.lock.RUnlock()
    83  
    84  	return b.eth1DepositIndex()
    85  }
    86  
    87  // eth1DepositIndex corresponds to the index of the deposit made to the
    88  // validator deposit contract at the time of this state's eth1 data.
    89  // This assumes that a lock is already held on BeaconState.
    90  func (b *BeaconState) eth1DepositIndex() uint64 {
    91  	if !b.hasInnerState() {
    92  		return 0
    93  	}
    94  
    95  	return b.state.Eth1DepositIndex
    96  }
    97  
    98  // eth1Root computes the HashTreeRoot Merkleization of
    99  // a BeaconBlockHeader struct according to the Ethereum
   100  // Simple Serialize specification.
   101  func eth1Root(hasher htrutils.HashFn, eth1Data *ethpb.Eth1Data) ([32]byte, error) {
   102  	if eth1Data == nil {
   103  		return [32]byte{}, errors.New("nil eth1 data")
   104  	}
   105  
   106  	enc := stateutil.Eth1DataEncKey(eth1Data)
   107  	if featureconfig.Get().EnableSSZCache {
   108  		if found, ok := cachedHasher.rootsCache.Get(string(enc)); ok && found != nil {
   109  			return found.([32]byte), nil
   110  		}
   111  	}
   112  
   113  	root, err := stateutil.Eth1DataRootWithHasher(hasher, eth1Data)
   114  	if err != nil {
   115  		return [32]byte{}, err
   116  	}
   117  
   118  	if featureconfig.Get().EnableSSZCache {
   119  		cachedHasher.rootsCache.Set(string(enc), root, 32)
   120  	}
   121  	return root, nil
   122  }
   123  
   124  // eth1DataVotesRoot computes the HashTreeRoot Merkleization of
   125  // a list of Eth1Data structs according to the Ethereum
   126  // Simple Serialize specification.
   127  func eth1DataVotesRoot(eth1DataVotes []*ethpb.Eth1Data) ([32]byte, error) {
   128  	hashKey, err := stateutil.Eth1DatasEncKey(eth1DataVotes)
   129  	if err != nil {
   130  		return [32]byte{}, err
   131  	}
   132  
   133  	if featureconfig.Get().EnableSSZCache {
   134  		if found, ok := cachedHasher.rootsCache.Get(string(hashKey[:])); ok && found != nil {
   135  			return found.([32]byte), nil
   136  		}
   137  	}
   138  	root, err := stateutil.Eth1DatasRoot(eth1DataVotes)
   139  	if err != nil {
   140  		return [32]byte{}, err
   141  	}
   142  	if featureconfig.Get().EnableSSZCache {
   143  		cachedHasher.rootsCache.Set(string(hashKey[:]), root, 32)
   144  	}
   145  	return root, nil
   146  }