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

Commit 359cc9e

Browse files
committed
Now passing all block tests again
1 parent fdfcf69 commit 359cc9e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

ethereum/pow/chain.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def add_block(self, block):
257257
apply_block(self.state, block)
258258
except (AssertionError, KeyError, ValueError, InvalidTransaction, VerificationFailed) as e:
259259
log.info('Block %d (%s) with parent %s invalid, reason: %s' %
260-
(block.number, encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
260+
(block.number, encode_hex(block.header.hash[:4]), encode_hex(block.header.prevhash[:4]), e))
261261
return False
262262
self.db.put(b'block:%d' % block.header.number, block.header.hash)
263263
block_score = self.get_score(block) # side effect: put 'score:' cache in db
@@ -269,14 +269,15 @@ def add_block(self, block):
269269
changed = self.state.changed
270270
# Or is the block being added to a chain that is not currently the head?
271271
elif block.header.prevhash in self.env.db:
272-
log.info('Receiving block not on head (%s), adding to secondary post state %s' %
273-
(encode_hex(self.head_hash), encode_hex(block.header.prevhash)))
272+
log.info('Receiving block %d (%s) not on head (%s), adding to secondary post state %s' %
273+
(block.number, encode_hex(block.header.hash[:4]),
274+
encode_hex(self.head_hash[:4]), encode_hex(block.header.prevhash[:4])))
274275
temp_state = self.mk_poststate_of_blockhash(block.header.prevhash)
275276
try:
276277
apply_block(temp_state, block)
277278
except (AssertionError, KeyError, ValueError, InvalidTransaction, VerificationFailed) as e:
278279
log.info('Block %s with parent %s invalid, reason: %s' %
279-
(encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
280+
(encode_hex(block.header.hash[:4]), encode_hex(block.header.prevhash[:4]), e))
280281
return False
281282
deletes = temp_state.deletes
282283
block_score = self.get_score(block)
@@ -295,38 +296,52 @@ def add_block(self, block):
295296
if b.prevhash not in self.db or self.db.get(b.prevhash) == 'GENESIS':
296297
break
297298
b = self.get_parent(b)
299+
replace_from = b.header.number
298300
# Replace block index and tx indices, and edit the state cache
301+
302+
# Get a list of all accounts that have been edited along the old and
303+
# new chains
299304
changed_accts = {}
300-
replace_from = b.header.number
305+
# Read: for i in range(common ancestor block number...new block number)
301306
for i in itertools.count(replace_from):
302307
log.info('Rewriting height %d' % i)
303308
key = b'block:%d' % i
309+
# Delete data for old blocks
304310
orig_at_height = self.db.get(key) if key in self.db else None
305311
if orig_at_height:
306-
self.db.delete(key)
307312
orig_block_at_height = self.get_block(orig_at_height)
308313
log.info('%s no longer in main chain' % encode_hex(orig_block_at_height.header.hash))
314+
# Delete from block index
315+
self.db.delete(key)
316+
# Delete from txindex
309317
for tx in orig_block_at_height.transactions:
310318
if b'txindex:' + tx.hash in self.db:
311319
self.db.delete(b'txindex:' + tx.hash)
320+
# Add to changed list
312321
acct_list = self.db.get(b'changed:'+orig_block_at_height.hash)
313322
for j in range(0, len(acct_list), 20):
314323
changed_accts[acct_list[j: j+20]] = True
324+
# Add data for new blocks
315325
if i in new_chain:
316326
new_block_at_height = new_chain[i]
317327
log.info('%s now in main chain' % encode_hex(new_block_at_height.header.hash))
328+
# Add to block index
318329
self.db.put(key, new_block_at_height.header.hash)
319-
for i, tx in enumerate(new_block_at_height.transactions):
330+
# Add to txindex
331+
for j, tx in enumerate(new_block_at_height.transactions):
320332
self.db.put(b'txindex:' + tx.hash,
321-
rlp.encode([new_block_at_height.number, i]))
333+
rlp.encode([new_block_at_height.number, j]))
334+
# Add to changed list
322335
if i < b.number:
323336
acct_list = self.db.get(b'changed:'+new_block_at_height.hash)
324337
for j in range(0, len(acct_list), 20):
325338
changed_accts[acct_list[j: j+20]] = True
326339
if i not in new_chain and not orig_at_height:
327340
break
341+
# Add changed list from new head to changed list
328342
for c in changed.keys():
329343
changed_accts[c] = True
344+
# Update the on-disk state cache
330345
for addr in changed_accts.keys():
331346
data = temp_state.trie.get(addr)
332347
if data:
@@ -373,8 +388,11 @@ def add_block(self, block):
373388
log.info('Added block %d (%s) with %d txs and %d gas' % \
374389
(block.header.number, encode_hex(block.header.hash)[:8],
375390
len(block.transactions), block.header.gas_used))
391+
# Call optional callback
376392
if self.new_head_cb and block.header.number != 0:
377393
self.new_head_cb(block)
394+
# Are there blocks that we received that were waiting for this block?
395+
# If so, process them.
378396
if block.header.hash in self.parent_queue:
379397
for _blk in self.parent_queue[block.header.hash]:
380398
self.add_block(_blk)

ethereum/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def add_block_header(self, block_header):
161161
def get_and_cache_account(self, address):
162162
if address in self.cache:
163163
return self.cache[address]
164-
if self.executing_on_head:
164+
if self.executing_on_head and False:
165165
try:
166166
rlpdata = self.db.get(b'address:'+address)
167167
except KeyError:

0 commit comments

Comments
 (0)