1
1
import time
2
+ from itertools import count
3
+ import sys
2
4
import rlp
3
5
from rlp .sedes import big_endian_int , Binary , binary , CountableList
4
- from rlp .utils import decode_hex , encode_hex , str_to_bytes
6
+ from rlp .utils import decode_hex , encode_hex
5
7
from ethereum import trie
6
8
from ethereum .trie import Trie
7
9
from ethereum .securetrie import SecureTrie
10
12
from ethereum import processblock
11
13
from ethereum .transactions import Transaction
12
14
from ethereum import bloom
13
- import sys
14
15
15
16
if sys .version_info .major == 2 :
16
17
from repoze .lru import lru_cache
@@ -95,21 +96,26 @@ def calc_difficulty(parent, timestamp):
95
96
# Auxiliary value for must_* error messages
96
97
aux = [None ]
97
98
99
+
98
100
def set_aux (auxval ):
99
101
aux [0 ] = auxval
100
102
103
+
101
104
def must (what , f , symb , a , b ):
102
105
if not f (a , b ):
103
106
if aux [0 ]:
104
107
sys .stderr .write ('%r' % aux [0 ])
105
108
raise VerificationFailed (what , a , symb , b )
106
109
110
+
107
111
def must_equal (what , a , b ):
108
112
return must (what , lambda x , y : x == y , "==" , a , b )
109
113
114
+
110
115
def must_ge (what , a , b ):
111
116
return must (what , lambda x , y : x >= y , ">=" , a , b )
112
117
118
+
113
119
def must_le (what , a , b ):
114
120
return must (what , lambda x , y : x <= y , "<=" , a , b )
115
121
@@ -390,6 +396,7 @@ def decorator(cls):
390
396
def make_gs_etter (source , attribute ):
391
397
def getter (self ):
392
398
return getattr (getattr (self , source ), attribute )
399
+
393
400
def setter (self , value ):
394
401
setattr (getattr (self , source ), attribute , value )
395
402
return getter , setter
@@ -403,7 +410,7 @@ def setter(self, value):
403
410
404
411
405
412
@mirror_from ('header' , set (field for field , _ in BlockHeader .fields ) -
406
- set (['state_root' , 'receipts_root' , 'tx_list_root' ]),
413
+ set (['state_root' , 'receipts_root' , 'tx_list_root' ]),
407
414
only_getters = False )
408
415
class Block (rlp .Serializable ):
409
416
"""A block.
@@ -430,7 +437,6 @@ class Block(rlp.Serializable):
430
437
('uncles' , CountableList (BlockHeader ))
431
438
]
432
439
433
-
434
440
def __init__ (self , header , transaction_list = [], uncles = [], db = None ,
435
441
parent = None , making = False ):
436
442
if db is None :
@@ -522,21 +528,17 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
522
528
if parent :
523
529
must_equal ('prev_hash' , self .prevhash , parent .hash )
524
530
must_ge ('gas_limit' , self .gas_limit ,
525
- parent .gas_limit * (GASLIMIT_ADJMAX_FACTOR - 1 ) //
526
- GASLIMIT_ADJMAX_FACTOR )
531
+ parent .gas_limit * (GASLIMIT_ADJMAX_FACTOR - 1 ) // GASLIMIT_ADJMAX_FACTOR )
527
532
must_le ('gas_limit' , self .gas_limit ,
528
- parent .gas_limit * (GASLIMIT_ADJMAX_FACTOR + 1 ) //
529
- GASLIMIT_ADJMAX_FACTOR )
533
+ parent .gas_limit * (GASLIMIT_ADJMAX_FACTOR + 1 ) // GASLIMIT_ADJMAX_FACTOR )
530
534
must_equal ('gas_used' , original_values ['gas_used' ], self .gas_used )
531
535
must_equal ('timestamp' , self .timestamp , original_values ['timestamp' ])
532
536
must_equal ('difficulty' , self .difficulty , original_values ['difficulty' ])
533
537
must_equal ('uncles_hash' , utils .sha3 (rlp .encode (uncles )), original_values ['uncles_hash' ])
534
538
assert header .block is None
535
539
must_equal ('state_root' , self .state .root_hash , header .state_root )
536
- must_equal ('tx_list_root' , self .transactions .root_hash ,
537
- header .tx_list_root )
538
- must_equal ('receipts_root' , self .receipts .root_hash ,
539
- header .receipts_root )
540
+ must_equal ('tx_list_root' , self .transactions .root_hash , header .tx_list_root )
541
+ must_equal ('receipts_root' , self .receipts .root_hash , header .receipts_root )
540
542
must_equal ('bloom' , self .bloom , original_values ['bloom' ])
541
543
set_aux (None )
542
544
@@ -553,8 +555,7 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
553
555
if not self .state .root_hash_valid ():
554
556
raise ValueError ("State Merkle root of block %r not found in "
555
557
"database" % self )
556
- if (not self .is_genesis () and self .nonce and
557
- not self .header .check_pow ()):
558
+ if (not self .is_genesis () and self .nonce and not self .header .check_pow ()):
558
559
raise ValueError ("PoW check failed" )
559
560
self .db .put ('validated:' + self .hash , '1' )
560
561
@@ -826,7 +827,20 @@ def get_receipt(self, num):
826
827
:returns: an instance of :class:`Receipt`
827
828
"""
828
829
index = rlp .encode (num )
829
- return rlp .decode (self .receipts .get (index ), Receipt )
830
+ receipt = self .receipts .get (index )
831
+ if receipt == trie .BLANK_NODE :
832
+ raise IndexError ('Receipt does not exist' )
833
+ else :
834
+ return rlp .decode (receipt , Receipt )
835
+
836
+ def get_receipts (self ):
837
+ """Build a list of all receipts in this block."""
838
+ receipts = []
839
+ for i in count ():
840
+ try :
841
+ receipts .append (self .get_receipt (i ))
842
+ except IndexError :
843
+ return receipts
830
844
831
845
def get_nonce (self , address ):
832
846
"""Get the nonce of an account.
@@ -1171,8 +1185,7 @@ def to_dict(self, with_state=False, full_transactions=False,
1171
1185
if with_state :
1172
1186
state_dump = {}
1173
1187
for address , v in self .state .to_dict ().items ():
1174
- state_dump [encode_hex (address )] = \
1175
- self .account_to_dict (address , with_storage_roots )
1188
+ state_dump [encode_hex (address )] = self .account_to_dict (address , with_storage_roots )
1176
1189
b ['state' ] = state_dump
1177
1190
if with_uncles :
1178
1191
b ['uncles' ] = [self .__class__ .deserialize_header (u )
@@ -1236,8 +1249,7 @@ def chain_difficulty(self):
1236
1249
1237
1250
def __eq__ (self , other ):
1238
1251
"""Two blocks are equal iff they have the same hash."""
1239
- return isinstance (other , (Block , CachedBlock )) and \
1240
- self .hash == other .hash
1252
+ return isinstance (other , (Block , CachedBlock )) and self .hash == other .hash
1241
1253
1242
1254
def __hash__ (self ):
1243
1255
return utils .big_endian_to_int (self .hash )
@@ -1257,10 +1269,12 @@ def __repr__(self):
1257
1269
def __structlog__ (self ):
1258
1270
return encode_hex (self .hash )
1259
1271
1272
+
1260
1273
@lru_cache (5 )
1261
1274
def get_cache_memoized (seedhash , size ):
1262
1275
return mkcache (size , seedhash )
1263
1276
1277
+
1264
1278
# Gas limit adjustment algo
1265
1279
def calc_gaslimit (parent ):
1266
1280
decay = parent .gas_limit // GASLIMIT_EMA_FACTOR
@@ -1370,9 +1384,8 @@ def genesis(db, start_alloc=GENESIS_INITIAL_ALLOC, difficulty=GENESIS_DIFFICULTY
1370
1384
block .set_nonce (addr , int (data ['nonce' ]))
1371
1385
if 'storage' in data :
1372
1386
for k , v in data ['storage' ].items ():
1373
- block .set_storage_data (addr ,
1374
- utils .big_endian_to_int (decode_hex (k [2 :])),
1375
- utils .big_endian_to_int (decode_hex (v [2 :])))
1387
+ block .set_storage_data (addr , utils .big_endian_to_int (decode_hex (k [2 :])),
1388
+ utils .big_endian_to_int (decode_hex (v [2 :])))
1376
1389
block .commit_state ()
1377
1390
block .state .db .commit ()
1378
1391
# genesis block has predefined state root (so no additional finalization
0 commit comments