Skip to content

Commit

Permalink
feat(anvil): add TipAboveFeeCap error (#4395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom authored Feb 20, 2023
1 parent c7db4af commit b44b045
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
17 changes: 14 additions & 3 deletions anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2015,9 +2015,20 @@ impl TransactionValidator for Backend {
return Err(InvalidTransactionError::NonceTooLow)
}

if (env.cfg.spec_id as u8) >= (SpecId::LONDON as u8) && tx.gas_price() < env.block.basefee {
warn!(target: "backend", "max fee per gas={}, too low, block basefee={}",tx.gas_price(), env.block.basefee);
return Err(InvalidTransactionError::FeeTooLow)
if (env.cfg.spec_id as u8) >= (SpecId::LONDON as u8) {
if tx.gas_price() < env.block.basefee {
warn!(target: "backend", "max fee per gas={}, too low, block basefee={}",tx.gas_price(), env.block.basefee);
return Err(InvalidTransactionError::FeeTooLow)
}

if let (Some(max_priority_fee_per_gas), Some(max_fee_per_gas)) =
(tx.essentials().max_priority_fee_per_gas, tx.essentials().max_fee_per_gas)
{
if max_priority_fee_per_gas > max_fee_per_gas {
warn!(target: "backend", "max priority fee per gas={}, too high, max fee per gas={}", max_priority_fee_per_gas, max_fee_per_gas);
return Err(InvalidTransactionError::TipAboveFeeCap)
}
}
}

let max_cost = tx.max_cost();
Expand Down
5 changes: 5 additions & 0 deletions anvil/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ pub enum InvalidTransactionError {
#[error("max fee per gas less than block base fee")]
FeeTooLow,

/// Thrown to ensure no one is able to specify a transaction with a tip higher than the total
/// fee cap.
#[error("max priority fee per gas higher than max fee per gas")]
TipAboveFeeCap,

/// Thrown when a tx was signed with a different chain_id
#[error("invalid chain id for signer")]
InvalidChainId,
Expand Down
25 changes: 24 additions & 1 deletion anvil/tests/it/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use anvil::{eth::fees::INITIAL_BASE_FEE, spawn, NodeConfig};
use ethers::{
prelude::Middleware,
types::{transaction::eip2718::TypedTransaction, Address, BlockNumber, TransactionRequest},
types::{
transaction::eip2718::TypedTransaction, Address, BlockNumber, Eip1559TransactionRequest,
TransactionRequest,
},
};

const GAS_TRANSFER: u64 = 21_000u64;
Expand Down Expand Up @@ -87,3 +90,23 @@ async fn test_respect_base_fee() {
let tx = provider.send_transaction(tx, None).await.unwrap().await.unwrap().unwrap();
assert_eq!(tx.status, Some(1u64.into()));
}

#[tokio::test(flavor = "multi_thread")]
async fn test_tip_above_fee_cap() {
let base_fee = 50u64;
let (_api, handle) = spawn(NodeConfig::test().with_base_fee(Some(base_fee))).await;
let provider = handle.http_provider();
let tx = TypedTransaction::Eip1559(
Eip1559TransactionRequest::new()
.max_fee_per_gas(base_fee)
.max_priority_fee_per_gas(base_fee + 1)
.to(Address::random())
.value(100u64),
);
let res = provider.send_transaction(tx, None).await;
assert!(res.is_err());
assert!(res
.unwrap_err()
.to_string()
.contains("max priority fee per gas higher than max fee per gas"));
}

0 comments on commit b44b045

Please sign in to comment.