From f0c50057fcd2d258b4e51da3a21ecd06420062af Mon Sep 17 00:00:00 2001 From: zeapoz Date: Thu, 23 May 2024 14:27:09 +0200 Subject: [PATCH 1/5] feat: procure `l2_fair_gas_price` from fork source --- src/fork.rs | 11 +++++++---- src/main.rs | 25 +++++++++++++++++++++---- src/node/in_memory.rs | 9 +++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/fork.rs b/src/fork.rs index 46713fac..6dc4ccb4 100644 --- a/src/fork.rs +++ b/src/fork.rs @@ -309,7 +309,7 @@ pub trait ForkSource { /// "S" is the implementation of the ForkSource. #[derive(Debug, Clone)] pub struct ForkDetails { - // Source of the fork data (for example HTTPForkSoruce) + // Source of the fork data (for example HTTPForkSource) pub fork_source: S, // Block number at which we forked (the next block to create is l1_block + 1) pub l1_block: L1BatchNumber, @@ -320,6 +320,7 @@ pub struct ForkDetails { pub block_timestamp: u64, pub overwrite_chain_id: Option, pub l1_gas_price: u64, + pub l2_fair_gas_price: u64, } const SUPPORTED_VERSIONS: &[ProtocolVersionId] = &[ @@ -385,8 +386,8 @@ impl ForkDetails { let l1_batch_number = block_details.l1_batch_number; tracing::info!( - "Creating fork from {:?} L1 block: {:?} L2 block: {:?} with timestamp {:?}, L1 gas price {:?} and protocol version: {:?}" , - url, l1_batch_number, miniblock, block_details.base.timestamp, block_details.base.l1_gas_price, block_details.protocol_version + "Creating fork from {:?} L1 block: {:?} L2 block: {:?} with timestamp {:?}, L1 gas price {:?}, L2 fair gas price {:?} and protocol version: {:?}" , + url, l1_batch_number, miniblock, block_details.base.timestamp, block_details.base.l1_gas_price, block_details.base.l2_fair_gas_price, block_details.protocol_version ); if !block_details @@ -410,6 +411,7 @@ impl ForkDetails { l2_miniblock_hash: root_hash, overwrite_chain_id: chain_id, l1_gas_price: block_details.base.l1_gas_price, + l2_fair_gas_price: block_details.base.l2_fair_gas_price, } } /// Create a fork from a given network at a given height. @@ -506,7 +508,7 @@ mod tests { use zksync_state::ReadStorage; use zksync_types::{api::TransactionVariant, StorageKey}; - use crate::{deps::InMemoryStorage, system_contracts, testing}; + use crate::{deps::InMemoryStorage, node::DEFAULT_L2_GAS_PRICE, system_contracts, testing}; use super::{ForkDetails, ForkStorage}; @@ -535,6 +537,7 @@ mod tests { block_timestamp: 0, overwrite_chain_id: None, l1_gas_price: 100, + l2_fair_gas_price: DEFAULT_L2_GAS_PRICE, }; let mut fork_storage = ForkStorage::new(Some(fork_details), &options); diff --git a/src/main.rs b/src/main.rs index 9f34103e..e565861f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -232,9 +232,10 @@ struct Cli { /// Show Gas details information show_gas_details: ShowGasDetails, - #[arg(long, default_value_t = DEFAULT_L2_GAS_PRICE)] - /// If provided, uses a custom value as the L2 gas price. - l2_gas_price: u64, + #[arg(long)] + /// If provided, uses a custom value as the L2 gas price. If not provided the gas price will be + /// inferred from the protocol version. + l2_gas_price: Option, #[arg(long)] /// If true, the tool will try to contact openchain to resolve the ABI & topic names. @@ -360,11 +361,27 @@ async fn main() -> anyhow::Result<()> { DevSystemContracts::Local => system_contracts::Options::Local, }; + // If L2 gas price has been supplied as an argument use that value, + // otherwise procure it from the fork source, or if that fails, use the + // `DEFAULT_L2_GAS_PRICE`. + let l2_fair_gas_price = opt.l2_gas_price.unwrap_or_else(|| { + if let Some(f) = &fork_details { + f.l2_fair_gas_price + } else { + DEFAULT_L2_GAS_PRICE + } + }); + + tracing::info!( + "Starting node with L2 gas price set to {}", + l2_fair_gas_price + ); + let node = InMemoryNode::new( fork_details, Some(observability), InMemoryNodeConfig { - l2_gas_price: opt.l2_gas_price, + l2_fair_gas_price, show_calls: opt.show_calls, show_outputs: opt.show_outputs, show_storage_logs: opt.show_storage_logs, diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index bbf495b4..ee62c1fd 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -880,7 +880,7 @@ pub struct Snapshot { #[derive(Debug, Clone)] pub struct InMemoryNodeConfig { // The values to be used when calculating gas. - pub l2_gas_price: u64, + pub l2_fair_gas_price: u64, pub show_calls: ShowCalls, pub show_outputs: bool, pub show_storage_logs: ShowStorageLogs, @@ -893,7 +893,7 @@ pub struct InMemoryNodeConfig { impl Default for InMemoryNodeConfig { fn default() -> Self { Self { - l2_gas_price: DEFAULT_L2_GAS_PRICE, + l2_fair_gas_price: DEFAULT_L2_GAS_PRICE, show_calls: Default::default(), show_outputs: Default::default(), show_storage_logs: Default::default(), @@ -952,7 +952,7 @@ impl InMemoryNode { current_miniblock_hash: f.l2_miniblock_hash, fee_input_provider: TestNodeFeeInputProvider::new( f.l1_gas_price, - config.l2_gas_price, + config.l2_fair_gas_price, ), tx_results: Default::default(), blocks, @@ -989,7 +989,7 @@ impl InMemoryNode { current_miniblock_hash: block_hash, fee_input_provider: TestNodeFeeInputProvider::new( L1_GAS_PRICE, - config.l2_gas_price, + config.l2_fair_gas_price, ), tx_results: Default::default(), blocks, @@ -1887,6 +1887,7 @@ mod tests { block_timestamp: 1002, overwrite_chain_id: None, l1_gas_price: 1000, + l2_fair_gas_price: DEFAULT_L2_GAS_PRICE, }), None, Default::default(), From cb4f7015e5fa8423d266d23d969689f66ed9d6e9 Mon Sep 17 00:00:00 2001 From: zeapoz Date: Fri, 24 May 2024 14:44:12 +0200 Subject: [PATCH 2/5] chore: explicit info when overriding l2 gas price --- src/main.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index e565861f..d61f9177 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use crate::cache::CacheConfig; use crate::node::{InMemoryNodeConfig, ShowGasDetails, ShowStorageLogs, ShowVMDetails}; use crate::observability::Observability; +use crate::utils::to_human_size; use clap::{Parser, Subcommand, ValueEnum}; use colored::Colorize; use fork::{ForkDetails, ForkSource}; @@ -361,21 +362,32 @@ async fn main() -> anyhow::Result<()> { DevSystemContracts::Local => system_contracts::Options::Local, }; - // If L2 gas price has been supplied as an argument use that value, - // otherwise procure it from the fork source, or if that fails, use the - // `DEFAULT_L2_GAS_PRICE`. - let l2_fair_gas_price = opt.l2_gas_price.unwrap_or_else(|| { + // If we're forking we set the price to be equal to that contained within + // `ForkDetails`. If not, we use the `DEFAULT_L2_GAS_PRICE` instead. + let mut l2_fair_gas_price = { if let Some(f) = &fork_details { f.l2_fair_gas_price } else { DEFAULT_L2_GAS_PRICE } - }); + }; - tracing::info!( - "Starting node with L2 gas price set to {}", - l2_fair_gas_price - ); + // If L2 gas price has been supplied as an argument, override the value + // procured previously. + match opt.l2_gas_price { + Some(l2_gas_price) => { + tracing::info!( + "Starting node with L2 gas price set to {} (overridden from {})", + to_human_size(l2_gas_price.into()), + to_human_size(l2_fair_gas_price.into()) + ); + l2_fair_gas_price = l2_gas_price; + } + None => tracing::info!( + "Starting node with L2 gas price set to {}", + to_human_size(l2_fair_gas_price.into()) + ), + } let node = InMemoryNode::new( fork_details, From c6942202bf8beced27fd0fbdbf4e82e502c82013 Mon Sep 17 00:00:00 2001 From: Vaclav Barta Date: Tue, 4 Jun 2024 13:42:53 +0200 Subject: [PATCH 3/5] feat:custom L1 gas price --- src/main.rs | 5 +++++ src/node/in_memory.rs | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index d61f9177..92dddd73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -233,6 +233,10 @@ struct Cli { /// Show Gas details information show_gas_details: ShowGasDetails, + #[arg(long)] + /// If provided, uses a custom value as the L1 gas price. + l1_gas_price: Option, + #[arg(long)] /// If provided, uses a custom value as the L2 gas price. If not provided the gas price will be /// inferred from the protocol version. @@ -393,6 +397,7 @@ async fn main() -> anyhow::Result<()> { fork_details, Some(observability), InMemoryNodeConfig { + l1_gas_price: opt.l1_gas_price, l2_fair_gas_price, show_calls: opt.show_calls, show_outputs: opt.show_outputs, diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index ee62c1fd..f131629a 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -74,7 +74,7 @@ pub const NON_FORK_FIRST_BLOCK_TIMESTAMP: u64 = 1_000; /// Network ID we use for the test node. pub const TEST_NODE_NETWORK_ID: u32 = 260; /// L1 Gas Price. -pub const L1_GAS_PRICE: u64 = 50_000_000_000; +pub const DEFAULT_L1_GAS_PRICE: u64 = 50_000_000_000; // TODO: for now, that's fine, as computation overhead is set to zero, but we may consider using calculated fee input everywhere. /// The default L2 Gas Price to be used if not supplied via the CLI argument. pub const DEFAULT_L2_GAS_PRICE: u64 = 25_000_000; @@ -880,6 +880,7 @@ pub struct Snapshot { #[derive(Debug, Clone)] pub struct InMemoryNodeConfig { // The values to be used when calculating gas. + pub l1_gas_price: Option, pub l2_fair_gas_price: u64, pub show_calls: ShowCalls, pub show_outputs: bool, @@ -893,6 +894,7 @@ pub struct InMemoryNodeConfig { impl Default for InMemoryNodeConfig { fn default() -> Self { Self { + l1_gas_price: None, l2_fair_gas_price: DEFAULT_L2_GAS_PRICE, show_calls: Default::default(), show_outputs: Default::default(), @@ -939,6 +941,18 @@ impl InMemoryNode { observability: Option, config: InMemoryNodeConfig, ) -> Self { + let default_l1_gas_price = if let Some(f) = &fork { f.l1_gas_price } else { DEFAULT_L1_GAS_PRICE }; + let l1_gas_price = if let Some(custom_l1_gas_price) = config.l1_gas_price { + tracing::info!( + "L1 gas price set to {} (overridden from {})", + to_human_size(custom_l1_gas_price.into()), + to_human_size(default_l1_gas_price.into()) + ); + custom_l1_gas_price + } else { + default_l1_gas_price + }; + let inner = if let Some(f) = &fork { let mut block_hashes = HashMap::::new(); block_hashes.insert(f.l2_block.number.as_u64(), f.l2_block.hash); @@ -951,7 +965,7 @@ impl InMemoryNode { current_miniblock: f.l2_miniblock, current_miniblock_hash: f.l2_miniblock_hash, fee_input_provider: TestNodeFeeInputProvider::new( - f.l1_gas_price, + l1_gas_price, config.l2_fair_gas_price, ), tx_results: Default::default(), @@ -988,7 +1002,7 @@ impl InMemoryNode { current_miniblock: 0, current_miniblock_hash: block_hash, fee_input_provider: TestNodeFeeInputProvider::new( - L1_GAS_PRICE, + l1_gas_price, config.l2_fair_gas_price, ), tx_results: Default::default(), From d0344396e77c73c7ed240cc63ff94397700b9558 Mon Sep 17 00:00:00 2001 From: Vaclav Barta Date: Thu, 20 Jun 2024 15:27:59 +0200 Subject: [PATCH 4/5] reformatted --- src/node/in_memory.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index f131629a..aabd92b6 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -941,7 +941,11 @@ impl InMemoryNode { observability: Option, config: InMemoryNodeConfig, ) -> Self { - let default_l1_gas_price = if let Some(f) = &fork { f.l1_gas_price } else { DEFAULT_L1_GAS_PRICE }; + let default_l1_gas_price = if let Some(f) = &fork { + f.l1_gas_price + } else { + DEFAULT_L1_GAS_PRICE + }; let l1_gas_price = if let Some(custom_l1_gas_price) = config.l1_gas_price { tracing::info!( "L1 gas price set to {} (overridden from {})", From 4d9c4f7c5a1d22eaa4c7b6c6d62808fbd7e2883e Mon Sep 17 00:00:00 2001 From: Vaclav Barta Date: Thu, 27 Jun 2024 14:32:07 +0200 Subject: [PATCH 5/5] clippy warning --- src/node/in_memory.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index bffa11f9..23d57ce8 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -1016,8 +1016,10 @@ impl InMemoryNode { create_empty_block(0, NON_FORK_FIRST_BLOCK_TIMESTAMP, 0, None), ); - let mut fee_input_provider = TestNodeFeeInputProvider::default(); - fee_input_provider.l1_gas_price = l1_gas_price; + let fee_input_provider = TestNodeFeeInputProvider { + l1_gas_price, + ..Default::default() + }; InMemoryNodeInner { current_timestamp: NON_FORK_FIRST_BLOCK_TIMESTAMP, current_batch: 0,