@@ -400,7 +400,7 @@ async def _calculate_td(self, headers: Tuple[BlockHeader, ...]) -> int:
400
400
async def _process_headers (
401
401
self , peer : HeaderRequestingPeer , headers : Tuple [BlockHeader , ...]) -> int :
402
402
target_td = await self ._calculate_td (headers )
403
- await self ._download_block_parts (
403
+ bodies = await self ._download_block_parts (
404
404
target_td ,
405
405
[header for header in headers if not _is_body_empty (header )],
406
406
self .request_bodies ,
@@ -423,10 +423,18 @@ async def _process_headers(
423
423
'receipt' )
424
424
self .logger .debug ("Got block receipts for chain segment" )
425
425
426
- # FIXME: Get the bodies returned by self._download_block_parts above and use persit_block
427
- # here.
428
426
for header in headers :
429
- await self .wait (self .db .coro_persist_header (header ))
427
+ if header .uncles_hash != EMPTY_UNCLE_HASH :
428
+ body = cast (BlockBody , bodies [_body_key (header )])
429
+ uncles = body .uncles
430
+ else :
431
+ uncles = tuple ()
432
+ vm_class = self .chain .get_vm_class_for_block_number (header .block_number )
433
+ block_class = vm_class .get_block_class ()
434
+ # We don't need to use our block transactions here because persist_block() doesn't do
435
+ # anything with them as it expects them to have been persisted already.
436
+ block = block_class (header , uncles = uncles )
437
+ await self .wait (self .db .coro_persist_block (block ))
430
438
431
439
head = await self .wait (self .db .coro_get_canonical_head ())
432
440
return head .block_number
@@ -438,7 +446,8 @@ async def _download_block_parts(
438
446
request_func : Callable [[int , List [BlockHeader ]], int ],
439
447
download_queue : 'asyncio.Queue[Tuple[ETHPeer, List[DownloadedBlockPart]]]' ,
440
448
key_func : Callable [[BlockHeader ], Union [bytes , Tuple [bytes , bytes ]]],
441
- part_name : str ) -> 'List[DownloadedBlockPart]' :
449
+ part_name : str
450
+ ) -> 'Dict[Union[bytes, Tuple[bytes, bytes]], Union[BlockBody, List[Receipt]]]' :
442
451
"""Download block parts for the given headers, using the given request_func.
443
452
444
453
Retry timed out parts until we have the parts for all headers.
@@ -483,7 +492,7 @@ async def _download_block_parts(
483
492
if key_func (header ) not in received_keys
484
493
]
485
494
486
- return parts
495
+ return dict (( part . unique_key , part . part ) for part in parts )
487
496
488
497
def _request_block_parts (
489
498
self ,
@@ -577,7 +586,7 @@ async def _handle_block_bodies(self,
577
586
578
587
# TODO: figure out why mypy is losing the type of the transactions_tries
579
588
# so we can get rid of the ignore
580
- for (body , (tx_root , trie_dict_data )) in zip (bodies , transactions_tries ): # type: ignore # noqa: E501
589
+ for (body , (tx_root , trie_dict_data )) in zip (bodies , transactions_tries ): # type: ignore
581
590
await self .wait (self .db .coro_persist_trie_data_dict (trie_dict_data ))
582
591
uncles_hash = await self .wait (self .db .coro_persist_uncles (body .uncles ))
583
592
downloaded .append (DownloadedBlockPart (body , (tx_root , uncles_hash )))
@@ -686,7 +695,6 @@ async def _process_headers(
686
695
'body' )
687
696
self .logger .info ("Got block bodies for chain segment" )
688
697
689
- parts_by_key = dict ((part .unique_key , part .part ) for part in downloaded_parts )
690
698
for header in headers :
691
699
vm_class = self .chain .get_vm_class_for_block_number (header .block_number )
692
700
block_class = vm_class .get_block_class ()
@@ -695,7 +703,7 @@ async def _process_headers(
695
703
transactions : List [BaseTransaction ] = []
696
704
uncles : List [BlockHeader ] = []
697
705
else :
698
- body = cast (eth .BlockBody , parts_by_key [_body_key (header )])
706
+ body = cast (eth .BlockBody , downloaded_parts [_body_key (header )])
699
707
tx_class = block_class .get_transaction_class ()
700
708
transactions = [tx_class .from_base_transaction (tx )
701
709
for tx in body .transactions ]
0 commit comments