Skip to content

Commit

Permalink
feat(l1 fee): many updates (scroll-tech#1316)
Browse files Browse the repository at this point in the history
* feat: circuit can decide l1 fee according to is_curie

* fix begin tx assginment

* half

* improve log

* ci: cargo test report time

* move l2_predeployed to eth-types

* move l2_predeployed to eth-types

* run

* full scroll trace parsing

* fix l1 fee for l1 tx

* fix default mode

* tmp

* lint

* tmp

* lint

* fix

* fix curie gadget

* upgrade l2geth

* update l1 fee contract

* clean comments

* fix mock block num 0

* fix l1 fee for 1559 tests

* update super circuit testing params

* fix tx circuit field_rlc

* lint

* update l2geth mod
  • Loading branch information
lispc authored Jun 4, 2024
1 parent a93a392 commit ef56646
Show file tree
Hide file tree
Showing 33 changed files with 449 additions and 308 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
- name: Cargo cache
uses: Swatinem/rust-cache@v2
- name: Run light tests # light tests are run in parallel
run: cargo test --verbose --release --all --exclude integration-tests --exclude circuit-benchmarks
run: cargo test --verbose --release --all --exclude integration-tests --exclude circuit-benchmarks -- -Zunstable-options --report-time
- name: Run heavy tests # heavy tests are run serially to avoid OOM
if: false
run: cargo test --verbose --release --all --all-features --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --test-threads 1
Expand Down Expand Up @@ -117,9 +117,9 @@ jobs:
- name: Cargo cache
uses: Swatinem/rust-cache@v2
- name: Run light tests # light tests are run in parallel
run: cargo test --verbose --release --all --features scroll --exclude integration-tests --exclude circuit-benchmarks --exclude testool
run: cargo test --verbose --release --all --features scroll --exclude integration-tests --exclude circuit-benchmarks --exclude testool -- -Zunstable-options --report-time
- name: Run heavy tests
run: cargo test --verbose --release --features scroll --all --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --skip max_tx
run: cargo test --verbose --release --features scroll --all --exclude integration-tests --exclude circuit-benchmarks serial_ -- --ignored --skip max_tx -Zunstable-options --report-time
- name: Run parallel assignment tests(bytecode)
if: false
run: cargo test --release --package zkevm-circuits --lib bytecode_circuit::test --features scroll,parallel_syn -- --nocapture
Expand Down
1 change: 0 additions & 1 deletion bus-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@ rpc-legacy-tracer = []
# and must be rebuild
fix-refund = ["rpc-legacy-tracer"]
retrace-tx = []
l1_fee_curie = []
2 changes: 1 addition & 1 deletion bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl<'a> CircuitInputBuilder {

// Curie sys contract upgrade
let is_curie_fork_block =
curie::is_curie_fork(state.block.chain_id, last_block_num.as_u64());
curie::is_curie_fork_block(state.block.chain_id, last_block_num.as_u64());
if is_curie_fork_block {
curie::apply_curie(&mut state, &mut end_block_step)?;
}
Expand Down
25 changes: 16 additions & 9 deletions bus-mapping/src/circuit_input_builder/curie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ use crate::{

use super::{CircuitInputStateRef, ExecStep};

/// Whether this blk is the hardfork height of curie
pub fn is_curie_fork(chain_id: u64, blk: u64) -> bool {
/// Get curie fork block height
pub fn get_curie_fork_block(chain_id: u64) -> u64 {
for (fork, fork_chain_id, fork_blk) in eth_types::forks::hardfork_heights() {
if fork == eth_types::forks::HardforkId::Curie
&& chain_id == fork_chain_id
&& fork_blk == blk
{
log::info!("enable curie fork: chain id {chain_id} block {blk}");
return true;
if fork == eth_types::forks::HardforkId::Curie && chain_id == fork_chain_id {
log::debug!("curie fork: chain id {chain_id} block {fork_blk}");
return fork_blk;
}
}
false
0
}

/// Whether this blk has enabled curie fork
pub fn is_curie_enabled(chain_id: u64, blk: u64) -> bool {
blk >= get_curie_fork_block(chain_id)
}

/// Whether this blk is the hardfork height of curie
pub fn is_curie_fork_block(chain_id: u64, blk: u64) -> bool {
blk == get_curie_fork_block(chain_id)
}

/// Insert needed rws for the contract upgrade
Expand Down
135 changes: 67 additions & 68 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Transaction & TransactionContext utility module.
use super::{call::ReversionGroup, Call, CallContext, CallKind, CodeSource, ExecStep};
use super::{
call::ReversionGroup, curie::is_curie_enabled, Call, CallContext, CallKind, CodeSource,
ExecStep,
};
use crate::{l2_predeployed::l1_gas_price_oracle, Error};
#[cfg(not(feature = "l1_fee_curie"))]
use eth_types::evm_types::gas_utils::tx_data_gas_cost;
use eth_types::{
evm_types::OpcodeId,
Expand Down Expand Up @@ -286,6 +288,8 @@ impl Transaction {
eth_tx: &eth_types::Transaction,
is_success: bool,
) -> Result<Self, Error> {
let chain_id = eth_tx.chain_id.unwrap_or_default().as_u64();
let block_num = eth_tx.block_number.unwrap().as_u64();
let (found, _) = sdb.get_account(&eth_tx.from);
if !found {
return Err(Error::AccountNotFound(eth_tx.from));
Expand Down Expand Up @@ -351,8 +355,8 @@ impl Transaction {
Default::default()
} else {
(
TxL1Fee::get_current_values_from_state_db(sdb),
TxL1Fee::get_committed_values_from_state_db(sdb),
TxL1Fee::get_current_values_from_state_db(sdb, chain_id, block_num),
TxL1Fee::get_committed_values_from_state_db(sdb, chain_id, block_num),
)
};

Expand All @@ -361,15 +365,17 @@ impl Transaction {
l1_fee,
l1_fee_committed
);
let rlp_signed_bytes = eth_tx.rlp().to_vec();
//debug_assert_eq!(H256(ethers_core::utils::keccak256(&bytes)), eth_tx.hash);

Ok(Self {
block_num: eth_tx.block_number.unwrap().as_u64(),
block_num,
hash: eth_tx.hash,
chain_id,
tx_type,
rlp_bytes: eth_tx.rlp().to_vec(),
rlp_unsigned_bytes: get_rlp_unsigned(eth_tx),
//rlp_signed_bytes: get_rlp_signed(eth_tx),
rlp_signed_bytes: eth_tx.rlp().to_vec(),
rlp_signed_bytes,
nonce: eth_tx.nonce.as_u64(),
gas: eth_tx.gas.as_u64(),
gas_price: eth_tx.gas_price.unwrap_or_default(),
Expand All @@ -379,7 +385,6 @@ impl Transaction {
to: eth_tx.to,
value: eth_tx.value,
input: eth_tx.input.to_vec(),
chain_id: eth_tx.chain_id.unwrap_or_default().as_u64(), // FIXME
calls: vec![call],
steps: Vec::new(),
signature: Signature {
Expand Down Expand Up @@ -440,17 +445,12 @@ impl Transaction {

/// Calculate L1 fee of this transaction.
pub fn l1_fee(&self) -> u64 {
#[cfg(not(feature = "l1_fee_curie"))]
{
let tx_data_gas_cost = tx_data_gas_cost(&self.rlp_bytes);
self.l1_fee.tx_l1_fee(tx_data_gas_cost, 0).0
}
#[cfg(feature = "l1_fee_curie")]
{
// TODO: calculate tx rlp signed length
let tx_signed_length = self.rlp_signed_bytes.len();
self.l1_fee.tx_l1_fee(0, tx_signed_length as u64).0
}
self.l1_fee
.tx_l1_fee(
tx_data_gas_cost(&self.rlp_bytes),
self.rlp_signed_bytes.len() as u64,
)
.0
}
}

Expand Down Expand Up @@ -486,67 +486,70 @@ impl Transaction {
/// Transaction L1 fee for L1GasPriceOracle contract
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TxL1Fee {
/// chain id
pub chain_id: u64,
/// block number
pub block_number: u64,
/// L1 base fee
pub base_fee: u64,
/// L1 fee overhead
pub fee_overhead: u64,
/// L1 fee scalar
pub fee_scalar: u64,
#[cfg(feature = "l1_fee_curie")]
/// L1 blob fee
pub l1_blob_basefee: u64,
#[cfg(feature = "l1_fee_curie")]
/// L1 commit scalar
pub commit_scalar: u64,
#[cfg(feature = "l1_fee_curie")]
/// l1 blob scalar
pub blob_scalar: u64,
}

impl TxL1Fee {
/// Calculate L1 fee and remainder of transaction.
/// for non curie upgrade case, tx_rlp_signed_len is not used, set to zero
pub fn tx_l1_fee(&self, _tx_data_gas_cost: u64, tx_rlp_signed_len: u64) -> (u64, u64) {
// <https://github.com/scroll-tech/go-ethereum/blob/49192260a177f1b63fc5ea3b872fb904f396260c/rollup/fees/rollup_fee.go#L118>
#[cfg(not(feature = "l1_fee_curie"))]
{
let tx_l1_gas = _tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST;
let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128;
(
(tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64,
(tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64,
)
pub fn tx_l1_fee(&self, tx_data_gas_cost: u64, tx_rlp_signed_len: u64) -> (u64, u64) {
let is_curie = is_curie_enabled(self.chain_id, self.block_number);
if is_curie {
self.tx_l1_fee_after_curie(tx_rlp_signed_len)
} else {
self.tx_l1_fee_before_curie(tx_data_gas_cost)
}
}

#[cfg(feature = "l1_fee_curie")]
{
// for curie upgrade:
// new formula: https://github.com/scroll-tech/go-ethereum/blob/develop/rollup/fees/rollup_fee.go#L165
// "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee",
let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128
+ self.blob_scalar as u128
* tx_rlp_signed_len as u128
* self.l1_blob_basefee as u128;
log::debug!(
"tx_l1_fee {} commit_scalar {} base_fee {} blob_scalar {}
fn tx_l1_fee_before_curie(&self, tx_data_gas_cost: u64) -> (u64, u64) {
// <https://github.com/scroll-tech/go-ethereum/blob/49192260a177f1b63fc5ea3b872fb904f396260c/rollup/fees/rollup_fee.go#L118>
let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST;
let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128;
(
(tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64,
(tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64,
)
}

fn tx_l1_fee_after_curie(&self, tx_rlp_signed_len: u64) -> (u64, u64) {
// for curie upgrade:
// new formula: https://github.com/scroll-tech/go-ethereum/blob/develop/rollup/fees/rollup_fee.go#L165
// "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee",
let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128
+ self.blob_scalar as u128 * tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128;
log::debug!(
"tx_l1_fee {} commit_scalar {} base_fee {} blob_scalar {}
tx_rlp_signed_len {} l1_blob_basefee {} tx_quient {},reminder {}",
tx_l1_fee,
self.commit_scalar,
self.base_fee,
self.blob_scalar,
tx_rlp_signed_len,
self.l1_blob_basefee,
tx_l1_fee / TX_L1_FEE_PRECISION as u128,
tx_l1_fee % TX_L1_FEE_PRECISION as u128
);
(
(tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64,
(tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64,
)
}
tx_l1_fee,
self.commit_scalar,
self.base_fee,
self.blob_scalar,
tx_rlp_signed_len,
self.l1_blob_basefee,
tx_l1_fee / TX_L1_FEE_PRECISION as u128,
tx_l1_fee % TX_L1_FEE_PRECISION as u128
);
(
(tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64,
(tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64,
)
}

fn get_current_values_from_state_db(sdb: &StateDB) -> Self {
fn get_current_values_from_state_db(sdb: &StateDB, chain_id: u64, block_number: u64) -> Self {
let [base_fee, fee_overhead, fee_scalar] = [
&l1_gas_price_oracle::BASE_FEE_SLOT,
&l1_gas_price_oracle::OVERHEAD_SLOT,
Expand All @@ -557,7 +560,6 @@ impl TxL1Fee {
.1
.as_u64()
});
#[cfg(feature = "l1_fee_curie")]
let [l1_blob_basefee, commit_scalar, blob_scalar] = [
&l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT,
&l1_gas_price_oracle::COMMIT_SCALAR_SLOT,
Expand All @@ -570,19 +572,18 @@ impl TxL1Fee {
});

Self {
chain_id,
block_number,
base_fee,
fee_overhead,
fee_scalar,
#[cfg(feature = "l1_fee_curie")]
l1_blob_basefee,
#[cfg(feature = "l1_fee_curie")]
commit_scalar,
#[cfg(feature = "l1_fee_curie")]
blob_scalar,
}
}

fn get_committed_values_from_state_db(sdb: &StateDB) -> Self {
fn get_committed_values_from_state_db(sdb: &StateDB, chain_id: u64, block_number: u64) -> Self {
let [base_fee, fee_overhead, fee_scalar] = [
&l1_gas_price_oracle::BASE_FEE_SLOT,
&l1_gas_price_oracle::OVERHEAD_SLOT,
Expand All @@ -594,7 +595,6 @@ impl TxL1Fee {
.as_u64()
});

#[cfg(feature = "l1_fee_curie")]
let [l1_blob_basefee, commit_scalar, blob_scalar] = [
&l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT,
&l1_gas_price_oracle::COMMIT_SCALAR_SLOT,
Expand All @@ -607,14 +607,13 @@ impl TxL1Fee {
});

Self {
chain_id,
block_number,
base_fee,
fee_overhead,
fee_scalar,
#[cfg(feature = "l1_fee_curie")]
l1_blob_basefee,
#[cfg(feature = "l1_fee_curie")]
commit_scalar,
#[cfg(feature = "l1_fee_curie")]
blob_scalar,
}
}
Expand Down
1 change: 0 additions & 1 deletion bus-mapping/src/data/v2_l1_oracle_bytecode.txt

This file was deleted.

Loading

0 comments on commit ef56646

Please sign in to comment.