Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update L1 data fee for curie #1293

Merged
merged 40 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5645e46
add new fields
DreamWuGit May 27, 2024
1de93d4
Merge branch 'develop' into l1fee_curie
DreamWuGit May 27, 2024
aa0efcb
update get_committed_values_from_state_db
DreamWuGit May 27, 2024
37e71c0
Merge branch 'l1fee_curie' of https://github.com/scroll-tech/zkevm-ci…
DreamWuGit May 27, 2024
6e52f58
add curies field into TxL1FeeGadget
DreamWuGit May 28, 2024
0bf279f
add curie fields reads
DreamWuGit May 28, 2024
b4b62be
assign curie fields
DreamWuGit May 28, 2024
a9113a9
update raw_construct helper
DreamWuGit May 28, 2024
af080ae
update l1fee formula
DreamWuGit May 28, 2024
7047e47
rename
DreamWuGit May 28, 2024
4295cff
use upper case
DreamWuGit May 28, 2024
2e787c4
fix build
DreamWuGit May 28, 2024
8ebf7e9
resolve conflict & merge to develop
DreamWuGit May 28, 2024
fa0f99b
fmt align
DreamWuGit May 28, 2024
a7062e4
Merge branch 'develop' into l1fee_curie
DreamWuGit May 28, 2024
830bb91
fix test build
DreamWuGit May 28, 2024
5a2048a
Merge branch 'l1fee_curie' of https://github.com/scroll-tech/zkevm-ci…
DreamWuGit May 28, 2024
e9bf085
fix ci test
DreamWuGit May 29, 2024
49a9459
Merge branch 'develop' into l1fee_curie
DreamWuGit May 29, 2024
0cd77fa
add get_rlp_signed
DreamWuGit May 29, 2024
c8355b1
Merge branch 'l1fee_curie' of https://github.com/scroll-tech/zkevm-ci…
DreamWuGit May 29, 2024
e3e7af7
update end_tx etc
DreamWuGit May 29, 2024
76faebc
update TxL1FeeGadgetTestContainer
DreamWuGit May 29, 2024
63cf7a0
fix l1fee_curie feature build err
DreamWuGit May 29, 2024
fc5acf9
add debug log
DreamWuGit May 29, 2024
b4dc8c9
change gen_tx_l1_fee_ops in buss
DreamWuGit May 29, 2024
2dceeba
add blob_scalar
DreamWuGit May 29, 2024
38c8978
correct l1BaseFee
DreamWuGit May 29, 2024
0941e1e
begin_tx adjust rw offset when curie
DreamWuGit May 29, 2024
da53bc2
upgrade l2geth (with latest l1 fee)
lispc May 29, 2024
5af852d
update tx_signed_length
DreamWuGit May 29, 2024
d6e9b5f
update committed assign
DreamWuGit May 29, 2024
de28405
fix build
DreamWuGit May 29, 2024
705622c
debug log
DreamWuGit May 29, 2024
7f115d6
minor fix
DreamWuGit May 29, 2024
e3cae39
some cleanup
DreamWuGit May 29, 2024
43e1200
update link
DreamWuGit May 30, 2024
5e0d566
try eth_tx.rlp
DreamWuGit May 30, 2024
ec016bd
remove get_rlp_signed
DreamWuGit May 30, 2024
deb6d5c
[FIX #1307] (#1308)
noel2004 May 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 98 additions & 11 deletions bus-mapping/src/circuit_input_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{l2_predeployed::l1_gas_price_oracle, Error};
use eth_types::{
evm_types::{gas_utils::tx_data_gas_cost, OpcodeId},
geth_types,
geth_types::{get_rlp_unsigned, TxType},
geth_types::{get_rlp_signed, get_rlp_unsigned, TxType},
state_db::{CodeDB, StateDB},
AccessList, Address, GethExecTrace, Signature, Word, H256,
};
Expand Down 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 signing
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,7 @@ 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),
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 +437,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 +490,57 @@ 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;
// check if the calculation changes for curie upgrade
#[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")]
{
// "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee",
let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128
lispc marked this conversation as resolved.
Show resolved Hide resolved
+ 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 +554,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 +591,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 commitScalar slot in L1GasPriceOracle after Curie forks
DreamWuGit marked this conversation as resolved.
Show resolved Hide resolved
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
46 changes: 44 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 @@ -158,6 +158,48 @@ pub fn get_rlp_unsigned(tx: &crate::Transaction) -> Vec<u8> {
}
}

/// Get the RLP signed bytes
/// NOTE: currently only used by curie upgrade calculating l1fee
pub fn get_rlp_signed(tx: &crate::Transaction) -> Vec<u8> {
DreamWuGit marked this conversation as resolved.
Show resolved Hide resolved
let sig_v = tx.v; // U64 type
let signature = Signature {
r: tx.r,
s: tx.s,
v: tx.v.as_u64(),
};

match TxType::get_tx_type(tx) {
TxType::Eip155 => {
let mut tx: TransactionRequest = tx.into();
tx.chain_id = Some(tx.chain_id.unwrap_or_else(|| {
let recv_v = TxType::Eip155.get_recovery_id(sig_v.as_u64()) as u64;
(sig_v - recv_v - 35) / 2
}));

tx.rlp_signed(&signature).to_vec()
}
TxType::PreEip155 => {
let tx: TransactionRequest = tx.into();
tx.rlp_signed(&signature).to_vec()
}
TxType::Eip1559 => {
let tx: Eip1559TransactionRequest = tx.into();
let typed_tx: TypedTransaction = tx.into();
typed_tx.rlp_signed(&signature).to_vec()
}
TxType::Eip2930 => {
let tx: Eip2930TransactionRequest = tx.into();
let typed_tx: TypedTransaction = tx.into();
//typed_tx.rlp().to_vec()
typed_tx.rlp_signed(&signature).to_vec()
}
TxType::L1Msg => {
// L1 msg does not have signature
vec![]
}
}
}

/// Definition of all of the data related to an account.
#[serde_as]
#[derive(PartialEq, Eq, Debug, Default, Clone, Serialize)]
Expand Down Expand Up @@ -289,7 +331,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
Loading