From abb4dd43c18f8d8afbc5661b182fa1c3d66d2b37 Mon Sep 17 00:00:00 2001 From: Steph Sinyakov Date: Tue, 29 Oct 2024 20:40:54 +0100 Subject: [PATCH] fix(alloy-provider): `get_block_by_number` arg (#1582) ## Motivation Different args in `alloy-provider` crate: ``` /// Gets a block by its [BlockHash], with full transactions or only hashes. async fn get_block_by_hash( &self, hash: BlockHash, kind: BlockTransactionsKind, <-- ) ... /// Get a block by its number. // TODO: Network associate async fn get_block_by_number( &self, number: BlockNumberOrTag, hydrate: bool, <-- ) ``` ## Solution Changing `hydrate` argument to `kind` to make methods symmetrical. ## PR Checklist - [ ] Added Tests - [ ] Added Documentation - [ ] Breaking changes --- crates/provider/src/fillers/gas.rs | 4 ++-- crates/provider/src/layers/cache.rs | 11 ++++++++--- crates/provider/src/provider/trait.rs | 28 ++++++++++++++------------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/crates/provider/src/fillers/gas.rs b/crates/provider/src/fillers/gas.rs index a9c1dffc4fd..fdcc1921984 100644 --- a/crates/provider/src/fillers/gas.rs +++ b/crates/provider/src/fillers/gas.rs @@ -10,7 +10,7 @@ use alloy_consensus::BlockHeader; use alloy_eips::eip4844::BLOB_TX_MIN_BLOB_GASPRICE; use alloy_json_rpc::RpcError; use alloy_network::{Network, TransactionBuilder, TransactionBuilder4844}; -use alloy_network_primitives::BlockResponse; +use alloy_network_primitives::{BlockResponse, BlockTransactionsKind}; use alloy_rpc_types_eth::BlockNumberOrTag; use alloy_transport::{Transport, TransportResult}; use futures::FutureExt; @@ -227,7 +227,7 @@ where } provider - .get_block_by_number(BlockNumberOrTag::Latest, false) + .get_block_by_number(BlockNumberOrTag::Latest, BlockTransactionsKind::Hashes) .await? .ok_or(RpcError::NullResp)? .header() diff --git a/crates/provider/src/layers/cache.rs b/crates/provider/src/layers/cache.rs index 671ddd4f442..7033f3f59ba 100644 --- a/crates/provider/src/layers/cache.rs +++ b/crates/provider/src/layers/cache.rs @@ -156,11 +156,16 @@ where async fn get_block_by_number( &self, number: BlockNumberOrTag, - hydrate: bool, + kind: BlockTransactionsKind, ) -> TransportResult> { - let req = RequestType::new("eth_getBlockByNumber", (number, hydrate)); + let full = match kind { + BlockTransactionsKind::Full => true, + BlockTransactionsKind::Hashes => false, + }; + + let req = RequestType::new("eth_getBlockByNumber", (number, full)); - cache_get_or_fetch(&self.cache, req, self.inner.get_block_by_number(number, hydrate)).await + cache_get_or_fetch(&self.cache, req, self.inner.get_block_by_number(number, kind)).await } async fn get_block_by_hash( diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index 8daa60e7988..4bda40517d1 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -225,7 +225,7 @@ pub trait Provider: Some(base_fee) if base_fee != 0 => base_fee, _ => { // empty response, fetch basefee from latest block directly - self.get_block_by_number(BlockNumberOrTag::Latest, false) + self.get_block_by_number(BlockNumberOrTag::Latest, BlockTransactionsKind::Hashes) .await? .ok_or(RpcError::NullResp)? .header() @@ -285,10 +285,7 @@ pub trait Provider: ) -> TransportResult> { match block { BlockId::Hash(hash) => self.get_block_by_hash(hash.into(), kind).await, - BlockId::Number(number) => { - let full = matches!(kind, BlockTransactionsKind::Full); - self.get_block_by_number(number, full).await - } + BlockId::Number(number) => self.get_block_by_number(number, kind).await, } } @@ -324,14 +321,19 @@ pub trait Provider: async fn get_block_by_number( &self, number: BlockNumberOrTag, - hydrate: bool, + kind: BlockTransactionsKind, ) -> TransportResult> { + let full = match kind { + BlockTransactionsKind::Full => true, + BlockTransactionsKind::Hashes => false, + }; + let block = self .client() - .request::<_, Option>("eth_getBlockByNumber", (number, hydrate)) + .request::<_, Option>("eth_getBlockByNumber", (number, full)) .await? .map(|mut block| { - if !hydrate { + if !full { // this ensures an empty response for `Hashes` has the expected form // this is required because deserializing [] is ambiguous block.transactions_mut().convert_to_hashes(); @@ -1515,7 +1517,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); let num = 0; let tag: BlockNumberOrTag = num.into(); - let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); + let block = provider.get_block_by_number(tag, BlockTransactionsKind::Full).await.unwrap().unwrap(); let hash = block.header.hash; let block = provider.get_block_by_hash(hash, BlockTransactionsKind::Full).await.unwrap().unwrap(); @@ -1527,7 +1529,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); let num = 0; let tag: BlockNumberOrTag = num.into(); - let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); + let block = provider.get_block_by_number(tag, BlockTransactionsKind::Full).await.unwrap().unwrap(); let hash = block.header.hash; let block: Block = provider .raw_request::<(B256, bool), Block>("eth_getBlockByHash".into(), (hash, true)) @@ -1541,7 +1543,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); let num = 0; let tag: BlockNumberOrTag = num.into(); - let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); + let block = provider.get_block_by_number(tag, BlockTransactionsKind::Full).await.unwrap().unwrap(); assert_eq!(block.header.number, num); } @@ -1550,7 +1552,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); let num = 0; let tag: BlockNumberOrTag = num.into(); - let block = provider.get_block_by_number(tag, true).await.unwrap().unwrap(); + let block = provider.get_block_by_number(tag, BlockTransactionsKind::Full).await.unwrap().unwrap(); assert_eq!(block.header.number, num); } @@ -1774,7 +1776,7 @@ mod tests { async fn test_empty_transactions() { let provider = ProviderBuilder::new().on_anvil(); - let block = provider.get_block_by_number(0.into(), false).await.unwrap().unwrap(); + let block = provider.get_block_by_number(0.into(), BlockTransactionsKind::Hashes).await.unwrap().unwrap(); assert!(block.transactions.is_hashes()); } }