forked from ethereum/py-evm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
483 lines (420 loc) · 15.1 KB
/
base.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
from __future__ import absolute_import
from abc import (
ABCMeta,
abstractmethod
)
import contextlib
import functools
import logging
from typing import List # noqa: F401
import rlp
from cytoolz import merge
from eth_utils import (
keccak,
to_tuple,
)
from evm.constants import (
GENESIS_PARENT_HASH,
MAX_PREV_HEADER_DEPTH,
MAX_UNCLES,
)
from evm.db.trie import make_trie_root_and_nodes
from evm.exceptions import (
BlockNotFound,
ValidationError,
)
from evm.rlp.headers import (
BlockHeader,
)
from evm.rlp.receipts import Receipt # noqa: F401
from evm.utils.datatypes import (
Configurable,
)
from evm.utils.db import (
get_parent_header,
get_block_header_by_hash,
)
from evm.utils.headers import (
generate_header_from_parent_header,
)
from evm.validation import (
validate_length_lte,
validate_gas_limit,
)
from evm.vm.message import (
Message,
)
class BaseVM(Configurable, metaclass=ABCMeta):
"""
The VM class represents the Chain rules for a specific protocol definition
such as the Frontier or Homestead network. Define a Chain which specifies
the individual VM classes for each fork of the protocol rules within that
network.
"""
fork = None
chaindb = None
_state_class = None
def __init__(self, header, chaindb):
self.chaindb = chaindb
block_class = self.get_block_class()
self.block = block_class.from_header(header=header, chaindb=self.chaindb)
self.receipts = [] # type: List[Receipt]
#
# Logging
#
@property
def logger(self):
return logging.getLogger('evm.vm.base.VM.{0}'.format(self.__class__.__name__))
#
# Execution
#
def apply_transaction(self, transaction):
"""
Apply the transaction to the vm in the current block.
"""
computation, block, receipt = self.state.apply_transaction(
transaction,
self.block,
)
self.block = block
self.receipts.append(receipt)
self.clear_journal()
return computation, self.block
def execute_bytecode(self,
origin,
gas_price,
gas,
to,
sender,
value,
data,
code,
code_address=None,
):
if origin is None:
origin = sender
# Construct a message
message = Message(
gas=gas,
to=to,
sender=sender,
value=value,
data=data,
code=code,
code_address=code_address,
)
# Construction a tx context
transaction_context = self.state.get_transaction_context_class()(
gas_price=gas_price,
origin=origin,
)
# Execute it in the VM
return self.state.get_computation(message, transaction_context).apply_computation(
self.state,
message,
transaction_context,
)
#
# Mining
#
def import_block(self, block):
self.receipts = []
self.configure_header(
coinbase=block.header.coinbase,
gas_limit=block.header.gas_limit,
timestamp=block.header.timestamp,
extra_data=block.header.extra_data,
mix_hash=block.header.mix_hash,
nonce=block.header.nonce,
uncles_hash=keccak(rlp.encode(block.uncles)),
)
# run all of the transactions.
for transaction in block.transactions:
self.apply_transaction(transaction)
tx_root_hash, tx_kv_nodes = make_trie_root_and_nodes(self.block.transactions)
receipt_root_hash, receipt_kv_nodes = make_trie_root_and_nodes(self.receipts)
trie_data = merge(tx_kv_nodes, receipt_kv_nodes)
self.chaindb.persist_trie_data_dict(trie_data)
self.block.header.transaction_root = tx_root_hash
self.block.header.receipt_root = receipt_root_hash
# transfer the list of uncles.
self.block.uncles = block.uncles
return self.mine_block()
def mine_block(self, *args, **kwargs):
"""
Mine the current block. Proxies to self.pack_block method.
"""
block = self.block
self.pack_block(block, *args, **kwargs)
if block.number == 0:
return block
block = self.state.finalize_block(block)
return block
def pack_block(self, block, *args, **kwargs):
"""
Pack block for mining.
:param bytes coinbase: 20-byte public address to receive block reward
:param bytes uncles_hash: 32 bytes
:param bytes state_root: 32 bytes
:param bytes transaction_root: 32 bytes
:param bytes receipt_root: 32 bytes
:param int bloom:
:param int gas_used:
:param bytes extra_data: 32 bytes
:param bytes mix_hash: 32 bytes
:param bytes nonce: 8 bytes
"""
if 'uncles' in kwargs:
block.uncles = kwargs.pop('uncles')
kwargs.setdefault('uncles_hash', keccak(rlp.encode(block.uncles)))
header = block.header
provided_fields = set(kwargs.keys())
known_fields = set(tuple(zip(*BlockHeader.fields))[0])
unknown_fields = provided_fields.difference(known_fields)
if unknown_fields:
raise AttributeError(
"Unable to set the field(s) {0} on the `BlockHeader` class. "
"Received the following unexpected fields: {1}.".format(
", ".join(known_fields),
", ".join(unknown_fields),
)
)
for key, value in kwargs.items():
setattr(header, key, value)
# Perform validation
self.validate_block(block)
return block
@contextlib.contextmanager
def state_in_temp_block(self):
header = self.block.header
temp_block = self.generate_block_from_parent_header_and_coinbase(header, header.coinbase)
prev_hashes = (header.hash, ) + self.previous_hashes
gas_used = 0
if self.receipts:
gas_used = self.receipts[-1].gas_used
state = self.get_state(self.chaindb, temp_block, prev_hashes, gas_used)
assert state.gas_used == 0, "There must not be any gas used in a fresh temporary block"
snapshot = state.snapshot()
yield state
state.revert(snapshot)
@classmethod
def generate_block_from_parent_header_and_coinbase(cls, parent_header, coinbase):
"""
Generate block from parent header and coinbase.
"""
block_header = generate_header_from_parent_header(
cls.compute_difficulty,
parent_header,
coinbase,
timestamp=parent_header.timestamp + 1,
)
block = cls.get_block_class()(
block_header,
transactions=[],
uncles=[],
)
return block
#
# Validate
#
def validate_block(self, block):
if not block.is_genesis:
parent_header = get_parent_header(block.header, self.chaindb)
validate_gas_limit(block.header.gas_limit, parent_header.gas_limit)
validate_length_lte(block.header.extra_data, 32, title="BlockHeader.extra_data")
# timestamp
if block.header.timestamp < parent_header.timestamp:
raise ValidationError(
"`timestamp` is before the parent block's timestamp.\n"
"- block : {0}\n"
"- parent : {1}. ".format(
block.header.timestamp,
parent_header.timestamp,
)
)
elif block.header.timestamp == parent_header.timestamp:
raise ValidationError(
"`timestamp` is equal to the parent block's timestamp\n"
"- block : {0}\n"
"- parent: {1}. ".format(
block.header.timestamp,
parent_header.timestamp,
)
)
# FIXME: make_trie_root_and_nodes() is rather expensive, and we already run that once in
# import_block(), so should refactor some of this code to avoid re-generating it here.
tx_root_hash, _ = make_trie_root_and_nodes(block.transactions)
if tx_root_hash != block.header.transaction_root:
raise ValidationError(
"Block's transaction_root ({0}) does not match expected value: {1}".format(
block.header.transaction_root, tx_root_hash))
if len(block.uncles) > MAX_UNCLES:
raise ValidationError(
"Blocks may have a maximum of {0} uncles. Found "
"{1}.".format(MAX_UNCLES, len(block.uncles))
)
for uncle in block.uncles:
self.validate_uncle(block, uncle)
if not self.state.is_key_exists(block.header.state_root):
raise ValidationError(
"`state_root` was not found in the db.\n"
"- state_root: {0}".format(
block.header.state_root,
)
)
local_uncle_hash = keccak(rlp.encode(block.uncles))
if local_uncle_hash != block.header.uncles_hash:
raise ValidationError(
"`uncles_hash` and block `uncles` do not match.\n"
" - num_uncles : {0}\n"
" - block uncle_hash : {1}\n"
" - header uncle_hash: {2}".format(
len(block.uncles),
local_uncle_hash,
block.header.uncle_hash,
)
)
def validate_uncle(self, block, uncle):
if uncle.block_number >= block.number:
raise ValidationError(
"Uncle number ({0}) is higher than block number ({1})".format(
uncle.block_number, block.number))
try:
parent_header = get_block_header_by_hash(uncle.parent_hash, self.chaindb)
except BlockNotFound:
raise ValidationError(
"Uncle ancestor not found: {0}".format(uncle.parent_hash))
if uncle.block_number != parent_header.block_number + 1:
raise ValidationError(
"Uncle number ({0}) is not one above ancestor's number ({1})".format(
uncle.block_number, parent_header.block_number))
if uncle.timestamp < parent_header.timestamp:
raise ValidationError(
"Uncle timestamp ({0}) is before ancestor's timestamp ({1})".format(
uncle.timestamp, parent_header.timestamp))
if uncle.gas_used > uncle.gas_limit:
raise ValidationError(
"Uncle's gas usage ({0}) is above the limit ({1})".format(
uncle.gas_used, uncle.gas_limit))
#
# Transactions
#
@classmethod
def get_transaction_class(cls):
"""
Return the class that this VM uses for transactions.
"""
return cls.get_block_class().get_transaction_class()
def get_pending_transaction(self, transaction_hash):
return self.chaindb.get_pending_transaction(transaction_hash, self.get_transaction_class())
def create_transaction(self, *args, **kwargs):
"""
Proxy for instantiating a transaction for this VM.
"""
return self.get_transaction_class()(*args, **kwargs)
def create_unsigned_transaction(self, *args, **kwargs):
"""
Proxy for instantiating a transaction for this VM.
"""
return self.get_transaction_class().create_unsigned_transaction(*args, **kwargs)
#
# Blocks
#
@classmethod
def get_block_class(cls):
"""
Return the class that the state class uses for blocks.
"""
return cls.get_state_class().get_block_class()
@classmethod
def get_block_by_header(cls, block_header, db):
return cls.get_block_class().from_header(block_header, db)
@classmethod
@functools.lru_cache(maxsize=32)
@to_tuple
def get_prev_hashes(cls, last_block_hash, db):
if last_block_hash == GENESIS_PARENT_HASH:
return
block_header = get_block_header_by_hash(last_block_hash, db)
for _ in range(MAX_PREV_HEADER_DEPTH):
yield block_header.hash
try:
block_header = get_parent_header(block_header, db)
except (IndexError, BlockNotFound):
break
@property
def previous_hashes(self):
return self.get_prev_hashes(self.block.header.parent_hash, self.chaindb)
#
# Headers
#
@classmethod
@abstractmethod
def create_header_from_parent(cls, parent_header, **header_params):
"""
Creates and initializes a new block header from the provided
`parent_header`.
"""
raise NotImplementedError("Must be implemented by subclasses")
@abstractmethod
def configure_header(self, **header_params):
"""
Setup the current header with the provided parameters. This can be
used to set fields like the gas limit or timestamp to value different
than their computed defaults.
"""
raise NotImplementedError("Must be implemented by subclasses")
@classmethod
@abstractmethod
def compute_difficulty(cls, parent_header, timestamp):
raise NotImplementedError("Must be implemented by subclasses")
#
# Snapshot and Revert
#
def clear_journal(self):
"""
Clear the journal. This should be called at any point of VM execution
where the statedb is being committed, such as after a transaction has
been applied to a block.
"""
self.chaindb.clear()
#
# State
#
@classmethod
def get_state_class(cls):
"""
Return the class that this VM uses for states.
"""
if cls._state_class is None:
raise AttributeError("No `_state_class` has been set for this VM")
return cls._state_class
@classmethod
def get_state(cls, chaindb, block, prev_hashes, gas_used):
"""
Return state object
:param ChainDB chaindb: chaindb which cointains the state data
:param Block block: block defining the state root and receipts
:param tuple prev_hashes: previous block headers
:return: state with root defined in block
"""
execution_context = block.header.create_execution_context(prev_hashes)
return cls.get_state_class()(
chaindb,
execution_context=execution_context,
state_root=block.header.state_root,
gas_used=gas_used,
)
@property
def state(self):
"""Return current state property
"""
gas_used = 0
if self.receipts:
gas_used = self.receipts[-1].gas_used
return self.get_state(
chaindb=self.chaindb,
block=self.block,
prev_hashes=self.previous_hashes,
gas_used=gas_used,
)