Skip to content

Commit

Permalink
feat: add get blobs exact (#4482)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Sep 5, 2023
1 parent 843d504 commit f6e5826
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
5 changes: 1 addition & 4 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,10 @@ where
// only determine cancun fields when active
if chain_spec.is_cancun_activated_at_timestamp(attributes.timestamp) {
// grab the blob sidecars from the executed txs
let blobs = pool.get_all_blobs(
blob_sidecars = pool.get_all_blobs_exact(
executed_txs.iter().filter(|tx| tx.is_eip4844()).map(|tx| tx.hash).collect(),
)?;

// map to just the sidecars
blob_sidecars = blobs.into_iter().map(|(_, sidecars)| sidecars).collect();

excess_blob_gas = if chain_spec.is_cancun_activated_at_timestamp(parent_block.timestamp) {
let parent_excess_blob_gas = parent_block.excess_blob_gas.unwrap_or_default();
let parent_blob_gas_used = parent_block.blob_gas_used.unwrap_or_default();
Expand Down
18 changes: 16 additions & 2 deletions crates/transaction-pool/src/blobstore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl BlobStore for InMemoryBlobStore {

// Retrieves the decoded blob data for the given transaction hash.
fn get(&self, tx: H256) -> Result<Option<BlobTransactionSidecar>, BlobStoreError> {
let store = self.inner.store.write();
let store = self.inner.store.read();
Ok(store.get(&tx).cloned())
}

Expand All @@ -92,7 +92,7 @@ impl BlobStore for InMemoryBlobStore {
txs: Vec<H256>,
) -> Result<Vec<(H256, BlobTransactionSidecar)>, BlobStoreError> {
let mut items = Vec::with_capacity(txs.len());
let store = self.inner.store.write();
let store = self.inner.store.read();
for tx in txs {
if let Some(item) = store.get(&tx) {
items.push((tx, item.clone()));
Expand All @@ -102,6 +102,20 @@ impl BlobStore for InMemoryBlobStore {
Ok(items)
}

fn get_exact(&self, txs: Vec<H256>) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError> {
let mut items = Vec::with_capacity(txs.len());
let store = self.inner.store.read();
for tx in txs {
if let Some(item) = store.get(&tx) {
items.push(item.clone());
} else {
return Err(BlobStoreError::MissingSidecar(tx))
}
}

Ok(items)
}

fn data_size_hint(&self) -> Option<usize> {
Some(self.inner.data_size.load(std::sync::atomic::Ordering::Relaxed))
}
Expand Down
9 changes: 9 additions & 0 deletions crates/transaction-pool/src/blobstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ pub trait BlobStore: fmt::Debug + Send + Sync + 'static {
txs: Vec<H256>,
) -> Result<Vec<(H256, BlobTransactionSidecar)>, BlobStoreError>;

/// Returns the exact [BlobTransactionSidecar] for the given transaction hashes in the order
/// they were requested.
///
/// Returns an error if any of the blobs are not found in the blob store.
fn get_exact(&self, txs: Vec<H256>) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError>;

/// Data size of all transactions in the blob store.
fn data_size_hint(&self) -> Option<usize>;

Expand All @@ -51,6 +57,9 @@ pub trait BlobStore: fmt::Debug + Send + Sync + 'static {
/// Error variants that can occur when interacting with a blob store.
#[derive(Debug, thiserror::Error)]
pub enum BlobStoreError {
/// Thrown if the blob sidecar is not found for a given transaction hash but was required.
#[error("blob sidecar not found for transaction {0:?}")]
MissingSidecar(H256),
/// Failed to decode the stored blob data.
#[error("failed to decode blob data: {0}")]
DecodeError(#[from] reth_rlp::DecodeError),
Expand Down
7 changes: 7 additions & 0 deletions crates/transaction-pool/src/blobstore/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ impl BlobStore for NoopBlobStore {
Ok(vec![])
}

fn get_exact(&self, txs: Vec<H256>) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError> {
if txs.is_empty() {
return Ok(vec![])
}
Err(BlobStoreError::MissingSidecar(txs[0]))
}

fn data_size_hint(&self) -> Option<usize> {
Some(0)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,13 @@ where
) -> Result<Vec<(TxHash, BlobTransactionSidecar)>, BlobStoreError> {
self.pool.blob_store().get_all(tx_hashes)
}

fn get_all_blobs_exact(
&self,
tx_hashes: Vec<TxHash>,
) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError> {
self.pool.blob_store().get_exact(tx_hashes)
}
}

impl<V: TransactionValidator, T: TransactionOrdering, S> TransactionPoolExt for Pool<V, T, S>
Expand Down
10 changes: 10 additions & 0 deletions crates/transaction-pool/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ impl TransactionPool for NoopTransactionPool {
) -> Result<Vec<(TxHash, BlobTransactionSidecar)>, BlobStoreError> {
Ok(vec![])
}

fn get_all_blobs_exact(
&self,
tx_hashes: Vec<TxHash>,
) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError> {
if tx_hashes.is_empty() {
return Ok(vec![])
}
Err(BlobStoreError::MissingSidecar(tx_hashes[0]))
}
}

/// A [`TransactionValidator`] that does nothing.
Expand Down
9 changes: 9 additions & 0 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ pub trait TransactionPool: Send + Sync + Clone {
&self,
tx_hashes: Vec<TxHash>,
) -> Result<Vec<(TxHash, BlobTransactionSidecar)>, BlobStoreError>;

/// Returns the exact [BlobTransactionSidecar] for the given transaction hashes in the order
/// they were requested.
///
/// Returns an error if any of the blobs are not found in the blob store.
fn get_all_blobs_exact(
&self,
tx_hashes: Vec<TxHash>,
) -> Result<Vec<BlobTransactionSidecar>, BlobStoreError>;
}

/// Extension for [TransactionPool] trait that allows to set the current block info.
Expand Down

0 comments on commit f6e5826

Please sign in to comment.