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

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