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

     1  // Package iface defines the actual beacon state interface used
     2  // by a Prysm beacon node, also containing useful, scoped interfaces such as
     3  // a ReadOnlyState and WriteOnlyBeaconState.
     4  package iface
     5  
     6  import (
     7  	"context"
     8  
     9  	types "github.com/prysmaticlabs/eth2-types"
    10  	"github.com/prysmaticlabs/go-bitfield"
    11  	pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    12  	v1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
    13  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    14  )
    15  
    16  // BeaconState has read and write access to beacon state methods.
    17  type BeaconState interface {
    18  	ReadOnlyBeaconState
    19  	WriteOnlyBeaconState
    20  	Copy() BeaconState
    21  	HashTreeRoot(ctx context.Context) ([32]byte, error)
    22  	ToProto() (*v1.BeaconState, error)
    23  	Version() int
    24  	FutureForkStub
    25  }
    26  
    27  // ReadOnlyBeaconState defines a struct which only has read access to beacon state methods.
    28  type ReadOnlyBeaconState interface {
    29  	ReadOnlyBlockRoots
    30  	ReadOnlyStateRoots
    31  	ReadOnlyRandaoMixes
    32  	ReadOnlyEth1Data
    33  	ReadOnlyValidators
    34  	ReadOnlyBalances
    35  	ReadOnlyCheckpoint
    36  	ReadOnlyAttestations
    37  	InnerStateUnsafe() interface{}
    38  	CloneInnerState() interface{}
    39  	GenesisTime() uint64
    40  	GenesisValidatorRoot() []byte
    41  	Slot() types.Slot
    42  	Fork() *pbp2p.Fork
    43  	LatestBlockHeader() *ethpb.BeaconBlockHeader
    44  	HistoricalRoots() [][]byte
    45  	Slashings() []uint64
    46  	FieldReferencesCount() map[string]uint64
    47  	MarshalSSZ() ([]byte, error)
    48  	IsNil() bool
    49  }
    50  
    51  // WriteOnlyBeaconState defines a struct which only has write access to beacon state methods.
    52  type WriteOnlyBeaconState interface {
    53  	WriteOnlyBlockRoots
    54  	WriteOnlyStateRoots
    55  	WriteOnlyRandaoMixes
    56  	WriteOnlyEth1Data
    57  	WriteOnlyValidators
    58  	WriteOnlyBalances
    59  	WriteOnlyCheckpoint
    60  	WriteOnlyAttestations
    61  	SetGenesisTime(val uint64) error
    62  	SetGenesisValidatorRoot(val []byte) error
    63  	SetSlot(val types.Slot) error
    64  	SetFork(val *pbp2p.Fork) error
    65  	SetLatestBlockHeader(val *ethpb.BeaconBlockHeader) error
    66  	SetHistoricalRoots(val [][]byte) error
    67  	SetSlashings(val []uint64) error
    68  	UpdateSlashingsAtIndex(idx, val uint64) error
    69  	AppendHistoricalRoots(root [32]byte) error
    70  }
    71  
    72  // ReadOnlyValidator defines a struct which only has read access to validator methods.
    73  type ReadOnlyValidator interface {
    74  	EffectiveBalance() uint64
    75  	ActivationEligibilityEpoch() types.Epoch
    76  	ActivationEpoch() types.Epoch
    77  	WithdrawableEpoch() types.Epoch
    78  	ExitEpoch() types.Epoch
    79  	PublicKey() [48]byte
    80  	WithdrawalCredentials() []byte
    81  	Slashed() bool
    82  	IsNil() bool
    83  }
    84  
    85  // ReadOnlyValidators defines a struct which only has read access to validators methods.
    86  type ReadOnlyValidators interface {
    87  	Validators() []*ethpb.Validator
    88  	ValidatorAtIndex(idx types.ValidatorIndex) (*ethpb.Validator, error)
    89  	ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (ReadOnlyValidator, error)
    90  	ValidatorIndexByPubkey(key [48]byte) (types.ValidatorIndex, bool)
    91  	PubkeyAtIndex(idx types.ValidatorIndex) [48]byte
    92  	NumValidators() int
    93  	ReadFromEveryValidator(f func(idx int, val ReadOnlyValidator) error) error
    94  }
    95  
    96  // ReadOnlyBalances defines a struct which only has read access to balances methods.
    97  type ReadOnlyBalances interface {
    98  	Balances() []uint64
    99  	BalanceAtIndex(idx types.ValidatorIndex) (uint64, error)
   100  	BalancesLength() int
   101  }
   102  
   103  // ReadOnlyCheckpoint defines a struct which only has read access to checkpoint methods.
   104  type ReadOnlyCheckpoint interface {
   105  	PreviousJustifiedCheckpoint() *ethpb.Checkpoint
   106  	CurrentJustifiedCheckpoint() *ethpb.Checkpoint
   107  	MatchCurrentJustifiedCheckpoint(c *ethpb.Checkpoint) bool
   108  	MatchPreviousJustifiedCheckpoint(c *ethpb.Checkpoint) bool
   109  	FinalizedCheckpoint() *ethpb.Checkpoint
   110  	FinalizedCheckpointEpoch() types.Epoch
   111  	JustificationBits() bitfield.Bitvector4
   112  }
   113  
   114  // ReadOnlyBlockRoots defines a struct which only has read access to block roots methods.
   115  type ReadOnlyBlockRoots interface {
   116  	BlockRoots() [][]byte
   117  	BlockRootAtIndex(idx uint64) ([]byte, error)
   118  }
   119  
   120  // ReadOnlyStateRoots defines a struct which only has read access to state roots methods.
   121  type ReadOnlyStateRoots interface {
   122  	StateRoots() [][]byte
   123  	StateRootAtIndex(idx uint64) ([]byte, error)
   124  }
   125  
   126  // ReadOnlyRandaoMixes defines a struct which only has read access to randao mixes methods.
   127  type ReadOnlyRandaoMixes interface {
   128  	RandaoMixes() [][]byte
   129  	RandaoMixAtIndex(idx uint64) ([]byte, error)
   130  	RandaoMixesLength() int
   131  }
   132  
   133  // ReadOnlyEth1Data defines a struct which only has read access to eth1 data methods.
   134  type ReadOnlyEth1Data interface {
   135  	Eth1Data() *ethpb.Eth1Data
   136  	Eth1DataVotes() []*ethpb.Eth1Data
   137  	Eth1DepositIndex() uint64
   138  }
   139  
   140  // ReadOnlyAttestations defines a struct which only has read access to attestations methods.
   141  type ReadOnlyAttestations interface {
   142  	PreviousEpochAttestations() ([]*pbp2p.PendingAttestation, error)
   143  	CurrentEpochAttestations() ([]*pbp2p.PendingAttestation, error)
   144  }
   145  
   146  // WriteOnlyBlockRoots defines a struct which only has write access to block roots methods.
   147  type WriteOnlyBlockRoots interface {
   148  	SetBlockRoots(val [][]byte) error
   149  	UpdateBlockRootAtIndex(idx uint64, blockRoot [32]byte) error
   150  }
   151  
   152  // WriteOnlyStateRoots defines a struct which only has write access to state roots methods.
   153  type WriteOnlyStateRoots interface {
   154  	SetStateRoots(val [][]byte) error
   155  	UpdateStateRootAtIndex(idx uint64, stateRoot [32]byte) error
   156  }
   157  
   158  // WriteOnlyEth1Data defines a struct which only has write access to eth1 data methods.
   159  type WriteOnlyEth1Data interface {
   160  	SetEth1Data(val *ethpb.Eth1Data) error
   161  	SetEth1DataVotes(val []*ethpb.Eth1Data) error
   162  	AppendEth1DataVotes(val *ethpb.Eth1Data) error
   163  	SetEth1DepositIndex(val uint64) error
   164  }
   165  
   166  // WriteOnlyValidators defines a struct which only has write access to validators methods.
   167  type WriteOnlyValidators interface {
   168  	SetValidators(val []*ethpb.Validator) error
   169  	ApplyToEveryValidator(f func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error)) error
   170  	UpdateValidatorAtIndex(idx types.ValidatorIndex, val *ethpb.Validator) error
   171  	AppendValidator(val *ethpb.Validator) error
   172  }
   173  
   174  // WriteOnlyBalances defines a struct which only has write access to balances methods.
   175  type WriteOnlyBalances interface {
   176  	SetBalances(val []uint64) error
   177  	UpdateBalancesAtIndex(idx types.ValidatorIndex, val uint64) error
   178  	AppendBalance(bal uint64) error
   179  }
   180  
   181  // WriteOnlyRandaoMixes defines a struct which only has write access to randao mixes methods.
   182  type WriteOnlyRandaoMixes interface {
   183  	SetRandaoMixes(val [][]byte) error
   184  	UpdateRandaoMixesAtIndex(idx uint64, val []byte) error
   185  }
   186  
   187  // WriteOnlyCheckpoint defines a struct which only has write access to check point methods.
   188  type WriteOnlyCheckpoint interface {
   189  	SetFinalizedCheckpoint(val *ethpb.Checkpoint) error
   190  	SetPreviousJustifiedCheckpoint(val *ethpb.Checkpoint) error
   191  	SetCurrentJustifiedCheckpoint(val *ethpb.Checkpoint) error
   192  	SetJustificationBits(val bitfield.Bitvector4) error
   193  }
   194  
   195  // WriteOnlyAttestations defines a struct which only has write access to attestations methods.
   196  type WriteOnlyAttestations interface {
   197  	AppendCurrentEpochAttestations(val *pbp2p.PendingAttestation) error
   198  	AppendPreviousEpochAttestations(val *pbp2p.PendingAttestation) error
   199  	RotateAttestations() error
   200  }
   201  
   202  // FutureForkStub defines methods that are used for future forks. This is a low cost solution to enable
   203  // various state casting of interface to work.
   204  type FutureForkStub interface {
   205  	AppendCurrentParticipationBits(val byte) error
   206  	AppendPreviousParticipationBits(val byte) error
   207  	AppendInactivityScore(s uint64) error
   208  	CurrentEpochParticipation() ([]byte, error)
   209  	PreviousEpochParticipation() ([]byte, error)
   210  	InactivityScores() ([]uint64, error)
   211  	SetInactivityScores(val []uint64) error
   212  	CurrentSyncCommittee() (*pbp2p.SyncCommittee, error)
   213  	SetCurrentSyncCommittee(val *pbp2p.SyncCommittee) error
   214  	SetPreviousParticipationBits(val []byte) error
   215  	SetCurrentParticipationBits(val []byte) error
   216  	NextSyncCommittee() (*pbp2p.SyncCommittee, error)
   217  	SetNextSyncCommittee(val *pbp2p.SyncCommittee) error
   218  }