Skip to content

Commit

Permalink
Merge pull request #74 from weaveVM/change-gas-price
Browse files Browse the repository at this point in the history
feat: Include minimum gas price according to gaseconomics.
  • Loading branch information
andreespirela authored Oct 2, 2024
2 parents 58ab679 + 0eb70de commit da3748d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 19 deletions.
61 changes: 58 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ brotlic = "0.8.2"
exex-wvm-da = { git = "https://github.com/weaveVM/exex-templates?a", branch = "main" }
exex-wvm-bigquery = { git = "https://github.com/weaveVM/exex-templates?b", branch = "main" }

# fees
fees = { git = "https://github.com/weaveVM/miscalleneous?c", branch = "main" }

tikv-jemalloc-ctl = "0.6"
tikv-jemallocator = "0.6"
tracy-client = "0.17.3"
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ serde.workspace = true
arbitrary = { workspace = true, features = ["derive"], optional = true }
proptest = { workspace = true, optional = true }
proptest-arbitrary-interop = { workspace = true, optional = true }
fees.workspace = true
tokio.workspace = true

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["arbitrary"] }
Expand Down
57 changes: 52 additions & 5 deletions crates/primitives-traits/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use alloy_primitives::{address, b256, Address, B256, U256};
use core::time::Duration;
use std::cell::LazyCell;
use std::sync::{Arc, LazyLock, RwLock};
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering::SeqCst;
use fees::util::raw_calculate_lowest_possible_gas_price;
use fees::wvm_fee::{WvmFee, WvmFeeManager};

