Skip to content

Commit

Permalink
Add tests for block_roots ancestry
Browse files Browse the repository at this point in the history
  • Loading branch information
pkakelas committed Nov 23, 2023
1 parent 0818f0a commit fe4196e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions feeder/src/eth/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ impl ConsensusRPC {
Ok(res.data.header.message)
}

pub async fn get_latest_beacon_block_header(&self) -> Result<BeaconBlockHeader> {
let req = format!("{}/eth/v1/beacon/headers/head", self.rpc);

let res: BeaconBlockHeaderResponse = get(&req)
.await
.map_err(|e| RpcError::new("beacon_header", e))?;

Ok(res.data.header.message)
}

pub async fn get_beacon_block(&self, slot: u64) -> Result<BeaconBlockAlias> {
let req = format!("{}/eth/v2/beacon/blocks/{}", self.rpc, slot);

Expand Down
2 changes: 1 addition & 1 deletion feeder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn main() {

let now = Instant::now();

let proof = prover
let _proof = prover
.prove_event(
first_message.clone(),
UpdateVariant::Finality(finality_update),
Expand Down
42 changes: 41 additions & 1 deletion feeder/src/prover/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub async fn prove_ancestry_with_block_roots(
* using the historical_roots beacon state property. The target block should be
* in a slot less than recent_block_slot - SLOTS_PER_HISTORICAL_ROOT.
*/
pub fn prove_ancestry_with_historical_roots(
pub fn _prove_ancestry_with_historical_roots(
_recent_block_state: &BeaconStateType,
_target_block_slot: u64,
) -> Result<AncestryProof> {
Expand All @@ -122,12 +122,18 @@ async fn get_state_proof(state_id: String, gindex: u64) -> Result<ProofResponse>

#[cfg(test)]
mod tests {
use crate::eth::consensus::ConsensusRPC;
use crate::eth::constants::CONSENSUS_RPC;
use crate::prover::consensus::{
generate_exec_payload_branch, generate_receipts_branch, generate_transactions_branch,
prove_ancestry_with_block_roots,
};
use consensus_types::consensus::BeaconBlockAlias;
use consensus_types::proofs::AncestryProof;
use core::panic;
use ssz_rs::{GeneralizedIndex, Merkleized};
use std::fs::File;
use tokio::test as tokio_test;

pub fn get_beacon_block() -> BeaconBlockAlias {
let file = File::open("./src/prover/testdata/beacon_block.json").unwrap();
Expand Down Expand Up @@ -207,4 +213,38 @@ mod tests {

assert!(is_proof_valid);
}

/**
* REQUIRES NETWORK REQUESTS
*/
#[tokio_test]
async fn test_block_roots_proof() {
let consensus = ConsensusRPC::new(CONSENSUS_RPC);
let latest_block = consensus.get_latest_beacon_block_header().await.unwrap();
let mut old_block = consensus
.get_beacon_block_header(latest_block.slot - 1000)
.await
.unwrap();

let proof =
prove_ancestry_with_block_roots(old_block.slot, latest_block.state_root.to_string())
.await
.unwrap();

match proof {
AncestryProof::BlockRoots {
block_roots_index,
block_root_proof,
} => {
let is_valid_proof = ssz_rs::verify_merkle_proof(
&old_block.hash_tree_root().unwrap(),
block_root_proof.as_slice(),
&GeneralizedIndex(block_roots_index as usize),
&latest_block.state_root,
);
assert!(is_valid_proof)
}
_ => panic!("Expected block roots proof"),
}
}
}

0 comments on commit fe4196e

Please sign in to comment.