Ejemplo n.º 1
0
 async def _handle_msg(self, peer: ETHPeer, cmd: protocol.Command,
                       msg: protocol._DecodedMsgType) -> None:
     if isinstance(cmd, eth.BlockHeaders):
         msg = cast(List[BlockHeader], msg)
         self.logger.debug("Got BlockHeaders from %d to %d",
                           msg[0].block_number, msg[-1].block_number)
         self._new_headers.put_nowait(msg)
     elif isinstance(cmd, eth.BlockBodies):
         await self._handle_block_bodies(peer, cast(List[eth.BlockBody],
                                                    msg))
     elif isinstance(cmd, eth.Receipts):
         await self._handle_block_receipts(
             peer, cast(List[List[eth.Receipt]], msg))
     elif isinstance(cmd, eth.NewBlock):
         msg = cast(Dict[str, Any], msg)
         header = msg['block'][0]
         actual_head = header.parent_hash
         actual_td = msg['total_difficulty'] - header.difficulty
         if actual_td > peer.head_td:
             peer.head_hash = actual_head
             peer.head_td = actual_td
             self._sync_requests.put_nowait(peer)
     elif isinstance(cmd, eth.Transactions):
         # TODO: Figure out what to do with those.
         pass
     elif isinstance(cmd, eth.NewBlockHashes):
         # TODO: Figure out what to do with those.
         pass
     else:
         # TODO: There are other msg types we'll want to handle here, but for now just log them
         # as a warning so we don't forget about it.
         self.logger.warn("Got unexpected msg: %s (%s)", cmd, msg)
Ejemplo n.º 2
0
 async def _handle_new_block(self, peer: ETHPeer, msg: Dict[str, Any]) -> None:
     header = msg['block'][0]
     actual_head = header.parent_hash
     actual_td = msg['total_difficulty'] - header.difficulty
     if actual_td > peer.head_td:
         peer.head_hash = actual_head
         peer.head_td = actual_td
         self._sync_requests.put_nowait(peer)
Ejemplo n.º 3
0
 async def handle_msg(self, peer: ETHPeer, cmd: protocol.Command,
                      msg: protocol._DecodedMsgType) -> None:
     loop = asyncio.get_event_loop()
     if isinstance(cmd, eth.BlockHeaders):
         msg = cast(List[BlockHeader], msg)
         self.logger.debug(
             "Got BlockHeaders from %d to %d", msg[0].block_number, msg[-1].block_number)
         self._new_headers.put_nowait(msg)
     elif isinstance(cmd, eth.BlockBodies):
         msg = cast(List[eth.BlockBody], msg)
         self.logger.debug("Got %d BlockBodies from %s", len(msg), peer)
         iterator = map(make_trie_root_and_nodes, [body.transactions for body in msg])
         transactions_tries = await loop.run_in_executor(
             self.executor, list, iterator)  # type: List[Tuple[bytes, Any]]
         for i in range(len(msg)):
             body = msg[i]
             tx_root, trie_dict_data = transactions_tries[i]
             await self.chaindb.coro_persist_trie_data_dict(trie_dict_data)
             # TODO: Add transactions to canonical chain; blocked by
             # https://github.com/ethereum/py-evm/issues/337
             uncles_hash = await self.chaindb.coro_persist_uncles(body.uncles)
             self._pending_bodies.pop((tx_root, uncles_hash), None)
     elif isinstance(cmd, eth.Receipts):
         msg = cast(List[List[eth.Receipt]], msg)
         self.logger.debug("Got Receipts for %d blocks from %s", len(msg), peer)
         iterator = map(make_trie_root_and_nodes, msg)
         receipts_tries = await loop.run_in_executor(
             self.executor, list, iterator)  # type: List[Tuple[bytes, Any]]
         for receipt_root, trie_dict_data in receipts_tries:
             if receipt_root not in self._pending_receipts:
                 self.logger.warning(
                     "Got unexpected receipt root: %s",
                     encode_hex(receipt_root),
                 )
                 continue
             await self.chaindb.coro_persist_trie_data_dict(trie_dict_data)
             self._pending_receipts.pop(receipt_root)
     elif isinstance(cmd, eth.NewBlock):
         msg = cast(Dict[str, Any], msg)
         header = msg['block'][0]
         actual_head = header.parent_hash
         actual_td = msg['total_difficulty'] - header.difficulty
         if actual_td > peer.head_td:
             peer.head_hash = actual_head
             peer.head_td = actual_td
             self._sync_requests.put_nowait(peer)
     elif isinstance(cmd, eth.Transactions):
         # TODO: Figure out what to do with those.
         pass
     elif isinstance(cmd, eth.NewBlockHashes):
         # TODO: Figure out what to do with those.
         pass
     else:
         # TODO: There are other msg types we'll want to handle here, but for now just log them
         # as a warning so we don't forget about it.
         self.logger.warn("Got unexpected msg: %s (%s)", cmd, msg)
Ejemplo n.º 4
0
 async def handle_msg(self, peer: ETHPeer, cmd: protocol.Command,
                      msg: protocol._DecodedMsgType) -> None:
     if isinstance(cmd, eth.BlockHeaders):
         msg = cast(List[BlockHeader], msg)
         self.logger.debug("Got BlockHeaders from %d to %d",
                           msg[0].block_number, msg[-1].block_number)
         self._new_headers.put_nowait(msg)
     elif isinstance(cmd, eth.BlockBodies):
         msg = cast(List[eth.BlockBody], msg)
         self.logger.debug("Got %d BlockBodies", len(msg))
         for body in msg:
             tx_root, trie_dict_data = make_trie_root_and_nodes(
                 body.transactions)
             await self.chaindb.coro_persist_trie_data_dict(trie_dict_data)
             # TODO: Add transactions to canonical chain; blocked by
             # https://github.com/ethereum/py-evm/issues/337
             uncles_hash = await self.chaindb.coro_persist_uncles(
                 body.uncles)
             self._pending_bodies.pop((tx_root, uncles_hash), None)
     elif isinstance(cmd, eth.Receipts):
         msg = cast(List[List[eth.Receipt]], msg)
         self.logger.debug("Got Receipts for %d blocks", len(msg))
         for block_receipts in msg:
             receipt_root, trie_dict_data = make_trie_root_and_nodes(
                 block_receipts)
             await self.chaindb.coro_persist_trie_data_dict(trie_dict_data)
             self._pending_receipts.pop(receipt_root, None)
     elif isinstance(cmd, eth.NewBlock):
         msg = cast(Dict[str, Any], msg)
         header = msg['block'][0]
         actual_head = header.parent_hash
         actual_td = msg['total_difficulty'] - header.difficulty
         if actual_td > peer.head_td:
             peer.head_hash = actual_head
             peer.head_td = actual_td
             self._sync_requests.put_nowait(peer)
     elif isinstance(cmd, eth.Transactions):
         # TODO: Figure out what to do with those.
         pass
     elif isinstance(cmd, eth.NewBlockHashes):
         # TODO: Figure out what to do with those.
         pass
     else:
         # TODO: There are other msg types we'll want to handle here, but for now just log them
         # as a warning so we don't forget about it.
         self.logger.warn("Got unexpected msg: %s (%s)", cmd, msg)