Skip to content

Commit ddbd100

Browse files
tomip01jrchatruc
authored andcommitted
feat(l2): config eth client through .toml (#2510)
**Motivation** Here we want to be able to configure some constant values in our L2. These changes aim to improve flexibility in the L2 and provide better control. **Description** * Added `elasticity_multiplier` to `BuildPayloadArgs` and passed it to `calculate_base_fee_per_gas`. * Incorporated `elasticity_multiplier` into `BlockProducer`. * Introduced new fields (`max_number_of_retries`, `backoff_factor`, `min_retry_delay`, `max_retry_delay`) in `EthClient`. Closes #2479 --------- Co-authored-by: Javier Rodríguez Chatruc <49622509+jrchatruc@users.noreply.github.com>
1 parent d78be48 commit ddbd100

File tree

22 files changed

+221
-47
lines changed

22 files changed

+221
-47
lines changed

cmd/ethrex/l2/options.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl From<SequencerOptions> for SequencerConfig {
4949
block_producer: BlockProducerConfig {
5050
block_time_ms: opts.proposer_opts.block_time_ms,
5151
coinbase_address: opts.proposer_opts.coinbase_address,
52+
elasticity_multiplier: opts.proposer_opts.elasticity_multiplier,
5253
},
5354
l1_committer: CommitterConfig {
5455
on_chain_proposer_address: opts.committer_opts.on_chain_proposer_address,
@@ -63,6 +64,10 @@ impl From<SequencerOptions> for SequencerConfig {
6364
},
6465
eth: EthConfig {
6566
rpc_url: opts.eth_opts.rpc_url,
67+
max_number_of_retries: opts.eth_opts.max_number_of_retries,
68+
backoff_factor: opts.eth_opts.backoff_factor,
69+
min_retry_delay: opts.eth_opts.min_retry_delay,
70+
max_retry_delay: opts.eth_opts.max_retry_delay,
6671
maximum_allowed_max_fee_per_gas: opts.eth_opts.maximum_allowed_max_fee_per_gas,
6772
maximum_allowed_max_fee_per_blob_gas: opts
6873
.eth_opts
@@ -115,6 +120,38 @@ pub struct EthOptions {
115120
help_heading = "Eth options"
116121
)]
117122
pub maximum_allowed_max_fee_per_blob_gas: u64,
123+
#[arg(
124+
long = "eth-max-number-of-retries",
125+
default_value = "10",
126+
value_name = "UINT64",
127+
env = "ETHREX_MAX_NUMBER_OF_RETRIES",
128+
help_heading = "Eth options"
129+
)]
130+
pub max_number_of_retries: u64,
131+
#[arg(
132+
long = "eth-backoff-factor",
133+
default_value = "2",
134+
value_name = "UINT64",
135+
env = "ETHREX_BACKOFF_FACTOR",
136+
help_heading = "Eth options"
137+
)]
138+
pub backoff_factor: u64,
139+
#[arg(
140+
long = "eth-min-retry-delay",
141+
default_value = "96",
142+
value_name = "UINT64",
143+
env = "ETHREX_MIN_RETRY_DELAY",
144+
help_heading = "Eth options"
145+
)]
146+
pub min_retry_delay: u64,
147+
#[arg(
148+
long = "eth-max-retry-delay",
149+
default_value = "1800",
150+
value_name = "UINT64",
151+
env = "ETHREX_MAX_RETRY_DELAY",
152+
help_heading = "Eth options"
153+
)]
154+
pub max_retry_delay: u64,
118155
}
119156

120157
#[derive(Parser)]
@@ -187,6 +224,14 @@ pub struct ProposerOptions {
187224
help_heading = "Proposer options"
188225
)]
189226
pub coinbase_address: Address,
227+
#[arg(
228+
long,
229+
default_value = "2",
230+
value_name = "UINT64",
231+
env = "ETHREX_PROPOSER_ELASTICITY_MULTIPLIER",
232+
help_heading = "Proposer options"
233+
)]
234+
pub elasticity_multiplier: u64,
190235
}
191236

