Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move fork impls into fork directory and add tests #12

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,434 changes: 2,434 additions & 0 deletions crates/consensus/data/mainnet_block_10265184.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions crates/consensus/data/mainnet_header_10265184.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"slot": 10265184,
"proposer_index": 1532287,
"parent_root": "0x417a55d3a74a1ec7b364b451823c642bd03af2756f8d62a47b28d1201dfd13d4",
"state_root": "0x21b01660ae708225e169a64dfce6b288a6b214cccd2cfe8e146bcbf2021c815c",
"body_root": "0xc60be046dfd6099f3dc74c55a37f5e8829f0ec7a5f47f050a2ffecb3659eb57c"
}
4 changes: 4 additions & 0 deletions crates/consensus/src/fork.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub mod bellatrix;
pub mod capella;
pub mod deneb;

use crate::beacon::{Epoch, Slot, Version};
use crate::errors::Error;
use crate::internal_prelude::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mod test {
gen_execution_payload_field_proof, gen_execution_payload_proof, BeaconBlockHeader,
};
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::bellatrix::{LightClientUpdate, EXECUTION_PAYLOAD_TREE_DEPTH};
use crate::fork::bellatrix::{LightClientUpdate, EXECUTION_PAYLOAD_TREE_DEPTH};
use crate::merkle::is_valid_merkle_branch;
use crate::sync_protocol::{SyncCommittee, EXECUTION_PAYLOAD_DEPTH};
use crate::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub use crate::fork::bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH;
use crate::{
beacon::{
Attestation, AttesterSlashing, BeaconBlockHeader, Deposit, Eth1Data, ProposerSlashing,
Root, SignedBlsToExecutionChange, SignedVoluntaryExit, Slot, ValidatorIndex, Withdrawal,
BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX,
},
bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH,
bls::Signature,
compute::hash_tree_root,
errors::Error,
Expand Down Expand Up @@ -410,19 +410,16 @@ pub fn gen_execution_payload_field_proof<

#[cfg(test)]
mod test {
use super::{
gen_execution_payload_field_proof, gen_execution_payload_proof, BeaconBlockHeader,
};
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH;
use crate::merkle::is_valid_merkle_branch;
use crate::sync_protocol::EXECUTION_PAYLOAD_DEPTH;
use crate::{compute::hash_tree_root, types::H256};
use ssz_rs::Merkleized;
use std::fs;

#[test]
fn beacon_block_serialization() {
fn beacon_header_serialization() {
use crate::execution::{
EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX, EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,92 @@ pub fn gen_execution_payload_field_proof<
);
Ok((H256(tree.root().unwrap()), branch))
}

#[cfg(test)]
mod test {
use super::*;
use crate::beacon::BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX;
use crate::merkle::is_valid_merkle_branch;
use crate::sync_protocol::EXECUTION_PAYLOAD_DEPTH;
use crate::{compute::hash_tree_root, types::H256};
use ssz_rs::Merkleized;
use std::fs;

#[test]
fn beacon_block_serialization() {
use crate::execution::{
EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX, EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX,
};
let mut header: BeaconBlockHeader = serde_json::from_str(
&fs::read_to_string("./data/mainnet_header_10265184.json").unwrap(),
)
.unwrap();

let mut block: crate::preset::mainnet::DenebBeaconBlock = serde_json::from_str(
&fs::read_to_string("./data/mainnet_block_10265184.json").unwrap(),
)
.unwrap();

assert_eq!(header, block.clone().to_header());
assert_eq!(
header.hash_tree_root().unwrap(),
block.hash_tree_root().unwrap()
);

let (block_root, payload_proof) = gen_execution_payload_proof(&block.body).unwrap();
assert_eq!(
block_root.as_bytes(),
block.body.hash_tree_root().unwrap().as_bytes()
);

let payload_root = block.body.execution_payload.hash_tree_root().unwrap();
let payload_header = block.body.execution_payload.clone().to_header();

assert!(is_valid_merkle_branch(
H256::from_slice(payload_root.as_bytes()),
&payload_proof,
EXECUTION_PAYLOAD_DEPTH as u32,
BLOCK_BODY_EXECUTION_PAYLOAD_LEAF_INDEX as u64,
block_root
)
.is_ok());

{
let (root, proof) = gen_execution_payload_field_proof(
&payload_header,
EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX,
)
.unwrap();
assert_eq!(root.as_bytes(), payload_root.as_bytes());

assert!(is_valid_merkle_branch(
hash_tree_root(payload_header.state_root).unwrap().0.into(),
&proof,
EXECUTION_PAYLOAD_TREE_DEPTH as u32,
EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX as u64,
root,
)
.is_ok());
}
{
let (root, proof) = gen_execution_payload_field_proof(
&payload_header,
EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX,
)
.unwrap();
assert_eq!(root.as_bytes(), payload_root.as_bytes());

assert!(is_valid_merkle_branch(
hash_tree_root(payload_header.block_number)
.unwrap()
.0
.into(),
&proof,
EXECUTION_PAYLOAD_TREE_DEPTH as u32,
EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX as u64,
root,
)
.is_ok());
}
}
}
3 changes: 0 additions & 3 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ mod internal_prelude {
}

pub mod beacon;
pub mod bellatrix;
pub mod bls;
pub mod capella;
pub mod compute;
pub mod config;
pub mod context;
pub mod deneb;
pub mod errors;
pub mod execution;
pub mod fork;
Expand Down
12 changes: 6 additions & 6 deletions crates/consensus/src/preset/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const PRESET: Preset = Preset {
MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096,
};

pub type BellatrixBeaconBlock = crate::bellatrix::BeaconBlock<
pub type BellatrixBeaconBlock = crate::fork::bellatrix::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -43,12 +43,12 @@ pub type BellatrixBeaconBlock = crate::bellatrix::BeaconBlock<
{ PRESET.SYNC_COMMITTEE_SIZE },
>;

pub type BellatrixExecutionPayloadHeader = crate::bellatrix::ExecutionPayloadHeader<
pub type BellatrixExecutionPayloadHeader = crate::fork::bellatrix::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;

pub type CapellaBeaconBlock = crate::capella::BeaconBlock<
pub type CapellaBeaconBlock = crate::fork::capella::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -65,12 +65,12 @@ pub type CapellaBeaconBlock = crate::capella::BeaconBlock<
{ PRESET.SYNC_COMMITTEE_SIZE },
>;

pub type CapellaExecutionPayloadHeader = crate::capella::ExecutionPayloadHeader<
pub type CapellaExecutionPayloadHeader = crate::fork::capella::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;

pub type DenebBeaconBlock = crate::deneb::BeaconBlock<
pub type DenebBeaconBlock = crate::fork::deneb::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -88,7 +88,7 @@ pub type DenebBeaconBlock = crate::deneb::BeaconBlock<
{ PRESET.MAX_BLOB_COMMITMENTS_PER_BLOCK },
>;

pub type DenebExecutionPayloadHeader = crate::deneb::ExecutionPayloadHeader<
pub type DenebExecutionPayloadHeader = crate::fork::deneb::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;
12 changes: 6 additions & 6 deletions crates/consensus/src/preset/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const PRESET: Preset = Preset {
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16,
};

pub type BellatrixBeaconBlock = crate::bellatrix::BeaconBlock<
pub type BellatrixBeaconBlock = crate::fork::bellatrix::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -43,12 +43,12 @@ pub type BellatrixBeaconBlock = crate::bellatrix::BeaconBlock<
{ PRESET.SYNC_COMMITTEE_SIZE },
>;

pub type BellatrixExecutionPayloadHeader = crate::bellatrix::ExecutionPayloadHeader<
pub type BellatrixExecutionPayloadHeader = crate::fork::bellatrix::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;

pub type CapellaBeaconBlock = crate::capella::BeaconBlock<
pub type CapellaBeaconBlock = crate::fork::capella::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -65,12 +65,12 @@ pub type CapellaBeaconBlock = crate::capella::BeaconBlock<
{ PRESET.SYNC_COMMITTEE_SIZE },
>;

pub type CapellaExecutionPayloadHeader = crate::capella::ExecutionPayloadHeader<
pub type CapellaExecutionPayloadHeader = crate::fork::capella::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;

pub type DenebBeaconBlock = crate::deneb::BeaconBlock<
pub type DenebBeaconBlock = crate::fork::deneb::BeaconBlock<
{ PRESET.MAX_PROPOSER_SLASHINGS },
{ PRESET.MAX_VALIDATORS_PER_COMMITTEE },
{ PRESET.MAX_ATTESTER_SLASHINGS },
Expand All @@ -88,7 +88,7 @@ pub type DenebBeaconBlock = crate::deneb::BeaconBlock<
{ PRESET.MAX_BLOB_COMMITMENTS_PER_BLOCK },
>;

pub type DenebExecutionPayloadHeader = crate::deneb::ExecutionPayloadHeader<
pub type DenebExecutionPayloadHeader = crate::fork::deneb::ExecutionPayloadHeader<
{ PRESET.BYTES_PER_LOGS_BLOOM },
{ PRESET.MAX_EXTRA_DATA_BYTES },
>;
2 changes: 2 additions & 0 deletions crates/light-client-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ impl Cli {
match self.cmd {
Command::Init(cmd) => cmd.run(ctx).await,
Command::Update(cmd) => cmd.run(ctx).await,
Command::Header(cmd) => cmd.run(ctx).await,
Command::Block(cmd) => cmd.run(ctx).await,
}
}
}
2 changes: 1 addition & 1 deletion crates/light-client-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use ethereum_consensus::{
beacon::{Root, Slot},
compute::compute_sync_committee_period_at_slot,
context::ChainContext,
deneb::{self, LightClientUpdate, EXECUTION_PAYLOAD_TREE_DEPTH},
execution::{
BlockNumber, EXECUTION_PAYLOAD_BLOCK_NUMBER_LEAF_INDEX,
EXECUTION_PAYLOAD_STATE_ROOT_LEAF_INDEX,
},
fork::deneb::{self, LightClientUpdate, EXECUTION_PAYLOAD_TREE_DEPTH},
sync_protocol::SyncCommitteePeriod,
types::{H256, U64},
};
Expand Down
8 changes: 8 additions & 0 deletions crates/light-client-cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pub use block::BlockCommand;
use clap::Parser;
pub use header::HeaderCommand;
pub use init::InitCommand;
pub use update::UpdateCommand;

mod block;
mod header;
mod init;
mod update;

Expand All @@ -11,4 +15,8 @@ pub enum Command {
Init(InitCommand),
#[clap(about = "Update light client")]
Update(UpdateCommand),
#[clap(about = "Fetch specific header")]
Header(HeaderCommand),
#[clap(about = "Fetch specific block")]
Block(BlockCommand),
}
39 changes: 39 additions & 0 deletions crates/light-client-cli/src/commands/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::context::Context;
use anyhow::Result;
use clap::Parser;
use lodestar_rpc::client::RPCClient;

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct BlockCommand {
#[clap(long = "slot", help = "Slot number")]
pub slot: Option<u64>,
}

impl BlockCommand {
pub async fn run<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const SYNC_COMMITTEE_SIZE: usize,
>(
self,
ctx: Context<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES, SYNC_COMMITTEE_SIZE>,
) -> Result<()> {
let client = RPCClient::new(ctx.beacon_endpoint());

let res = match self.slot {
Some(slot) => client.get_beacon_block_by_slot(slot.into()).await?,
None => {
let res = client.get_finality_update::<
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>().await?;
client
.get_beacon_block_by_slot(res.data.finalized_header.beacon.slot)
.await?
}
};
println!("{}", serde_json::to_string_pretty(&res.data.message)?);
Ok(())
}
}
41 changes: 41 additions & 0 deletions crates/light-client-cli/src/commands/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::context::Context;
use anyhow::Result;
use clap::Parser;
use lodestar_rpc::client::RPCClient;

#[derive(Clone, Debug, Parser, PartialEq)]
pub struct HeaderCommand {
#[clap(long = "slot", help = "Slot number")]
pub slot: Option<u64>,
}

impl HeaderCommand {
pub async fn run<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
const SYNC_COMMITTEE_SIZE: usize,
>(
self,
ctx: Context<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES, SYNC_COMMITTEE_SIZE>,
) -> Result<()> {
let client = RPCClient::new(ctx.beacon_endpoint());
let res = match self.slot {
Some(slot) => client.get_beacon_header_by_slot(slot.into()).await?,
None => {
let res = client.get_finality_update::<
SYNC_COMMITTEE_SIZE,
BYTES_PER_LOGS_BLOOM,
MAX_EXTRA_DATA_BYTES,
>().await?;
client
.get_beacon_header_by_slot(res.data.finalized_header.beacon.slot)
.await?
}
};
println!(
"{}",
serde_json::to_string_pretty(&res.data.header.message)?
);
Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/light-client-cli/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::db::{FileDB, DB};
use crate::{errors::Error, state::LightClientStore};
use ethereum_consensus::config::Config;
use ethereum_consensus::context::ChainContext;
use ethereum_consensus::deneb::LightClientBootstrap;
use ethereum_consensus::fork::deneb::LightClientBootstrap;
use log::*;
use std::str::FromStr;

Expand Down
Loading