Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Feb 12, 2025
1 parent effe793 commit a94bc40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
27 changes: 13 additions & 14 deletions crates/optimism/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ mod tests {
tx.base.gas_limit = 100;
tx.enveloped_tx = None;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK);

let gas = call_last_frame_return(ctx, InstructionResult::Revert, Gas::new(90));
assert_eq!(gas.remaining(), 90);
Expand All @@ -527,7 +527,7 @@ mod tests {
tx.deposit.source_hash = B256::ZERO;
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);

let gas = call_last_frame_return(ctx, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 90);
Expand All @@ -543,7 +543,7 @@ mod tests {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);

let mut ret_gas = Gas::new(90);
ret_gas.record_refund(20);
Expand All @@ -567,7 +567,7 @@ mod tests {
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::BEDROCK);
let gas = call_last_frame_return(ctx, InstructionResult::Stop, Gas::new(90));
assert_eq!(gas.remaining(), 0);
assert_eq!(gas.spent(), 100);
Expand All @@ -594,7 +594,7 @@ mod tests {
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);
ctx.modify_tx(|tx| {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
Expand Down Expand Up @@ -630,7 +630,7 @@ mod tests {
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into())
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
Expand Down Expand Up @@ -668,7 +668,7 @@ mod tests {
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into())
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 100;
tx.deposit.source_hash = B256::ZERO;
Expand Down Expand Up @@ -704,7 +704,7 @@ mod tests {
operator_fee_constant: Some(U256::from(50)),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS.into())
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::ISTHMUS)
.modify_tx_chained(|tx| {
tx.base.gas_limit = 10;
tx.enveloped_tx = Some(bytes!("FACADE"));
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {
l1_base_fee_scalar: U256::from(1_000),
..Default::default()
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into())
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH)
.modify_tx_chained(|tx| {
tx.enveloped_tx = Some(bytes!("FACADE"));
});
Expand Down Expand Up @@ -771,7 +771,7 @@ mod tests {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.is_system_transaction = true;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);

let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
Expand All @@ -783,8 +783,7 @@ mod tests {
))
);

evm.ctx()
.modify_cfg(|cfg| cfg.spec = OpSpecId::BEDROCK.into());
evm.ctx().modify_cfg(|cfg| cfg.spec = OpSpecId::BEDROCK);

// Pre-regolith system transactions should be allowed.
assert!(handler.validate_env(&mut evm).is_ok());
Expand All @@ -798,7 +797,7 @@ mod tests {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);

let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
Expand All @@ -814,7 +813,7 @@ mod tests {
tx.base.tx_type = DEPOSIT_TRANSACTION_TYPE;
tx.deposit.source_hash = B256::ZERO;
})
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH.into());
.modify_cfg_chained(|cfg| cfg.spec = OpSpecId::REGOLITH);

let mut evm = ctx.build_op();
let handler = OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<_, _, _>>::new();
Expand Down
44 changes: 22 additions & 22 deletions crates/optimism/src/l1block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl L1BlockInfo {
U256::from(estimate_tx_compressed_size(input))
}

/// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [OpSpec] passed.
/// Calculate the gas cost of a transaction based on L1 block data posted on L2, depending on the [OpSpecId] passed.
pub fn calculate_tx_l1_cost(&self, input: &[u8], spec_id: OpSpecId) -> U256 {
// If the input is a deposit transaction or empty, the default value is zero.
if input.is_empty() || input.first() == Some(&0x7F) {
Expand Down Expand Up @@ -321,17 +321,17 @@ mod tests {
// gas cost = 3 non-zero bytes * NON_ZERO_BYTE_COST + NON_ZERO_BYTE_COST * 68
// gas cost = 3 * 16 + 68 * 16 = 1136
let input = bytes!("FACADE");
let bedrock_data_gas = l1_block_info.data_gas(&input, OpSpecId::BEDROCK.into());
let bedrock_data_gas = l1_block_info.data_gas(&input, OpSpecId::BEDROCK);
assert_eq!(bedrock_data_gas, U256::from(1136));

// Regolith has no added 68 non zero bytes
// gas cost = 3 * 16 = 48
let regolith_data_gas = l1_block_info.data_gas(&input, OpSpecId::REGOLITH.into());
let regolith_data_gas = l1_block_info.data_gas(&input, OpSpecId::REGOLITH);
assert_eq!(regolith_data_gas, U256::from(48));

// Fjord has a minimum compressed size of 100 bytes
// gas cost = 100 * 16 = 1600
let fjord_data_gas = l1_block_info.data_gas(&input, OpSpecId::FJORD.into());
let fjord_data_gas = l1_block_info.data_gas(&input, OpSpecId::FJORD);
assert_eq!(fjord_data_gas, U256::from(1600));
}

Expand All @@ -351,17 +351,17 @@ mod tests {
// gas cost = 3 non-zero * NON_ZERO_BYTE_COST + 2 * ZERO_BYTE_COST + NON_ZERO_BYTE_COST * 68
// gas cost = 3 * 16 + 2 * 4 + 68 * 16 = 1144
let input = bytes!("FA00CA00DE");
let bedrock_data_gas = l1_block_info.data_gas(&input, OpSpecId::BEDROCK.into());
let bedrock_data_gas = l1_block_info.data_gas(&input, OpSpecId::BEDROCK);
assert_eq!(bedrock_data_gas, U256::from(1144));

// Regolith has no added 68 non zero bytes
// gas cost = 3 * 16 + 2 * 4 = 56
let regolith_data_gas = l1_block_info.data_gas(&input, OpSpecId::REGOLITH.into());
let regolith_data_gas = l1_block_info.data_gas(&input, OpSpecId::REGOLITH);
assert_eq!(regolith_data_gas, U256::from(56));

// Fjord has a minimum compressed size of 100 bytes
// gas cost = 100 * 16 = 1600
let fjord_data_gas = l1_block_info.data_gas(&input, OpSpecId::FJORD.into());
let fjord_data_gas = l1_block_info.data_gas(&input, OpSpecId::FJORD);
assert_eq!(fjord_data_gas, U256::from(1600));
}

Expand All @@ -375,17 +375,17 @@ mod tests {
};

let input = bytes!("FACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH);
assert_eq!(gas_cost, U256::from(1048));

// Zero rollup data gas cost should result in zero
let input = bytes!("");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH);
assert_eq!(gas_cost, U256::ZERO);

// Deposit transactions with the EIP-2718 type of 0x7F should result in zero
let input = bytes!("7FFACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::REGOLITH);
assert_eq!(gas_cost, U256::ZERO);
}

Expand All @@ -404,23 +404,23 @@ mod tests {
// = (16 * 3) * (1000 * 16 * 1000 + 1000 * 1000) / (16 * 1e6)
// = 51
let input = bytes!("FACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE);
assert_eq!(gas_cost, U256::from(51));

// Zero rollup data gas cost should result in zero
let input = bytes!("");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE);
assert_eq!(gas_cost, U256::ZERO);

// Deposit transactions with the EIP-2718 type of 0x7F should result in zero
let input = bytes!("7FFACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE);
assert_eq!(gas_cost, U256::ZERO);

// If the scalars are empty, the bedrock cost function should be used.
l1_block_info.empty_ecotone_scalars = true;
let input = bytes!("FACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::ECOTONE);
assert_eq!(gas_cost, U256::from(1048));
}

Expand Down Expand Up @@ -459,11 +459,11 @@ mod tests {

// test

let gas_used = l1_block_info.data_gas(TX, OpSpecId::ECOTONE.into());
let gas_used = l1_block_info.data_gas(TX, OpSpecId::ECOTONE);

assert_eq!(gas_used, expected_l1_gas_used);

let l1_fee = l1_block_info.calculate_tx_l1_cost_ecotone(TX, OpSpecId::ECOTONE.into());
let l1_fee = l1_block_info.calculate_tx_l1_cost_ecotone(TX, OpSpecId::ECOTONE);

assert_eq!(l1_fee, expected_l1_fee)
}
Expand All @@ -489,7 +489,7 @@ mod tests {
// l1Cost = estimatedSize * l1FeeScaled / 1e12
// = 100e6 * 17 / 1e6
// = 1700
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD);
assert_eq!(gas_cost, U256::from(1700));

// fastLzSize = 202
Expand All @@ -500,17 +500,17 @@ mod tests {
// l1Cost = estimatedSize * l1FeeScaled / 1e12
// = 126387400 * 17 / 1e6
// = 2148
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD);
assert_eq!(gas_cost, U256::from(2148));

// Zero rollup data gas cost should result in zero
let input = bytes!("");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD);
assert_eq!(gas_cost, U256::ZERO);

// Deposit transactions with the EIP-2718 type of 0x7F should result in zero
let input = bytes!("7FFACADE");
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD.into());
let gas_cost = l1_block_info.calculate_tx_l1_cost(&input, OpSpecId::FJORD);
assert_eq!(gas_cost, U256::ZERO);
}

Expand Down Expand Up @@ -541,7 +541,7 @@ mod tests {

// test

let data_gas = l1_block_info.data_gas(TX, OpSpecId::FJORD.into());
let data_gas = l1_block_info.data_gas(TX, OpSpecId::FJORD);

assert_eq!(data_gas, expected_data_gas);

Expand All @@ -562,7 +562,7 @@ mod tests {
..Default::default()
};

let refunded = l1_block_info.operator_fee_refund(&gas, OpSpecId::ISTHMUS.into());
let refunded = l1_block_info.operator_fee_refund(&gas, OpSpecId::ISTHMUS);

assert_eq!(refunded, U256::from(100))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum OpSpecId {
}

impl OpSpecId {
/// Converts the [`OpSpec`] into a [`SpecId`].
/// Converts the [`OpSpecId`] into a [`SpecId`].
pub const fn into_eth_spec(self) -> SpecId {
match self {
Self::BEDROCK | Self::REGOLITH => SpecId::MERGE,
Expand Down

0 comments on commit a94bc40

Please sign in to comment.