BeaconRootsVerify
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
Name | Type | Description |
---|---|---|
timestamp | uint256 | The timestamp for which to fetch the parent beacon block root |
Returns
Name | Type | Description |
---|---|---|
root | bytes32 | The 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
Returns
Name | Type | Description |
---|---|---|
root | bytes32 | The 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
root | bytes32 | The root to verify against |
Returns
Name | Type | Description |
---|---|---|
validRoot | bool | True 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
nextBlockTimestamp | uint256 | timestamp of following block to header to verify parent root in beaconroots call |
Returns
Name | Type | Description |
---|---|---|
validRoot | bool | True 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
Name | Type | Description |
---|---|---|
beaconBlockHeaderRoot | bytes32 | merkle root of the beacon block header |
beaconStateRoot | bytes32 | merkle root of the beacon state |
proof | bytes32[] | merkle proof of its inclusion under beaconBlockHeaderRoot |
Returns
Name | Type | Description |
---|---|---|
validStateRoot | bool | True if successfully verified |
calculateValidatorMerkleRoot
Calculates the Merkle root of a given Validator
function calculateValidatorMerkleRoot(Validator calldata validator)
public
pure
returns (bytes32 root);
Parameters
Name | Type | Description |
---|---|---|
validator | Validator | The Validator data |
Returns
Name | Type | Description |
---|---|---|
root | bytes32 | The 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
Name | Type | Description |
---|---|---|
beaconStateRoot | bytes32 | merkle root of the beacon state |
validator | Validator | Validator struct data |
proof | bytes32[] | merkle proof of the validator |
valIndex | uint256 | index of validator |
Returns
Name | Type | Description |
---|---|---|
validValidator | bool | True 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
validator | Validator | Validator struct data |
proof | bytes32[] | merkle proof of the validator against state root in header |
valIndex | uint256 | index of validator |
nextBlockTimestamp | uint256 | timestamp of following block to header to verify parent root in beaconroots call |
Returns
Name | Type | Description |
---|---|---|
validValidator | bool | True 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
proof | bytes32[] | merkle proof of the validator balance |
valIndex | uint256 | index of validator |
balance | uint256 | declared balance of validator to prove |
balanceLeaf | bytes32 | 32 bytes chunk including packed balance |
nextBlockTimestamp | uint256 | timestamp of following block to header to verify parent root in beaconroots call |
Returns
Name | Type | Description |
---|---|---|
validValidatorBalance | bool | True 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
validator | Validator | Validator struct data |
proof | bytes32[] | merkle proof of the validator |
valIndex | uint256 | index of validator |
pubkey | bytes | public key to verify |
nextBlockTimestamp | uint256 | timestamp of following block to header to verify parent root in beaconroots call |
Returns
Name | Type | Description |
---|---|---|
validValidator | bool | True 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
Name | Type | Description |
---|---|---|
header | BeaconBlockHeader | The Beacon block header data |
validator | Validator | Validator struct data |
proof | bytes32[] | merkle proof of the validator |
valIndex | uint256 | index of validator |
effectiveBalance | uint64 | balance to verify |
nextBlockTimestamp | uint256 | timestamp of following block to header to verify parent root in beaconroots call |
Returns
Name | Type | Description |
---|---|---|
validValidator | bool | True 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
Name | Type | Description |
---|---|---|
beaconStateRoot | bytes32 | merkle root of the beacon state |
validator | Validator | Validator struct data |
proof | bytes32[] | merkle proof of the validator |
valIndex | uint256 | index of validator |
withdrawalAddress | address | staker address to verify |
Returns
Name | Type | Description |
---|---|---|
validValidator | bool | True 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;
}