blocks

package
v0.0.0-...-c85edb6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2019 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package blocks contains block processing libraries. These libraries process and verify block specific messages such as PoW receipt root, RANDAO, validator deposits, exits and slashing proofs.

Package blocks contains block processing libraries. These libraries process and verify block specific messages such as PoW receipt root, RANDAO, validator deposits, exits and slashing proofs.

Package blocks contains block processing libraries. These libraries process and verify block specific messages such as PoW receipt root, RANDAO, validator deposits, exits and slashing proofs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockChildren

func BlockChildren(block *pb.BeaconBlock, observedBlocks []*pb.BeaconBlock) ([]*pb.BeaconBlock, error)

BlockChildren obtains the blocks in a list of observed blocks which have the current beacon block's hash as their parent root hash.

Spec pseudocode definition:

Let get_children(store: Store, block: BeaconBlock) ->
	List[BeaconBlock] returns the child blocks of the given block.

func BlockRoot

func BlockRoot(state *pb.BeaconState, slot uint64) ([]byte, error)

BlockRoot returns the block root stored in the BeaconState for a given slot. It returns an error if the requested block root is not within the BeaconState. Spec pseudocode definition:

def get_block_root(state: BeaconState, slot: int) -> Hash32:
	"""
	returns the block root at a recent ``slot``.
	"""
	assert state.slot <= slot + LATEST_BLOCK_ROOTS_LENGTH
	assert slot < state.slot
	return state.latest_block_roots[slot % LATEST_BLOCK_ROOTS_LENGTH]

func DecodeDepositAmountAndTimeStamp

func DecodeDepositAmountAndTimeStamp(depositData []byte) (uint64, int64, error)

DecodeDepositAmountAndTimeStamp extracts the deposit amount and timestamp from the given deposit data.

func DecodeDepositInput

func DecodeDepositInput(depositData []byte) (*pb.DepositInput, error)