192237
#[derive(Parser)]

crates/blockchain/blockchain.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ethrex_common::types::{
1616
validate_prague_header_fields, validate_pre_cancun_header_fields, Block, BlockHash,
1717
BlockHeader, BlockNumber, ChainConfig, EIP4844Transaction, Receipt, Transaction,
1818
};
19-
use ethrex_common::types::{BlobsBundle, Fork};
19+
use ethrex_common::types::{BlobsBundle, Fork, ELASTICITY_MULTIPLIER};
2020

2121
use ethrex_common::{Address, H256};
2222
use mempool::Mempool;
@@ -75,7 +75,7 @@ impl Blockchain {
7575
let chain_config = self.storage.get_chain_config()?;
7676

7777
// Validate the block pre-execution
78-
validate_block(block, &parent_header, &chain_config)?;
78+
validate_block(block, &parent_header, &chain_config, ELASTICITY_MULTIPLIER)?;
7979

8080
let mut vm = Evm::new(
8181
self.evm_engine,
@@ -102,7 +102,7 @@ impl Blockchain {
102102
vm: &mut Evm,
103103
) -> Result<BlockExecutionResult, ChainError> {
104104
// Validate the block pre-execution
105-
validate_block(block, parent_header, chain_config)?;
105+
validate_block(block, parent_header, chain_config, ELASTICITY_MULTIPLIER)?;
106106

107107
let execution_result = vm.execute_block(block)?;
108108

@@ -560,9 +560,11 @@ pub fn validate_block(
560560
block: &Block,
561561
parent_header: &BlockHeader,
562562
chain_config: &ChainConfig,
563+
elasticity_multiplier: u64,
563564
) -> Result<(), ChainError> {
564565
// Verify initial header validity against parent
565-
validate_block_header(&block.header, parent_header).map_err(InvalidBlockError::from)?;
566+
validate_block_header(&block.header, parent_header, elasticity_multiplier)
567+
.map_err(InvalidBlockError::from)?;
566568

567569
if chain_config.is_prague_activated(block.header.timestamp) {
568570
validate_prague_header_fields(&block.header, parent_header, chain_config)

crates/blockchain/payload.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct BuildPayloadArgs {
4949
pub withdrawals: Option<Vec<Withdrawal>>,
5050
pub beacon_root: Option<H256>,
5151
pub version: u8,
52+
pub elasticity_multiplier: u64,
5253
}
5354

5455
impl BuildPayloadArgs {
@@ -111,6 +112,7 @@ pub fn create_payload(args: &BuildPayloadArgs, storage: &Store) -> Result<Block,
111112
parent_block.gas_limit,
112113
parent_block.gas_used,
113114
parent_block.base_fee_per_gas.unwrap_or_default(),
115+
args.elasticity_multiplier,
114116
),
115117
withdrawals_root: chain_config
116118
.is_shanghai_activated(args.timestamp)

crates/blockchain/smoke_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod blockchain_integration_test {
1111
};
1212

1313
use ethrex_common::{
14-
types::{Block, BlockHeader},
14+
types::{Block, BlockHeader, ELASTICITY_MULTIPLIER},
1515
H160, H256,
1616
};
1717
use ethrex_storage::{EngineType, Store};
@@ -301,6 +301,7 @@ mod blockchain_integration_test {
301301
withdrawals: Some(Vec::new()),
302302
beacon_root: Some(H256::random()),
303303
version: 1,
304+
elasticity_multiplier: ELASTICITY_MULTIPLIER,
304305
};
305306

306307
// Create blockchain

crates/common/types/block.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
ChainConfig, BASE_FEE_MAX_CHANGE_DENOMINATOR, ELASTICITY_MULTIPLIER,
3-
GAS_LIMIT_ADJUSTMENT_FACTOR, GAS_LIMIT_MINIMUM, INITIAL_BASE_FEE,
2+
ChainConfig, BASE_FEE_MAX_CHANGE_DENOMINATOR, GAS_LIMIT_ADJUSTMENT_FACTOR, GAS_LIMIT_MINIMUM,
3+
INITIAL_BASE_FEE,
44
};
55
use crate::{
66
constants::{GAS_PER_BLOB, MIN_BASE_FEE_PER_BLOB_GAS},
@@ -432,14 +432,15 @@ pub fn calculate_base_fee_per_gas(
432432
parent_gas_limit: u64,
433433
parent_gas_used: u64,
434434
parent_base_fee_per_gas: u64,
435+
elasticity_multiplier: u64,
435436
) -> Option<u64> {
436437
// Check gas limit, if the check passes we can also rest assured that none of the
437438
// following divisions will have zero as a divider
438439
if !check_gas_limit(block_gas_limit, parent_gas_limit) {
439440
return None;
440441
}
441442

442-
let parent_gas_target = parent_gas_limit / ELASTICITY_MULTIPLIER;
443+
let parent_gas_target = parent_gas_limit / elasticity_multiplier;
443444

444445
Some(match parent_gas_used.cmp(&parent_gas_target) {
445446
Ordering::Equal => parent_base_fee_per_gas,
@@ -513,6 +514,7 @@ pub enum InvalidBlockHeaderError {
513514
pub fn validate_block_header(
514515
header: &BlockHeader,
515516
parent_header: &BlockHeader,
517+
elasticity_multiplier: u64,
516518
) -> Result<(), InvalidBlockHeaderError> {
517519
if header.gas_used > header.gas_limit {
518520
return Err(InvalidBlockHeaderError::GasUsedGreaterThanGasLimit);
@@ -523,6 +525,7 @@ pub fn validate_block_header(
523525
parent_header.gas_limit,
524526
parent_header.gas_used,
525527
parent_header.base_fee_per_gas.unwrap_or(INITIAL_BASE_FEE),
528+
elasticity_multiplier,
526529
) {
527530
base_fee
528531
} else {
@@ -669,7 +672,7 @@ pub fn calc_excess_blob_gas(
669672
#[cfg(test)]
670673
mod test {
671674
use super::*;
672-
use crate::types::EMPTY_KECCACK_HASH;
675+
use crate::types::{ELASTICITY_MULTIPLIER, EMPTY_KECCACK_HASH};
673676
use ethereum_types::H160;
674677
use hex_literal::hex;
675678
use std::str::FromStr;
@@ -787,7 +790,7 @@ mod test {
787790
parent_beacon_block_root: Some(H256::zero()),
788791
requests_hash: Some(*EMPTY_KECCACK_HASH),
789792
};
790-
assert!(validate_block_header(&block, &parent_block).is_ok())
793+
assert!(validate_block_header(&block, &parent_block, ELASTICITY_MULTIPLIER).is_ok())
791794
}
792795

793796
#[test]

crates/l2/contracts/bin/deployer/cli.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ pub struct DeployerOptions {
4242
help = "Private key corresponding of a funded account that will be used for L1 contract deployment.",
4343
)]
4444
pub private_key: SecretKey,
45+
#[arg(
46+
long,
47+
default_value = "10",
48+
value_name = "UINT64",
49+
env = "ETHREX_ETH_MAX_NUMBER_OF_RETRIES",
50+
help_heading = "Eth options"
51+
)]
52+
pub max_number_of_retries: u64,
53+
#[arg(
54+
long,
55+
default_value = "2",
56+
value_name = "UINT64",
57+
env = "ETHREX_ETH_BACKOFF_FACTOR",
58+
help_heading = "Eth options"
59+
)]
60+
pub backoff_factor: u64,
61+
#[arg(
62+
long,
63+
default_value = "96",
64+
value_name = "UINT64",
65+
env = "ETHREX_ETH_MIN_RETRY_DELAY",
66+
help_heading = "Eth options"
67+
)]
68+
pub min_retry_delay: u64,
69+
#[arg(
70+
long,
71+
default_value = "1800",
72+
value_name = "UINT64",
73+
env = "ETHREX_ETH_MAX_RETRY_DELAY",
74+
help_heading = "Eth options"
75+
)]
76+
pub max_retry_delay: u64,
4577
#[arg(
4678
long,
4779
value_name = "PATH",
@@ -181,6 +213,10 @@ impl Default for DeployerOptions {
181213
rpc_url: "http://localhost:8545".to_string(),
182214
maximum_allowed_max_fee_per_gas: 10_000_000_000,
183215
maximum_allowed_max_fee_per_blob_gas: 10_000_000_000,
216+
max_number_of_retries: 10,
217+
backoff_factor: 2,
218+
min_retry_delay: 96,
219+
max_retry_delay: 1800,
184220
#[allow(clippy::unwrap_used)]
185221
private_key: SecretKey::from_slice(
186222
H256([

crates/l2/contracts/bin/deployer/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ const BRIDGE_INITIALIZER_SIGNATURE: &str = "initialize(address)";
3535
async fn main() -> Result<(), DeployerError> {
3636
let opts = DeployerOptions::parse();
3737

38-
let eth_client = EthClient::new_with_maximum_fees(
38+
let eth_client = EthClient::new_with_config(
3939
&opts.rpc_url,
40-
opts.maximum_allowed_max_fee_per_gas,
41-
opts.maximum_allowed_max_fee_per_blob_gas,
40+
opts.max_number_of_retries,
41+
opts.backoff_factor,
42+
opts.min_retry_delay,
43+
opts.max_retry_delay,
44+
Some(opts.maximum_allowed_max_fee_per_gas),
45+
Some(opts.maximum_allowed_max_fee_per_blob_gas),
4246
);
4347

4448
download_contract_deps(&opts)?;

crates/l2/prover/src/backends/exec.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn execution_program(input: ProgramInput) -> Result<ProgramOutput, Box<dyn std::
4242
blocks,
4343
parent_block_header,
4444
mut db,
45+
elasticity_multiplier,
4546
} = input;
4647

4748
// Tries used for validating initial and final state root
@@ -63,7 +64,12 @@ fn execution_program(input: ProgramInput) -> Result<ProgramOutput, Box<dyn std::
6364

6465
for block in blocks {
6566
// Validate the block
66-
validate_block(&block, &parent_header, &db.chain_config)?;
67+
validate_block(
68+
&block,
69+
&parent_header,
70+
&db.chain_config,
71+
elasticity_multiplier,
72+
)?;
6773

6874
// Execute block
6975
let mut vm = Evm::from_execution_db(db.clone());

crates/l2/prover/src/prover.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ impl Prover {
8383
input: ProgramInput {
8484
blocks: input.blocks,
8585
parent_block_header: input.parent_block_header,
86-
db: input.db
86+
db: input.db,
87+
elasticity_multiplier: input.elasticity_multiplier,
8788
}
8889
};
8990
Ok(prover_data)

crates/l2/prover/tests/perf_zkvm.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::expect_used)]
22
#![allow(clippy::unwrap_used)]
33
use ethrex_blockchain::Blockchain;
4-
use ethrex_common::types::Block;
4+
use ethrex_common::types::{Block, ELASTICITY_MULTIPLIER};
55
use ethrex_prover_lib::execute;
66
use ethrex_storage::{EngineType, Store};
77
use ethrex_vm::Evm;
@@ -16,7 +16,6 @@ async fn test_performance_zkvm() {
1616
let (input, block_to_prove) = setup().await;
1717

1818
let start = std::time::Instant::now();
19-
2019
// this is only executing because these tests run as a CI job and should be fast
2120
// TODO: create a test for actual proving
2221
execute(input).unwrap();
@@ -69,10 +68,14 @@ async fn setup() -> (ProgramInput, Block) {
6968
.await
7069
.unwrap();
7170

71+
// This is just a test, so we can use the default value for the elasticity multiplier.
72+
let elasticity_multiplier = ELASTICITY_MULTIPLIER;
73+
7274
let input = ProgramInput {
7375
blocks: vec![block_to_prove.clone()],
7476
parent_block_header,
7577
db,
78+
elasticity_multiplier,
7679
};
7780
(input, block_to_prove.clone())
7881
}

0 commit comments

Comments
 (0)