Skip to content

Commit

Permalink
add deneb serialization tests and some commands to cli
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Oct 27, 2024
1 parent f533bfd commit 20b80f8
Show file tree
Hide file tree
Showing 10 changed files with 2,648 additions and 11 deletions.
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"
}
9 changes: 3 additions & 6 deletions crates/consensus/src/fork/capella.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use crate::fork::bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH;
use crate::{
beacon::{
Attestation, AttesterSlashing, BeaconBlockHeader, Deposit, Eth1Data, ProposerSlashing,
Expand All @@ -8,7 +9,6 @@ use crate::{
compute::hash_tree_root,
errors::Error,
execution::BlockNumber,
fork::bellatrix::EXECUTION_PAYLOAD_TREE_DEPTH,
internal_prelude::*,
merkle::MerkleTree,
sync_protocol::{
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::fork::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
89 changes: 89 additions & 0 deletions crates/consensus/src/fork/deneb.rs
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());
}
}
}
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,
}
}
}
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(())
}
}
11 changes: 8 additions & 3 deletions crates/lodestar-rpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::errors::Error;
use crate::types::{
BeaconBlockRootResponse, BeaconHeaderResponse, FinalityCheckpointsResponse,
GenesisDataResponse, LightClientBootstrapResponse, LightClientFinalityUpdateResponse,
LightClientUpdatesResponse,
BeaconBlockResponse, BeaconBlockRootResponse, BeaconHeaderResponse,
FinalityCheckpointsResponse, GenesisDataResponse, LightClientBootstrapResponse,
LightClientFinalityUpdateResponse, LightClientUpdatesResponse,
};
use ethereum_consensus::beacon::Slot;
use ethereum_consensus::sync_protocol::SyncCommitteePeriod;
Expand Down Expand Up @@ -42,6 +42,11 @@ impl RPCClient {
.await
}

pub async fn get_beacon_block_by_slot(&self, slot: Slot) -> Result<BeaconBlockResponse> {
self.request_get(format!("/eth/v2/beacon/blocks/{}", slot))
.await
}

pub async fn get_finality_checkpoints(&self) -> Result<FinalityCheckpointsResponse> {
self.request_get("/eth/v1/beacon/states/head/finality_checkpoints")
.await
Expand Down
19 changes: 17 additions & 2 deletions crates/lodestar-rpc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use ethereum_consensus::{
beacon::{BeaconBlockHeader, Checkpoint, Root, Slot},
bls::Signature,
fork::deneb::{LightClientBootstrap, LightClientHeader, LightClientUpdate},
sync_protocol::FINALIZED_ROOT_DEPTH,
preset::mainnet::DenebBeaconBlock,
sync_protocol::{
SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, NEXT_SYNC_COMMITTEE_DEPTH,
SyncAggregate, SyncCommittee, CURRENT_SYNC_COMMITTEE_DEPTH, FINALIZED_ROOT_DEPTH,
NEXT_SYNC_COMMITTEE_DEPTH,
},
types::{H256, U64},
};
Expand Down Expand Up @@ -50,6 +51,20 @@ pub struct BeaconHeaderSignature {
pub signature: Signature,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BeaconBlockResponse {
pub data: BeaconBlockData,
pub execution_optimistic: bool,
pub finalized: bool,
pub version: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BeaconBlockData {
pub message: DenebBeaconBlock,
pub signature: Signature,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FinalityCheckpointsResponse {
pub data: FinalityCheckpoints,
Expand Down

0 comments on commit 20b80f8

Please sign in to comment.