Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

BeaconRootsVerify

Git Source

Verifies Beacon chain data using Merkle hashing to reconstruct the beacon block root, which can be queried on-chain via beacon roots contract (EIP-4788)

State Variables

BALANCES_INDEX

Beacon state balances list container field index in state

uint256 public constant BALANCES_INDEX = 10;

VALIDATORS_INDEX

Beacon state validators list container field index in state

uint256 public constant VALIDATORS_INDEX = 9;

VALIDATOR_PROOF_DEPTH

Beacon state validator proof depth in list container

uint256 public constant VALIDATOR_PROOF_DEPTH = 41;

BALANCE_PROOF_DEPTH

Beacon state balance proof depth in list container

uint256 public constant BALANCE_PROOF_DEPTH = 39;

BEACON_ROOTS

Address of beacon roots contract on ethereum (https://eips.ethereum.org/EIPS/eip-4788)

address public constant BEACON_ROOTS =
    0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02;

Functions

getParentBeaconBlockRoot

Fetches the parent block root from the Beacon Roots contract at a specific timestamp

function getParentBeaconBlockRoot(uint256 timestamp)
    public
    view
    returns (bytes32 root);

Parameters

NameTypeDescription
timestampuint256The timestamp for which to fetch the parent beacon block root

Returns

NameTypeDescription
rootbytes32The parent block root at the given timestamp

calculateBeaconHeaderMerkleRoot

Calculates the Merkle root of a given Beacon block header

function calculateBeaconHeaderMerkleRoot(BeaconBlockHeader calldata header)
    public
    pure
    returns (bytes32 root);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data

Returns

NameTypeDescription
rootbytes32The Merkle root of the block header

verifyBeaconHeaderMerkleRoot

Verifies the Merkle root of a given Beacon block header and root

function verifyBeaconHeaderMerkleRoot(
    BeaconBlockHeader calldata header,
    bytes32 root
) public pure returns (bool validRoot);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
rootbytes32The root to verify against

Returns

NameTypeDescription
validRootboolTrue if root matches Merkleized Header

verifyBeaconHeaderMerkleRoot

Verifies the Merkle root of a given Beacon block header against beacon roots contract

will only work for slots within the last 24 hours

function verifyBeaconHeaderMerkleRoot(
    BeaconBlockHeader calldata header,
    uint256 nextBlockTimestamp
) public view returns (bool validRoot);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
nextBlockTimestampuint256timestamp of following block to header to verify parent root in beaconroots call

Returns

NameTypeDescription
validRootboolTrue if beacon roots call matches Merkleized Header

verifyStateRoot

Verify a merkle proof of the beacon state root against a beacon block header root

function verifyStateRoot(
    bytes32 beaconBlockHeaderRoot,
    bytes32 beaconStateRoot,
    bytes32[] calldata proof
) public pure returns (bool validStateRoot);

Parameters

NameTypeDescription
beaconBlockHeaderRootbytes32merkle root of the beacon block header
beaconStateRootbytes32merkle root of the beacon state
proofbytes32[]merkle proof of its inclusion under beaconBlockHeaderRoot

Returns

NameTypeDescription
validStateRootboolTrue if successfully verified

calculateValidatorMerkleRoot

Calculates the Merkle root of a given Validator

function calculateValidatorMerkleRoot(Validator calldata validator)
    public
    pure
    returns (bytes32 root);

Parameters

NameTypeDescription
validatorValidatorThe Validator data

Returns

NameTypeDescription
rootbytes32The Merkle root of the block header

verifyValidator

Verify a merkle proof of the validator against a beacon state root

function verifyValidator(
    bytes32 beaconStateRoot,
    Validator calldata validator,
    bytes32[] calldata proof,
    uint256 valIndex
) public pure returns (bool validValidator);

Parameters

NameTypeDescription
beaconStateRootbytes32merkle root of the beacon state
validatorValidatorValidator struct data
proofbytes32[]merkle proof of the validator
valIndexuint256index of validator

Returns

NameTypeDescription
validValidatorboolTrue for successful verification

verifyValidator

Verify a merkle proof of the validator against beacon roots contract

function verifyValidator(
    BeaconBlockHeader calldata header,
    Validator calldata validator,
    bytes32[] calldata proof,
    uint256 valIndex,
    uint256 nextBlockTimestamp
) public view returns (bool validValidator);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
validatorValidatorValidator struct data
proofbytes32[]merkle proof of the validator against state root in header
valIndexuint256index of validator
nextBlockTimestampuint256timestamp of following block to header to verify parent root in beaconroots call

Returns

NameTypeDescription
validValidatorboolTrue for successful verification

verifyValidatorBalance

Verify a merkle proof of the validator balance against a beacon state root

function verifyValidatorBalance(
    BeaconBlockHeader calldata header,
    bytes32[] calldata proof,
    uint256 valIndex,
    uint256 balance,
    bytes32 balanceLeaf,
    uint256 nextBlockTimestamp
) public view returns (bool validValidatorBalance);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
proofbytes32[]merkle proof of the validator balance
valIndexuint256index of validator
balanceuint256declared balance of validator to prove
balanceLeafbytes3232 bytes chunk including packed balance
nextBlockTimestampuint256timestamp of following block to header to verify parent root in beaconroots call

Returns

NameTypeDescription
validValidatorBalanceboolTrue for successful verification

verifyValidatorPublicKey

Verify public key of a validator by merkle proof of the validator against a beacon state root

function verifyValidatorPublicKey(
    BeaconBlockHeader calldata header,
    Validator calldata validator,
    bytes32[] calldata proof,
    uint256 valIndex,
    bytes calldata pubkey,
    uint256 nextBlockTimestamp
) public view returns (bool validValidator);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
validatorValidatorValidator struct data
proofbytes32[]merkle proof of the validator
valIndexuint256index of validator
pubkeybytespublic key to verify
nextBlockTimestampuint256timestamp of following block to header to verify parent root in beaconroots call

Returns

NameTypeDescription
validValidatorboolTrue for successful verification

verifyValidatorEffectiveBalance

Verify effective balance of a validator by merkle proof of the validator against a beacon state root

function verifyValidatorEffectiveBalance(
    BeaconBlockHeader calldata header,
    Validator calldata validator,
    bytes32[] calldata proof,
    uint256 valIndex,
    uint64 effectiveBalance,
    uint256 nextBlockTimestamp
) public view returns (bool validValidator);

Parameters

NameTypeDescription
headerBeaconBlockHeaderThe Beacon block header data
validatorValidatorValidator struct data
proofbytes32[]merkle proof of the validator
valIndexuint256index of validator
effectiveBalanceuint64balance to verify
nextBlockTimestampuint256timestamp of following block to header to verify parent root in beaconroots call

Returns

NameTypeDescription
validValidatorboolTrue for successful verification

verifyValidatorWithdrawalAddress

Verify withdrawal address of a validator by merkle proof of the validator against a beacon state root

function verifyValidatorWithdrawalAddress(
    bytes32 beaconStateRoot,
    Validator calldata validator,
    bytes32[] calldata proof,
    uint256 valIndex,
    address withdrawalAddress
) public pure returns (bool validValidator);

Parameters

NameTypeDescription
beaconStateRootbytes32merkle root of the beacon state
validatorValidatorValidator struct data
proofbytes32[]merkle proof of the validator
valIndexuint256index of validator
withdrawalAddressaddressstaker address to verify

Returns

NameTypeDescription
validValidatorboolTrue for successful verification

extractBalance

function extractBalance(bytes32 chunk, uint256 offset)
    internal
    pure
    returns (uint64);

Errors

RootNotFound

error RootNotFound();

FieldMismatch

error FieldMismatch();

Structs

BeaconBlockHeader

https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#beaconblockheader

struct BeaconBlockHeader {
    uint64 slot;
    uint64 proposerIndex;
    bytes32 parentRoot;
    bytes32 stateRoot;
    bytes32 bodyRoot;
}

Validator

https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#validator

struct Validator {
    bytes pubkey;
    bytes32 withdrawalCredentials;
    uint64 effectiveBalance;
    bool slashed;
    uint64 activationEligibilityEpoch;
    uint64 activationEpoch;
    uint64 exitEpoch;
    uint64 withdrawableEpoch;
}