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

Commit

Permalink
Merge branch 'main' into mpt-storage-not-exist
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar authored Feb 1, 2024
2 parents cc98424 + fc4964c commit 7d0c5a8
Show file tree
Hide file tree
Showing 31 changed files with 481 additions and 225 deletions.
72 changes: 69 additions & 3 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ use std::{
pub use transaction::{Transaction, TransactionContext};
pub use withdrawal::{Withdrawal, WithdrawalContext};

/// Runtime Config
///
/// Default to mainnet block
#[derive(Debug, Clone, Copy)]
pub struct FeatureConfig {
/// Zero difficulty
pub zero_difficulty: bool,
/// Free first transaction
pub free_first_tx: bool,
/// Enable EIP1559
pub enable_eip1559: bool,
/// Allow invalid transactions to be included in a block
///
/// Transactions with mismatched nonce, insufficient gas limit, or insufficient balance
/// shouldn't be included in a mainnet block. However, rollup developers might want to
/// include invalid tx in the L2 block to support forced exit feature.
pub invalid_tx: bool,
}

impl Default for FeatureConfig {
fn default() -> Self {
Self {
zero_difficulty: true,
free_first_tx: false,
enable_eip1559: true,
invalid_tx: false,
}
}
}

impl FeatureConfig {
/// Check if we are mainnet config
pub fn is_mainnet(&self) -> bool {
self.zero_difficulty && !self.free_first_tx && self.enable_eip1559 && !self.invalid_tx
}
}

/// Circuit Setup Parameters
#[derive(Debug, Clone, Copy)]
pub struct FixedCParams {
Expand Down Expand Up @@ -150,18 +187,27 @@ pub struct CircuitInputBuilder<C: CircuitsParams> {
pub circuits_params: C,
/// Block Context
pub block_ctx: BlockContext,
/// Feature config
pub feature_config: FeatureConfig,
}

impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
/// Create a new CircuitInputBuilder from the given `eth_block` and
/// `constants`.
pub fn new(sdb: StateDB, code_db: CodeDB, block: Block, params: C) -> Self {
pub fn new(
sdb: StateDB,
code_db: CodeDB,
block: Block,
params: C,
feature_config: FeatureConfig,
) -> Self {
Self {
sdb,
code_db,
block,
circuits_params: params,
block_ctx: BlockContext::new(),
feature_config,
}
}

Expand Down Expand Up @@ -273,13 +319,15 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
let end_tx_step =
gen_associated_steps(&mut self.state_ref(&mut tx, &mut tx_ctx), ExecState::EndTx)?;
tx.steps_mut().push(end_tx_step);
} else {
} else if self.feature_config.invalid_tx {
// Generate InvalidTx step
let invalid_tx_step = gen_associated_steps(
&mut self.state_ref(&mut tx, &mut tx_ctx),
ExecState::InvalidTx,
)?;
tx.steps_mut().push(invalid_tx_step);
} else {
panic!("invalid tx support not enabled")
}

self.sdb.commit_tx();
Expand Down Expand Up @@ -457,6 +505,7 @@ impl CircuitInputBuilder<DynamicCParams> {
block: self.block,
circuits_params: c_params,
block_ctx: self.block_ctx,
feature_config: self.feature_config,
};

cib.set_end_block(c_params.max_rws)?;
Expand Down Expand Up @@ -567,6 +616,7 @@ pub struct BuilderClient<P: JsonRpcClient> {
cli: GethClient<P>,
chain_id: Word,
circuits_params: FixedCParams,
feature_config: FeatureConfig,
}

/// Get State Accesses from TxExecTraces
Expand Down Expand Up @@ -624,12 +674,22 @@ pub fn build_state_code_db(
impl<P: JsonRpcClient> BuilderClient<P> {
/// Create a new BuilderClient
pub async fn new(client: GethClient<P>, circuits_params: FixedCParams) -> Result<Self, Error> {
Self::new_with_features(client, circuits_params, FeatureConfig::default()).await
}

/// Create a new BuilderClient
pub async fn new_with_features(
client: GethClient<P>,
circuits_params: FixedCParams,
feature_config: FeatureConfig,
) -> Result<Self, Error> {
let chain_id = client.get_chain_id().await?;

Ok(Self {
cli: client,
chain_id: chain_id.into(),
circuits_params,
feature_config,
})
}

Expand Down Expand Up @@ -741,7 +801,13 @@ impl<P: JsonRpcClient> BuilderClient<P> {
prev_state_root: Word,
) -> Result<CircuitInputBuilder<FixedCParams>, Error> {
let block = Block::new(self.chain_id, history_hashes, prev_state_root, eth_block)?;
let mut builder = CircuitInputBuilder::new(sdb, code_db, block, self.circuits_params);
let mut builder = CircuitInputBuilder::new(
sdb,
code_db,
block,
self.circuits_params,
self.feature_config,
);
builder.handle_block(eth_block, geth_traces)?;
Ok(builder)
}
Expand Down
11 changes: 10 additions & 1 deletion bus-mapping/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
circuit_input_builder::{
get_state_accesses, Block, CircuitInputBuilder, CircuitsParams, DynamicCParams,
FixedCParams,
FeatureConfig, FixedCParams,
},
state_db::{self, CodeDB, StateDB},
};
Expand Down Expand Up @@ -34,6 +34,14 @@ impl<C: CircuitsParams> BlockData<C> {
/// Generate a new CircuitInputBuilder initialized with the context of the
/// BlockData.
pub fn new_circuit_input_builder(&self) -> CircuitInputBuilder<C> {
self.new_circuit_input_builder_with_feature(FeatureConfig::default())
}
/// Generate a new CircuitInputBuilder initialized with the context of the
/// BlockData.
pub fn new_circuit_input_builder_with_feature(
&self,
feature_config: FeatureConfig,
) -> CircuitInputBuilder<C> {
CircuitInputBuilder::new(
self.sdb.clone(),
self.code_db.clone(),
Expand All @@ -45,6 +53,7 @@ impl<C: CircuitsParams> BlockData<C> {
)
.unwrap(),
self.circuits_params,
feature_config,
)
}

Expand Down
2 changes: 2 additions & 0 deletions circuit-benchmarks/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ mod tests {
}
}

let max_nodes = 720;
let circuit = MPTCircuit::<Fr> {
nodes,
keccak_data,
degree: degree as usize,
max_nodes,
disable_preimage_check: false,
_marker: PhantomData,
};
Expand Down
4 changes: 3 additions & 1 deletion light-client-poc/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ mod witness;

pub use prover::StateUpdateCircuitKeys;

pub use state_update::{StateUpdateCircuit, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT};
pub use state_update::{
StateUpdateCircuit, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_NODES, DEFAULT_MAX_PROOF_COUNT,
};
pub use witness::{PublicInputs, StateUpdateWitness};
17 changes: 11 additions & 6 deletions light-client-poc/src/circuit/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use zkevm_circuits::{
util::{SubCircuit, SubCircuitConfig},
};

pub const DEFAULT_MAX_NODES: usize = 4000;
pub const DEFAULT_MAX_PROOF_COUNT: usize = 20;
pub const DEFAULT_CIRCUIT_DEGREE: usize = 14;

Expand Down Expand Up @@ -90,6 +91,7 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {
MPTCircuitParams {
degree: self.mpt_circuit.degree,
disable_preimage_check: self.mpt_circuit.disable_preimage_check,
max_nodes: self.mpt_circuit.max_nodes,
}
}

Expand Down Expand Up @@ -301,14 +303,15 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {

// assign MPT witness

let height =
config
.mpt_config
.assign(&mut layouter, &self.mpt_circuit.nodes, &challenges)?;
config.mpt_config.load_fixed_table(&mut layouter)?;
config
.mpt_config
.load_mult_table(&mut layouter, &challenges, height)?;
.assign(&mut layouter, &self.mpt_circuit.nodes, &challenges)?;
config.mpt_config.load_fixed_table(&mut layouter)?;
config.mpt_config.load_mult_table(
&mut layouter,
&challenges,
self.mpt_circuit.max_nodes,
)?;

#[cfg(feature = "disable-keccak")]
config.mpt_config.keccak_table.dev_load(
Expand Down Expand Up @@ -466,6 +469,7 @@ impl StateUpdateCircuit<Fr> {
pub fn new(
witness: StateUpdateWitness<Fr>,
degree: usize,
max_nodes: usize,
max_proof_count: usize,
) -> Result<StateUpdateCircuit<Fr>> {
let StateUpdateWitness {
Expand All @@ -489,6 +493,7 @@ impl StateUpdateCircuit<Fr> {
nodes: mpt_witness,
keccak_data: keccak_data.clone(),
degree,
max_nodes,
disable_preimage_check,
_marker: std::marker::PhantomData,
};
Expand Down
10 changes: 7 additions & 3 deletions light-client-poc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{collections::HashMap, str::FromStr, time::SystemTime};

use crate::circuit::{
PublicInputs, StateUpdateCircuit, StateUpdateCircuitKeys, StateUpdateWitness,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_NODES, DEFAULT_MAX_PROOF_COUNT,
};

pub async fn serve() -> Result<()> {
Expand Down Expand Up @@ -62,8 +62,12 @@ pub async fn serve() -> Result<()> {
};

let public_inputs: PublicInputs<Fr> = (&witness.lc_witness).into();
let circuit =
StateUpdateCircuit::new(witness, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT)?;
let circuit = StateUpdateCircuit::new(
witness,
DEFAULT_CIRCUIT_DEGREE,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)?;

println!("trns: {:#?}", circuit.transforms);

Expand Down
Loading

0 comments on commit 7d0c5a8

Please sign in to comment.