diff --git a/crates/firehose-client/examples/fetch_beacon.rs b/crates/firehose-client/examples/fetch_beacon.rs new file mode 100644 index 00000000..732ef34f --- /dev/null +++ b/crates/firehose-client/examples/fetch_beacon.rs @@ -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!(), + }; +} diff --git a/crates/firehose-client/src/lib.rs b/crates/firehose-client/src/lib.rs index 17089592..4c1a38f5 100644 --- a/crates/firehose-client/src/lib.rs +++ b/crates/firehose-client/src/lib.rs @@ -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!(), - }; - } -}