Skip to content

Commit

Permalink
fix(anvil): eth_gasPrice returned 1000000000 with `--block-base-f…
Browse files Browse the repository at this point in the history
…ee-per-gas 0`, adds new `--disable-min-priority-fee` to return `0` (#9049)

* add new flag to disable min suggested priority fee: `--disable-min-priority-fee`

* documentation

* remove unnecessary value_name
  • Loading branch information
zerosnacks authored Oct 7, 2024
1 parent 1ba5d6f commit e215f3f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl NodeArgs {
.fork_compute_units_per_second(compute_units_per_second)
.with_eth_rpc_url(self.evm_opts.fork_url.map(|fork| fork.url))
.with_base_fee(self.evm_opts.block_base_fee_per_gas)
.disable_min_priority_fee(self.evm_opts.disable_min_priority_fee)
.with_storage_caching(self.evm_opts.no_storage_caching)
.with_server_config(self.server_config)
.with_host(self.host)
Expand Down Expand Up @@ -547,6 +548,10 @@ pub struct AnvilEvmArgs {
)]
pub block_base_fee_per_gas: Option<u64>,

/// Disable the enforcement of a minimum suggested priority fee.
#[arg(long, visible_alias = "no-priority-fee", help_heading = "Environment config")]
pub disable_min_priority_fee: bool,

/// The chain ID.
#[arg(long, alias = "chain", help_heading = "Environment config")]
pub chain_id: Option<Chain>,
Expand Down
11 changes: 11 additions & 0 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct NodeConfig {
pub gas_price: Option<u128>,
/// Default base fee
pub base_fee: Option<u64>,
/// If set to `true`, disables the enforcement of a minimum suggested priority fee
pub disable_min_priority_fee: bool,
/// Default blob excess gas and price
pub blob_excess_gas_and_price: Option<BlobExcessGasAndPrice>,
/// The hardfork to use
Expand Down Expand Up @@ -432,6 +434,7 @@ impl Default for NodeConfig {
fork_choice: None,
account_generator: None,
base_fee: None,
disable_min_priority_fee: false,
blob_excess_gas_and_price: None,
enable_tracing: true,
enable_steps_tracing: false,
Expand Down Expand Up @@ -623,6 +626,13 @@ impl NodeConfig {
self
}

/// Disable the enforcement of a minimum suggested priority fee
#[must_use]
pub fn disable_min_priority_fee(mut self, disable_min_priority_fee: bool) -> Self {
self.disable_min_priority_fee = disable_min_priority_fee;
self
}

/// Sets the init genesis (genesis.json)
#[must_use]
pub fn with_genesis(mut self, genesis: Option<Genesis>) -> Self {
Expand Down Expand Up @@ -994,6 +1004,7 @@ impl NodeConfig {
let fees = FeeManager::new(
cfg.handler_cfg.spec_id,
self.get_base_fee(),
!self.disable_min_priority_fee,
self.get_gas_price(),
self.get_blob_excess_gas_and_price(),
);
Expand Down
6 changes: 5 additions & 1 deletion crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ impl EthApi {
/// Returns the current gas price
pub fn gas_price(&self) -> u128 {
if self.backend.is_eip1559() {
(self.backend.base_fee() as u128).saturating_add(self.lowest_suggestion_tip())
if self.backend.is_min_priority_fee_enforced() {
(self.backend.base_fee() as u128).saturating_add(self.lowest_suggestion_tip())
} else {
self.backend.base_fee() as u128
}
} else {
self.backend.fees().raw_gas_price()
}
Expand Down
5 changes: 5 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ impl Backend {
self.fees.base_fee()
}

/// Returns whether the minimum suggested priority fee is enforced
pub fn is_min_priority_fee_enforced(&self) -> bool {
self.fees.is_min_priority_fee_enforced()
}

pub fn excess_blob_gas_and_price(&self) -> Option<BlobExcessGasAndPrice> {
self.fees.excess_blob_gas_and_price()
}
Expand Down
8 changes: 8 additions & 0 deletions crates/anvil/src/eth/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct FeeManager {
///
/// This value will be updated after a new block was mined
base_fee: Arc<RwLock<u64>>,
/// Whether the minimum suggested priority fee is enforced
is_min_priority_fee_enforced: bool,
/// Tracks the excess blob gas, and the base fee, for the next block post Cancun
///
/// This value will be updated after a new block was mined
Expand All @@ -63,12 +65,14 @@ impl FeeManager {
pub fn new(
spec_id: SpecId,
base_fee: u64,
is_min_priority_fee_enforced: bool,
gas_price: u128,
blob_excess_gas_and_price: BlobExcessGasAndPrice,
) -> Self {
Self {
spec_id,
base_fee: Arc::new(RwLock::new(base_fee)),
is_min_priority_fee_enforced,
gas_price: Arc::new(RwLock::new(gas_price)),
blob_excess_gas_and_price: Arc::new(RwLock::new(blob_excess_gas_and_price)),
elasticity: Arc::new(RwLock::new(default_elasticity())),
Expand Down Expand Up @@ -105,6 +109,10 @@ impl FeeManager {
}
}

pub fn is_min_priority_fee_enforced(&self) -> bool {
self.is_min_priority_fee_enforced
}

/// Raw base gas price
pub fn raw_gas_price(&self) -> u128 {
*self.gas_price.read()
Expand Down

0 comments on commit e215f3f

Please sign in to comment.