-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathtest_balance.py
145 lines (125 loc) · 3.85 KB
/
test_balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
abstract: Tests [EIP-4762: Statelessness gas cost changes]
(https://eips.ethereum.org/EIPS/eip-4762)
Tests for [EIP-4762: Statelessness gas cost changes]
(https://eips.ethereum.org/EIPS/eip-4762).
"""
import pytest
from ethereum_test_forks import Verkle
from ethereum_test_tools import (
Account,
Address,
Block,
BlockchainTestFiller,
Environment,
TestAddress,
TestAddress2,
Transaction,
WitnessCheck,
)
from ethereum_test_tools.vm.opcode import Opcodes as Op
from ethereum_test_forks import Fork
from ethereum_test_types.verkle.helpers import chunkify_code
REFERENCE_SPEC_GIT_PATH = "EIPS/eip-4762.md"
REFERENCE_SPEC_VERSION = "2f8299df31bb8173618901a03a8366a3183479b0"
precompile_address = Address("0x04")
system_contract_address = Address("0xfffffffffffffffffffffffffffffffffffffffe")
example_address = Address("0xd94f5374fce5edbc8e2a8697c15331677e6ebf0c")
@pytest.mark.valid_from("Verkle")
@pytest.mark.parametrize(
"target",
[
example_address,
precompile_address,
system_contract_address,
],
)
@pytest.mark.parametrize("warm", [True, False])
def test_balance(blockchain_test: BlockchainTestFiller, fork: Fork, target, warm):
"""
Test BALANCE witness with/without WARM access.
"""
_balance(blockchain_test, fork, target, True, warm=warm)
@pytest.mark.valid_from("Verkle")
@pytest.mark.parametrize(
"target",
[
example_address,
precompile_address,
],
)
@pytest.mark.parametrize(
"gas, exp_target_basic_data",
[
(21_203 + 2099, False),
(21_203 + 2100, True),
],
)
def test_balance_insufficient_gas(
blockchain_test: BlockchainTestFiller, fork: Fork, target, gas, exp_target_basic_data
):
"""
Test BALANCE with insufficient gas.
"""
_balance(blockchain_test, fork, target, exp_target_basic_data, gas, fails=True)
def _balance(
blockchain_test: BlockchainTestFiller,
fork: Fork,
target: Address,
exp_target_basic_data: bool,
gas_limit=1_000_000,
warm=False,
fails=False,
):
env = Environment(
fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
difficulty=0x20000,
gas_limit=10000000000,
number=1,
timestamp=1000,
)
pre = {
TestAddress: Account(balance=1000000000000000000000),
TestAddress2: Account(code=Op.BALANCE(target) * (2 if warm else 1) + Op.PUSH0 + Op.SSTORE),
precompile_address: Account(balance=0xF0),
}
if target != precompile_address and target != system_contract_address:
pre[target] = Account(balance=0xF2)
tx = Transaction(
ty=0x0,
chain_id=0x01,
nonce=0,
to=TestAddress2,
gas_limit=gas_limit,
gas_price=10,
)
witness_check = WitnessCheck(fork=Verkle)
for address in [TestAddress, TestAddress2, env.fee_recipient]:
witness_check.add_account_full(address=address, account=pre.get(address))
code_chunks = chunkify_code(pre[TestAddress2].code)
for i, chunk in enumerate(code_chunks, start=0):
witness_check.add_code_chunk(address=TestAddress2, chunk_number=i, value=chunk)
target_account = (
pre[target]
if target != system_contract_address
else Account(**fork.pre_allocation_blockchain()[system_contract_address])
)
if exp_target_basic_data:
witness_check.add_account_basic_data(address=target, account=target_account)
if not fails:
witness_check.add_storage_slot(address=TestAddress2, storage_slot=0, value=None)
blocks = [
Block(
txs=[tx],
witness_check=witness_check,
)
]
post = {
TestAddress2: Account(code=pre[TestAddress2].code, storage={0: target_account.balance}),
}
blockchain_test(
genesis_environment=env,
pre=pre,
post=post,
blocks=blocks,
)