Skip to content

Commit

Permalink
Handle transaction filtering edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
ilitteri committed Oct 30, 2024
1 parent c80e3c5 commit 9b752e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions crates/blockchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum ChainError {
StoreError(#[from] StoreError),
#[error("EVM error: {0}")]
EvmError(#[from] EvmError),
#[error("Chain error: {0}")]
Custom(String),
}

#[derive(Debug, thiserror::Error)]
Expand Down
24 changes: 14 additions & 10 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub fn apply_withdrawals(context: &mut PayloadBuildContext) -> Result<(), EvmErr
/// Returns two transaction queues, one for plain and one for blob txs
fn fetch_mempool_transactions(
context: &mut PayloadBuildContext,
) -> Result<(TransactionQueue, TransactionQueue), StoreError> {
) -> Result<(TransactionQueue, TransactionQueue), ChainError> {
let tx_filter = PendingTxFilter {
/*TODO(https://github.com/lambdaclass/ethereum_rust/issues/680): add tip filter */
base_fee: context.base_fee_per_gas(),
Expand All @@ -238,12 +238,12 @@ fn fetch_mempool_transactions(
TransactionQueue::new(
mempool::filter_transactions(&plain_tx_filter, context.store())?,
context.base_fee_per_gas(),
),
)?,
// Blob txs
TransactionQueue::new(
mempool::filter_transactions(&blob_tx_filter, context.store())?,
context.base_fee_per_gas(),
),
)?,
))
}

Expand Down Expand Up @@ -308,7 +308,7 @@ pub fn fill_transactions(context: &mut PayloadBuildContext) -> Result<(), ChainE
// Execute tx
let receipt = match apply_transaction(&head_tx, context) {
Ok(receipt) => {
txs.shift();
txs.shift()?;
// Pull transaction from the mempool
mempool::remove_transaction(tx_hash, context.store())?;
receipt
Expand Down Expand Up @@ -439,26 +439,29 @@ impl From<HeadTransaction> for Transaction {

impl TransactionQueue {
/// Creates a new TransactionQueue from a set of transactions grouped by sender and sorted by nonce
fn new(mut txs: HashMap<Address, Vec<MempoolTransaction>>, base_fee: Option<u64>) -> Self {
fn new(
mut txs: HashMap<Address, Vec<MempoolTransaction>>,
base_fee: Option<u64>,
) -> Result<Self, ChainError> {
let mut heads = Vec::new();
for (address, txs) in txs.iter_mut() {
// Pull the first tx from each list and add it to the heads list
// This should be a newly filtered tx list so we are guaranteed to have a first element
let head_tx = txs.remove(0);
heads.push(HeadTransaction {
// We already ran this method when filtering the transactions from the mempool so it shouldn't fail
tip: head_tx.effective_gas_tip(base_fee).unwrap(),
tip: head_tx.effective_gas_tip(base_fee).ok_or(ChainError::Custom("Attempted to add and invalid transaction to the block. The transaction filter must have been failed.".to_owned()))?,
tx: head_tx,
sender: *address,
});
}
// Sort heads by higest tip (and lowest timestamp if tip is equal)
heads.sort();
TransactionQueue {
Ok(TransactionQueue {
heads,
txs,
base_fee,
}
})
}

/// Remove all transactions from the queue
Expand Down Expand Up @@ -488,15 +491,15 @@ impl TransactionQueue {

/// Remove the top transaction
/// Add a tx from the same sender to the head transactions
fn shift(&mut self) {
fn shift(&mut self) -> Result<(), ChainError> {
let tx = self.heads.remove(0);
if let Some(txs) = self.txs.get_mut(&tx.sender) {
// Fetch next head
if !txs.is_empty() {
let head_tx = txs.remove(0);
let head = HeadTransaction {
// We already ran this method when filtering the transactions from the mempool so it shouldn't fail
tip: head_tx.effective_gas_tip(self.base_fee).unwrap(),
tip: head_tx.effective_gas_tip(self.base_fee).ok_or(ChainError::Custom("Attempted to add and invalid transaction to the block. The transaction filter must have been failed.".to_owned()))?,
tx: head_tx,
sender: tx.sender,
};
Expand All @@ -508,6 +511,7 @@ impl TransactionQueue {
self.heads.insert(index, head);
}
}
Ok(())
}
}

Expand Down

0 comments on commit 9b752e0

Please sign in to comment.