From 8094ab18dbb230f177babceeb3d72c7a4c424490 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 22 Feb 2023 15:28:00 +0100 Subject: [PATCH] feat: add BlockProvider::ommers (#1509) --- crates/storage/provider/src/providers/mod.rs | 12 ++++++++++++ crates/storage/provider/src/test_utils/mock.rs | 4 ++++ crates/storage/provider/src/test_utils/noop.rs | 8 ++++++-- crates/storage/provider/src/traits/block.rs | 11 +++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index 8b8b1b2b87..0fffa67634 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -135,6 +135,18 @@ impl BlockProvider for ShareableDatabase { Ok(None) } + + fn ommers(&self, id: BlockId) -> Result>> { + if let Some(number) = self.block_number_for_id(id)? { + let tx = self.db.tx()?; + // TODO: this can be optimized to return empty Vec post-merge + let ommers = + tx.get::(number)?.map(|o| o.ommers).unwrap_or_default(); + return Ok(Some(ommers)) + } + + Ok(None) + } } impl TransactionsProvider for ShareableDatabase { diff --git a/crates/storage/provider/src/test_utils/mock.rs b/crates/storage/provider/src/test_utils/mock.rs index d609de6b35..1c3c6ad022 100644 --- a/crates/storage/provider/src/test_utils/mock.rs +++ b/crates/storage/provider/src/test_utils/mock.rs @@ -198,6 +198,10 @@ impl BlockProvider for MockEthProvider { } } } + + fn ommers(&self, _id: BlockId) -> Result>> { + Ok(None) + } } impl AccountProvider for MockEthProvider { diff --git a/crates/storage/provider/src/test_utils/noop.rs b/crates/storage/provider/src/test_utils/noop.rs index 67900d2020..a71d4882ab 100644 --- a/crates/storage/provider/src/test_utils/noop.rs +++ b/crates/storage/provider/src/test_utils/noop.rs @@ -35,14 +35,18 @@ impl BlockProvider for NoopProvider { fn block(&self, _id: BlockId) -> Result> { Ok(None) } + + fn ommers(&self, _id: BlockId) -> Result>> { + Ok(None) + } } impl TransactionsProvider for NoopProvider { - fn transaction_by_hash(&self, _hash: TxHash) -> Result> { + fn transaction_by_id(&self, _id: TxNumber) -> Result> { Ok(None) } - fn transaction_by_id(&self, _id: TxNumber) -> Result> { + fn transaction_by_hash(&self, _hash: TxHash) -> Result> { Ok(None) } diff --git a/crates/storage/provider/src/traits/block.rs b/crates/storage/provider/src/traits/block.rs index 7d289e2e9a..2b04b601e0 100644 --- a/crates/storage/provider/src/traits/block.rs +++ b/crates/storage/provider/src/traits/block.rs @@ -1,15 +1,22 @@ use crate::{BlockIdProvider, HeaderProvider, TransactionsProvider}; use reth_interfaces::Result; -use reth_primitives::{Block, BlockId, BlockNumberOrTag, H256}; +use reth_primitives::{Block, BlockId, BlockNumberOrTag, Header, H256}; /// Api trait for fetching `Block` related data. #[auto_impl::auto_impl(&, Arc)] pub trait BlockProvider: BlockIdProvider + HeaderProvider + TransactionsProvider + Send + Sync { - /// Returns the block. Returns `None` if block is not found. + /// Returns the block. + /// + /// Returns `None` if block is not found. fn block(&self, id: BlockId) -> Result>; + /// Returns the ommers/uncle headers of the given block. + /// + /// Returns `None` if block is not found. + fn ommers(&self, id: BlockId) -> Result>>; + /// Returns the block. Returns `None` if block is not found. fn block_by_hash(&self, hash: H256) -> Result> { self.block(hash.into())