Skip to content

Commit

Permalink
split basic tload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Apr 9, 2024
1 parent 5d372cc commit 9568184
Showing 1 changed file with 249 additions and 45 deletions.
294 changes: 249 additions & 45 deletions tests/cancun/eip1153_tstore/test_basic_tload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,203 @@
)
from ethereum_test_tools.vm.opcode import Opcodes as Op

REFERENCE_SPEC_GIT_PATH = "EIPS/eip-1153.md"
REFERENCE_SPEC_VERSION = "2f8299df31bb8173618901a03a8366a3183479b0"
from .spec import Spec, ref_spec_1153

REFERENCE_SPEC_GIT_PATH = ref_spec_1153.git_path
REFERENCE_SPEC_VERSION = ref_spec_1153.version


@pytest.mark.valid_from("Cancun")
def test_basic_tload(
def test_basic_tload_transaction_begin(
state_test: StateTestFiller,
):
"""
Covered .json vectors:
Ported .json vectors:
(01_tloadBeginningTxnFiller.yml)
load arbitrary value is 0 at beginning of transaction
"""
slot_tload_at_transaction_begin_result = 1
slot_code_worked = 2

address_to = Address("A00000000000000000000000000000000000000A")
pre = {
address_to: Account(
balance=1000000000000000000,
nonce=0,
code=Op.JUMPDEST()
# 01 test
+ Op.SSTORE(slot_tload_at_transaction_begin_result, Op.TLOAD(0))
+ Op.SSTORE(slot_code_worked, 1),
storage={
slot_tload_at_transaction_begin_result: 0xFF,
},
),
TestAddress: Account(
balance=7000000000000000000,
nonce=0,
code="0x",
storage={},
),
}

post: Dict[Address, Union[Account, object]] = {}

post[address_to] = Account(
storage={
slot_tload_at_transaction_begin_result: 0x00,
slot_code_worked: 0x01,
}
)

tx = Transaction(
nonce=0,
to=address_to,
gas_price=10,
data=b"",
gas_limit=5000000,
value=0,
)

state_test(env=Environment(), pre=pre, post=post, tx=tx)


@pytest.mark.valid_from("Cancun")
def test_basic_tload_works(
state_test: StateTestFiller,
):
"""
Ported .json vectors:
(02_tloadAfterTstoreFiller.yml)
tload from same slot after tstore returns correct value
"""
tstore_value = 88

slot_tload_after_tstore_result = 0
slot_tload_after_tstore_result_second_time = 1
slot_code_worked = 2

address_to = Address("A00000000000000000000000000000000000000A")
pre = {
address_to: Account(
balance=1000000000000000000,
nonce=0,
code=Op.JUMPDEST()
# 02 test
+ Op.TSTORE(2, tstore_value)
+ Op.SSTORE(slot_tload_after_tstore_result, Op.TLOAD(2))
+ Op.SSTORE(slot_tload_after_tstore_result_second_time, Op.TLOAD(2))
+ Op.SSTORE(slot_code_worked, 1),
storage={
slot_tload_after_tstore_result: 0xFF,
slot_tload_after_tstore_result_second_time: 0xFF,
},
),
TestAddress: Account(
balance=7000000000000000000,
nonce=0,
code="0x",
storage={},
),
}

post: Dict[Address, Union[Account, object]] = {}

post[address_to] = Account(
storage={
slot_tload_after_tstore_result: tstore_value,
slot_tload_after_tstore_result_second_time: tstore_value,
slot_code_worked: 0x01,
}
)

tx = Transaction(
nonce=0,
to=address_to,
gas_price=10,
data=b"",
gas_limit=5000000,
value=0,
)

state_test(env=Environment(), pre=pre, post=post, tx=tx)


@pytest.mark.valid_from("Cancun")
def test_basic_tload_other_after_tstore(
state_test: StateTestFiller,
):
"""
Ported .json vectors:
(03_tloadAfterStoreIs0Filler.yml)
Loading any other slot after storing to a slot returns 0.
"""
tstore_value = 88

slot_tload_untouched_slot_after_tstore_result = 1
slot_code_worked = 2

address_to = Address("A00000000000000000000000000000000000000A")
pre = {
address_to: Account(
balance=1000000000000000000,
nonce=0,
code=Op.JUMPDEST()
# 03 test
+ Op.TSTORE(3, tstore_value)
+ Op.SSTORE(slot_tload_untouched_slot_after_tstore_result, Op.TLOAD(0))
+ Op.SSTORE(slot_code_worked, 1),
storage={
slot_tload_untouched_slot_after_tstore_result: 0xFF,
},
),
TestAddress: Account(
balance=7000000000000000000,
nonce=0,
code="0x",
storage={},
),
}

post: Dict[Address, Union[Account, object]] = {}

post[address_to] = Account(
storage={
slot_tload_untouched_slot_after_tstore_result: 0x00,
slot_code_worked: 0x01,
}
)

tx = Transaction(
nonce=0,
to=address_to,
gas_price=10,
data=b"",
gas_limit=5000000,
value=0,
)

state_test(env=Environment(), pre=pre, post=post, tx=tx)


@pytest.mark.valid_from("Cancun")
def test_basic_tload_gasprice(
state_test: StateTestFiller,
):
"""
Ported .json vectors:
(16_tloadGasFiller.yml)
tload costs 100 gas same as a warm sload
(18_tloadAfterStoreFiller.yml)
tload from same slot after store returns 0
"""
address_to = Address("A00000000000000000000000000000000000000A")
tload_at_transaction_begin_result = 1

tstore_value = 88
tload_after_tstore_result = 2
tload_after_tstore_result_second_time = 3
tload_wrong_after_tstore_result = 4
slot_tload_nonzero_gas_price_result = 1
slot_tload_zero_gas_price_result = 2
slot_code_worked = 3

address_to = Address("A00000000000000000000000000000000000000A")

"""
N OPNAME GAS_COST TOTAL_GAS REMAINING_GAS STACK
Expand All @@ -64,48 +228,93 @@ def test_basic_tload(
"""
extra_opcode_gas = 11 # mstore(3), push1(3),gas(2),push1(3)

tload_nonzero_gas_price_result = 16
tload_zero_gas_price_result = 1601

tload_from_sstore_result = 18

pre = {
address_to: Account(
balance=1000000000000000000,
nonce=0,
code=Op.JUMPDEST()
# 01 test
+ Op.SSTORE(tload_at_transaction_begin_result, Op.TLOAD(0))
# 02 test
+ Op.TSTORE(2, tstore_value)
+ Op.SSTORE(tload_after_tstore_result, Op.TLOAD(2))
+ Op.SSTORE(tload_after_tstore_result_second_time, Op.TLOAD(2))
# 03 test
+ Op.TSTORE(3, tstore_value) + Op.SSTORE(tload_wrong_after_tstore_result, Op.TLOAD(0))
# 16 test
+ Op.TSTORE(16, 2)
+ Op.MSTORE(0, Op.GAS()) # hot load the memory to make the extra_opcode_gas be 11
+ Op.MSTORE(0, Op.GAS())
+ Op.TLOAD(16)
+ Op.MSTORE(32, Op.GAS())
+ Op.SSTORE(tload_nonzero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
+ Op.SSTORE(tload_nonzero_gas_price_result, Op.SUB(Op.SLOAD(16), extra_opcode_gas))
+ Op.SSTORE(slot_tload_nonzero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
+ Op.SSTORE(
slot_tload_nonzero_gas_price_result,
Op.SUB(Op.SLOAD(slot_tload_nonzero_gas_price_result), extra_opcode_gas),
)
# from zero slot
+ Op.MSTORE(0, Op.GAS())
+ Op.TLOAD(5)
+ Op.MSTORE(32, Op.GAS())
+ Op.SSTORE(tload_zero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
+ Op.SSTORE(tload_zero_gas_price_result, Op.SUB(Op.SLOAD(1601), extra_opcode_gas))
+ Op.SSTORE(slot_tload_zero_gas_price_result, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
+ Op.SSTORE(
slot_tload_zero_gas_price_result,
Op.SUB(Op.SLOAD(slot_tload_zero_gas_price_result), extra_opcode_gas),
)
+ Op.SSTORE(slot_code_worked, 1),
storage={
slot_tload_nonzero_gas_price_result: 0xFF,
slot_tload_zero_gas_price_result: 0xFF,
},
),
TestAddress: Account(
balance=7000000000000000000,
nonce=0,
code="0x",
storage={},
),
}

post: Dict[Address, Union[Account, object]] = {}

post[address_to] = Account(
storage={
slot_tload_nonzero_gas_price_result: Spec.TLOAD_GAS_COST,
slot_tload_zero_gas_price_result: Spec.TLOAD_GAS_COST,
slot_code_worked: 0x01,
}
)

tx = Transaction(
nonce=0,
to=address_to,
gas_price=10,
data=b"",
gas_limit=5000000,
value=0,
)

state_test(env=Environment(), pre=pre, post=post, tx=tx)


@pytest.mark.valid_from("Cancun")
def test_basic_tload_after_store(
state_test: StateTestFiller,
):
"""
Ported .json vectors:
(18_tloadAfterStoreFiller.yml)
tload from same slot after store returns 0
"""
address_to = Address("A00000000000000000000000000000000000000A")

slot_tload_from_sstore_result = 1
slot_code_worked = 2

pre = {
address_to: Account(
balance=1000000000000000000,
nonce=0,
code=Op.JUMPDEST()
# 18 test
+ Op.SSTORE(18, 22) + Op.SSTORE(tload_from_sstore_result, Op.TLOAD(18)),
+ Op.SSTORE(slot_tload_from_sstore_result, 22)
+ Op.SSTORE(slot_tload_from_sstore_result, Op.TLOAD(slot_tload_from_sstore_result))
+ Op.SSTORE(slot_code_worked, 1),
storage={
tload_at_transaction_begin_result: 0xFF,
tload_after_tstore_result: 0xFF,
tload_after_tstore_result_second_time: 0xFF,
tload_wrong_after_tstore_result: 0xFF,
tload_nonzero_gas_price_result: 0xFF,
tload_zero_gas_price_result: 0xFF,
tload_from_sstore_result: 0xFF,
slot_tload_from_sstore_result: 0xFF,
},
),
TestAddress: Account(
Expand All @@ -120,13 +329,8 @@ def test_basic_tload(

post[address_to] = Account(
storage={
tload_at_transaction_begin_result: 0x00,
tload_after_tstore_result: tstore_value,
tload_after_tstore_result_second_time: tstore_value,
tload_wrong_after_tstore_result: 0x00,
tload_nonzero_gas_price_result: 100,
tload_zero_gas_price_result: 100,
tload_from_sstore_result: 0x00,
slot_tload_from_sstore_result: 0x00,
slot_code_worked: 0x01,
}
)

Expand Down

0 comments on commit 9568184

Please sign in to comment.