Skip to content

Commit

Permalink
add prover feature to light-client-verifier
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 4, 2024
1 parent 52d0ac1 commit 7621de9
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 149 deletions.
4 changes: 3 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc", "serde
milagro_bls = { git = "https://github.com/datachainlab/milagro_bls", rev = "bc2b5b5e8d48b7e2e1bfaa56dc2d93e13cb32095", default-features = false }
ssz-rs = { git = "https://github.com/bluele/ssz_rs", branch = "serde-no-std", default-features = false, features = ["serde"] }
ssz-rs-derive = { git = "https://github.com/bluele/ssz_rs", branch = "serde-no-std", default-features = false }
rs_merkle = { version = "1.2.0", default-features = false }
rs_merkle = { version = "1.2.0", default-features = false, optional = true}
primitive-types = { version = "0.12.1", default-features = false }

[dev-dependencies]
serde_json = "1.0.91"
rs_merkle = { version = "1.2.0", default-features = false }

[features]
default = ["std"]
std = []
prover = ["rs_merkle"]
87 changes: 46 additions & 41 deletions crates/consensus/src/fork/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use crate::{
},
bls::Signature,
compute::hash_tree_root,
errors::Error,
internal_prelude::*,
merkle::MerkleTree,
sync_protocol::{SyncAggregate, SyncCommittee},
types::{Address, ByteList, ByteVector, Bytes32, H256, U256, U64},
};
Expand Down Expand Up @@ -259,49 +257,56 @@ pub struct LightClientUpdate<const SYNC_COMMITTEE_SIZE: usize> {
pub signature_slot: Slot,
}

pub fn gen_execution_payload_field_proof<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
subtree_index: usize,
) -> Result<(Root, Vec<H256>), Error> {
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
Default::default(),
Default::default(),
] as [_; 16])
.as_ref(),
);
Ok((
H256(tree.root().unwrap()),
tree.proof(&[subtree_index])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect::<Vec<H256>>(),
))
#[cfg(any(feature = "prover", test))]
pub mod prover {
use super::*;
use crate::{errors::Error, merkle::MerkleTree};

pub fn gen_execution_payload_field_proof<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
subtree_index: usize,
) -> Result<(Root, Vec<H256>), Error> {
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
Default::default(),
Default::default(),
] as [_; 16])
.as_ref(),
);
Ok((
H256(tree.root().unwrap()),
tree.proof(&[subtree_index])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect::<Vec<H256>>(),
))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::errors::Error;
use crate::fork::bellatrix::{LightClientUpdate, BELLATRIX_FORK_SPEC};
use crate::merkle::{get_subtree_index, is_valid_normalized_merkle_branch};
use crate::merkle::{get_subtree_index, is_valid_normalized_merkle_branch, MerkleTree};
use crate::sync_protocol::SyncCommittee;
use crate::{
beacon::DOMAIN_SYNC_COMMITTEE,
Expand Down Expand Up @@ -354,7 +359,7 @@ mod tests {
.is_ok());

{
let (root, proof) = gen_execution_payload_field_proof(
let (root, proof) = prover::gen_execution_payload_field_proof(
&payload_header,
get_subtree_index(BELLATRIX_FORK_SPEC.execution_payload_state_root_gindex) as usize,
)
Expand All @@ -369,7 +374,7 @@ mod tests {
.is_ok());
}
{
let (root, proof) = gen_execution_payload_field_proof(
let (root, proof) = prover::gen_execution_payload_field_proof(
&payload_header,
get_subtree_index(BELLATRIX_FORK_SPEC.execution_payload_block_number_gindex)
as usize,
Expand Down
87 changes: 46 additions & 41 deletions crates/consensus/src/fork/capella.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::{
},
bls::Signature,
compute::hash_tree_root,
errors::Error,
internal_prelude::*,
merkle::MerkleTree,
sync_protocol::{SyncAggregate, SyncCommittee},
types::{Address, ByteList, ByteVector, Bytes32, H256, U256, U64},
};
Expand Down Expand Up @@ -293,49 +291,56 @@ pub struct LightClientHeader<const BYTES_PER_LOGS_BLOOM: usize, const MAX_EXTRA_
pub execution_branch: Vec<H256>,
}

pub fn gen_execution_payload_field_proof<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, Vec<H256>), Error> {
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
payload.withdrawals_root.0,
Default::default(),
] as [_; 16])
.as_ref(),
);
Ok((
H256(tree.root().unwrap()),
tree.proof(&[leaf_index])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect::<Vec<H256>>(),
))
#[cfg(any(feature = "prover", test))]
pub mod prover {
use super::*;
use crate::{errors::Error, merkle::MerkleTree};

pub fn gen_execution_payload_field_proof<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
>(
payload: &ExecutionPayloadHeader<BYTES_PER_LOGS_BLOOM, MAX_EXTRA_DATA_BYTES>,
leaf_index: usize,
) -> Result<(Root, Vec<H256>), Error> {
let tree = MerkleTree::from_leaves(
([
payload.parent_hash.0,
hash_tree_root(payload.fee_recipient.clone()).unwrap().0,
payload.state_root.0,
payload.receipts_root.0,
hash_tree_root(payload.logs_bloom.clone()).unwrap().0,
payload.prev_randao.0,
hash_tree_root(payload.block_number).unwrap().0,
hash_tree_root(payload.gas_limit).unwrap().0,
hash_tree_root(payload.gas_used).unwrap().0,
hash_tree_root(payload.timestamp).unwrap().0,
hash_tree_root(payload.extra_data.clone()).unwrap().0,
hash_tree_root(payload.base_fee_per_gas.clone()).unwrap().0,
payload.block_hash.0,
payload.transactions_root.0,
payload.withdrawals_root.0,
Default::default(),
] as [_; 16])
.as_ref(),
);
Ok((
H256(tree.root().unwrap()),
tree.proof(&[leaf_index])
.proof_hashes()
.iter()
.map(|h| H256::from_slice(h))
.collect::<Vec<H256>>(),
))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::merkle::{get_subtree_index, is_valid_normalized_merkle_branch};
use crate::merkle::{get_subtree_index, is_valid_normalized_merkle_branch, MerkleTree};
use crate::{compute::hash_tree_root, types::H256};
use rs_merkle::Error;
use ssz_rs::Merkleized;
use std::fs;

Expand Down Expand Up @@ -375,7 +380,7 @@ mod tests {
.is_ok());

{
let (root, proof) = gen_execution_payload_field_proof(
let (root, proof) = prover::gen_execution_payload_field_proof(
&payload_header,
get_subtree_index(CAPELLA_FORK_SPEC.execution_payload_state_root_gindex) as usize,
)
Expand All @@ -390,7 +395,7 @@ mod tests {
.is_ok());
}
{
let (root, proof) = gen_execution_payload_field_proof(
let (root, proof) = prover::gen_execution_payload_field_proof(
&payload_header,
get_subtree_index(CAPELLA_FORK_SPEC.execution_payload_block_number_gindex) as usize,
)
Expand Down
Loading

0 comments on commit 7621de9

Please sign in to comment.