Skip to content

Commit

Permalink
feat: custom l1 price (#294)
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

* feat:custom L1 gas price

* reformatted

* clippy warning

---------

Co-authored-by: zeapoz <zeapo@pm.me>
Co-authored-by: Nicolas Villanueva <nicolasvillanueva@msn.com>
  • Loading branch information
3 people committed Jun 27, 2024
1 parent dd6d2f4 commit 788dc0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
Expand Down Expand Up @@ -395,6 +399,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,
Expand Down
6 changes: 3 additions & 3 deletions src/node/fee_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zksync_types::L1_GAS_PER_PUBDATA_BYTE;

use super::{
DEFAULT_ESTIMATE_GAS_PRICE_SCALE_FACTOR, DEFAULT_ESTIMATE_GAS_SCALE_FACTOR,
DEFAULT_L2_GAS_PRICE, L1_GAS_PRICE,
DEFAULT_L1_GAS_PRICE, DEFAULT_L2_GAS_PRICE,
};

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -84,8 +84,8 @@ impl BatchFeeModelInputProvider for TestNodeFeeInputProvider {
impl Default for TestNodeFeeInputProvider {
fn default() -> Self {
Self {
l1_gas_price: L1_GAS_PRICE,
l1_pubdata_price: L1_GAS_PRICE * L1_GAS_PER_PUBDATA_BYTE as u64,
l1_gas_price: DEFAULT_L1_GAS_PRICE,
l1_pubdata_price: DEFAULT_L1_GAS_PRICE * L1_GAS_PER_PUBDATA_BYTE as u64,
l2_gas_price: DEFAULT_L2_GAS_PRICE,
compute_overhead_part: 0.0,
pubdata_overhead_part: 1.0,
Expand Down
27 changes: 25 additions & 2 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -884,6 +884,7 @@ pub struct Snapshot {
#[derive(Debug, Clone)]
pub struct InMemoryNodeConfig {
// The values to be used when calculating gas.
pub l1_gas_price: Option<u64>,
pub l2_fair_gas_price: u64,
pub show_calls: ShowCalls,
pub show_outputs: bool,
Expand All @@ -897,6 +898,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(),
Expand Down Expand Up @@ -943,6 +945,22 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
observability: Option<Observability>,
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::<u64, H256>::new();
block_hashes.insert(f.l2_block.number.as_u64(), f.l2_block.hash);
Expand All @@ -961,6 +979,7 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
f.estimate_gas_scale_factor,
)
};
fee_input_provider.l1_gas_price = l1_gas_price;
fee_input_provider.l2_gas_price = config.l2_fair_gas_price;

InMemoryNodeInner {
Expand Down Expand Up @@ -997,12 +1016,16 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
create_empty_block(0, NON_FORK_FIRST_BLOCK_TIMESTAMP, 0, None),
);

let fee_input_provider = TestNodeFeeInputProvider {
l1_gas_price,
..Default::default()
};
InMemoryNodeInner {
current_timestamp: NON_FORK_FIRST_BLOCK_TIMESTAMP,
current_batch: 0,
current_miniblock: 0,
current_miniblock_hash: block_hash,
fee_input_provider: TestNodeFeeInputProvider::default(),
fee_input_provider,
tx_results: Default::default(),
blocks,
block_hashes,
Expand Down

0 comments on commit 788dc0a

Please sign in to comment.