Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit fc4964c

Browse files
Runtime config (#1728)
### Description We add an initial structure for runtime config. In this PR, we plan to add only the invalid tx configuration for the starter. ### Issue Link #1636 ### Type of change New feature (non-breaking change which adds functionality) ### Decision - Allow Geth to take invalid tx. The config doesn't affect geth util ### TODO - [x] Fix invalid tx test - [ ] Add tx validity check. (Will continue #1740)
1 parent 7f35654 commit fc4964c

File tree

15 files changed

+300
-103
lines changed

15 files changed

+300
-103
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ use std::{
4242
pub use transaction::{Transaction, TransactionContext};
4343
pub use withdrawal::{Withdrawal, WithdrawalContext};
4444

45+
/// Runtime Config
46+
///
47+
/// Default to mainnet block
48+
#[derive(Debug, Clone, Copy)]
49+
pub struct FeatureConfig {
50+
/// Zero difficulty
51+
pub zero_difficulty: bool,
52+
/// Free first transaction
53+
pub free_first_tx: bool,
54+
/// Enable EIP1559
55+
pub enable_eip1559: bool,
56+
/// Allow invalid transactions to be included in a block
57+
///
58+
/// Transactions with mismatched nonce, insufficient gas limit, or insufficient balance
59+
/// shouldn't be included in a mainnet block. However, rollup developers might want to
60+
/// include invalid tx in the L2 block to support forced exit feature.
61+
pub invalid_tx: bool,
62+
}
63+
64+
impl Default for FeatureConfig {
65+
fn default() -> Self {
66+
Self {
67+
zero_difficulty: true,
68+
free_first_tx: false,
69+
enable_eip1559: true,
70+
invalid_tx: false,
71+
}
72+
}
73+
}
74+
75+
impl FeatureConfig {
76+
/// Check if we are mainnet config
77+
pub fn is_mainnet(&self) -> bool {
78+
self.zero_difficulty && !self.free_first_tx && self.enable_eip1559 && !self.invalid_tx
79+
}
80+
}
81+
4582
/// Circuit Setup Parameters
4683
#[derive(Debug, Clone, Copy)]
4784
pub struct FixedCParams {
@@ -150,18 +187,27 @@ pub struct CircuitInputBuilder<C: CircuitsParams> {
150187
pub circuits_params: C,
151188
/// Block Context
152189
pub block_ctx: BlockContext,
190+
/// Feature config
191+
pub feature_config: FeatureConfig,
153192
}
154193

155194
impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
156195
/// Create a new CircuitInputBuilder from the given `eth_block` and
157196
/// `constants`.
158-
pub fn new(sdb: StateDB, code_db: CodeDB, block: Block, params: C) -> Self {
197+
pub fn new(
198+
sdb: StateDB,
199+
code_db: CodeDB,
200+
block: Block,
201+
params: C,
202+
feature_config: FeatureConfig,
203+
) -> Self {
159204
Self {
160205
sdb,
161206
code_db,
162207
block,
163208
circuits_params: params,
164209
block_ctx: BlockContext::new(),
210+
feature_config,
165211
}
166212
}
167213

@@ -273,13 +319,15 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
273319
let end_tx_step =
274320
gen_associated_steps(&mut self.state_ref(&mut tx, &mut tx_ctx), ExecState::EndTx)?;
275321
tx.steps_mut().push(end_tx_step);
276-
} else {
322+
} else if self.feature_config.invalid_tx {
277323
// Generate InvalidTx step
278324
let invalid_tx_step = gen_associated_steps(
279325
&mut self.state_ref(&mut tx, &mut tx_ctx),
280326
ExecState::InvalidTx,
281327
)?;
282328
tx.steps_mut().push(invalid_tx_step);
329+
} else {
330+
panic!("invalid tx support not enabled")
283331
}
284332

285333
self.sdb.commit_tx();
@@ -457,6 +505,7 @@ impl CircuitInputBuilder<DynamicCParams> {
457505
block: self.block,
458506
circuits_params: c_params,
459507
block_ctx: self.block_ctx,
508+
feature_config: self.feature_config,
460509
};
461510

462511
cib.set_end_block(c_params.max_rws)?;
@@ -567,6 +616,7 @@ pub struct BuilderClient<P: JsonRpcClient> {
567616
cli: GethClient<P>,
568617
chain_id: Word,
569618
circuits_params: FixedCParams,
619+
feature_config: FeatureConfig,
570620
}
571621

572622
/// Get State Accesses from TxExecTraces
@@ -624,12 +674,22 @@ pub fn build_state_code_db(
624674
impl<P: JsonRpcClient> BuilderClient<P> {
625675
/// Create a new BuilderClient
626676
pub async fn new(client: GethClient<P>, circuits_params: FixedCParams) -> Result<Self, Error> {
677+
Self::new_with_features(client, circuits_params, FeatureConfig::default()).await
678+
}
679+
680+
/// Create a new BuilderClient
681+
pub async fn new_with_features(
682+
client: GethClient<P>,
683+
circuits_params: FixedCParams,
684+
feature_config: FeatureConfig,
685+
) -> Result<Self, Error> {
627686
let chain_id = client.get_chain_id().await?;
628687

629688
Ok(Self {
630689
cli: client,
631690
chain_id: chain_id.into(),
632691
circuits_params,
692+
feature_config,
633693
})
634694
}
635695

@@ -741,7 +801,13 @@ impl<P: JsonRpcClient> BuilderClient<P> {
741801
prev_state_root: Word,
742802
) -> Result<CircuitInputBuilder<FixedCParams>, Error> {
743803
let block = Block::new(self.chain_id, history_hashes, prev_state_root, eth_block)?;
744-
let mut builder = CircuitInputBuilder::new(sdb, code_db, block, self.circuits_params);
804+
let mut builder = CircuitInputBuilder::new(
805+
sdb,
806+
code_db,
807+
block,
808+
self.circuits_params,
809+
self.feature_config,
810+
);
745811
builder.handle_block(eth_block, geth_traces)?;
746812
Ok(builder)
747813
}

bus-mapping/src/mock.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
circuit_input_builder::{
55
get_state_accesses, Block, CircuitInputBuilder, CircuitsParams, DynamicCParams,
6-
FixedCParams,
6+
FeatureConfig, FixedCParams,
77
},
88
state_db::{self, CodeDB, StateDB},
99
};
@@ -34,6 +34,14 @@ impl<C: CircuitsParams> BlockData<C> {
3434
/// Generate a new CircuitInputBuilder initialized with the context of the
3535
/// BlockData.
3636
pub fn new_circuit_input_builder(&self) -> CircuitInputBuilder<C> {
37+
self.new_circuit_input_builder_with_feature(FeatureConfig::default())
38+
}
39+
/// Generate a new CircuitInputBuilder initialized with the context of the
40+
/// BlockData.
41+
pub fn new_circuit_input_builder_with_feature(
42+
&self,
43+
feature_config: FeatureConfig,
44+
) -> CircuitInputBuilder<C> {
3745
CircuitInputBuilder::new(
3846
self.sdb.clone(),
3947
self.code_db.clone(),
@@ -45,6 +53,7 @@ impl<C: CircuitsParams> BlockData<C> {
4553
)
4654
.unwrap(),
4755
self.circuits_params,
56+
feature_config,
4857
)
4958
}
5059

zkevm-circuits/src/bin/stats/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bus_mapping::circuit_input_builder::FeatureConfig;
12
use cli_table::{print_stdout, Cell, Style, Table};
23
use eth_types::{bytecode, evm_types::OpcodeId, ToWord};
34
use halo2_proofs::{
@@ -113,7 +114,7 @@ fn copy_states_stats() {
113114
/// cell consumers of each EVM Cell type.
114115
fn get_exec_steps_occupancy() {
115116
let mut meta = ConstraintSystem::<Fr>::default();
116-
let circuit = EvmCircuit::configure(&mut meta);
117+
let circuit = EvmCircuit::configure_with_params(&mut meta, FeatureConfig::default());
117118

118119
let report = circuit.0.execution.instrument().clone().analyze();
119120
macro_rules! gen_report {

zkevm-circuits/src/evm_circuit.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
},
2727
util::{Challenges, SubCircuit, SubCircuitConfig},
2828
};
29-
use bus_mapping::evm::OpcodeId;
29+
use bus_mapping::{circuit_input_builder::FeatureConfig, evm::OpcodeId};
3030
use eth_types::Field;
3131
use execution::ExecutionConfig;
3232
use itertools::Itertools;
@@ -74,6 +74,8 @@ pub struct EvmCircuitConfigArgs<F: Field> {
7474
pub u8_table: UXTable<8>,
7575
/// U16Table
7676
pub u16_table: UXTable<16>,
77+
/// Feature config
78+
pub feature_config: FeatureConfig,
7779
}
7880

7981
impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
@@ -93,6 +95,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
9395
exp_table,
9496
u8_table,
9597
u16_table,
98+
feature_config,
9699
}: Self::ConfigArgs,
97100
) -> Self {
98101
let fixed_table = [(); 4].map(|_| meta.fixed_column());
@@ -109,6 +112,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
109112
&copy_table,
110113
&keccak_table,
111114
&exp_table,
115+
feature_config,
112116
));
113117

114118
u8_table.annotate_columns(meta);
@@ -310,7 +314,8 @@ pub(crate) mod cached {
310314
/// Circuit configuration. These values are calculated just once.
311315
static ref CACHE: Cache = {
312316
let mut meta = ConstraintSystem::<Fr>::default();
313-
let config = EvmCircuit::<Fr>::configure(&mut meta);
317+
// Cached EVM circuit is configured with Mainnet FeatureConfig
318+
let config = EvmCircuit::<Fr>::configure_with_params(&mut meta, FeatureConfig::default());
314319
Cache { cs: meta, config }
315320
};
316321
}
@@ -356,13 +361,21 @@ pub(crate) mod cached {
356361
impl<F: Field> Circuit<F> for EvmCircuit<F> {
357362
type Config = (EvmCircuitConfig<F>, Challenges);
358363
type FloorPlanner = SimpleFloorPlanner;
359-
type Params = ();
364+
type Params = FeatureConfig;
360365

361366
fn without_witnesses(&self) -> Self {
362367
Self::default()
363368
}
364369

365-
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
370+
/// Try to get the [`FeatureConfig`] from the block or fallback to default
371+
fn params(&self) -> Self::Params {
372+
self.block
373+
.as_ref()
374+
.map(|block| block.feature_config)
375+
.unwrap_or_default()
376+
}
377+
378+
fn configure_with_params(meta: &mut ConstraintSystem<F>, params: Self::Params) -> Self::Config {
366379
let tx_table = TxTable::construct(meta);
367380
let rw_table = RwTable::construct(meta);
368381
let bytecode_table = BytecodeTable::construct(meta);
@@ -390,12 +403,17 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
390403
exp_table,
391404
u8_table,
392405
u16_table,
406+
feature_config: params,
393407
},
394408
),
395409
challenges,
396410
)
397411
}
398412

413+
fn configure(_meta: &mut ConstraintSystem<F>) -> Self::Config {
414+
unreachable!();
415+
}
416+
399417
fn synthesize(
400418
&self,
401419
config: Self::Config,
@@ -443,7 +461,10 @@ mod evm_circuit_stats {
443461
util::{unusable_rows, SubCircuit},
444462
witness::block_convert,
445463
};
446-
use bus_mapping::{circuit_input_builder::FixedCParams, mock::BlockData};
464+
use bus_mapping::{
465+
circuit_input_builder::{FeatureConfig, FixedCParams},
466+
mock::BlockData,
467+
};
447468

448469
use eth_types::{bytecode, geth_types::GethData};
449470
use halo2_proofs::{self, dev::MockProver, halo2curves::bn256::Fr};
@@ -455,9 +476,20 @@ mod evm_circuit_stats {
455476

456477
#[test]
457478
fn evm_circuit_unusable_rows() {
479+
let computed = EvmCircuit::<Fr>::unusable_rows();
480+
let mainnet_config = FeatureConfig::default();
481+
let invalid_tx_config = FeatureConfig {
482+
invalid_tx: true,
483+
..Default::default()
484+
};
485+
486+
assert_eq!(
487+
computed,
488+
unusable_rows::<Fr, EvmCircuit::<Fr>>(mainnet_config),
489+
);
458490
assert_eq!(
459-
EvmCircuit::<Fr>::unusable_rows(),
460-
unusable_rows::<Fr, EvmCircuit::<Fr>>(()),
491+
computed,
492+
unusable_rows::<Fr, EvmCircuit::<Fr>>(invalid_tx_config),
461493
)
462494
}
463495

0 commit comments

Comments
 (0)