Documentation ¶
Overview ¶
Package state implements the whole state transition function which consists of per slot, per-epoch transitions, and bootstrapping the genesis state according to the eth2 spec.
Package state implements the whole state transition function which consists of per slot, per-epoch transitions. It also bootstraps the genesis beacon state for slot 0.
Index ¶
- Variables
- func CalculateStateRoot(ctx context.Context, state *stateTrie.BeaconState, ...) ([32]byte, error)
- func CanProcessEpoch(state *stateTrie.BeaconState) bool
- func EmptyGenesisState() (*stateTrie.BeaconState, error)
- func ExecuteStateTransition(ctx context.Context, state *stateTrie.BeaconState, ...) (*stateTrie.BeaconState, error)
- func ExecuteStateTransitionNoVerifyAnySig(ctx context.Context, state *stateTrie.BeaconState, ...) (*bls.SignatureSet, *stateTrie.BeaconState, error)
- func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (*stateTrie.BeaconState, error)
- func IsValidGenesisState(chainStartDepositCount, currentTime uint64) bool
- func NextSlotState(ctx context.Context, root []byte) (*state.BeaconState, error)
- func OptimizedGenesisBeaconState(genesisTime uint64, preState *stateTrie.BeaconState, eth1Data *ethpb.Eth1Data) (*stateTrie.BeaconState, error)
- func ProcessBlock(ctx context.Context, state *stateTrie.BeaconState, ...) (*stateTrie.BeaconState, error)
- func ProcessBlockForStateRoot(ctx context.Context, state *stateTrie.BeaconState, ...) (*stateTrie.BeaconState, error)
- func ProcessBlockNoVerifyAnySig(ctx context.Context, state *stateTrie.BeaconState, ...) (*bls.SignatureSet, *stateTrie.BeaconState, error)
- func ProcessEpochPrecompute(ctx context.Context, state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)
- func ProcessOperationsNoVerifyAttsSigs(ctx context.Context, state *stateTrie.BeaconState, ...) (*stateTrie.BeaconState, error)
- func ProcessSlot(ctx context.Context, state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)
- func ProcessSlots(ctx context.Context, state *stateTrie.BeaconState, slot uint64) (*stateTrie.BeaconState, error)
- func ProcessSlotsUsingNextSlotCache(ctx context.Context, parentState *stateTrie.BeaconState, parentRoot []byte, ...) (*stateTrie.BeaconState, error)
- func UpdateNextSlotCache(ctx context.Context, root []byte, state *state.BeaconState) error
- func VerifyOperationLengths(_ context.Context, state *stateTrie.BeaconState, b *ethpb.SignedBeaconBlock) (*stateTrie.BeaconState, error)
Constants ¶
This section is empty.
Variables ¶
var SkipSlotCache = cache.NewSkipSlotCache()
SkipSlotCache exists for the unlikely scenario that is a large gap between the head state and the current slot. If the beacon chain were ever to be stalled for several epochs, it may be difficult or impossible to compute the appropriate beacon state for assignments within a reasonable amount of time.
Functions ¶
func CalculateStateRoot ¶
func CalculateStateRoot( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) ([32]byte, error)
CalculateStateRoot defines the procedure for a state transition function. This does not validate any BLS signatures in a block, it is used for calculating the state root of the state for the block proposer to use. This does not modify state.
WARNING: This method does not validate any BLS signatures. This is used for proposer to compute state root before proposing a new block, and this does not modify state.
Spec pseudocode definition:
def state_transition(state: BeaconState, block: BeaconBlock, validate_state_root: bool=False) -> BeaconState: # Process slots (including those with no blocks) since block process_slots(state, block.slot) # Process block process_block(state, block) # Return post-state return state
func CanProcessEpoch ¶
func CanProcessEpoch(state *stateTrie.BeaconState) bool
CanProcessEpoch checks the eligibility to process epoch. The epoch can be processed at the end of the last slot of every epoch
Spec pseudocode definition:
If (state.slot + 1) % SLOTS_PER_EPOCH == 0:
func EmptyGenesisState ¶ added in v0.3.0
func EmptyGenesisState() (*stateTrie.BeaconState, error)
EmptyGenesisState returns an empty beacon state object.
func ExecuteStateTransition ¶
func ExecuteStateTransition( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) (*stateTrie.BeaconState, error)
ExecuteStateTransition defines the procedure for a state transition function.
Spec pseudocode definition:
def state_transition(state: BeaconState, signed_block: SignedBeaconBlock, validate_result: bool=True) -> BeaconState: block = signed_block.message # Process slots (including those with no blocks) since block process_slots(state, block.slot) # Verify signature if validate_result: assert verify_block_signature(state, signed_block) # Process block process_block(state, block) # Verify state root if validate_result: assert block.state_root == hash_tree_root(state) # Return post-state return state
func ExecuteStateTransitionNoVerifyAnySig ¶ added in v1.0.0
func ExecuteStateTransitionNoVerifyAnySig( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) (*bls.SignatureSet, *stateTrie.BeaconState, error)
ExecuteStateTransitionNoVerifyAnySig defines the procedure for a state transition function. This does not validate any BLS signatures of attestations, block proposer signature, randao signature, it is used for performing a state transition as quickly as possible. This function also returns a signature set of all signatures not verified, so that they can be stored and verified later.
WARNING: This method does not validate any signatures in a block. This method also modifies the passed in state.
Spec pseudocode definition:
def state_transition(state: BeaconState, block: BeaconBlock, validate_state_root: bool=False) -> BeaconState: # Process slots (including those with no blocks) since block process_slots(state, block.slot) # Process block process_block(state, block) # Return post-state return state
func GenesisBeaconState ¶
func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (*stateTrie.BeaconState, error)
GenesisBeaconState gets called when MinGenesisActiveValidatorCount count of full deposits were made to the deposit contract and the ChainStart log gets emitted.
Spec pseudocode definition:
def initialize_beacon_state_from_eth1(eth1_block_hash: Hash, eth1_timestamp: uint64, deposits: Sequence[Deposit]) -> BeaconState: state = BeaconState( genesis_time=eth1_timestamp - eth1_timestamp % SECONDS_PER_DAY + 2 * SECONDS_PER_DAY, eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=len(deposits)), latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())), randao_mixes=[eth1_block_hash] * EPOCHS_PER_HISTORICAL_VECTOR, # Seed RANDAO with Eth1 entropy ) # Process deposits leaves = list(map(lambda deposit: deposit.data, deposits)) for index, deposit in enumerate(deposits): deposit_data_list = List[DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH](*leaves[:index + 1]) state.eth1_data.deposit_root = hash_tree_root(deposit_data_list) process_deposit(state, deposit) # Process activations for index, validator in enumerate(state.validators): balance = state.balances[index] validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE) if validator.effective_balance == MAX_EFFECTIVE_BALANCE: validator.activation_eligibility_epoch = GENESIS_EPOCH validator.activation_epoch = GENESIS_EPOCH # Populate active_index_roots and compact_committees_roots indices_list = List[ValidatorIndex, VALIDATOR_REGISTRY_LIMIT](get_active_validator_indices(state, GENESIS_EPOCH)) active_index_root = hash_tree_root(indices_list) committee_root = get_compact_committees_root(state, GENESIS_EPOCH) for index in range(EPOCHS_PER_HISTORICAL_VECTOR): state.active_index_roots[index] = active_index_root state.compact_committees_roots[index] = committee_root return state
This method differs from the spec so as to process deposits beforehand instead of the end of the function.
func IsValidGenesisState ¶
IsValidGenesisState gets called whenever there's a deposit event, it checks whether there's enough effective balance to trigger and if the minimum genesis time arrived already.
Spec pseudocode definition:
def is_valid_genesis_state(state: BeaconState) -> bool: if state.genesis_time < MIN_GENESIS_TIME: return False if len(get_active_validator_indices(state, GENESIS_EPOCH)) < MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: return False return True
This method has been modified from the spec to allow whole states not to be saved but instead only cache the relevant information.
func NextSlotState ¶ added in v1.2.0
NextSlotState returns the saved state if the input root matches the root in `nextSlotCache`. Returns nil otherwise. This is useful to check before processing slots. With a cache hit, it will return last processed state with slot plus one advancement.
func OptimizedGenesisBeaconState ¶ added in v0.3.0
func OptimizedGenesisBeaconState(genesisTime uint64, preState *stateTrie.BeaconState, eth1Data *ethpb.Eth1Data) (*stateTrie.BeaconState, error)
OptimizedGenesisBeaconState is used to create a state that has already processed deposits. This is to efficiently create a mainnet state at chainstart.
func ProcessBlock ¶
func ProcessBlock( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) (*stateTrie.BeaconState, error)
ProcessBlock creates a new, modified beacon state by applying block operation transformations as defined in the Ethereum Serenity specification, including processing proposer slashings, processing block attestations, and more.
Spec pseudocode definition:
def process_block(state: BeaconState, block: BeaconBlock) -> None: process_block_header(state, block) process_randao(state, block.body) process_eth1_data(state, block.body) process_operations(state, block.body)
func ProcessBlockForStateRoot ¶ added in v0.3.2
func ProcessBlockForStateRoot( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) (*stateTrie.BeaconState, error)
ProcessBlockForStateRoot processes the state for state root computation. It skips proposer signature and randao signature verifications.
func ProcessBlockNoVerifyAnySig ¶ added in v1.0.0
func ProcessBlockNoVerifyAnySig( ctx context.Context, state *stateTrie.BeaconState, signed *ethpb.SignedBeaconBlock, ) (*bls.SignatureSet, *stateTrie.BeaconState, error)
ProcessBlockNoVerifyAnySig creates a new, modified beacon state by applying block operation transformations as defined in the Ethereum Serenity specification. It does not validate any block signature except for deposit and slashing signatures. It also returns the relevant signature set from all the respective methods.
Spec pseudocode definition:
def process_block(state: BeaconState, block: BeaconBlock) -> None: process_block_header(state, block) process_randao(state, block.body) process_eth1_data(state, block.body) process_operations(state, block.body)
func ProcessEpochPrecompute ¶
func ProcessEpochPrecompute(ctx context.Context, state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)
ProcessEpochPrecompute describes the per epoch operations that are performed on the beacon state. It's optimized by pre computing validator attested info and epoch total/attested balances upfront.
func ProcessOperationsNoVerifyAttsSigs ¶ added in v1.0.0
func ProcessOperationsNoVerifyAttsSigs( ctx context.Context, state *stateTrie.BeaconState, signedBeaconBlock *ethpb.SignedBeaconBlock) (*stateTrie.BeaconState, error)
ProcessOperationsNoVerifyAttsSigs processes the operations in the beacon block and updates beacon state with the operations in block. It does not verify attestation signatures.
WARNING: This method does not verify attestation signatures. This is used to perform the block operations as fast as possible.
Spec pseudocode definition:
def process_operations(state: BeaconState, body: BeaconBlockBody) -> None: # Verify that outstanding deposits are processed up to the maximum number of deposits assert len(body.deposits) == min(MAX_DEPOSITS, state.eth1_data.deposit_count - state.eth1_deposit_index) # Verify that there are no duplicate transfers assert len(body.transfers) == len(set(body.transfers)) all_operations = ( (body.proposer_slashings, process_proposer_slashing), (body.attester_slashings, process_attester_slashing), (body.attestations, process_attestation), (body.deposits, process_deposit), (body.voluntary_exits, process_voluntary_exit), (body.transfers, process_transfer), ) # type: Sequence[Tuple[List, Callable]] for operations, function in all_operations: for operation in operations: function(state, operation)
func ProcessSlot ¶
func ProcessSlot(ctx context.Context, state *stateTrie.BeaconState) (*stateTrie.BeaconState, error)
ProcessSlot happens every slot and focuses on the slot counter and block roots record updates. It happens regardless if there's an incoming block or not. Spec pseudocode definition:
def process_slot(state: BeaconState) -> None: # Cache state root previous_state_root = hash_tree_root(state) state.state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_state_root # Cache latest block header state root if state.latest_block_header.state_root == Bytes32(): state.latest_block_header.state_root = previous_state_root # Cache block root previous_block_root = hash_tree_root(state.latest_block_header) state.block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root
func ProcessSlots ¶
func ProcessSlots(ctx context.Context, state *stateTrie.BeaconState, slot uint64) (*stateTrie.BeaconState, error)
ProcessSlots process through skip slots and apply epoch transition when it's needed
Spec pseudocode definition:
def process_slots(state: BeaconState, slot: Slot) -> None: assert state.slot <= slot while state.slot < slot: process_slot(state) # Process epoch on the first slot of the next epoch if (state.slot + 1) % SLOTS_PER_EPOCH == 0: process_epoch(state) state.slot += 1 ]
func ProcessSlotsUsingNextSlotCache ¶ added in v1.2.0
func ProcessSlotsUsingNextSlotCache( ctx context.Context, parentState *stateTrie.BeaconState, parentRoot []byte, slot uint64) (*stateTrie.BeaconState, error)
ProcessSlotsUsingNextSlotCache processes slots by using next slot cache for higher efficiency.
func UpdateNextSlotCache ¶ added in v1.2.0
UpdateNextSlotCache updates the `nextSlotCache`. It saves the input state after advancing the state slot by 1 by calling `ProcessSlots`, it also saves the input root for later look up. This is useful to call after successfully processing a block.
func VerifyOperationLengths ¶ added in v1.0.0
func VerifyOperationLengths(_ context.Context, state *stateTrie.BeaconState, b *ethpb.SignedBeaconBlock) (*stateTrie.BeaconState, error)
VerifyOperationLengths verifies that block operation lengths are valid.
Types ¶
This section is empty.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package interop contains useful utilities for persisting ssz-encoded states and blocks to disk during each state transition for development purposes.
|
Package interop contains useful utilities for persisting ssz-encoded states and blocks to disk during each state transition for development purposes. |
Package stateutils contains useful tools for faster computation of state transitions using maps to represent validators instead of slices.
|
Package stateutils contains useful tools for faster computation of state transitions using maps to represent validators instead of slices. |