12
12
cast ,
13
13
Dict ,
14
14
Generator ,
15
+ Iterable ,
15
16
Iterator ,
16
17
List ,
17
18
Optional ,
32
33
encode_hex ,
33
34
)
34
35
36
+ from eth .constants import (
37
+ BLANK_ROOT_HASH ,
38
+ EMPTY_UNCLE_HASH ,
39
+ MAX_UNCLE_DEPTH ,
40
+ )
41
+
35
42
from eth .db .backends .base import BaseAtomicDB
36
43
from eth .db .chain import (
37
44
BaseChainDB ,
40
47
from eth .db .header import (
41
48
HeaderDB ,
42
49
)
43
- from eth .constants import (
44
- BLANK_ROOT_HASH ,
45
- EMPTY_UNCLE_HASH ,
46
- MAX_UNCLE_DEPTH ,
47
- )
50
+
48
51
from eth .estimators import (
49
52
get_gas_estimator ,
50
53
)
53
56
TransactionNotFound ,
54
57
VMNotFound ,
55
58
)
56
- from eth .utils .spoof import (
57
- SpoofTransaction ,
58
- )
59
- from eth .validation import (
60
- validate_block_number ,
61
- validate_uint256 ,
62
- validate_word ,
63
- validate_vm_configuration ,
64
- )
59
+
65
60
from eth .rlp .blocks import (
66
61
BaseBlock ,
67
62
)
76
71
BaseTransaction ,
77
72
BaseUnsignedTransaction ,
78
73
)
74
+
75
+ from eth .typing import (
76
+ AccountState ,
77
+ )
78
+
79
+ from eth .utils .spoof import (
80
+ SpoofTransaction ,
81
+ )
79
82
from eth .utils .db import (
80
83
apply_state_dict ,
81
84
)
88
91
from eth .utils .rlp import (
89
92
validate_imported_block_unchanged ,
90
93
)
91
- from eth .typing import (
92
- AccountState ,
94
+
95
+ from eth .validation import (
96
+ validate_block_number ,
97
+ validate_uint256 ,
98
+ validate_word ,
99
+ validate_vm_configuration ,
93
100
)
101
+ from eth .vm .computation import BaseComputation
102
+ from eth .vm .state import BaseState # noqa: F401
94
103
95
104
from eth ._warnings import catch_and_ignore_import_warning
96
105
with catch_and_ignore_import_warning ():
107
116
)
108
117
109
118
if TYPE_CHECKING :
110
- from eth .vm .base import BaseVM # noqa: F401
119
+ from eth .vm .base import ( # noqa: F401
120
+ BaseVM ,
121
+ VM ,
122
+ )
111
123
112
124
113
125
class BaseChain (Configurable , ABC ):
@@ -164,7 +176,7 @@ def get_vm_class(cls, header: BlockHeader) -> Type['BaseVM']:
164
176
return cls .get_vm_class_for_block_number (header .block_number )
165
177
166
178
@abstractmethod
167
- def get_vm (self , header : BlockHeader = None ) -> 'BaseVM ' :
179
+ def get_vm (self , header : BlockHeader = None ) -> 'VM ' :
168
180
raise NotImplementedError ("Chain classes must implement this method" )
169
181
170
182
@classmethod
@@ -196,11 +208,11 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
196
208
raise NotImplementedError ("Chain classes must implement this method" )
197
209
198
210
@abstractmethod
199
- def get_canonical_head (self ):
211
+ def get_canonical_head (self ) -> BlockHeader :
200
212
raise NotImplementedError ("Chain classes must implement this method" )
201
213
202
214
@abstractmethod
203
- def get_score (self , block_hash ) :
215
+ def get_score (self , block_hash : Hash32 ) -> int :
204
216
raise NotImplementedError ("Chain classes must implement this method" )
205
217
206
218
#
@@ -227,11 +239,14 @@ def get_canonical_block_by_number(self, block_number: BlockNumber) -> BaseBlock:
227
239
raise NotImplementedError ("Chain classes must implement this method" )
228
240
229
241
@abstractmethod
230
- def get_canonical_block_hash (self , block_number ) :
242
+ def get_canonical_block_hash (self , block_number : BlockNumber ) -> Hash32 :
231
243
raise NotImplementedError ("Chain classes must implement this method" )
232
244
233
245
@abstractmethod
234
- def build_block_with_transactions (self , transactions , parent_header ):
246
+ def build_block_with_transactions (self ,
247
+ transactions : Tuple [BaseTransaction , ...],
248
+ parent_header : BlockHeader = None
249
+ ) -> Tuple [BaseBlock , Tuple [Receipt , ...], Tuple [BaseComputation , ...]]: # noqa: E501
235
250
raise NotImplementedError ("Chain classes must implement this method" )
236
251
237
252
#
@@ -320,7 +335,7 @@ class Chain(BaseChain):
320
335
current block number.
321
336
"""
322
337
logger = logging .getLogger ("eth.chain.chain.Chain" )
323
- gas_estimator = None # type: Callable
338
+ gas_estimator = None # type: Callable[[BaseState, BaseTransaction], int]
324
339
325
340
chaindb_class = ChainDB # type: Type[BaseChainDB]
326
341
@@ -334,8 +349,8 @@ def __init__(self, base_db: BaseAtomicDB) -> None:
334
349
335
350
self .chaindb = self .get_chaindb_class ()(base_db )
336
351
self .headerdb = HeaderDB (base_db )
337
- if self .gas_estimator is None :
338
- self .gas_estimator = get_gas_estimator () # type: ignore
352
+ if self .gas_estimator is None : # type: ignore
353
+ self .gas_estimator = get_gas_estimator () # type: ignore
339
354
340
355
#
341
356
# Helpers
@@ -403,7 +418,7 @@ def from_genesis_header(cls,
403
418
#
404
419
# VM API
405
420
#
406
- def get_vm (self , at_header : BlockHeader = None ) -> 'BaseVM ' :
421
+ def get_vm (self , at_header : BlockHeader = None ) -> 'VM ' :
407
422
"""
408
423
Returns the VM instance for the given block number.
409
424
"""
@@ -414,7 +429,9 @@ def get_vm(self, at_header: BlockHeader=None) -> 'BaseVM':
414
429
#
415
430
# Header API
416
431
#
417
- def create_header_from_parent (self , parent_header , ** header_params ):
432
+ def create_header_from_parent (self ,
433
+ parent_header : BlockHeader ,
434
+ ** header_params : HeaderParams ) -> BlockHeader :
418
435
"""
419
436
Passthrough helper to the VM class of the block descending from the
420
437
given header.
@@ -432,15 +449,15 @@ def get_block_header_by_hash(self, block_hash: Hash32) -> BlockHeader:
432
449
validate_word (block_hash , title = "Block Hash" )
433
450
return self .chaindb .get_block_header_by_hash (block_hash )
434
451
435
- def get_canonical_head (self ):
452
+ def get_canonical_head (self ) -> BlockHeader :
436
453
"""
437
454
Returns the block header at the canonical chain head.
438
455
439
456
Raises CanonicalHeadNotFound if there's no head defined for the canonical chain.
440
457
"""
441
458
return self .chaindb .get_canonical_head ()
442
459
443
- def get_score (self , block_hash ) :
460
+ def get_score (self , block_hash : Hash32 ) -> int :
444
461
"""
445
462
Returns the difficulty score of the block with the given hash.
446
463
@@ -498,7 +515,7 @@ def get_block_by_hash(self, block_hash: Hash32) -> BaseBlock:
498
515
block_header = self .get_block_header_by_hash (block_hash )
499
516
return self .get_block_by_header (block_header )
500
517
501
- def get_block_by_header (self , block_header ) :
518
+ def get_block_by_header (self , block_header : BlockHeader ) -> BaseBlock :
502
519
"""
503
520
Returns the requested block as specified by the block header.
504
521
"""
@@ -524,7 +541,10 @@ def get_canonical_block_hash(self, block_number: BlockNumber) -> Hash32:
524
541
"""
525
542
return self .chaindb .get_canonical_block_hash (block_number )
526
543
527
- def build_block_with_transactions (self , transactions , parent_header = None ):
544
+ def build_block_with_transactions (self ,
545
+ transactions : Tuple [BaseTransaction , ...],
546
+ parent_header : BlockHeader = None
547
+ ) -> Tuple [BaseBlock , Tuple [Receipt , ...], Tuple [BaseComputation , ...]]: # noqa: E501
528
548
"""
529
549
Generate a block with the provided transactions. This does *not* import
530
550
that block into your chain. If you want this new block in your chain,
@@ -554,12 +574,12 @@ def get_canonical_transaction(self, transaction_hash: Hash32) -> BaseTransaction
554
574
found in the main chain.
555
575
"""
556
576
(block_num , index ) = self .chaindb .get_transaction_index (transaction_hash )
557
- VM = self .get_vm_class_for_block_number (block_num )
577
+ VM_class = self .get_vm_class_for_block_number (block_num )
558
578
559
579
transaction = self .chaindb .get_transaction_by_index (
560
580
block_num ,
561
581
index ,
562
- VM .get_transaction_class (),
582
+ VM_class .get_transaction_class (),
563
583
)
564
584
565
585
if transaction .hash == transaction_hash :
@@ -627,7 +647,7 @@ def estimate_gas(
627
647
if at_header is None :
628
648
at_header = self .get_canonical_head ()
629
649
with self .get_vm (at_header ).state_in_temp_block () as state :
630
- return self .gas_estimator (state , transaction )
650
+ return self .gas_estimator (state , transaction ) # type: ignore
631
651
632
652
def import_block (self ,
633
653
block : BaseBlock ,
@@ -689,8 +709,8 @@ def import_block(self,
689
709
# Validation API
690
710
#
691
711
def validate_receipt (self , receipt : Receipt , at_header : BlockHeader ) -> None :
692
- VM = self .get_vm_class (at_header )
693
- VM .validate_receipt (receipt )
712
+ VM_class = self .get_vm_class (at_header )
713
+ VM_class .validate_receipt (receipt )
694
714
695
715
def validate_block (self , block : BaseBlock ) -> None :
696
716
"""
@@ -704,18 +724,18 @@ def validate_block(self, block: BaseBlock) -> None:
704
724
"""
705
725
if block .is_genesis :
706
726
raise ValidationError ("Cannot validate genesis block this way" )
707
- VM = self .get_vm_class_for_block_number (BlockNumber (block .number ))
727
+ VM_class = self .get_vm_class_for_block_number (BlockNumber (block .number ))
708
728
parent_block = self .get_block_by_hash (block .header .parent_hash )
709
- VM .validate_header (block .header , parent_block .header , check_seal = True )
729
+ VM_class .validate_header (block .header , parent_block .header , check_seal = True )
710
730
self .validate_uncles (block )
711
731
self .validate_gaslimit (block .header )
712
732
713
733
def validate_seal (self , header : BlockHeader ) -> None :
714
734
"""
715
735
Validate the seal on the given header.
716
736
"""
717
- VM = self .get_vm_class_for_block_number (BlockNumber (header .block_number ))
718
- VM .validate_seal (header )
737
+ VM_class = self .get_vm_class_for_block_number (BlockNumber (header .block_number ))
738
+ VM_class .validate_seal (header )
719
739
720
740
def validate_gaslimit (self , header : BlockHeader ) -> None :
721
741
"""
@@ -830,7 +850,7 @@ def validate_chain(
830
850
831
851
832
852
@to_set
833
- def _extract_uncle_hashes (blocks ) :
853
+ def _extract_uncle_hashes (blocks : Iterable [ BaseBlock ]) -> Iterable [ Hash32 ] :
834
854
for block in blocks :
835
855
for uncle in block .uncles :
836
856
yield uncle .hash
@@ -843,7 +863,9 @@ def __init__(self, base_db: BaseAtomicDB, header: BlockHeader=None) -> None:
843
863
super ().__init__ (base_db )
844
864
self .header = self .ensure_header (header )
845
865
846
- def apply_transaction (self , transaction ):
866
+ def apply_transaction (self ,
867
+ transaction : BaseTransaction
868
+ ) -> Tuple [BaseBlock , Receipt , BaseComputation ]:
847
869
"""
848
870
Applies the transaction to the current tip block.
849
871
@@ -890,7 +912,7 @@ def mine_block(self, *args: Any, **kwargs: Any) -> BaseBlock:
890
912
self .header = self .create_header_from_parent (mined_block .header )
891
913
return mined_block
892
914
893
- def get_vm (self , at_header : BlockHeader = None ) -> 'BaseVM ' :
915
+ def get_vm (self , at_header : BlockHeader = None ) -> 'VM ' :
894
916
if at_header is None :
895
917
at_header = self .header
896
918
0 commit comments