@@ -257,7 +257,7 @@ def add_block(self, block):
257
257
apply_block (self .state , block )
258
258
except (AssertionError , KeyError , ValueError , InvalidTransaction , VerificationFailed ) as e :
259
259
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 ))
261
261
return False
262
262
self .db .put (b'block:%d' % block .header .number , block .header .hash )
263
263
block_score = self .get_score (block ) # side effect: put 'score:' cache in db
@@ -269,14 +269,15 @@ def add_block(self, block):
269
269
changed = self .state .changed
270
270
# Or is the block being added to a chain that is not currently the head?
271
271
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 ])))
274
275
temp_state = self .mk_poststate_of_blockhash (block .header .prevhash )
275
276
try :
276
277
apply_block (temp_state , block )
277
278
except (AssertionError , KeyError , ValueError , InvalidTransaction , VerificationFailed ) as e :
278
279
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 ))
280
281
return False
281
282
deletes = temp_state .deletes
282
283
block_score = self .get_score (block )
@@ -295,38 +296,52 @@ def add_block(self, block):
295
296
if b .prevhash not in self .db or self .db .get (b .prevhash ) == 'GENESIS' :
296
297
break
297
298
b = self .get_parent (b )
299
+ replace_from = b .header .number
298
300
# 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
299
304
changed_accts = {}
300
- replace_from = b . header . number
305
+ # Read: for i in range(common ancestor block number...new block number)
301
306
for i in itertools .count (replace_from ):
302
307
log .info ('Rewriting height %d' % i )
303
308
key = b'block:%d' % i
309
+ # Delete data for old blocks
304
310
orig_at_height = self .db .get (key ) if key in self .db else None
305
311
if orig_at_height :
306
- self .db .delete (key )
307
312
orig_block_at_height = self .get_block (orig_at_height )
308
313
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
309
317
for tx in orig_block_at_height .transactions :
310
318
if b'txindex:' + tx .hash in self .db :
311
319
self .db .delete (b'txindex:' + tx .hash )
320
+ # Add to changed list
312
321
acct_list = self .db .get (b'changed:' + orig_block_at_height .hash )
313
322
for j in range (0 , len (acct_list ), 20 ):
314
323
changed_accts [acct_list [j : j + 20 ]] = True
324
+ # Add data for new blocks
315
325
if i in new_chain :
316
326
new_block_at_height = new_chain [i ]
317
327
log .info ('%s now in main chain' % encode_hex (new_block_at_height .header .hash ))
328
+ # Add to block index
318
329
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 ):
320
332
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
322
335
if i < b .number :
323
336
acct_list = self .db .get (b'changed:' + new_block_at_height .hash )
324
337
for j in range (0 , len (acct_list ), 20 ):
325
338
changed_accts [acct_list [j : j + 20 ]] = True
326
339
if i not in new_chain and not orig_at_height :
327
340
break
341
+ # Add changed list from new head to changed list
328
342
for c in changed .keys ():
329
343
changed_accts [c ] = True
344
+ # Update the on-disk state cache
330
345
for addr in changed_accts .keys ():
331
346
data = temp_state .trie .get (addr )
332
347
if data :
@@ -373,8 +388,11 @@ def add_block(self, block):
373
388
log .info ('Added block %d (%s) with %d txs and %d gas' % \
374
389
(block .header .number , encode_hex (block .header .hash )[:8 ],
375
390
len (block .transactions ), block .header .gas_used ))
391
+ # Call optional callback
376
392
if self .new_head_cb and block .header .number != 0 :
377
393
self .new_head_cb (block )
394
+ # Are there blocks that we received that were waiting for this block?
395
+ # If so, process them.
378
396
if block .header .hash in self .parent_queue :
379
397
for _blk in self .parent_queue [block .header .hash ]:
380
398
self .add_block (_blk )
0 commit comments