-
Notifications
You must be signed in to change notification settings - Fork 645
/
context.py
173 lines (116 loc) · 4.69 KB
/
context.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from evm import constants
from evm.exceptions import (
OutOfBoundsRead,
)
from evm.utils.address import (
force_bytes_to_address,
)
from evm.utils.numeric import (
ceil32,
)
def balance(computation):
addr = force_bytes_to_address(computation.stack_pop(type_hint=constants.BYTES))
balance = computation.state.account_db.get_balance(addr)
computation.stack_push(balance)
def origin(computation):
computation.stack_push(computation.transaction_context.origin)
def address(computation):
computation.stack_push(computation.msg.storage_address)
def caller(computation):
computation.stack_push(computation.msg.sender)
def callvalue(computation):
computation.stack_push(computation.msg.value)
def calldataload(computation):
"""
Load call data into memory.
"""
start_position = computation.stack_pop(type_hint=constants.UINT256)
value = computation.msg.data[start_position:start_position + 32]
padded_value = value.ljust(32, b'\x00')
normalized_value = padded_value.lstrip(b'\x00')
computation.stack_push(normalized_value)
def calldatasize(computation):
size = len(computation.msg.data)
computation.stack_push(size)
def calldatacopy(computation):
(
mem_start_position,
calldata_start_position,
size,
) = computation.stack_pop(num_items=3, type_hint=constants.UINT256)
computation.extend_memory(mem_start_position, size)
word_count = ceil32(size) // 32
copy_gas_cost = word_count * constants.GAS_COPY
computation.consume_gas(copy_gas_cost, reason="CALLDATACOPY fee")
value = computation.msg.data[calldata_start_position: calldata_start_position + size]
padded_value = value.ljust(size, b'\x00')
computation.memory_write(mem_start_position, size, padded_value)
def codesize(computation):
size = len(computation.code)
computation.stack_push(size)
def codecopy(computation):
(
mem_start_position,
code_start_position,
size,
) = computation.stack_pop(num_items=3, type_hint=constants.UINT256)
computation.extend_memory(mem_start_position, size)
word_count = ceil32(size) // 32
copy_gas_cost = constants.GAS_COPY * word_count
computation.consume_gas(
copy_gas_cost,
reason="CODECOPY: word gas cost",
)
with computation.code.seek(code_start_position):
code_bytes = computation.code.read(size)
padded_code_bytes = code_bytes.ljust(size, b'\x00')
computation.memory_write(mem_start_position, size, padded_code_bytes)
def gasprice(computation):
computation.stack_push(computation.transaction_context.gas_price)
def extcodesize(computation):
account = force_bytes_to_address(computation.stack_pop(type_hint=constants.BYTES))
code_size = len(computation.state.account_db.get_code(account))
computation.stack_push(code_size)
def extcodecopy(computation):
account = force_bytes_to_address(computation.stack_pop(type_hint=constants.BYTES))
(
mem_start_position,
code_start_position,
size,
) = computation.stack_pop(num_items=3, type_hint=constants.UINT256)
computation.extend_memory(mem_start_position, size)
word_count = ceil32(size) // 32
copy_gas_cost = constants.GAS_COPY * word_count
computation.consume_gas(
copy_gas_cost,
reason='EXTCODECOPY: word gas cost',
)
code = computation.state.account_db.get_code(account)
code_bytes = code[code_start_position:code_start_position + size]
padded_code_bytes = code_bytes.ljust(size, b'\x00')
computation.memory_write(mem_start_position, size, padded_code_bytes)
def returndatasize(computation):
size = len(computation.return_data)
computation.stack_push(size)
def returndatacopy(computation):
(
mem_start_position,
returndata_start_position,
size,
) = computation.stack_pop(num_items=3, type_hint=constants.UINT256)
if returndata_start_position + size > len(computation.return_data):
raise OutOfBoundsRead(
"Return data length is not sufficient to satisfy request. Asked "
"for data from index {0} to {1}. Return data is {2} bytes in "
"length.".format(
returndata_start_position,
returndata_start_position + size,
len(computation.return_data),
)
)
computation.extend_memory(mem_start_position, size)
word_count = ceil32(size) // 32
copy_gas_cost = word_count * constants.GAS_COPY
computation.consume_gas(copy_gas_cost, reason="RETURNDATACOPY fee")
value = computation.return_data[returndata_start_position: returndata_start_position + size]
computation.memory_write(mem_start_position, size, value)