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: Improve OP txpool batch validation #13918

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
120 changes: 103 additions & 17 deletions crates/optimism/node/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,26 +325,56 @@ where
}
}

/// Validates a single transaction.
///
/// See also [`TransactionValidator::validate_transaction`]
///
/// This behaves the same as [`EthTransactionValidator::validate_one`], but in addition, ensures
/// that the account has enough balance to cover the L1 gas cost.
pub fn validate_one(
fn validate_batch(
&self,
origin: TransactionOrigin,
transaction: Tx,
) -> TransactionValidationOutcome<Tx> {
if transaction.is_eip4844() {
return TransactionValidationOutcome::Invalid(
transaction,
InvalidTransactionError::TxTypeNotSupported.into(),
)
mut transactions: Vec<(TransactionOrigin, Tx)>,
) -> Vec<TransactionValidationOutcome<Tx>> {
// final vector containing all outcomes
let mut outcomes = Vec::with_capacity(transactions.len());
// vector containing transactions with valid type that need further validation
let mut transactions_to_validate = Vec::with_capacity(transactions.len());

// populate our vectors
for (tx_origin, tx) in transactions.drain(..) {
match Self::validate_transaction_type(tx) {
Ok(tx) => {
transactions_to_validate.push((tx_origin, tx));
outcomes.push(None); // placeholder to keep the ordering, outcome will be added
// after performing full validation
}
Err(outcome) => {
outcomes.push(Some(outcome));
}
}
}

let outcome = self.inner.validate_one(origin, transaction);
// validate the transactions
let mut validated_outcomes = self.inner.validate_all(transactions_to_validate);

// populate final vector with validated outcomes, performing the final validation on the way
let mut it = validated_outcomes.drain(..);
outcomes
.into_iter()
.map(|outcome| outcome.or(it.next().map(|val| self.validate_gas_fee(val))))
.flatten()
.collect()
}

fn validate_transaction_type(tx: Tx) -> Result<Tx, TransactionValidationOutcome<Tx>> {
if tx.is_eip4844() {
Err(TransactionValidationOutcome::Invalid(
tx,
InvalidTransactionError::TxTypeNotSupported.into(),
))
} else {
Ok(tx)
}
}

fn validate_gas_fee(
&self,
outcome: TransactionValidationOutcome<Tx>,
) -> TransactionValidationOutcome<Tx> {
if !self.requires_l1_data_gas_fee() {
// no need to check L1 gas fee
return outcome
Expand Down Expand Up @@ -399,6 +429,25 @@ where
outcome
}

/// Validates a single transaction.
///
/// See also [`TransactionValidator::validate_transaction`]
///
/// This behaves the same as [`EthTransactionValidator::validate_one`], but in addition, ensures
/// that the account has enough balance to cover the L1 gas cost.
pub fn validate_one(
&self,
origin: TransactionOrigin,
transaction: Tx,
) -> TransactionValidationOutcome<Tx> {
let transaction = match Self::validate_transaction_type(transaction) {
Ok(tx) => tx,
Err(outcome) => return outcome,
};
let outcome = self.inner.validate_one(origin, transaction);
self.validate_gas_fee(outcome)
}

/// Validates all given transactions.
///
/// Returns all outcomes for the given transactions in the same order.
Expand All @@ -408,7 +457,7 @@ where
&self,
transactions: Vec<(TransactionOrigin, Tx)>,
) -> Vec<TransactionValidationOutcome<Tx>> {
transactions.into_iter().map(|(origin, tx)| self.validate_one(origin, tx)).collect()
self.validate_batch(transactions)
}
}

Expand Down Expand Up @@ -503,4 +552,41 @@ mod tests {
};
assert_eq!(err.to_string(), "transaction type not supported");
}

#[test]
fn validate_optimism_transaction_all() {
let client = MockEthProvider::default();
let validator = EthTransactionValidatorBuilder::new(MAINNET.clone())
.no_shanghai()
.no_cancun()
.build(client, InMemoryBlobStore::default());
let validator = OpTransactionValidator::new(validator);

let origin = TransactionOrigin::External;
let signer = Default::default();
let deposit_tx = OpTypedTransaction::Deposit(TxDeposit {
source_hash: Default::default(),
from: signer,
to: TxKind::Create,
mint: None,
value: U256::ZERO,
gas_limit: 0,
is_system_transaction: false,
input: Default::default(),
});
let signature = Signature::test_signature();
let signed_tx = OpTransactionSigned::new_unhashed(deposit_tx, signature);
let signed_recovered = Recovered::new_unchecked(signed_tx, signer);
let len = signed_recovered.encode_2718_len();
let pooled_tx = OpPooledTransaction::new(signed_recovered, len);
let outcomes = validator.validate_all(vec![(origin, pooled_tx)]);

for outcome in outcomes {
let err = match outcome {
TransactionValidationOutcome::Invalid(_, err) => err,
_ => panic!("Expected invalid transaction"),
};
assert_eq!(err.to_string(), "transaction type not supported");
}
}
}