Skip to content

Commit

Permalink
feat: procure l2 gas price when forking (#290)
Browse files Browse the repository at this point in the history
* feat: procure `l2_fair_gas_price` from fork source

* chore: explicit info when overriding l2 gas price
  • Loading branch information
zeapoz authored Jun 16, 2024
1 parent 1d72eae commit 93946cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub trait ForkSource {
/// "S" is the implementation of the ForkSource.
#[derive(Debug, Clone)]
pub struct ForkDetails<S> {
// 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,
Expand All @@ -320,6 +320,7 @@ pub struct ForkDetails<S> {
pub block_timestamp: u64,
pub overwrite_chain_id: Option<L2ChainId>,
pub l1_gas_price: u64,
pub l2_fair_gas_price: u64,
}

const SUPPORTED_VERSIONS: &[ProtocolVersionId] = &[
Expand Down Expand Up @@ -385,8 +386,8 @@ impl ForkDetails<HttpForkSource> {
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
Expand All @@ -410,6 +411,7 @@ impl ForkDetails<HttpForkSource> {
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.
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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);
Expand Down
37 changes: 33 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -232,9 +233,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<u64>,

#[arg(long)]
/// If true, the tool will try to contact openchain to resolve the ABI & topic names.
Expand Down Expand Up @@ -360,11 +362,38 @@ async fn main() -> anyhow::Result<()> {
DevSystemContracts::Local => system_contracts::Options::Local,
};

// 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
}
};

// 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,
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,
Expand Down
9 changes: 5 additions & 4 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -952,7 +952,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
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,
Expand Down Expand Up @@ -989,7 +989,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
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,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 93946cc

Please sign in to comment.