Skip to content

Commit

Permalink
feat: add proof verification example
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrohba1 committed Nov 6, 2024
1 parent 0df8e71 commit 48702bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/forrestrie-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ prost-wkt.workspace = true
prost-wkt-types.workspace = true
reqwest = { workspace = true, features = ["json"] }
reth-primitives.workspace = true
reth-trie-common.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
ssz_types.workspace = true
Expand Down
25 changes: 23 additions & 2 deletions crates/forrestrie-examples/examples/receipts_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
//!
use firehose_client::client::{Chain, FirehoseClient};
use firehose_protos::ethereum_v2::{self, eth_block::FullReceipt, Block};
use forrestrie::execution_layer::build_trie_with_proofs;
use forrestrie::execution_layer::{build_trie_with_proofs, TargetLeaf, TargetLeaves};
use reth_primitives::ReceiptWithBloom;
use reth_trie_common::proof::verify_proof;

const EXECUTION_BLOCK_NUMBER: u64 = 20759937;

Expand Down Expand Up @@ -34,12 +35,32 @@ async fn main() {
.map(|full_receipt| full_receipt.receipt.clone())
.collect();

let mut hb = build_trie_with_proofs(&receipts_with_bloom, &[1, 2, 3]);
// These are de indexes of receipts on which proofs have to be generated
let target_idxs = &[1, 2, 3];
let targets = TargetLeaves::from_indices(target_idxs, &receipts_with_bloom).unwrap();
let mut hb = build_trie_with_proofs(&receipts_with_bloom, target_idxs);

// produces the root, which matches the root of the blocks.
// hb.root() also calculates the proofs and store them in the HashBuilder.
let root = hb.root();

let calc_root = eth1_block.calculate_receipt_root();
println!("roots: {:?}, {:?}", root, calc_root);

// proofs can be taken and sorted, so each proof matches one of the target.
// each proof of a specific target receipt is provided in `take_proof_nodes()`
// and can be stored or verified singularly
let proof = hb.take_proof_nodes();
for target in targets {
let _verification = verify_proof(
hb.root(),
target.nibbles.clone(),
Some(target.value.to_vec()),
proof
.clone()
.matching_nodes_sorted(&target.nibbles)
.iter()
.map(|(_, node)| node),
);
}
}

0 comments on commit 48702bb

Please sign in to comment.