diff --git a/crates/blockchain/mempool.rs b/crates/blockchain/mempool.rs index aa566f239..91303d488 100644 --- a/crates/blockchain/mempool.rs +++ b/crates/blockchain/mempool.rs @@ -27,13 +27,15 @@ pub fn add_blob_transaction( // Validate blobs bundle blobs_bundle.validate(&transaction)?; - // Validate transaction let transaction = Transaction::EIP4844Transaction(transaction); - validate_transaction(&transaction, store.clone())?; + let sender = transaction.sender(); + + // Validate transaction + validate_transaction(&transaction, sender, store.clone())?; // Add transaction and blobs bundle to storage let hash = transaction.compute_hash(); - store.add_transaction_to_pool(hash, MempoolTransaction::new(transaction))?; + store.add_transaction_to_pool(hash, MempoolTransaction::new(transaction, sender))?; store.add_blobs_bundle_to_pool(hash, blobs_bundle)?; Ok(hash) } @@ -44,13 +46,14 @@ pub fn add_transaction(transaction: Transaction, store: Store) -> Result Result<(), MempoolError> { +fn validate_transaction( + tx: &Transaction, + sender: Address, + store: Store, +) -> Result<(), MempoolError> { // TODO: Add validations here let header_no = store @@ -207,7 +214,7 @@ fn validate_transaction(tx: &Transaction, store: Store) -> Result<(), MempoolErr } }; - let maybe_sender_acc_info = store.get_account_info(header_no, tx.sender())?; + let maybe_sender_acc_info = store.get_account_info(header_no, sender)?; if let Some(sender_acc_info) = maybe_sender_acc_info { if tx.nonce() < sender_acc_info.nonce { @@ -541,7 +548,7 @@ mod tests { }; let tx = Transaction::EIP1559Transaction(tx); - let validation = validate_transaction(&tx, store); + let validation = validate_transaction(&tx, Address::random(), store); assert!(matches!( validation, Err(MempoolError::TxMaxInitCodeSizeError) @@ -567,7 +574,7 @@ mod tests { }; let tx = Transaction::EIP1559Transaction(tx); - let validation = validate_transaction(&tx, store); + let validation = validate_transaction(&tx, Address::random(), store); assert!(matches!( validation, Err(MempoolError::TxGasLimitExceededError) @@ -593,7 +600,7 @@ mod tests { }; let tx = Transaction::EIP1559Transaction(tx); - let validation = validate_transaction(&tx, store); + let validation = validate_transaction(&tx, Address::random(), store); assert!(matches!( validation, Err(MempoolError::TxTipAboveFeeCapError) @@ -620,7 +627,7 @@ mod tests { }; let tx = Transaction::EIP1559Transaction(tx); - let validation = validate_transaction(&tx, store); + let validation = validate_transaction(&tx, Address::random(), store); assert!(matches!( validation, Err(MempoolError::TxIntrinsicGasCostAboveLimitError) @@ -646,7 +653,7 @@ mod tests { }; let tx = Transaction::EIP4844Transaction(tx); - let validation = validate_transaction(&tx, store); + let validation = validate_transaction(&tx, Address::random(), store); assert!(matches!( validation, Err(MempoolError::TxBlobBaseFeeTooLowError) diff --git a/crates/common/types/transaction.rs b/crates/common/types/transaction.rs index 95fea476f..0a81f34f7 100644 --- a/crates/common/types/transaction.rs +++ b/crates/common/types/transaction.rs @@ -2122,22 +2122,28 @@ mod mempool { pub struct MempoolTransaction { // Unix timestamp (in microseconds) created once the transaction reached the MemPool timestamp: u128, + sender: Address, inner: Transaction, } impl MempoolTransaction { - pub fn new(tx: Transaction) -> Self { + pub fn new(tx: Transaction, sender: Address) -> Self { Self { timestamp: SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Invalid system time") .as_micros(), + sender, inner: tx, } } pub fn time(&self) -> u128 { self.timestamp } + + pub fn sender(&self) -> Address { + self.sender + } } impl RLPEncode for MempoolTransaction { @@ -2153,8 +2159,16 @@ mod mempool { fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> { let decoder = Decoder::new(rlp)?; let (timestamp, decoder) = decoder.decode_field("timestamp")?; + let (sender, decoder) = decoder.decode_field("sender")?; let (inner, decoder) = decoder.decode_field("inner")?; - Ok((Self { timestamp, inner }, decoder.finish()?)) + Ok(( + Self { + timestamp, + sender, + inner, + }, + decoder.finish()?, + )) } } diff --git a/crates/storage/store/storage.rs b/crates/storage/store/storage.rs index c500093e4..d6fbbe736 100644 --- a/crates/storage/store/storage.rs +++ b/crates/storage/store/storage.rs @@ -1213,8 +1213,12 @@ mod tests { use hex_literal::hex; fn test_filter_mempool_transactions(store: Store) { - let plain_tx = MempoolTransaction::new(Transaction::decode_canonical(&hex!("f86d80843baa0c4082f618946177843db3138ae69679a54b95cf345ed759450d870aa87bee538000808360306ba0151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65da064c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4")).unwrap()); - let blob_tx = MempoolTransaction::new(Transaction::decode_canonical(&hex!("03f88f0780843b9aca008506fc23ac00830186a09400000000000000000000000000000000000001008080c001e1a0010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c44401401a0840650aa8f74d2b07f40067dc33b715078d73422f01da17abdbd11e02bbdfda9a04b2260f6022bf53eadb337b3e59514936f7317d872defb891a708ee279bdca90")).unwrap()); + let plain_tx_decoded = Transaction::decode_canonical(&hex!("f86d80843baa0c4082f618946177843db3138ae69679a54b95cf345ed759450d870aa87bee538000808360306ba0151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65da064c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4")).unwrap(); + let plain_tx_sender = plain_tx_decoded.sender(); + let plain_tx = MempoolTransaction::new(plain_tx_decoded, plain_tx_sender); + let blob_tx_decoded = Transaction::decode_canonical(&hex!("03f88f0780843b9aca008506fc23ac00830186a09400000000000000000000000000000000000001008080c001e1a0010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c44401401a0840650aa8f74d2b07f40067dc33b715078d73422f01da17abdbd11e02bbdfda9a04b2260f6022bf53eadb337b3e59514936f7317d872defb891a708ee279bdca90")).unwrap(); + let blob_tx_sender = blob_tx_decoded.sender(); + let blob_tx = MempoolTransaction::new(blob_tx_decoded, blob_tx_sender); let plain_tx_hash = plain_tx.compute_hash(); let blob_tx_hash = blob_tx.compute_hash(); let filter =