Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 9a3fbf3

Browse files
lispckunxian-xiasilathdiir
committed
Release v0.5.x allow skip l1msg (#760)
* stage * do not copy num_txs to block table * fix * export two api public * add log * add more log * fix start_l1_queue_index import * Update `ChunkHash::from_witness_block`. * Fix to pass `start_l1_queue_index`. * Update logs and fix lint. --------- Co-authored-by: kunxian xia <xiakunxian130@gmail.com> Co-authored-by: Steven Gu <asongala@163.com>
1 parent 16827b9 commit 9a3fbf3

File tree

8 files changed

+253
-26
lines changed

8 files changed

+253
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ zkevm-circuits = { path = "../zkevm-circuits" }
1313
ark-std = "0.3.0"
1414
env_logger = "0.10.0"
1515
ethers-core = "0.17.0"
16+
hex = "0.4.3"
1617
log = "0.4"
1718
itertools = "0.10.3"
1819
serde = { version = "1.0", features = ["derive"] }

aggregator/src/chunk.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,35 @@ impl ChunkHash {
3535
pub fn from_witness_block(block: &Block<Fr>, is_padding: bool) -> Self {
3636
// <https://github.com/scroll-tech/zkevm-circuits/blob/25dd32aa316ec842ffe79bb8efe9f05f86edc33e/bus-mapping/src/circuit_input_builder.rs#L690>
3737

38+
let mut total_l1_popped = block.start_l1_queue_index;
39+
log::debug!("chunk-hash: start_l1_queue_index = {}", total_l1_popped);
3840
let data_bytes = iter::empty()
41+
// .chain(block_headers.iter().flat_map(|(&block_num, block)| {
3942
.chain(block.context.ctxs.iter().flat_map(|(b_num, b_ctx)| {
40-
let num_txs = block
43+
let num_l2_txs = block
4144
.txs
4245
.iter()
43-
.filter(|tx| tx.block_number == *b_num)
44-
.count() as u16;
46+
.filter(|tx| !tx.tx_type.is_l1_msg() && tx.block_number == *b_num)
47+
.count() as u64;
48+
let num_l1_msgs = block
49+
.txs
50+
.iter()
51+
.filter(|tx| tx.tx_type.is_l1_msg() && tx.block_number == *b_num)
52+
// tx.nonce alias for queue_index for l1 msg tx
53+
.map(|tx| tx.nonce)
54+
.max()
55+
.map_or(0, |max_queue_index| max_queue_index - total_l1_popped + 1);
56+
total_l1_popped += num_l1_msgs;
57+
58+
let num_txs = (num_l2_txs + num_l1_msgs) as u16;
59+
log::debug!(
60+
"chunk-hash: [block {}] total_l1_popped = {}, num_l1_msgs = {}, num_l2_txs = {}, num_txs = {}",
61+
b_num,
62+
total_l1_popped,
63+
num_l1_msgs,
64+
num_l2_txs,
65+
num_txs,
66+
);
4567

4668
iter::empty()
4769
// Block Values
@@ -56,6 +78,10 @@ impl ChunkHash {
5678
.collect::<Vec<u8>>();
5779

5880
let data_hash = H256(keccak256(data_bytes));
81+
log::debug!(
82+
"chunk-hash: data hash = {}",
83+
hex::encode(data_hash.to_fixed_bytes())
84+
);
5985

6086
let post_state_root = block
6187
.context

bus-mapping/src/circuit_input_builder.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ pub fn keccak_inputs(block: &Block, code_db: &CodeDB) -> Result<Vec<Vec<u8>>, Er
644644
// PI circuit
645645
keccak_inputs.extend(keccak_inputs_pi_circuit(
646646
block.chain_id,
647+
block.start_l1_queue_index,
647648
block.prev_state_root,
648649
block.withdraw_root,
649650
&block.headers,
@@ -736,17 +737,41 @@ pub fn get_dummy_tx_hash() -> H256 {
736737

737738
fn keccak_inputs_pi_circuit(
738739
chain_id: u64,
740+
start_l1_queue_index: u64,
739741
prev_state_root: Word,
740742
withdraw_trie_root: Word,
741743
block_headers: &BTreeMap<u64, BlockHead>,
742744
transactions: &[Transaction],
743745
) -> Vec<Vec<u8>> {
746+
let mut total_l1_popped = start_l1_queue_index;
747+
log::debug!(
748+
"start_l1_queue_index in keccak_inputs: {}",
749+
start_l1_queue_index
750+
);
744751
let data_bytes = iter::empty()
745-
.chain(block_headers.iter().flat_map(|(block_num, block)| {
746-
let num_txs = transactions
752+
.chain(block_headers.iter().flat_map(|(&block_num, block)| {
753+
let num_l2_txs = transactions
747754
.iter()
748-
.filter(|tx| tx.block_num == *block_num)
749-
.count() as u16;
755+
.filter(|tx| !tx.tx_type.is_l1_msg() && tx.block_num == block_num)
756+
.count() as u64;
757+
let num_l1_msgs = transactions
758+
.iter()
759+
.filter(|tx| tx.tx_type.is_l1_msg() && tx.block_num == block_num)
760+
// tx.nonce alias for queue_index for l1 msg tx
761+
.map(|tx| tx.nonce)
762+
.max()
763+
.map_or(0, |max_queue_index| max_queue_index - total_l1_popped + 1);
764+
total_l1_popped += num_l1_msgs;
765+
766+
let num_txs = (num_l2_txs + num_l1_msgs) as u16;
767+
log::debug!(
768+
"[block {}] total_l1_popped: {}, num_l1_msgs: {}, num_l2_txs: {}, num_txs: {}",
769+
block_num,
770+
total_l1_popped,
771+
num_l1_msgs,
772+
num_l2_txs,
773+
num_txs,
774+
);
750775

751776
iter::empty()
752777
// Block Values
@@ -760,6 +785,10 @@ fn keccak_inputs_pi_circuit(
760785
.chain(transactions.iter().flat_map(|tx| tx.hash.to_fixed_bytes()))
761786
.collect::<Vec<u8>>();
762787
let data_hash = H256(keccak256(&data_bytes));
788+
log::debug!(
789+
"chunk data hash: {}",
790+
hex::encode(data_hash.to_fixed_bytes())
791+
);
763792
let after_state_root = block_headers
764793
.last_key_value()
765794
.map(|(_, blk)| blk.eth_block.state_root)

bus-mapping/src/circuit_input_builder/block.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub struct BlockHead {
8888
pub difficulty: Word,
8989
/// base fee
9090
pub base_fee: Word,
91+
/// start l1 queue index
92+
pub start_l1_queue_index: u64,
9193
/// Original block from geth
9294
pub eth_block: eth_types::Block<eth_types::Transaction>,
9395
}
@@ -108,6 +110,49 @@ impl BlockHead {
108110
Ok(Self {
109111
chain_id,
110112
history_hashes,
113+
start_l1_queue_index: 0,
114+
coinbase: eth_block
115+
.author
116+
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
117+
gas_limit: eth_block.gas_limit.low_u64(),
118+
number: eth_block
119+
.number
120+
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?
121+
.low_u64()
122+
.into(),
123+
timestamp: eth_block.timestamp,
124+
difficulty: if eth_block.difficulty.is_zero() {
125+
eth_block
126+
.mix_hash
127+
.unwrap_or_default()
128+
.to_fixed_bytes()
129+
.into()
130+
} else {
131+
eth_block.difficulty
132+
},
133+
base_fee: eth_block.base_fee_per_gas.unwrap_or_default(),
134+
eth_block: eth_block.clone(),
135+
})
136+
}
137+
138+
/// Create a new block.
139+
pub fn new_with_l1_queue_index(
140+
chain_id: u64,
141+
start_l1_queue_index: u64,
142+
history_hashes: Vec<Word>,
143+
eth_block: &eth_types::Block<eth_types::Transaction>,
144+
) -> Result<Self, Error> {
145+
if eth_block.base_fee_per_gas.is_none() {
146+
// FIXME: resolve this once we have proper EIP-1559 support
147+
log::debug!(
148+
"This does not look like a EIP-1559 block - base_fee_per_gas defaults to zero"
149+
);
150+
}
151+
152+
Ok(Self {
153+
chain_id,
154+
history_hashes,
155+
start_l1_queue_index,
111156
coinbase: eth_block
112157
.author
113158
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
@@ -163,6 +208,8 @@ pub struct Block {
163208
pub circuits_params: CircuitsParams,
164209
/// chain id
165210
pub chain_id: u64,
211+
/// start_l1_queue_index
212+
pub start_l1_queue_index: u64,
166213
/// IO to/from the precompiled contract calls.
167214
pub precompile_events: PrecompileEvents,
168215
}
@@ -217,6 +264,41 @@ impl Block {
217264
Ok(block)
218265
}
219266

267+
/// Create a new block.
268+
pub fn new_with_l1_queue_index(
269+
chain_id: u64,
270+
start_l1_queue_index: u64,
271+
history_hashes: Vec<Word>,
272+
eth_block: &eth_types::Block<eth_types::Transaction>,
273+
circuits_params: CircuitsParams,
274+
) -> Result<Self, Error> {
275+
let mut block = Self {
276+
block_steps: BlockSteps {
277+
end_block_not_last: ExecStep {
278+
exec_state: ExecState::EndBlock,
279+
..ExecStep::default()
280+
},
281+
end_block_last: ExecStep {
282+
exec_state: ExecState::EndBlock,
283+
..ExecStep::default()
284+
},
285+
},
286+
exp_events: Vec::new(),
287+
chain_id,
288+
start_l1_queue_index,
289+
circuits_params,
290+
..Default::default()
291+
};
292+
let info = BlockHead::new_with_l1_queue_index(
293+
chain_id,
294+
start_l1_queue_index,
295+
history_hashes,
296+
eth_block,
297+
)?;
298+
block.headers.insert(info.number.as_u64(), info);
299+
Ok(block)
300+
}
301+
220302
/// Return the list of transactions of this block.
221303
pub fn txs(&self) -> &[Transaction] {
222304
&self.txs

0 commit comments

Comments
 (0)