/// Gas units, for example [`GIGAGAS`].
pub mod gas_units;
Expand Down Expand Up @@ -54,10 +59,35 @@ pub const ETHEREUM_BLOCK_GAS_LIMIT: LazyCell<u64> = LazyCell::new(|| {
///
/// Note that min base fee under different 1559 parameterizations may differ, but there's no
/// significant harm in leaving this setting as is.
pub const MIN_PROTOCOL_BASE_FEE: u64 = 7;
// pub const MIN_PROTOCOL_BASE_FEE: u64 = 7;

pub static MIN_PROTOCOL_BASE_FEE: LazyLock<AtomicU64> = LazyLock::new(|| {
AtomicU64::new(7)
});

pub(crate) static WVM_FEE_MANAGER: LazyLock<Arc<WvmFeeManager>> = LazyLock::new(|| {
let fee = WvmFee::new(Some(Box::new(move |price| {
let original_price = price as f64 / 1_000_000_000f64;
let lowest_possible_gas_price_in_gwei = raw_calculate_lowest_possible_gas_price(original_price, *ETHEREUM_BLOCK_GAS_LIMIT);
let to_wei = lowest_possible_gas_price_in_gwei * 1e9;
MIN_PROTOCOL_BASE_FEE.store(to_wei as u64, SeqCst);
Ok(())
})));

fee.init();

let manager = WvmFeeManager::new(Arc::new(fee));
manager.init();

Arc::new(manager)
});

pub fn get_latest_min_protocol_base_fee() -> u64 {
MIN_PROTOCOL_BASE_FEE.load(SeqCst)
}

/// Same as [`MIN_PROTOCOL_BASE_FEE`] but as a U256.
pub const MIN_PROTOCOL_BASE_FEE_U256: U256 = U256::from_limbs([7u64, 0, 0, 0]);
pub const MIN_PROTOCOL_BASE_FEE_U256: U256 = U256::from_limbs([640_000_000u64, 0u64, 0u64, 0u64]);

/// Initial base fee as defined in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)
pub const EIP1559_INITIAL_BASE_FEE: u64 = 1_000_000_000;
Expand Down Expand Up @@ -175,8 +205,25 @@ pub const ALLOWED_FUTURE_BLOCK_TIME_SECONDS: u64 = 15;
mod tests {
use super::*;

#[test]
fn min_protocol_sanity() {
assert_eq!(MIN_PROTOCOL_BASE_FEE_U256.to::<u64>(), MIN_PROTOCOL_BASE_FEE);
use std::time::Duration;
use crate::constants::{get_latest_min_protocol_base_fee, WVM_FEE_MANAGER};

#[tokio::test]
pub async fn test_wvm_fee_manager() {
std::env::set_var("ETHEREUM_BLOCK_GAS_LIMIT", "500000000");
let init = &*WVM_FEE_MANAGER;
tokio::time::sleep(Duration::from_secs(10)).await;
let latest_gas = get_latest_min_protocol_base_fee();
assert!(&latest_gas > &630000000);
println!("{}", latest_gas);
assert!(&latest_gas < &650000000);
}

#[tokio::test]
async fn min_protocol_sanity() {
std::env::set_var("ETHEREUM_BLOCK_GAS_LIMIT", "500000000");
let init = &*WVM_FEE_MANAGER;
tokio::time::sleep(Duration::from_secs(10)).await;
assert_eq!(MIN_PROTOCOL_BASE_FEE_U256.to::<u64>(), get_latest_min_protocol_base_fee());
}
}
6 changes: 2 additions & 4 deletions crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use crate::{
ValidPoolTransaction, U256,
};
use alloy_primitives::{Address, TxHash, B256};
use reth_primitives::constants::{
eip4844::BLOB_TX_MIN_BLOB_GASPRICE, ETHEREUM_BLOCK_GAS_LIMIT, MIN_PROTOCOL_BASE_FEE,
};
use reth_primitives::constants::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, ETHEREUM_BLOCK_GAS_LIMIT, get_latest_min_protocol_base_fee, MIN_PROTOCOL_BASE_FEE};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use std::{
Expand Down Expand Up @@ -1705,7 +1703,7 @@ impl<T: PoolTransaction> Default for AllTransactions<T> {
fn default() -> Self {
Self {
max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE,
minimal_protocol_basefee: get_latest_min_protocol_base_fee(),
block_gas_limit: *ETHEREUM_BLOCK_GAS_LIMIT,
by_hash: Default::default(),
txs: Default::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/transaction-pool/src/test_utils/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use reth_primitives::{
constants::MIN_PROTOCOL_BASE_FEE, sign_message, AccessList, Bytes, Transaction,
TransactionSigned, TxEip1559, TxEip4844, TxKind, TxLegacy,
};
use reth_primitives::constants::get_latest_min_protocol_base_fee;

/// A generator for transactions for testing purposes.
#[derive(Debug)]
Expand All @@ -31,7 +32,7 @@ impl<R: Rng> TransactionGenerator<R> {
Self {
rng,
signer_keys: (0..num_signers).map(|_| B256::random()).collect(),
base_fee: MIN_PROTOCOL_BASE_FEE as u128,
base_fee: get_latest_min_protocol_base_fee() as u128,
gas_limit: 300_000,
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use reth_primitives::{
};

use std::{ops::Range, sync::Arc, time::Instant, vec::IntoIter};
use reth_primitives::constants::get_latest_min_protocol_base_fee;

/// A transaction pool implementation using [`MockOrdering`] for transaction ordering.
///
Expand Down Expand Up @@ -255,8 +256,8 @@ impl MockTransaction {
hash: B256::random(),
sender: Address::random(),
nonce: 0,
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
max_priority_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
max_fee_per_gas: get_latest_min_protocol_base_fee() as u128,
max_priority_fee_per_gas: get_latest_min_protocol_base_fee() as u128,
gas_limit: 0,
to: Address::random().into(),
value: Default::default(),
Expand All @@ -273,8 +274,8 @@ impl MockTransaction {
hash: B256::random(),
sender: Address::random(),
nonce: 0,
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
max_priority_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
max_fee_per_gas: get_latest_min_protocol_base_fee() as u128,
max_priority_fee_per_gas: get_latest_min_protocol_base_fee() as u128,
max_fee_per_blob_gas: DATA_GAS_PER_BLOB as u128,
gas_limit: 0,
to: Address::random(),
Expand Down
4 changes: 2 additions & 2 deletions crates/transaction-pool/tests/it/evict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use alloy_primitives::{Address, B256};
use rand::distributions::Uniform;
use reth_primitives::constants::MIN_PROTOCOL_BASE_FEE;
use reth_primitives::constants::{get_latest_min_protocol_base_fee, MIN_PROTOCOL_BASE_FEE};
use reth_transaction_pool::{
error::PoolErrorKind,
test_utils::{
Expand Down Expand Up @@ -66,7 +66,7 @@ async fn only_blobs_eviction() {
// we need to set the max fee to at least the min protocol base fee, or transactions
// generated could be rejected
max_fee: Uniform::from(
MIN_PROTOCOL_BASE_FEE as u128..(block_info.pending_basefee as u128 + 2000),
get_latest_min_protocol_base_fee() as u128..(block_info.pending_basefee as u128 + 2000),
),
max_fee_blob: Uniform::from(pending_blob_fee..(pending_blob_fee + 1000)),
};
Expand Down

0 comments on commit da3748d

Please sign in to comment.