Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit c4e77e1

Browse files
committed
fix: get_parent recursion on chain init
1 parent 0593022 commit c4e77e1

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

ethereum/blocks.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,12 @@ def __init__(self,
258258
self.block = None
259259
super(BlockHeader, self).__init__(**fields)
260260

261-
262261
@classmethod
263262
def from_block_rlp(self, rlp_data):
264263
block_data = rlp.decode_lazy(rlp_data)
265-
return super(BlockHeader, self).deserialize(block_data[0])
264+
r = super(BlockHeader, self).deserialize(block_data[0])
265+
assert isinstance(r, BlockHeader)
266+
return r
266267

267268
@property
268269
def state_root(self):
@@ -306,10 +307,12 @@ def receipts_root(self, value):
306307
else:
307308
self._receipts_root = value
308309

310+
_fimxe_hash = None
311+
309312
@property
310313
def hash(self):
311314
"""The binary block hash"""
312-
return utils.sha3(rlp.encode(self))
315+
return self._fimxe_hash or utils.sha3(rlp.encode(self))
313316

314317
def hex_hash(self):
315318
"""The hex encoded block hash"""
@@ -458,7 +461,7 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
458461

459462
# do some consistency checks on parent if given
460463
if parent:
461-
if self.db != parent.db:
464+
if hasattr(parent, 'db') and self.db != parent.db:
462465
raise ValueError("Parent lives in different database")
463466
if self.prevhash != parent.header.hash:
464467
raise ValueError("Block's prevhash and parent's hash do not match")
@@ -486,12 +489,12 @@ def __init__(self, header, transaction_list=[], uncles=[], db=None,
486489
state_unknown = (header.prevhash != GENESIS_PREVHASH and
487490
header.state_root != trie.BLANK_ROOT and
488491
(len(header.state_root) != 32 or
489-
'validated:'+self.hash not in db) and
492+
'validated:' + self.hash not in db) and
490493
not making)
491494
if state_unknown:
492495
assert transaction_list is not None
493496
if not parent:
494-
parent = self.get_parent()
497+
parent = self.get_parent_header()
495498
self.state = SecureTrie(Trie(db, parent.state_root))
496499
self.transaction_count = 0
497500
self.gas_used = 0
@@ -1190,6 +1193,17 @@ def get_parent(self):
11901193
# assert parent.state.db.db == self.state.db.db
11911194
return parent
11921195

1196+
def get_parent_header(self):
1197+
"""Get the parent of this block."""
1198+
if self.number == 0:
1199+
raise UnknownParentException('Genesis block has no parent')
1200+
try:
1201+
parent_header = get_block_header(self.db, self.prevhash)
1202+
except KeyError:
1203+
raise UnknownParentException(encode_hex(self.prevhash))
1204+
# assert parent.state.db.db == self.state.db.db
1205+
return parent_header
1206+
11931207
def has_parent(self):
11941208
"""`True` if this block has a known parent, otherwise `False`."""
11951209
try:
@@ -1296,6 +1310,15 @@ def create_cached(cls, blk):
12961310
return blk
12971311

12981312

1313+
def get_block_header(db, blockhash):
1314+
bh = BlockHeader.from_block_rlp(db.get(blockhash))
1315+
if bh.hash != blockhash:
1316+
log.warn('BlockHeader.hash is broken')
1317+
bh._fimxe_hash = blockhash
1318+
1319+
return bh
1320+
1321+
12991322
@lru_cache(500)
13001323
def get_block(db, blockhash):
13011324
"""

0 commit comments

Comments
 (0)