forked from ethereum/py-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
501 lines (424 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
from __future__ import absolute_import
import rlp
import logging
from evm.constants import (
GENESIS_PARENT_HASH,
MAX_PREV_HEADER_DEPTH,
)
from evm.exceptions import (
BlockNotFound,
)
from evm.db.backends.memory import MemoryDB
from evm.db.chain import BaseChainDB
from evm.rlp.headers import (
BlockHeader,
)
from evm.utils.keccak import (
keccak,
)
from evm.utils.headers import (
generate_header_from_parent_header,
)
class VM(object):
"""
The VM class represents the Chain rules for a specific protocol definition
such as the Frontier or Homestead network. Defining an Chain defining
individual VM classes for each fork of the protocol rules within that
network.
"""
chaindb = None
_block_class = None
_state_class = None
_is_stateless = 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)
@classmethod
def configure(cls,
name=None,
**overrides):
if name is None:
name = cls.__name__
for key in overrides:
if not hasattr(cls, key):
raise TypeError(
"The VM.configure cannot set attributes that are not "
"already present on the base class. The attribute `{0}` was "
"not found on the base class `{1}`".format(key, cls)
)
return type(name, (cls,), overrides)
#
# Logging
#
@property
def logger(self):
return logging.getLogger('evm.vm.base.VM.{0}'.format(self.__class__.__name__))
#
# Execution
#
def add_transaction(self, transaction, computation):
"""
Add a transaction to the given block and save the block data into chaindb.
"""
receipt = self.state.make_receipt(self.state, transaction, computation)
transaction_idx = len(self.block.transactions)
index_key = rlp.encode(transaction_idx, sedes=rlp.sedes.big_endian_int)
self.block.transactions.append(transaction)
tx_root_hash = self.chaindb.add_transaction(self.block.header, index_key, transaction)
receipt_root_hash = self.chaindb.add_receipt(self.block.header, index_key, receipt)
self.block.bloom_filter |= receipt.bloom
self.block.header.transaction_root = tx_root_hash
self.block.header.receipt_root = receipt_root_hash
self.block.header.bloom = int(self.block.bloom_filter)
self.block.header.gas_used = receipt.gas_used
return self.block
def apply_transaction(self, transaction):
"""
Apply the transaction to the vm in the current block.
"""
if self.is_stateless:
computation, block, trie_data = self.get_state_class().apply_transaction(
self.state,
transaction,
self.block,
is_stateless=True,
)
self.block = block
# TODO: Modify Chain.apply_transaction to update the local vm state before
# Persist changed transaction and receipt key-values to self.chaindb.
for key, value in trie_data.items():
self.chaindb.db[key] = value
else:
computation, _, _ = self.get_state_class().apply_transaction(
self.state,
transaction,
self.block,
is_stateless=False,
)
self.add_transaction(transaction, computation)
self.clear_journal()
return computation, self.block
#
# Mining
#
@staticmethod
def get_block_reward():
raise NotImplementedError("Must be implemented by subclasses")
@staticmethod
def get_uncle_reward(block_number, uncle):
raise NotImplementedError("Must be implemented by subclasses")
@classmethod
def get_nephew_reward(cls):
raise NotImplementedError("Must be implemented by subclasses")
def import_block(self, block):
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)
# 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 the current block's mine method.
See example with FrontierBlock. :meth:`~evm.vm.forks.frontier.blocks.FrontierBlock.mine`
"""
block = self.block
self.pack_block(block, *args, **kwargs)
if block.number == 0:
return block
block_reward = self.get_block_reward() + (
len(block.uncles) * self.get_nephew_reward()
)
with self.state.state_db() as state_db:
state_db.delta_balance(block.header.coinbase, block_reward)
self.logger.debug(
"BLOCK REWARD: %s -> %s",
block_reward,
block.header.coinbase,
)
for uncle in block.uncles:
uncle_reward = self.get_uncle_reward(block.number, uncle)
state_db.delta_balance(uncle.coinbase, uncle_reward)
self.logger.debug(
"UNCLE REWARD REWARD: %s -> %s",
uncle_reward,
uncle.coinbase,
)
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.state.validate_block(block)
return block
@classmethod
def create_block(
cls,
transaction_packages,
prev_headers,
coinbase):
"""
Create a block with transaction witness
"""
parent_header = prev_headers[0]
block = cls.generate_block_from_parent_header_and_coinbase(
parent_header,
coinbase,
)
recent_trie_nodes = {}
receipts = []
for (transaction, transaction_witness) in transaction_packages:
transaction_witness.update(recent_trie_nodes)
witness_db = BaseChainDB(MemoryDB(transaction_witness))
vm_state = cls.get_state_class()(
chaindb=witness_db,
block_header=block.header,
prev_headers=prev_headers,
receipts=receipts,
)
computation, result_block, _ = cls.get_state_class().apply_transaction(
vm_state=vm_state,
transaction=transaction,
block=block,
is_stateless=True,
)
if not computation.is_error:
block = result_block
receipts = computation.vm_state.receipts
recent_trie_nodes.update(computation.vm_state.access_logs.writes)
else:
pass
# Finalize
witness_db = BaseChainDB(MemoryDB(recent_trie_nodes))
vm_state = cls.get_state_class()(
chaindb=witness_db,
block_header=block.header,
prev_headers=prev_headers,
)
block = cls.finalize_block(vm_state, block)
return block
@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,
parent_header.timestamp + 1,
coinbase,
)
block = cls.get_block_class()(
block_header,
transactions=[],
uncles=[],
)
return block
@classmethod
def finalize_block(cls, vm_state, block):
"""
Finalize the given block (set rewards).
"""
block_reward = cls.get_block_reward() + (
len(block.uncles) * cls.get_nephew_reward(cls)
)
with vm_state.state_db() as state_db:
state_db.delta_balance(block.header.coinbase, block_reward)
vm_state.logger.debug(
"BLOCK REWARD: %s -> %s",
block_reward,
block.header.coinbase,
)
for uncle in block.uncles:
uncle_reward = cls.get_uncle_reward(block.number, uncle)
state_db.delta_balance(uncle.coinbase, uncle_reward)
vm_state.logger.debug(
"UNCLE REWARD REWARD: %s -> %s",
uncle_reward,
uncle.coinbase,
)
block.state_root = vm_state.block_header.state_root
return block
#
# Transactions
#
def get_transaction_class(self):
"""
Return the class that this VM uses for transactions.
"""
return self.get_block_class().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)
def validate_transaction(self, transaction):
"""
Perform chain-aware validation checks on the transaction.
"""
raise NotImplementedError("Must be implemented by subclasses")
#
# Blocks
#
@classmethod
def get_block_class(cls):
"""
Return the class that this VM uses for blocks.
"""
if cls._block_class is None:
raise AttributeError("No `_block_class` has been set for this VM")
return cls._block_class
@classmethod
def get_block_by_header(cls, block_header, db):
return cls.get_block_class().from_header(block_header, db)
@staticmethod
def get_parent_header(block_header, db):
"""
Returns the header for the parent block.
"""
return db.get_block_header_by_hash(block_header.parent_hash)
@staticmethod
def get_block_header_by_hash(block_hash, db):
"""
Returns the header for the parent block.
"""
return db.get_block_header_by_hash(block_hash)
@classmethod
def get_prev_headers(cls, last_block_hash, db):
prev_headers = []
if last_block_hash == GENESIS_PARENT_HASH:
return prev_headers
block_header = cls.get_block_header_by_hash(last_block_hash, db)
for _ in range(MAX_PREV_HEADER_DEPTH):
prev_headers.append(block_header)
try:
block_header = cls.get_parent_header(block_header, db)
except (IndexError, BlockNotFound):
break
return prev_headers
#
# Gas Usage API
#
def get_cumulative_gas_used(self, block):
"""
Note return value of this function can be cached based on
`self.receipt_db.root_hash`
"""
if len(block.transactions):
return block.get_receipts(self.chaindb)[-1].gas_used
else:
return 0
#
# Headers
#
@classmethod
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")
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
def compute_difficulty(cls, parent_header, timestamp):
raise NotImplementedError("Must be implemented by subclasses")
#
# Snapshot and Revert
#
def clear_journal(self):
"""
Cleare 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
#
@property
def is_stateless(self):
return self._is_stateless
@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
def get_state(self, chaindb=None, block_header=None, prev_headers=None):
"""Return state object
"""
if chaindb is None:
chaindb = self.chaindb
if block_header is None: # TODO: remove
block_header = self.block.header
if prev_headers is None:
prev_headers = self.get_prev_headers(
last_block_hash=self.block.header.parent_hash,
db=self.chaindb,
)
receipts = self.block.get_receipts(self.chaindb)
return self.get_state_class()(
chaindb,
block_header,
prev_headers,
receipts=receipts,
)
@property
def state(self):
"""Return current state property
"""
return self.get_state(
chaindb=self.chaindb,
block_header=self.block.header,
)