Skip to content

Commit

Permalink
update L1 data fee for curie (#1293)
Browse files Browse the repository at this point in the history
* add new fields

* update get_committed_values_from_state_db

* add curies field into TxL1FeeGadget

* add curie fields reads

* assign curie fields

* update raw_construct helper

* update l1fee formula

* rename

* use upper case

* fix build

* fmt align

* fix test build

* fix ci test

* add get_rlp_signed

* update end_tx etc

* update TxL1FeeGadgetTestContainer

* fix l1fee_curie feature build err

* add debug log

* change gen_tx_l1_fee_ops in buss

* add blob_scalar

* correct l1BaseFee

* begin_tx adjust rw offset when curie

* upgrade l2geth (with latest l1 fee)

* update tx_signed_length

* update committed assign

* fix build

* debug log

* minor fix

* some cleanup

* update link

* try eth_tx.rlp

* remove get_rlp_signed

* [FIX #1307] (#1308)

* fix constraint for remainder in l1fee

Signed-off-by: noelwei <[email protected]>

* fmt

Signed-off-by: noelwei <[email protected]>

---------

Signed-off-by: noelwei <[email protected]>
Co-authored-by: DreamWuGit <[email protected]>

---------

Signed-off-by: noelwei <[email protected]>
Co-authored-by: Zhang Zhuo <[email protected]>
Co-authored-by: Ho <[email protected]>
  • Loading branch information
3 people authored May 30, 2024
1 parent f6a1acb commit 6368e07
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 34 deletions.
3 changes: 2 additions & 1 deletion bus-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ rpc-legacy-tracer = []
# For the trace obtained from erigon node, refund field is missed
# and must be rebuild
fix-refund = ["rpc-legacy-tracer"]
retrace-tx = []
retrace-tx = []
l1_fee_curie = []
109 changes: 99 additions & 10 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ pub struct Transaction {
pub rlp_bytes: Vec<u8>,
/// RLP bytes for signing
pub rlp_unsigned_bytes: Vec<u8>,
/// RLP bytes for signed tx
pub rlp_signed_bytes: Vec<u8>,
/// Current values of L1 fee
pub l1_fee: TxL1Fee,
/// Committed values of L1 fee
Expand Down Expand Up @@ -233,6 +235,7 @@ impl From<&Transaction> for geth_types::Transaction {
gas_fee_cap: Some(tx.gas_fee_cap),
gas_tip_cap: Some(tx.gas_tip_cap),
rlp_unsigned_bytes: tx.rlp_unsigned_bytes.clone(),
//rlp_signed_bytes: tx.rlp_signed_bytes.clone(),
rlp_bytes: tx.rlp_bytes.clone(),
tx_type: tx.tx_type,
..Default::default()
Expand Down Expand Up @@ -261,6 +264,7 @@ impl Transaction {
},
rlp_bytes: vec![],
rlp_unsigned_bytes: vec![],
rlp_signed_bytes: vec![],
calls: Vec::new(),
steps: Vec::new(),
block_num: Default::default(),
Expand Down Expand Up @@ -362,6 +366,8 @@ impl Transaction {
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(),
nonce: eth_tx.nonce.as_u64(),
gas: eth_tx.gas.as_u64(),
gas_price: eth_tx.gas_price.unwrap_or_default(),
Expand Down Expand Up @@ -432,9 +438,18 @@ impl Transaction {

/// Calculate L1 fee of this transaction.
pub fn l1_fee(&self) -> u64 {
let tx_data_gas_cost = tx_data_gas_cost(&self.rlp_bytes);

self.l1_fee.tx_l1_fee(tx_data_gas_cost).0
//TODO: check if need to update for curie
#[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
}
}
}

Expand Down Expand Up @@ -476,19 +491,58 @@ pub struct TxL1Fee {
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.
pub fn tx_l1_fee(&self, tx_data_gas_cost: u64) -> (u64, u64) {
/// 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>
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;
#[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,
)
}

(
(tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64,
(tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64,
)
#[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 {}
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,
)
}
}

fn get_current_values_from_state_db(sdb: &StateDB) -> Self {
Expand All @@ -502,11 +556,28 @@ 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,
&l1_gas_price_oracle::BLOB_SCALAR_SLOT,
]
.map(|slot| {
sdb.get_storage(&l1_gas_price_oracle::ADDRESS, slot)
.1
.as_u64()
});

Self {
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 All @@ -522,10 +593,28 @@ 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,
&l1_gas_price_oracle::BLOB_SCALAR_SLOT,
]
.map(|slot| {
sdb.get_committed_storage(&l1_gas_price_oracle::ADDRESS, slot)
.1
.as_u64()
});

Self {
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,
}
}
}
59 changes: 57 additions & 2 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result<Vec<ExecSt
}
}
} else {
// else, add 3 RW read operations for transaction L1 fee.
// else, add 3 ( or 6 after curie) RW read operations for transaction L1 fee.
gen_tx_l1_fee_ops(state, &mut exec_step)?;
}

Expand Down Expand Up @@ -729,7 +729,7 @@ fn write_tx_receipt(
Ok(())
}

