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

Commit 7c2942f

Browse files
authored
fix l1fee constraint (#557)
1 parent 90b591c commit 7c2942f

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ use std::{
4848
collections::{BTreeMap, HashMap},
4949
iter,
5050
};
51-
pub use transaction::{Transaction, TransactionContext, TxL1Fee, TX_L1_FEE_PRECISION};
51+
pub use transaction::{
52+
Transaction, TransactionContext, TxL1Fee, TX_L1_COMMIT_EXTRA_COST, TX_L1_FEE_PRECISION,
53+
};
5254

5355
/// Circuit Setup Parameters
5456
#[derive(Debug, Clone, Copy)]

bus-mapping/src/circuit_input_builder/transaction.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use super::{call::ReversionGroup, Call, CallContext, CallKind, CodeSource, ExecS
2020

2121
/// Precision of transaction L1 fee
2222
pub const TX_L1_FEE_PRECISION: u64 = 1_000_000_000;
23+
/// Extra cost as the bytes of rlped tx commited to L1 (assume to non-zero, overestimated a bit)
24+
pub const TX_L1_COMMIT_EXTRA_COST: u64 = 64;
2325

2426
#[derive(Debug, Default)]
2527
/// Context of a [`Transaction`] which can mutate in an [`ExecStep`].
@@ -460,7 +462,7 @@ impl TxL1Fee {
460462
/// Calculate L1 fee and remainder of transaction.
461463
pub fn tx_l1_fee(&self, tx_data_gas_cost: u64) -> (u64, u64) {
462464
// <https://github.com/scroll-tech/go-ethereum/blob/49192260a177f1b63fc5ea3b872fb904f396260c/rollup/fees/rollup_fee.go#L118>
463-
let tx_l1_gas = tx_data_gas_cost + self.fee_overhead;
465+
let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST;
464466
let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128;
465467

466468
(

zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
util::Expr,
1111
};
1212
use bus_mapping::{
13-
circuit_input_builder::{TxL1Fee, TX_L1_FEE_PRECISION},
13+
circuit_input_builder::{TxL1Fee, TX_L1_COMMIT_EXTRA_COST, TX_L1_FEE_PRECISION},
1414
l2_predeployed::l1_gas_price_oracle,
1515
};
1616
use eth_types::{Field, ToLittleEndian, ToScalar};
@@ -153,7 +153,7 @@ impl<F: Field> TxL1FeeGadget<F> {
153153
.map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64]));
154154

155155
// <https://github.com/scroll-tech/go-ethereum/blob/49192260a177f1b63fc5ea3b872fb904f396260c/rollup/fees/rollup_fee.go#L118>
156-
let tx_l1_gas = tx_data_gas_cost + 1088.expr() + fee_overhead;
156+
let tx_l1_gas = tx_data_gas_cost + TX_L1_COMMIT_EXTRA_COST.expr() + fee_overhead;
157157
cb.require_equal(
158158
"fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder",
159159
fee_scalar * base_fee * tx_l1_gas,

zkevm-circuits/src/witness/l1_msg.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,63 @@ pub fn rom_table_rows() -> Vec<RomTableRow> {
3434
.map(|row| (row.0, row.1, row.2, L1MsgHash, row.3).into())
3535
.collect()
3636
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use bus_mapping::circuit_input_builder::TxL1Fee;
41+
use eth_types::{evm_types::gas_utils::tx_data_gas_cost, Transaction};
42+
43+
#[test]
44+
fn test_l1fee_calc_pre_eip155() {
45+
let txs: Vec<Transaction> = serde_json::from_str(
46+
r#"[
47+
{
48+
"type": "0x0",
49+
"nonce": "0x4",
50+
"hash": "0x8da3fedb103b6da8ccc2514094336d1a76df166238f4d8e8558fbe54cce2516a",
51+
"gas": "0xd25e",
52+
"gasPrice": "0x3b9aca00",
53+
"from": "0x1c5a77d9fa7ef466951b2f01f724bca3a5820b63",
54+
"to": "0x03f8133dd5ed58838b20af1296f62f44e69baa48",
55+
"chainId": "0xcf55",
56+
"value": "0x0",
57+
"input": "0xa9059cbb000000000000000000000000c0c4c8baea3f6acb49b6e1fb9e2adeceeacb0ca200000000000000000000000000000000000000000000000000000000000003e8",
58+
"isCreate": false,
59+
"v": "0x19ecd",
60+
"r": "0xaaa87d285f44e2683266d83116ee3df09313f38e91393bfe2966e947c31e4002",
61+
"s": "0x9e105efcad78b8e836aa9c588e39f0d81b2d6552d04762d0e02652a9ea94b1d"
62+
},
63+
{
64+
"type": "0x0",
65+
"nonce": "0x14",
66+
"hash": "0xfef778b40acae6c4f00205f3dafae2af1dff90d402c19b090c4b12cad08e7461",
67+
"gas": "0x5cb2",
68+
"gasPrice": "0x3b9aca00",
69+
"from": "0x1c5a77d9fa7ef466951b2f01f724bca3a5820b63",
70+
"to": "0x58a2239aa5412f78d8f675c4d8ad5102a3fa5837",
71+
"chainId": "0xcf55",
72+
"value": "0x0",
73+
"input": "0xb0f2b72a000000000000000000000000000000000000000000000000000000000000000a",
74+
"isCreate": false,
75+
"v": "0x19ece",
76+
"r": "0xa0ed5a985f5b74215ba05b0c3fc2a2af1c26c65d9426867eda637fa5d7d388eb",
77+
"s": "0x81054ba4a31ee6f0715f36d1005393623b97703c061afe5518a7e31ecbfda6f"
78+
}
79+
]"#,
80+
).unwrap();
81+
82+
let l1fee = TxL1Fee {
83+
base_fee: 0x64,
84+
fee_overhead: 0x17d4,
85+
fee_scalar: 0x4a42fc80,
86+
};
87+
88+
let expected = [(173usize, 0xfffe8u64), (140, 0xf3f2f)];
89+
90+
for (tx, (rlp_expected, l1fee_expected)) in txs.into_iter().zip(expected) {
91+
let rlp = tx.rlp().to_vec();
92+
assert_eq!(rlp.len(), rlp_expected);
93+
assert_eq!(l1fee.tx_l1_fee(tx_data_gas_cost(&rlp)).0, l1fee_expected)
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)