Module consensus

Module consensus 

Source
Expand description

Consensus types for Era post-merge history files

§Decoding

This crate only handles compression/decompression. To decode the SSZ data into concrete beacon types, use the Lighthouse types crate or another SSZ-compatible library.

§Examples

§Decoding a CompressedBeaconState

use types::{BeaconState, ChainSpec, MainnetEthSpec};
use reth_era::era::types::consensus::CompressedBeaconState;

fn decode_state(
    compressed_state: &CompressedBeaconState,
) -> Result<(), Box<dyn std::error::Error>> {
    let spec = ChainSpec::mainnet();

    // Decompress to get SSZ bytes
    let ssz_bytes = compressed_state.decompress()?;

    // Decode with fork-aware method, chainSpec determines fork from slot in SSZ
    let state = BeaconState::<MainnetEthSpec>::from_ssz_bytes(&ssz_bytes, &spec)
        .map_err(|e| format!("{:?}", e))?;

    println!("State slot: {}", state.slot());
    println!("Fork: {:?}", state.fork_name_unchecked());
    println!("Validators: {}", state.validators().len());
    println!("Finalized checkpoint: {:?}", state.finalized_checkpoint());
    Ok(())
}

§Decoding a CompressedSignedBeaconBlock

use consensus_types::{ForkName, ForkVersionDecode, MainnetEthSpec, SignedBeaconBlock};
use reth_era::era::types::consensus::CompressedSignedBeaconBlock;

// Decode using fork-aware decoding, fork must be known beforehand
fn decode_block(
    compressed: &CompressedSignedBeaconBlock,
    fork: ForkName,
) -> Result<(), Box<dyn std::error::Error>> {
    // Decompress to get SSZ bytes
    let ssz_bytes = compressed.decompress()?;

    let block = SignedBeaconBlock::<MainnetEthSpec>::from_ssz_bytes_by_fork(&ssz_bytes, fork)
        .map_err(|e| format!("{:?}", e))?;

    println!("Block slot: {}", block.message().slot());
    println!("Proposer index: {}", block.message().proposer_index());
    println!("Parent root: {:?}", block.message().parent_root());
    println!("State root: {:?}", block.message().state_root());

    Ok(())
}

Structs§

CompressedBeaconState
Compressed beacon state
CompressedSignedBeaconBlock
Compressed signed beacon block

Constants§

COMPRESSED_BEACON_STATE
CompressedBeaconState record type: [0x02, 0x00]
COMPRESSED_SIGNED_BEACON_BLOCK
CompressedSignedBeaconBlock record type: [0x01, 0x00]