Ejemplo n.º 1
0
    async def _handle_new_block(self, sender: SessionAPI,
                                payload: NewBlockPayload) -> None:
        header = payload.block.header
        sender_peer = ETHProxyPeer.from_session(
            sender, self._event_bus, TO_NETWORKING_BROADCAST_CONFIG)
        sender_peer_str = str(sender_peer)

        # Add peer to tracker if we've seen this block before
        if header.hash in self._peer_block_tracker:
            if sender_peer_str not in self._peer_block_tracker[header.hash]:
                self._peer_block_tracker[header.hash].append(sender_peer_str)
        else:
            # Verify the validity of block, add to tracker and broadcast to eligible peers
            with get_eth1_chain_with_remote_db(self._boot_info,
                                               self._event_bus) as chain:
                try:
                    chain.validate_seal(header)
                except ValidationError as exc:
                    self.logger.info(
                        "Received invalid block from peer: %s. %s",
                        sender_peer_str,
                        exc,
                    )
                else:
                    self.manager.run_task(self._fetch_witnesses, sender_peer,
                                          header.hash, header.block_number)
                    self._peer_block_tracker[header.hash] = [sender_peer_str]
                    # Here we only broadcast a NewBlock msg to a subset of our peers, and once the
                    # block is imported into our chain a NewBlockImported event will be generated
                    # and we'll announce it to the remaining ones, as per the spec.
                    await self._broadcast_new_block(payload.block,
                                                    payload.total_difficulty)
Ejemplo n.º 2
0
 async def _handle_msg(self, session: SessionAPI,
                       cmd: CommandAPI[Any]) -> None:
     peer = ETHProxyPeer.from_session(session, self.event_bus,
                                      self.broadcast_config)
     if isinstance(cmd, GetBlockWitnessHashes):
         await self.handle_get_block_witnesses(peer, cmd)
     else:
         self.logger.warning("Unexpected command type: %s", cmd)
Ejemplo n.º 3
0
 async def _handle_new_block_hashes(self) -> None:
     async for event in self._event_bus.stream(NewBlockHashesEvent):
         sender_peer = ETHProxyPeer.from_session(
             event.session, self._event_bus, TO_NETWORKING_BROADCAST_CONFIG)
         for new_block_hash in event.command.payload:
             self.manager.run_task(self._fetch_witnesses, sender_peer,
                                   new_block_hash.hash,
                                   new_block_hash.number)
Ejemplo n.º 4
0
    async def _handle_new_block(self, sender: SessionAPI,
                                payload: NewBlockPayload) -> None:
        header = payload.block.header
        sender_peer = ETHProxyPeer.from_session(
            sender, self._event_bus, TO_NETWORKING_BROADCAST_CONFIG)
        sender_peer_str = str(sender_peer)
        self.logger.debug("Received NewBlock from %s: %s", sender, header)

        # Add peer to tracker if we've seen this block before
        if header.hash in self._peer_block_tracker:
            if sender_peer_str not in self._peer_block_tracker[header.hash]:
                self._peer_block_tracker[header.hash].append(sender_peer_str)
        else:
            # Verify the validity of block, add to tracker and broadcast to eligible peers
            with get_eth1_chain_with_remote_db(self._boot_info,
                                               self._event_bus) as chain:
                try:
                    chain.validate_seal(header)
                except ValidationError as exc:
                    self.logger.info(
                        "Received invalid block from peer: %s. %s",
                        sender_peer_str,
                        exc,
                    )
                else:
                    try:
                        with trio.fail_after(5):
                            # Sometimes we get a NewBlock/NewBlockHashes msg before the BeamSyncer
                            # service has started, and there will be no subscribers to
                            # FetchBlockWitness in that case. This ensures we wait for it to start
                            # before attempting to fire CollectMissingTrieNodes events.
                            await self._event_bus.wait_until_any_endpoint_subscribed_to(
                                FetchBlockWitness)
                    except trio.TooSlowError:
                        self.logger.warning(
                            "No subscribers for FetchBlockWitness, couldn't feth witness for %s",
                            header,
                        )
                    else:
                        self.manager.run_task(
                            self._event_bus.request,
                            FetchBlockWitness(sender, header.hash,
                                              header.block_number),
                        )
                    self._peer_block_tracker[header.hash] = [sender_peer_str]
                    # Here we only broadcast a NewBlock msg to a subset of our peers, and once the
                    # block is imported into our chain a NewBlockImported event will be generated
                    # and we'll announce it to the remaining ones, as per the spec.
                    await self._broadcast_new_block(payload.block,
                                                    payload.total_difficulty)
Ejemplo n.º 5
0
    async def _handle_msg(self,
                          session: SessionAPI,
                          cmd: CommandAPI[Any]) -> None:

        self.logger.debug2("Peer %s requested %s", session, cmd)
        peer = ETHProxyPeer.from_session(session, self.event_bus, self.broadcast_config)

        if isinstance(cmd, commands.GetBlockHeadersV65):
            await self._handler.handle_get_block_headers(peer, cmd)
        elif isinstance(cmd, commands.GetBlockBodiesV65):
            await self._handler.handle_get_block_bodies(peer, cmd)
        elif isinstance(cmd, commands.GetReceiptsV65):
            await self._handler.handle_get_receipts(peer, cmd)
        elif isinstance(cmd, commands.GetNodeDataV65):
            await self._handler.handle_get_node_data(peer, cmd)
        else:
            self.logger.debug("%s msg not handled yet, needs to be implemented", cmd)
Ejemplo n.º 6
0
    async def _handle_msg(self,
                          remote: Node,
                          cmd: Command,
                          msg: protocol._DecodedMsgType) -> None:

        self.logger.debug2("Peer %s requested %s", remote, cmd)
        peer = ETHProxyPeer.from_node(remote, self.event_bus, self.broadcast_config)

        if isinstance(cmd, commands.GetBlockHeaders):
            await self._handler.handle_get_block_headers(peer, cast(Dict[str, Any], msg))
        elif isinstance(cmd, commands.GetBlockBodies):
            block_hashes = cast(Sequence[Hash32], msg)
            await self._handler.handle_get_block_bodies(peer, block_hashes)
        elif isinstance(cmd, commands.GetReceipts):
            block_hashes = cast(Sequence[Hash32], msg)
            await self._handler.handle_get_receipts(peer, block_hashes)
        elif isinstance(cmd, commands.GetNodeData):
            node_hashes = cast(Sequence[Hash32], msg)
            await self._handler.handle_get_node_data(peer, node_hashes)
        else:
            self.logger.debug("%s msg not handled yet, needs to be implemented", cmd)
Ejemplo n.º 7
0
    async def _handle_new_block(self, sender: SessionAPI,
                                block: NewBlockPayload) -> None:
        header = block.block.header
        sender_peer_str = str(
            ETHProxyPeer.from_session(sender, self._event_bus,
                                      TO_NETWORKING_BROADCAST_CONFIG))

        # Add peer to tracker if we've seen this block before
        if header.hash in self._peer_block_tracker:
            if sender_peer_str not in self._peer_block_tracker[header.hash]:
                self._peer_block_tracker[header.hash].append(sender_peer_str)
        else:
            # Verify the validity of block, add to tracker and broadcast to eligible peers
            try:
                check_pow(header.block_number, header.mining_hash,
                          header.mix_hash, header.nonce, header.difficulty)
            except ValidationError:
                self.logger.info("Received invalid block from peer: %s",
                                 sender_peer_str)
            else:
                self._peer_block_tracker[header.hash] = [sender_peer_str]
                await self._broadcast_newly_seen_block(header)