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

Commit 253b7d4

Browse files
author
Jan Xie
committed
rewrite get_score to avoid segment fault caused by stack overflow
1 parent da9ff33 commit 253b7d4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

ethereum/chain.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def mk_poststate_of_blockhash(self, blockhash, convert=False):
140140
def get_parent(self, block):
141141
if block.header.number == int(self.db.get('GENESIS_NUMBER')):
142142
return None
143-
return rlp.decode(self.db.get(block.header.prevhash), Block)
143+
return self.get_block(block.header.prevhash)
144144

145145
def get_block(self, blockhash):
146146
try:
@@ -200,13 +200,19 @@ def get_score(self, block):
200200
if not block:
201201
return 0
202202
key = 'score:' + block.header.hash
203-
if key not in self.db:
204-
try:
205-
parent_score = self.get_score(self.get_parent(block))
206-
self.db.put(key, str(parent_score + block.difficulty))
207-
except:
208-
return int(self.db.get('score:' + block.prevhash))
209-
return int(self.db.get(key))
203+
204+
fills = []
205+
while key not in self.db:
206+
fills.insert(0, block)
207+
key = 'score:' + block.header.prevhash
208+
block = self.get_parent(fills[0])
209+
score = int(self.db.get(key))
210+
for block in fills:
211+
key = 'score:' + block.header.hash
212+
score = score + block.difficulty + random.randrange(block.difficulty // 10**6 + 1)
213+
self.db.put(key, str(score))
214+
215+
return score
210216

211217
# These two functions should be called periodically so as to
212218
# process blocks that were received but laid aside because
@@ -252,6 +258,7 @@ def add_block(self, block):
252258
return False
253259
self.db.put('block:' + str(block.header.number), block.header.hash)
254260
self.db.put('state:' + block.header.hash, self.state.trie.root_hash)
261+
block_score = self.get_score(block) # side effect: put 'score:' cache in db
255262
self.head_hash = block.header.hash
256263
for i, tx in enumerate(block.transactions):
257264
self.db.put('txindex:' + tx.hash, rlp.encode([block.number, i]))

0 commit comments

Comments
 (0)