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

     1  package v1
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/pkg/errors"
     7  	iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
     8  	"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
     9  	pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    10  	"github.com/prysmaticlabs/prysm/shared/params"
    11  )
    12  
    13  // Ensure type BeaconState below implements BeaconState interface.
    14  var _ iface.BeaconState = (*BeaconState)(nil)
    15  
    16  func init() {
    17  	fieldMap = make(map[fieldIndex]dataType, params.BeaconConfig().BeaconStateFieldCount)
    18  
    19  	// Initialize the fixed sized arrays.
    20  	fieldMap[blockRoots] = basicArray
    21  	fieldMap[stateRoots] = basicArray
    22  	fieldMap[randaoMixes] = basicArray
    23  
    24  	// Initialize the composite arrays.
    25  	fieldMap[eth1DataVotes] = compositeArray
    26  	fieldMap[validators] = compositeArray
    27  	fieldMap[previousEpochAttestations] = compositeArray
    28  	fieldMap[currentEpochAttestations] = compositeArray
    29  }
    30  
    31  type fieldIndex int
    32  
    33  // dataType signifies the data type of the field.
    34  type dataType int
    35  
    36  // Below we define a set of useful enum values for the field
    37  // indices of the beacon state. For example, genesisTime is the
    38  // 0th field of the beacon state. This is helpful when we are
    39  // updating the Merkle branches up the trie representation
    40  // of the beacon state.
    41  const (
    42  	genesisTime fieldIndex = iota
    43  	genesisValidatorRoot
    44  	slot
    45  	fork
    46  	latestBlockHeader
    47  	blockRoots
    48  	stateRoots
    49  	historicalRoots
    50  	eth1Data
    51  	eth1DataVotes
    52  	eth1DepositIndex
    53  	validators
    54  	balances
    55  	randaoMixes
    56  	slashings
    57  	previousEpochAttestations
    58  	currentEpochAttestations
    59  	justificationBits
    60  	previousJustifiedCheckpoint
    61  	currentJustifiedCheckpoint
    62  	finalizedCheckpoint
    63  )
    64  
    65  // List of current data types the state supports.
    66  const (
    67  	basicArray dataType = iota
    68  	compositeArray
    69  )
    70  
    71  // fieldMap keeps track of each field
    72  // to its corresponding data type.
    73  var fieldMap map[fieldIndex]dataType
    74  
    75  // ErrNilInnerState returns when the inner state is nil and no copy set or get
    76  // operations can be performed on state.
    77  var ErrNilInnerState = errors.New("nil inner state")
    78  
    79  // BeaconState defines a struct containing utilities for the Ethereum Beacon Chain state, defining
    80  // getters and setters for its respective values and helpful functions such as HashTreeRoot().
    81  type BeaconState struct {
    82  	state                 *pbp2p.BeaconState
    83  	lock                  sync.RWMutex
    84  	dirtyFields           map[fieldIndex]bool
    85  	dirtyIndices          map[fieldIndex][]uint64
    86  	stateFieldLeaves      map[fieldIndex]*FieldTrie
    87  	rebuildTrie           map[fieldIndex]bool
    88  	valMapHandler         *stateutil.ValidatorMapHandler
    89  	merkleLayers          [][][]byte
    90  	sharedFieldReferences map[fieldIndex]*stateutil.Reference
    91  }
    92  
    93  // String returns the name of the field index.
    94  func (f fieldIndex) String() string {
    95  	switch f {
    96  	case genesisTime:
    97  		return "genesisTime"
    98  	case genesisValidatorRoot:
    99  		return "genesisValidatorRoot"
   100  	case slot:
   101  		return "slot"
   102  	case fork:
   103  		return "fork"
   104  	case latestBlockHeader:
   105  		return "latestBlockHeader"
   106  	case blockRoots:
   107  		return "blockRoots"
   108  	case stateRoots:
   109  		return "stateRoots"
   110  	case historicalRoots:
   111  		return "historicalRoots"
   112  	case eth1Data:
   113  		return "eth1Data"
   114  	case eth1DataVotes:
   115  		return "eth1DataVotes"
   116  	case eth1DepositIndex:
   117  		return "eth1DepositIndex"
   118  	case validators:
   119  		return "validators"
   120  	case balances:
   121  		return "balances"
   122  	case randaoMixes:
   123  		return "randaoMixes"
   124  	case slashings:
   125  		return "slashings"
   126  	case previousEpochAttestations:
   127  		return "previousEpochAttestations"
   128  	case currentEpochAttestations:
   129  		return "currentEpochAttestations"
   130  	case justificationBits:
   131  		return "justificationBits"
   132  	case previousJustifiedCheckpoint:
   133  		return "previousJustifiedCheckpoint"
   134  	case currentJustifiedCheckpoint:
   135  		return "currentJustifiedCheckpoint"
   136  	case finalizedCheckpoint:
   137  		return "finalizedCheckpoint"
   138  	default:
   139  		return ""
   140  	}
   141  }