// Add 3 RW read operations for transaction L1 fee.
// Add 3(or 6 after curie) RW read operations for transaction L1 fee.
fn gen_tx_l1_fee_ops(
state: &mut CircuitInputStateRef,
exec_step: &mut ExecStep,
Expand All @@ -739,11 +739,24 @@ fn gen_tx_l1_fee_ops(
let base_fee = Word::from(state.tx.l1_fee.base_fee);
let fee_overhead = Word::from(state.tx.l1_fee.fee_overhead);
let fee_scalar = Word::from(state.tx.l1_fee.fee_scalar);
#[cfg(feature = "l1_fee_curie")]
let l1_blob_basefee = Word::from(state.tx.l1_fee.l1_blob_basefee);
#[cfg(feature = "l1_fee_curie")]
let commit_scalar = Word::from(state.tx.l1_fee.commit_scalar);
#[cfg(feature = "l1_fee_curie")]
let blob_scalar = Word::from(state.tx.l1_fee.blob_scalar);

let base_fee_committed = Word::from(state.tx.l1_fee_committed.base_fee);
let fee_overhead_committed = Word::from(state.tx.l1_fee_committed.fee_overhead);
let fee_scalar_committed = Word::from(state.tx.l1_fee_committed.fee_scalar);

#[cfg(feature = "l1_fee_curie")]
let l1_blob_basefee_committed = Word::from(state.tx.l1_fee_committed.l1_blob_basefee);
#[cfg(feature = "l1_fee_curie")]
let commit_scalar_committed = Word::from(state.tx.l1_fee_committed.commit_scalar);
#[cfg(feature = "l1_fee_curie")]
let blob_scalar_committed = Word::from(state.tx.l1_fee_committed.blob_scalar);

state.push_op(
exec_step,
RW::READ,
Expand Down Expand Up @@ -780,6 +793,48 @@ fn gen_tx_l1_fee_ops(
fee_scalar_committed,
),
)?;

// curie operations
#[cfg(feature = "l1_fee_curie")]
{
state.push_op(
exec_step,
RW::READ,
StorageOp::new(
*l1_gas_price_oracle::ADDRESS,
*l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT,
l1_blob_basefee,
l1_blob_basefee,
tx_id,
l1_blob_basefee_committed,
),
)?;
state.push_op(
exec_step,
RW::READ,
StorageOp::new(
*l1_gas_price_oracle::ADDRESS,
*l1_gas_price_oracle::COMMIT_SCALAR_SLOT,
commit_scalar,
commit_scalar,
tx_id,
commit_scalar_committed,
),
)?;
state.push_op(
exec_step,
RW::READ,
StorageOp::new(
*l1_gas_price_oracle::ADDRESS,
*l1_gas_price_oracle::BLOB_SCALAR_SLOT,
blob_scalar,
blob_scalar,
tx_id,
blob_scalar_committed,
),
)?;
}

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions bus-mapping/src/l2_predeployed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ pub mod l1_gas_price_oracle {
pub static SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(3));

/// THe following 3 slots plus `BASE_FEE_SLOT` will be used for l1 fee after curie fork
/// L1 BlobBaseFee slot in L1GasPriceOracle after Curie fork
pub static L1_BLOB_BASEFEE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(5));
/// L1 commitScalar slot in L1GasPriceOracle after Curie fork
pub static COMMIT_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(6));
/// L1 blob_scalar slot in L1GasPriceOracle after Curie fork
pub static BLOB_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(7));
pub static IS_CURIE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(8));
pub static INITIAL_COMMIT_SCALAR: LazyLock<U256> =
Expand Down
4 changes: 2 additions & 2 deletions eth-types/src/geth_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use ethers_core::types::{
transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, Eip2930TransactionRequest,
NameOrAddress, TransactionRequest, H256,
NameOrAddress, Signature, TransactionRequest, H256,
};
use halo2curves::{group::ff::PrimeField, secp256k1::Fq};
use num::Integer;
Expand Down Expand Up @@ -289,7 +289,7 @@ pub struct Transaction {
pub rlp_bytes: Vec<u8>,
/// RLP unsigned bytes
pub rlp_unsigned_bytes: Vec<u8>,

// TODO: add rlp_signed_bytes as well ?
/// Transaction hash
pub hash: H256,
}
Expand Down
2 changes: 1 addition & 1 deletion geth-utils/l2geth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/imdario/mergo v0.3.16
github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac
github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions geth-utils/l2geth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ github.com/scroll-tech/go-ethereum v1.10.14-0.20240428082752-c5836a42d3b9 h1:j+r
github.com/scroll-tech/go-ethereum v1.10.14-0.20240428082752-c5836a42d3b9/go.mod h1:i4VBgWoaW/y0D8MmQb7hSOulyw1dKhuiSFAbznwivCA=
github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac h1:A7tBJeDFXVjL4URIR89Ir8amS345Kgnh//+FA2v3lEw=
github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac/go.mod h1:GLhY3RmqBezwgPCqrPM/dTjrweDzVSAQ+Ggldk4dCTM=
github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1 h1:U7c23zVBjt4H01scnaglcPth0eXL9nPJCMA7RfZPXAQ=
github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1/go.mod h1:GLhY3RmqBezwgPCqrPM/dTjrweDzVSAQ+Ggldk4dCTM=
github.com/scroll-tech/zktrie v0.7.1 h1:NrmZNjuBzsbrKePqdHDG+t2cXnimbtezPAFS0+L9ElE=
github.com/scroll-tech/zktrie v0.7.1/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/scroll-tech/zktrie v0.8.2 h1:UMuIfA+jdgWMLmTgTL64Emo+zzMOdcnH0+eYdDcshxQ=
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ debug-annotations = []
enable-stack = ["bus-mapping/enable-stack"]
enable-memory = ["bus-mapping/enable-memory"]
enable-storage = ["bus-mapping/enable-storage"]
l1_fee_curie = ["bus-mapping/l1_fee_curie"]
Loading

0 comments on commit 6368e07

Please sign in to comment.