Skip to content

Commit f77d65f

Browse files
committed
Apply formatting fixes and use saturating arithmetic for state gas spent
1 parent 9d5d898 commit f77d65f

3 files changed

Lines changed: 62 additions & 41 deletions

File tree

crates/context/interface/src/result.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ impl ResultGas {
269269
/// Returns the regular gas used by the block.
270270
#[inline]
271271
pub const fn block_regular_gas_used(&self) -> u64 {
272-
let execution_gas_spent = self.total_gas_spent().saturating_sub(self.state_gas_spent());
272+
let execution_gas_spent = self
273+
.total_gas_spent()
274+
.saturating_sub(self.state_gas_spent());
273275
max(execution_gas_spent, self.floor_gas())
274276
}
275277

crates/ee-tests/src/op_revm_tests.rs

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ fn p256verify_test_tx(
115115
{
116116
const SPEC_ID: OpSpecId = OpSpecId::FJORD;
117117

118-
let InitialAndFloorGas { initial_total_gas, .. } =
119-
calculate_initial_tx_gas(SPEC_ID.into(), &[], false, 0, 0, 0);
118+
let InitialAndFloorGas {
119+
initial_total_gas, ..
120+
} = calculate_initial_tx_gas(SPEC_ID.into(), &[], false, 0, 0, 0);
120121

121122
Context::op()
122123
.with_tx(
@@ -147,8 +148,9 @@ fn test_tx_call_p256verify() {
147148
#[test]
148149
fn test_halted_tx_call_p256verify() {
149150
const SPEC_ID: OpSpecId = OpSpecId::FJORD;
150-
let InitialAndFloorGas { initial_total_gas, .. } =
151-
calculate_initial_tx_gas(SPEC_ID.into(), &[], false, 0, 0, 0);
151+
let InitialAndFloorGas {
152+
initial_total_gas, ..
153+
} = calculate_initial_tx_gas(SPEC_ID.into(), &[], false, 0, 0, 0);
152154
let original_gas_limit = initial_total_gas + secp256r1::P256VERIFY_BASE_GAS_FEE;
153155

154156
let ctx = Context::op()
@@ -183,8 +185,9 @@ fn bn254_pair_test_tx(
183185
) -> Context<BlockEnv, OpTransaction<TxEnv>, CfgEnv<OpSpecId>, EmptyDB, Journal<EmptyDB>, L1BlockInfo>
184186
{
185187
let input = Bytes::from([1; GRANITE_MAX_INPUT_SIZE + 2]);
186-
let InitialAndFloorGas { initial_total_gas, .. } =
187-
calculate_initial_tx_gas(spec.into(), &input[..], false, 0, 0, 0);
188+
let InitialAndFloorGas {
189+
initial_total_gas, ..
190+
} = calculate_initial_tx_gas(spec.into(), &input[..], false, 0, 0, 0);
188191

189192
Context::op()
190193
.with_tx(
@@ -317,8 +320,9 @@ fn g1_msm_test_tx(
317320
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
318321

319322
let input = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH]);
320-
let InitialAndFloorGas { initial_total_gas, .. } =
321-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
323+
let InitialAndFloorGas {
324+
initial_total_gas, ..
325+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
322326
let gs1_msm_gas = bls12_381_utils::msm_required_gas(
323327
1,
324328
&bls12_381_const::DISCOUNT_TABLE_G1_MSM,
@@ -347,8 +351,9 @@ fn g1_msm_test_tx(
347351
fn test_halted_tx_call_bls12_381_g1_msm_input_wrong_size() {
348352
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
349353
let input = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH]);
350-
let InitialAndFloorGas { initial_total_gas, .. } =
351-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
354+
let InitialAndFloorGas {
355+
initial_total_gas, ..
356+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
352357
let gs1_msm_gas = bls12_381_utils::msm_required_gas(
353358
1,
354359
&bls12_381_const::DISCOUNT_TABLE_G1_MSM,
@@ -394,8 +399,9 @@ fn test_halted_tx_call_bls12_381_g1_msm_input_wrong_size() {
394399
fn test_halted_tx_call_bls12_381_g1_msm_out_of_gas() {
395400
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
396401
let input = Bytes::from([1; bls12_381_const::G1_MSM_INPUT_LENGTH]);
397-
let InitialAndFloorGas { initial_total_gas, .. } =
398-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
402+
let InitialAndFloorGas {
403+
initial_total_gas, ..
404+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
399405
let gs1_msm_gas = bls12_381_utils::msm_required_gas(
400406
1,
401407
&bls12_381_const::DISCOUNT_TABLE_G1_MSM,
@@ -539,8 +545,9 @@ fn g2_msm_test_tx(
539545
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
540546

541547
let input = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH]);
542-
let InitialAndFloorGas { initial_total_gas, .. } =
543-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
548+
let InitialAndFloorGas {
549+
initial_total_gas, ..
550+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
544551
let gs2_msm_gas = bls12_381_utils::msm_required_gas(
545552
1,
546553
&bls12_381_const::DISCOUNT_TABLE_G2_MSM,
@@ -569,8 +576,9 @@ fn g2_msm_test_tx(
569576
fn test_halted_tx_call_bls12_381_g2_msm_input_wrong_size() {
570577
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
571578
let input = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH]);
572-
let InitialAndFloorGas { initial_total_gas, .. } =
573-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
579+
let InitialAndFloorGas {
580+
initial_total_gas, ..
581+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
574582
let gs2_msm_gas = bls12_381_utils::msm_required_gas(
575583
1,
576584
&bls12_381_const::DISCOUNT_TABLE_G2_MSM,
@@ -616,8 +624,9 @@ fn test_halted_tx_call_bls12_381_g2_msm_input_wrong_size() {
616624
fn test_halted_tx_call_bls12_381_g2_msm_out_of_gas() {
617625
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
618626
let input = Bytes::from([1; bls12_381_const::G2_MSM_INPUT_LENGTH]);
619-
let InitialAndFloorGas { initial_total_gas, .. } =
620-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
627+
let InitialAndFloorGas {
628+
initial_total_gas, ..
629+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
621630
let gs2_msm_gas = bls12_381_utils::msm_required_gas(
622631
1,
623632
&bls12_381_const::DISCOUNT_TABLE_G2_MSM,
@@ -687,8 +696,9 @@ fn bl12_381_pairing_test_tx(
687696
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
688697

689698
let input = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
690-
let InitialAndFloorGas { initial_total_gas, .. } =
691-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
699+
let InitialAndFloorGas {
700+
initial_total_gas, ..
701+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
692702

693703
let pairing_gas: u64 =
694704
bls12_381_const::PAIRING_MULTIPLIER_BASE + bls12_381_const::PAIRING_OFFSET_BASE;
@@ -715,8 +725,9 @@ fn bl12_381_pairing_test_tx(
715725
fn test_halted_tx_call_bls12_381_pairing_input_wrong_size() {
716726
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
717727
let input = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
718-
let InitialAndFloorGas { initial_total_gas, .. } =
719-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
728+
let InitialAndFloorGas {
729+
initial_total_gas, ..
730+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
720731
let pairing_gas: u64 =
721732
bls12_381_const::PAIRING_MULTIPLIER_BASE + bls12_381_const::PAIRING_OFFSET_BASE;
722733

@@ -759,8 +770,9 @@ fn test_halted_tx_call_bls12_381_pairing_input_wrong_size() {
759770
fn test_halted_tx_call_bls12_381_pairing_out_of_gas() {
760771
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
761772
let input = Bytes::from([1; bls12_381_const::PAIRING_INPUT_LENGTH]);
762-
let InitialAndFloorGas { initial_total_gas, .. } =
763-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
773+
let InitialAndFloorGas {
774+
initial_total_gas, ..
775+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
764776
let pairing_gas: u64 =
765777
bls12_381_const::PAIRING_MULTIPLIER_BASE + bls12_381_const::PAIRING_OFFSET_BASE;
766778

@@ -825,8 +837,9 @@ fn test_tx_call_bls12_381_pairing_wrong_input_layout() {
825837
fn test_halted_tx_call_bls12_381_map_fp_to_g1_out_of_gas() {
826838
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
827839
let input = Bytes::from([1; bls12_381_const::PADDED_FP_LENGTH]);
828-
let InitialAndFloorGas { initial_total_gas, .. } =
829-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
840+
let InitialAndFloorGas {
841+
initial_total_gas, ..
842+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
830843

831844
let ctx = Context::op()
832845
.with_tx(
@@ -835,7 +848,9 @@ fn test_halted_tx_call_bls12_381_map_fp_to_g1_out_of_gas() {
835848
TxEnv::builder()
836849
.kind(TxKind::Call(bls12_381_const::MAP_FP_TO_G1_ADDRESS))
837850
.data(input)
838-
.gas_limit(initial_total_gas + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE - 1),
851+
.gas_limit(
852+
initial_total_gas + bls12_381_const::MAP_FP_TO_G1_BASE_GAS_FEE - 1,
853+
),
839854
)
840855
.build_fill(),
841856
)
@@ -867,8 +882,9 @@ fn test_halted_tx_call_bls12_381_map_fp_to_g1_out_of_gas() {
867882
fn test_halted_tx_call_bls12_381_map_fp_to_g1_input_wrong_size() {
868883
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
869884
let input = Bytes::from([1; bls12_381_const::PADDED_FP_LENGTH]);
870-
let InitialAndFloorGas { initial_total_gas, .. } =
871-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
885+
let InitialAndFloorGas {
886+
initial_total_gas, ..
887+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
872888

873889
let ctx = Context::op()
874890
.with_tx(
@@ -909,8 +925,9 @@ fn test_halted_tx_call_bls12_381_map_fp_to_g1_input_wrong_size() {
909925
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_out_of_gas() {
910926
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
911927
let input = Bytes::from([1; bls12_381_const::PADDED_FP2_LENGTH]);
912-
let InitialAndFloorGas { initial_total_gas, .. } =
913-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
928+
let InitialAndFloorGas {
929+
initial_total_gas, ..
930+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
914931

915932
let ctx = Context::op()
916933
.with_tx(
@@ -919,7 +936,9 @@ fn test_halted_tx_call_bls12_381_map_fp2_to_g2_out_of_gas() {
919936
TxEnv::builder()
920937
.kind(TxKind::Call(bls12_381_const::MAP_FP2_TO_G2_ADDRESS))
921938
.data(input)
922-
.gas_limit(initial_total_gas + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE - 1),
939+
.gas_limit(
940+
initial_total_gas + bls12_381_const::MAP_FP2_TO_G2_BASE_GAS_FEE - 1,
941+
),
923942
)
924943
.build_fill(),
925944
)
@@ -989,8 +1008,9 @@ fn test_l1block_load_for_pre_regolith() {
9891008
fn test_halted_tx_call_bls12_381_map_fp2_to_g2_input_wrong_size() {
9901009
const SPEC_ID: OpSpecId = OpSpecId::ISTHMUS;
9911010
let input = Bytes::from([1; bls12_381_const::PADDED_FP2_LENGTH]);
992-
let InitialAndFloorGas { initial_total_gas, .. } =
993-
calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
1011+
let InitialAndFloorGas {
1012+
initial_total_gas, ..
1013+
} = calculate_initial_tx_gas(SPEC_ID.into(), &input[..], false, 0, 0, 0);
9941014

9951015
let ctx = Context::op()
9961016
.with_tx(

crates/handler/src/post_execution.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ pub fn build_result_gas(gas: &Gas, init_and_floor_gas: InitialAndFloorGas) -> Re
1313
// EIP-8037: tx_gas_used = tx.gas - gas_left - state_gas_left
1414
// total_gas_spent = limit - remaining = tx.gas - gas_left
1515
// Subtract reservoir to get the actual gas used (excluding unused state gas).
16-
let res = ResultGas::default()
16+
ResultGas::default()
1717
.with_total_gas_spent(gas.total_gas_spent())
1818
.with_refunded(gas.refunded() as u64)
1919
.with_floor_gas(init_and_floor_gas.floor_gas)
2020
.with_state_gas_spent(
21-
gas.state_gas_spent() + init_and_floor_gas.initial_state_gas
22-
- init_and_floor_gas.eip7702_reservoir_refund,
23-
);
24-
25-
res
21+
gas.state_gas_spent()
22+
.saturating_add(init_and_floor_gas.initial_state_gas)
23+
.saturating_sub(init_and_floor_gas.eip7702_reservoir_refund),
24+
)
2625
}
2726

2827
/// Ensures minimum gas floor is spent according to EIP-7623.

0 commit comments

Comments
 (0)