Skip to content

Commit

Permalink
chore(forehose-client): add fetch beacon block example
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Oct 16, 2024
1 parent 77fff9b commit 32ea66e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
58 changes: 58 additions & 0 deletions crates/firehose-client/examples/fetch_beacon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! # Fetch Beacon Block
//!
//! Demonstrates how to fetch a single block from Beacon Firehose, using the `Fetch` API.

use firehose_client::{Chain, FirehoseClient};
use sf_protos::{
beacon::r#type::v1::Block as FirehoseBeaconBlock,
ethereum::r#type::v2::Block as FirehoseEthBlock,
};

#[tokio::test]
async fn main() {
// Show matching data from execution layer and beacon chain
let mut execution_layer_client = FirehoseClient::new(Chain::Ethereum);

let response = execution_layer_client
.fetch_block(20672593)
.await
.unwrap()
.unwrap();

let block = FirehoseEthBlock::try_from(response.into_inner()).unwrap();

assert_eq!(block.number, 20672593);
assert_eq!(
format!("0x{}", hex::encode(block.hash)).as_str(),
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4"
);

let mut beacon_client = FirehoseClient::new(Chain::Beacon);
// This is the slot number for the Beacon block we want to fetch, but right now
// we don't have a way to map the block number of the execution block to the slot number
// of the Beacon block.
let response = beacon_client.fetch_block(9881091).await.unwrap().unwrap();
let block = FirehoseBeaconBlock::try_from(response.into_inner()).unwrap();

assert_eq!(block.slot, 9881091);

let body = block.body.as_ref().unwrap();

match body {
Body::Deneb(body) => {
let execution_payload = body.execution_payload.as_ref().unwrap();

let block_hash = &execution_payload.block_hash;

assert_eq!(
format!("0x{}", hex::encode(block_hash)).as_str(),
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4"
);

let block_number = execution_payload.block_number;

assert_eq!(block_number, 20672593);
}
_ => unimplemented!(),
};
}
57 changes: 0 additions & 57 deletions crates/firehose-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,60 +293,3 @@ pub mod tls {
&TLS_CONFIG
}
}

#[cfg(test)]
mod tests {
use super::{FirehoseBeaconBlock, FirehoseEthBlock};
use crate::{Chain, FirehoseClient};
use sf_protos::beacon::r#type::v1::block::Body;

/// Demonstrates how to fetch a single block from Beacon Firehose, using the `Fetch` API.
#[tokio::test]
async fn test_firehose_beacon_fetch_block_by_slot() {
// Show matching data from execution layer and beacon chain
let mut execution_layer_client = FirehoseClient::new(Chain::Ethereum);

let response = execution_layer_client
.fetch_block(20672593)
.await
.unwrap()
.unwrap();

let block = FirehoseEthBlock::try_from(response.into_inner()).unwrap();

assert_eq!(block.number, 20672593);
assert_eq!(
format!("0x{}", hex::encode(block.hash)).as_str(),
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4"
);

let mut beacon_client = FirehoseClient::new(Chain::Beacon);
// This is the slot number for the Beacon block we want to fetch, but right now
// we don't have a way to map the block number of the execution block to the slot number
// of the Beacon block.
let response = beacon_client.fetch_block(9881091).await.unwrap().unwrap();
let block = FirehoseBeaconBlock::try_from(response.into_inner()).unwrap();

assert_eq!(block.slot, 9881091);

let body = block.body.as_ref().unwrap();

match body {
Body::Deneb(body) => {
let execution_payload = body.execution_payload.as_ref().unwrap();

let block_hash = &execution_payload.block_hash;

assert_eq!(
format!("0x{}", hex::encode(block_hash)).as_str(),
"0xea48ba1c8e38ea586239e9c5ec62949ddd79404c6006c099bb02a8b22ddd18e4"
);

let block_number = execution_payload.block_number;

assert_eq!(block_number, 20672593);
}
_ => unimplemented!(),
};
}
}

0 comments on commit 32ea66e

Please sign in to comment.