Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pending block with receipts #3404

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/blockchain-tree/src/shareable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTreeViewer
self.tree.read().pending_block().cloned()
}

fn pending_block_and_receipts(&self) -> Option<(SealedBlock, Vec<Receipt>)> {
let tree = self.tree.read();
let pending_block = tree.pending_block()?.clone();
let receipts = tree.receipts_by_block_hash(pending_block.hash)?.to_vec();
Some((pending_block, receipts))
}

fn receipts_by_block_hash(&self, block_hash: BlockHash) -> Option<Vec<Receipt>> {
let tree = self.tree.read();
Some(tree.receipts_by_block_hash(block_hash)?.to_vec())
Expand Down
6 changes: 6 additions & 0 deletions crates/interfaces/src/blockchain_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ pub trait BlockchainTreeViewer: Send + Sync {
self.block_by_hash(self.pending_block_num_hash()?.hash)
}

/// Returns the pending block and its receipts in one call.
///
/// This exists to prevent a potential data race if the pending block changes in between
/// [Self::pending_block] and [Self::pending_receipts] calls.
fn pending_block_and_receipts(&self) -> Option<(SealedBlock, Vec<Receipt>)>;

/// Returns the pending receipts if there is one.
fn pending_receipts(&self) -> Option<Vec<Receipt>> {
self.receipts_by_block_hash(self.pending_block_num_hash()?.hash)
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ impl<DB: Database> BlockReader for ProviderFactory<DB> {
self.provider()?.pending_block()
}

fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>> {
self.provider()?.pending_block_and_receipts()
}

fn ommers(&self, id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
self.provider()?.ommers(id)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,10 @@ impl<'this, TX: DbTx<'this>> BlockReader for DatabaseProvider<'this, TX> {
Ok(None)
}

fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>> {
Ok(None)
}

fn ommers(&self, id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
if let Some(number) = self.convert_hash_or_number(id)? {
// If the Paris (Merge) hardfork block is known and block is after it, return empty
Expand Down
8 changes: 8 additions & 0 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ where
Ok(self.tree.pending_block())
}

fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>> {
Ok(self.tree.pending_block_and_receipts())
}

fn ommers(&self, id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
self.database.provider()?.ommers(id)
}
Expand Down Expand Up @@ -628,6 +632,10 @@ where
self.tree.pending_block_num_hash()
}

fn pending_block_and_receipts(&self) -> Option<(SealedBlock, Vec<Receipt>)> {
self.tree.pending_block_and_receipts()
}

fn receipts_by_block_hash(&self, block_hash: BlockHash) -> Option<Vec<Receipt>> {
self.tree.receipts_by_block_hash(block_hash)
}
Expand Down
12 changes: 8 additions & 4 deletions crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ impl TransactionsProvider for MockEthProvider {
Ok(map.into_values().collect())
}

fn senders_by_tx_range(&self, _range: impl RangeBounds<TxNumber>) -> Result<Vec<Address>> {
unimplemented!()
}

fn transactions_by_tx_range(
&self,
_range: impl RangeBounds<TxNumber>,
) -> Result<Vec<reth_primitives::TransactionSignedNoHash>> {
unimplemented!()
}

fn senders_by_tx_range(&self, _range: impl RangeBounds<TxNumber>) -> Result<Vec<Address>> {
unimplemented!()
}

fn transaction_sender(&self, _id: TxNumber) -> Result<Option<Address>> {
unimplemented!()
}
Expand Down Expand Up @@ -324,6 +324,10 @@ impl BlockReader for MockEthProvider {
Ok(None)
}

fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>> {
Ok(None)
}

fn ommers(&self, _id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
Ok(None)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/provider/src/test_utils/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl BlockReader for NoopProvider {
Ok(None)
}

fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>> {
Ok(None)
}

fn ommers(&self, _id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
Ok(None)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/storage/provider/src/traits/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reth_db::models::StoredBlockBodyIndices;
use reth_interfaces::Result;
use reth_primitives::{
Block, BlockHashOrNumber, BlockId, BlockNumber, BlockNumberOrTag, BlockWithSenders, Header,
SealedBlock, SealedHeader, H256,
Receipt, SealedBlock, SealedHeader, H256,
};

/// A helper enum that represents the origin of the requested block.
Expand Down Expand Up @@ -72,6 +72,9 @@ pub trait BlockReader:
/// and the caller does not know the hash.
fn pending_block(&self) -> Result<Option<SealedBlock>>;

/// Returns the pending block and receipts if available.
fn pending_block_and_receipts(&self) -> Result<Option<(SealedBlock, Vec<Receipt>)>>;

/// Returns the ommers/uncle headers of the given block from the database.
///
/// Returns `None` if block is not found.
Expand Down