DecodeDepositInput unmarshalls a depositData byte slice into a proto *pb.DepositInput by using the Simple Serialize (SSZ) algorithm. TODO(#1253): Do not assume we will receive serialized proto objects - instead, replace completely by a common struct which can be simple serialized.

func EncodeDepositData

func EncodeDepositData(
	depositInput *pb.DepositInput,
	depositValue uint64,
	depositTimestamp int64,
) ([]byte, error)

EncodeDepositData converts a deposit input proto into an a byte slice of Simple Serialized deposit input followed by 8 bytes for a deposit value and 8 bytes for a unix timestamp, all in BigEndian format.

func IsRandaoValid

func IsRandaoValid(blockRandao []byte, stateRandao []byte) bool

IsRandaoValid verifies the validity of randao from block by comparing it with the proposer's randao from the beacon state.

func IsSlotValid

func IsSlotValid(slot uint64, genesisTime time.Time) bool

IsSlotValid compares the slot to the system clock to determine if the block is valid.

func IsValidBlock

func IsValidBlock(
	ctx context.Context,
	state *pb.BeaconState,
	block *pb.BeaconBlock,
	enablePOWChain bool,
	HasBlock func(hash [32]byte) bool,
	GetPOWBlock func(ctx context.Context, hash common.Hash) (*gethTypes.Block, error),
	genesisTime time.Time) error

IsValidBlock ensures that the block is compliant with the block processing validity conditions. Spec:

For a beacon chain block, block, to be processed by a node, the following conditions must be met:
The parent block with root block.parent_root has been processed and accepted.
The node has processed its state up to slot, block.slot - 1.
The Ethereum 1.0 block pointed to by the state.processed_pow_receipt_root has been processed and accepted.
The node's local clock time is greater than or equal to state.genesis_time + block.slot * SLOT_DURATION.

func NewGenesisBlock

func NewGenesisBlock(stateRoot []byte) *pb.BeaconBlock

NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol.

func ProcessAttesterSlashings

func ProcessAttesterSlashings(
	beaconState *pb.BeaconState,
	block *pb.BeaconBlock,
	verifySignatures bool,
) (*pb.BeaconState, error)

ProcessAttesterSlashings is one of the operations performed on each processed beacon block to penalize validators based on Casper FFG slashing conditions if any slashable events occurred.

Official spec definition for attester slashings:

  Verify that len(block.body.attester_slashings) <= MAX_CASPER_SLASHINGS.
  For each attester_slashing in block.body.attester_slashings:

  Verify that verify_attester_votes(state, attester_slashing.votes_1).
  Verify that verify_attester_votes(state, attester_slashing.votes_2).
  Verify that attester_slashing.votes_1.data != attester_slashing.votes_2.data.
  Let indices(vote) = vote.aggregate_signature_poc_0_indices +
    vote.aggregate_signature_poc_1_indices.
  Let intersection = [x for x in indices(attester_slashing.votes_1)
    if x in indices(attester_slashing.votes_2)].
  Verify that len(intersection) >= 1.
	 Verify the following about the attester votes:
    (vote1.justified_slot < vote2.justified_slot) &&
    (vote2.justified_slot + 1 == vote2.slot) &&
    (vote2.slot < vote1.slot)
    OR
    vote1.slot == vote.slot
  Verify that attester_slashing.votes_1.data.justified_slot + 1 <
    attester_slashing.votes_2.data.justified_slot + 1 ==
    attester_slashing.votes_2.data.slot < attester_slashing.votes_1.data.slot
    or attester_slashing.votes_1.data.slot == attester_slashing.votes_2.data.slot.
  For each validator index i in intersection,
    if state.validator_registry[i].penalized_slot > state.slot, then
	   run penalize_validator(state, i)

func ProcessBlockAttestations

func ProcessBlockAttestations(
	beaconState *pb.BeaconState,
	block *pb.BeaconBlock,
	verifySignatures bool,
) (*pb.BeaconState, error)

ProcessBlockAttestations applies processing operations to a block's inner attestation records. This function returns a list of pending attestations which can then be appended to the BeaconState's latest attestations.

Official spec definition for block attestation processing:

Verify that len(block.body.attestations) <= MAX_ATTESTATIONS.

For each attestation in block.body.attestations:
Verify that attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot.
Verify that attestation.data.slot + EPOCH_LENGTH >= state.slot.
Verify that attestation.data.justified_slot is equal to
  state.justified_slot if attestation.data.slot >=
  state.slot - (state.slot % EPOCH_LENGTH) else state.previous_justified_slot.
Verify that attestation.data.justified_block_root is equal to
  get_block_root(state, attestation.data.justified_slot).
Verify that either attestation.data.latest_crosslink_root or
  attestation.data.shard_block_root equals
  state.latest_crosslinks[shard].shard_block_root
Aggregate_signature verification:
  Let participants = get_attestation_participants(
    state,
    attestation.data,
    attestation.participation_bitfield,
  )
  Let group_public_key = BLSAddPubkeys([
    state.validator_registry[v].pubkey for v in participants
  ])
  Verify that bls_verify(
    pubkey=group_public_key,
    message=hash_tree_root(attestation.data) + bytes1(0),
    signature=attestation.aggregate_signature,
    domain=get_domain(state.fork_data, attestation.data.slot, DOMAIN_ATTESTATION)).

[TO BE REMOVED IN PHASE 1] Verify that attestation.data.shard_block_hash == ZERO_HASH.
return PendingAttestationRecord(
  data=attestation.data,
  participation_bitfield=attestation.participation_bitfield,
  custody_bitfield=attestation.custody_bitfield,
  slot_included=state.slot,
) which can then be appended to state.latest_attestations.

func ProcessBlockRandao

func ProcessBlockRandao(beaconState *pb.BeaconState, block *pb.BeaconBlock) (*pb.BeaconState, error)

ProcessBlockRandao checks the block proposer's randao commitment and generates a new randao mix to update in the beacon state's latest randao mixes and set the proposer's randao fields.

Official spec definition for block randao verification:

Let repeat_hash(x, n) = x if n == 0 else repeat_hash(hash(x), n-1).
Let proposer = state.validator_registry[get_beacon_proposer_index(state, state.slot)].
Verify that repeat_hash(block.randao_reveal, proposer.randao_layers) == proposer.randao_commitment.
Set state.latest_randao_mixes[state.slot % LATEST_RANDAO_MIXES_LENGTH] =
  xor(state.latest_randao_mixes[state.slot % LATEST_RANDAO_MIXES_LENGTH], block.randao_reveal)
Set proposer.randao_commitment = block.randao_reveal.
Set proposer.randao_layers = 0

func ProcessBlockRoots

func ProcessBlockRoots(state *pb.BeaconState, prevBlockRoot [32]byte) *pb.BeaconState

ProcessBlockRoots processes the previous block root into the state, by appending it to the most recent block roots. Spec:

 Let previous_block_root be the tree_hash_root of the previous beacon block processed in the chain.
	Set state.latest_block_roots[(state.slot - 1) % LATEST_BLOCK_ROOTS_LENGTH] = previous_block_root.
	If state.slot % LATEST_BLOCK_ROOTS_LENGTH == 0 append merkle_root(state.latest_block_roots) to state.batched_block_roots.

func ProcessEth1Data

func ProcessEth1Data(beaconState *pb.BeaconState, block *pb.BeaconBlock) *pb.BeaconState

ProcessEth1Data is an operation performed on each beacon block to ensure the ETH1 data votes are processed into the beacon state.

Official spec definition of ProcessEth1Data

If block.eth1_data equals eth1_data_vote.eth1_data for some eth1_data_vote
in state.eth1_data_votes, set eth1_data_vote.vote_count += 1.
Otherwise, append to state.eth1_data_votes a new Eth1DataVote(eth1_data=block.eth1_data, vote_count=1).

func ProcessProposerSlashings

func ProcessProposerSlashings(
	beaconState *pb.BeaconState,
	block *pb.BeaconBlock,
	verifySignatures bool,
) (*pb.BeaconState, error)

ProcessProposerSlashings is one of the operations performed on each processed beacon block to penalize proposers based on slashing conditions if any slashable events occurred.

Official spec definition for proposer slashings:

 Verify that len(block.body.proposer_slashings) <= MAX_PROPOSER_SLASHINGS.

 For each proposer_slashing in block.body.proposer_slashings:

 Let proposer = state.validator_registry[proposer_slashing.proposer_index].
 Verify that bls_verify(pubkey=proposer.pubkey, msg=hash_tree_root(
  proposer_slashing.proposal_data_1),
	sig=proposer_slashing.proposal_signature_1,
  domain=get_domain(state.fork_data, proposer_slashing.proposal_data_1.slot, DOMAIN_PROPOSAL)).
 Verify that bls_verify(pubkey=proposer.pubkey, msg=hash_tree_root(
   proposer_slashing.proposal_data_2),
   sig=proposer_slashing.proposal_signature_2,
   domain=get_domain(state.fork_data, proposer_slashing.proposal_data_2.slot, DOMAIN_PROPOSAL)).
 Verify that proposer_slashing.proposal_data_1.slot == proposer_slashing.proposal_data_2.slot.
 Verify that proposer_slashing.proposal_data_1.shard == proposer_slashing.proposal_data_2.shard.
 Verify that proposer_slashing.proposal_data_1.block_root != proposer_slashing.proposal_data_2.block_root.
 Verify that validator.penalized_slot > state.slot.
 Run penalize_validator(state, proposer_slashing.proposer_index).

func ProcessValidatorDeposits

func ProcessValidatorDeposits(
	beaconState *pb.BeaconState,
	block *pb.BeaconBlock,
) (*pb.BeaconState, error)

ProcessValidatorDeposits is one of the operations performed on each processed beacon block to verify queued validators from the Ethereum 1.0 Deposit Contract into the beacon chain.

Official spec definition for processing validator deposits:

Verify that len(block.body.deposits) <= MAX_DEPOSITS.
For each deposit in block.body.deposits:
  Let serialized_deposit_data be the serialized form of deposit.deposit_data.
  It should be the DepositInput followed by 8 bytes for deposit_data.value
  and 8 bytes for deposit_data.timestamp. That is, it should match
  deposit_data in the Ethereum 1.0 deposit contract of which the hash
  was placed into the Merkle tree.

  Verify deposit merkle_branch, setting leaf=serialized_deposit_data,
  depth=DEPOSIT_CONTRACT_TREE_DEPTH and root=state.latest_deposit_root:

  Run the following:
  process_deposit(
    state=state,
    pubkey=deposit.deposit_data.deposit_input.pubkey,
    deposit=deposit.deposit_data.value,
    proof_of_possession=deposit.deposit_data.deposit_input.proof_of_possession,
    withdrawal_credentials=deposit.deposit_data.deposit_input.withdrawal_credentials,
    randao_commitment=deposit.deposit_data.deposit_input.randao_commitment,
    poc_commitment=deposit.deposit_data.deposit_input.poc_commitment,
  )

func ProcessValidatorExits

func ProcessValidatorExits(
	beaconState *pb.BeaconState,
	block *pb.BeaconBlock,
	verifySignatures bool,
) (*pb.BeaconState, error)

ProcessValidatorExits is one of the operations performed on each processed beacon block to determine which validators should exit the state's validator registry.

Official spec definition for processing exits:

Verify that len(block.body.exits) <= MAX_EXITS.

For each exit in block.body.exits:
  Let validator = state.validator_registry[exit.validator_index].
  Verify that validator.exit_slot > state.slot + ENTRY_EXIT_DELAY.
  Verify that state.slot >= exit.slot.
  Verify that state.slot >= validator.latest_status_change_slot +
    SHARD_PERSISTENT_COMMITTEE_CHANGE_PERIOD.
  Let exit_message = hash_tree_root(
    Exit(
      slot=exit.slot,
      validator_index=exit.validator_index,
      signature=EMPTY_SIGNATURE
    )
  ).
  Verify that bls_verify(
    pubkey=validator.pubkey,
    message=exit_message,
    signature=exit.signature,
    domain=get_domain(state.fork_data, exit.slot, DOMAIN_EXIT),
  )
  Run initiate_validator_exit(
    state, exit.validator_index,
  )

func VerifyProposerSignature

func VerifyProposerSignature(
	block *pb.BeaconBlock,
) error

VerifyProposerSignature uses BLS signature verification to ensure the correct proposer created an incoming beacon block during state transition processing.

WIP - this is stubbed out until BLS is integrated into Prysm.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL