Skip to content

Commit 38e40be

Browse files
authored
refactor(tests): leftover manual gas calculation/hard-coded gas costs (#2222)
1 parent e271298 commit 38e40be

2 files changed

Lines changed: 41 additions & 61 deletions

File tree

packages/testing/src/execution_testing/tools/tools_code/generators.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ def __new__(
188188
res += (
189189
Op.SWAP1
190190
+ Op.SUB
191-
+ Op.PUSH1(overhead_cost + 2)
191+
+ Op.PUSH1[overhead_cost]
192+
+ Op.GAS
193+
+ Op.GAS
194+
+ Op.SWAP1
195+
+ Op.SUB
196+
+ Op.ADD
192197
+ Op.SWAP1
193198
+ Op.SSTORE(sstore_key, Op.SUB)
194199
)

tests/shanghai/eip3860_initcode/test_initcode.py

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Transaction,
2525
TransactionException,
2626
TransactionReceipt,
27-
ceiling_division,
2827
compute_create_address,
2928
)
3029

@@ -141,11 +140,9 @@ def test_contract_creating_tx(
141140
)
142141

143142
tx = Transaction(
144-
nonce=0,
145143
to=None,
146144
data=initcode,
147-
gas_limit=10000000,
148-
gas_price=10,
145+
gas_limit=10_000_000,
149146
sender=sender,
150147
)
151148

@@ -320,12 +317,10 @@ def tx(
320317
pytest.fail("Invalid gas test case provided.")
321318

322319
return Transaction(
323-
nonce=0,
324320
to=None,
325321
access_list=tx_access_list,
326322
data=initcode,
327323
gas_limit=gas_limit,
328-
gas_price=10,
329324
error=tx_error,
330325
sender=sender,
331326
# The entire gas limit is expected to be consumed.
@@ -415,29 +410,52 @@ def create2_salt(self) -> int:
415410
return 0xDEADBEEF
416411

417412
@pytest.fixture
418-
def creator_code(self, opcode: Op, create2_salt: int) -> Bytecode:
413+
def create_code(
414+
self, opcode: Op, create2_salt: int, initcode: Initcode
415+
) -> Bytecode:
416+
"""
417+
Generate the CREATE/CREATE2 bytecode.
418+
"""
419+
return (
420+
opcode(
421+
size=Op.CALLDATASIZE,
422+
salt=create2_salt,
423+
init_code_size=len(initcode),
424+
)
425+
if opcode == Op.CREATE2
426+
else opcode(size=Op.CALLDATASIZE, init_code_size=len(initcode))
427+
)
428+
429+
@pytest.fixture
430+
def creator_code(self, fork: Fork, create_code: Bytecode) -> Bytecode:
419431
"""
420432
Generate code for the creator contract which calls CREATE/CREATE2.
421433
"""
422434
return (
423435
Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
424436
+ Op.GAS
425-
+ (
426-
opcode(size=Op.CALLDATASIZE, salt=create2_salt)
427-
if opcode == Op.CREATE2
428-
else opcode(size=Op.CALLDATASIZE)
429-
)
437+
+ create_code
430438
+ Op.GAS
431439
# stack: [Gas 2, Call Result, Gas 1]
432440
+ Op.SWAP1
433441
# stack: [Call Result, Gas 2, Gas 1]
434-
+ Op.SSTORE(0, unchecked=True)
442+
+ Op.PUSH1[0]
443+
# stack: [0, Call Result, Gas 2, Gas 1]
444+
+ Op.SSTORE
435445
# stack: [Gas 2, Gas 1]
436446
+ Op.SWAP1
437447
# stack: [Gas 1, Gas 2]
438448
+ Op.SUB
439449
# stack: [Gas 1 - Gas 2]
440-
+ Op.SSTORE(1, unchecked=True)
450+
+ Op.PUSH1[Op.GAS.gas_cost(fork)]
451+
# stack: [Op.GAS cost, Gas 1 - Gas 2]
452+
+ Op.SWAP1
453+
# stack: [Gas 1 - Gas 2, Op.GAS cost]
454+
+ Op.SUB
455+
# stack: [Gas 1 - Gas 2 - Op.GAS cost]
456+
+ Op.PUSH1[1]
457+
# stack: [1, Gas 1 - Gas 2 - Op.GAS cost]
458+
+ Op.SSTORE
441459
)
442460

443461
@pytest.fixture
@@ -491,45 +509,12 @@ def tx(
491509
) -> Transaction:
492510
"""Generate transaction that executes the caller contract."""
493511
return Transaction(
494-
nonce=0,
495512
to=caller_contract_address,
496513
data=initcode,
497-
gas_limit=10000000,
498-
gas_price=10,
514+
gas_limit=10_000_000,
499515
sender=sender,
500516
)
501517

502-
@pytest.fixture
503-
def contract_creation_gas_cost(
504-
self, fork: Fork, opcode: Op, create2_salt: int
505-
) -> int:
506-
"""Calculate gas cost of the contract creation operation."""
507-
create_code = (
508-
opcode(size=Op.CALLDATASIZE, salt=create2_salt)
509-
if opcode == Op.CREATE2
510-
else opcode(size=Op.CALLDATASIZE)
511-
)
512-
return (create_code + Op.GAS).gas_cost(fork)
513-
514-
@pytest.fixture
515-
def initcode_word_cost(self, fork: Fork, initcode: Initcode) -> int:
516-
"""Calculate gas cost charged for the initcode length."""
517-
gas_costs = fork.gas_costs()
518-
return ceiling_division(len(initcode), 32) * gas_costs.G_INITCODE_WORD
519-
520-
@pytest.fixture
521-
def create2_word_cost(
522-
self, opcode: Op, fork: Fork, initcode: Initcode
523-
) -> int:
524-
"""Calculate gas cost charged for the initcode length."""
525-
if opcode == Op.CREATE:
526-
return 0
527-
528-
gas_costs = fork.gas_costs()
529-
return (
530-
ceiling_division(len(initcode), 32) * gas_costs.G_KECCAK_256_WORD
531-
)
532-
533518
@pytest.mark.xdist_group(name="bigmem")
534519
@pytest.mark.slow()
535520
def test_create_opcode_initcode(
@@ -543,9 +528,7 @@ def test_create_opcode_initcode(
543528
caller_contract_address: Address,
544529
creator_contract_address: Address,
545530
created_contract_address: Address,
546-
contract_creation_gas_cost: int,
547-
initcode_word_cost: int,
548-
create2_word_cost: int,
531+
create_code: Bytecode,
549532
fork: Fork,
550533
) -> None:
551534
"""
@@ -574,18 +557,10 @@ def test_create_opcode_initcode(
574557
)
575558

576559
else:
577-
expected_gas_usage = contract_creation_gas_cost
560+
expected_gas_usage = create_code.gas_cost(fork)
578561
# The initcode is only executed if the length check succeeds
579562
expected_gas_usage += initcode.gas_cost(fork)
580563

581-
# CREATE2 hashing cost should only be deducted if the initcode
582-
# does not exceed the max length
583-
expected_gas_usage += create2_word_cost
584-
585-
# Initcode word cost is only deducted if the length check
586-
# succeeds
587-
expected_gas_usage += initcode_word_cost
588-
589564
# Call returns 1 as valid initcode length s[0]==1 && s[1]==1
590565
post[caller_contract_address] = Account(
591566
nonce=1,

0 commit comments

Comments
 (0)