Skip to content

Commit

Permalink
Creates example code that processes beacon blocks from slots 2000-2034
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoAlice committed Oct 24, 2024
1 parent cf3c404 commit de0648b
Show file tree
Hide file tree
Showing 38 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ quote = "1.0.18"
clap = { version = "4.5.3", features = ["derive"] }
convert_case = "0.6.0"
walkdir = "2.3.3"

[profile.release]
debug = true
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":404,"message":"NOT_FOUND: beacon block at slot 2005","stacktraces":[]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":404,"message":"NOT_FOUND: beacon block at slot 2006","stacktraces":[]}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code":404,"message":"NOT_FOUND: beacon block at slot 2033","stacktraces":[]}
Binary file not shown.
Binary file not shown.
49 changes: 49 additions & 0 deletions ethereum-consensus/examples/state_transition_mainnet_blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use ethereum_consensus::{
phase0::mainnet as spec,
state_transition::mainnet::{Context, Executor},
types::{mainnet::SignedBeaconBlock, BeaconState},
};
use ssz_rs::prelude::*;
use std::{error::Error, fs};

fn main() -> std::result::Result<(), Box<dyn Error>> {
println!("this example illustrates how the spec applies state transitions to mainnet data.");

// Read and deserialize prestate
let state_path = "./ethereum-consensus/examples/data/beacon_states/state_1999.ssz";
let f = fs::read(state_path).unwrap();
let prestate = spec::BeaconState::deserialize(&f)?;
let prestate = BeaconState::Phase0(prestate);

// Create executor
let context = Context::for_mainnet();
let mut executor = Executor::new(prestate, context);

// Read and process blocks 2000-2034
for slot in 2000..=2034 {
let block_path =
format!("./ethereum-consensus/examples/data/beacon_blocks/block_{}.ssz", slot);
let block_bytes = fs::read(&block_path)?;

// Error handling: skip missed slots
if block_bytes.len() < 100 {
match std::str::from_utf8(&block_bytes) {
Ok(text) if text.contains("NOT_FOUND") => {
println!("Slot {} was skipped (no block produced)", slot);
continue;
}
_ => {
println!("Unexpected small file for slot {}", slot);
continue;
}
}
}

// Process block
let signed_block = spec::SignedBeaconBlock::deserialize(&block_bytes)?;
let block = SignedBeaconBlock::Phase0(signed_block);
executor.apply_block(&block)?;
println!("Block at slot {} was processed.", slot)
}
Ok(())
}

0 comments on commit de0648b

Please sign in to comment.