Ejemplo n.º 1
0
 async def _run(self) -> None:
     self.logger.info("Running server...")
     mapped_external_ip = None
     if self.upnp_service:
         mapped_external_ip = await self.upnp_service.add_nat_portmap()
     external_ip = mapped_external_ip or "0.0.0.0"
     await self._start_tcp_listener()
     self.logger.info(
         "this server: enode://%s@%s:%s",
         self.privkey.public_key.to_hex()[2:],
         external_ip,
         self.port,
     )
     self.logger.info("network: %s", self.network_id)
     self.logger.info("peers: max_peers=%s", self.max_peers)
     addr = Address(external_ip, self.port, self.port)
     if self.use_discv5:
         topic = self._get_discv5_topic()
         self.logger.info(
             "Using experimental v5 (topic) discovery mechanism; topic: %s", topic
         )
         discovery_proto = DiscoveryByTopicProtocol(
             topic,
             self.privkey,
             addr,
             self.bootstrap_nodes,
             self.network_id,
             self.cancel_token,
         )
     else:
         discovery_proto = PreferredNodeDiscoveryProtocol(
             self.privkey,
             addr,
             self.bootstrap_nodes,
             self.preferred_nodes,
             self.network_id,
             self.cancel_token,
         )
     self.discovery = DiscoveryService(
         discovery_proto, self.peer_pool, self.port, token=self.cancel_token
     )
     self.run_daemon(self.peer_pool)
     self.run_daemon(self.discovery)
     if self.upnp_service:
         # UPNP service is still experimental and not essential, so we don't use run_daemon() for
         # it as that means if it crashes we'd be terminated as well.
         self.run_child_service(self.upnp_service)
     self.syncer = self._make_syncer()
     await self.cancel_token.wait()
Ejemplo n.º 2
0
    async def _run(self) -> None:
        self.logger.info("Running server...")
        mapped_external_ip = None
        if self.upnp_service:
            mapped_external_ip = await self.upnp_service.add_nat_portmap()
        external_ip = mapped_external_ip or "0.0.0.0"
        await self._start_tcp_listener()
        self.logger.info(
            "this server: enode://%s@%s:%s",
            self.privkey.public_key.to_hex()[2:],
            external_ip,
            self.port,
        )
        self.logger.info("network: %s", self.network_id)
        self.logger.info("peers: max_peers=%s", self.max_peers)
        addr = Address(external_ip, self.port, self.port)
        if self.use_discv5:
            topic = self._get_discv5_topic()
            self.logger.info(
                "Using experimental v5 (topic) discovery mechanism; topic: %s",
                topic)
            discovery_proto = DiscoveryByTopicProtocol(
                topic,
                self.privkey,
                addr,
                self.bootstrap_nodes,
                self.network_id,
                self.cancel_token,
            )
        else:
            discovery_proto = PreferredNodeDiscoveryProtocol(
                self.privkey,
                addr,
                self.bootstrap_nodes,
                self.preferred_nodes,
                self.network_id,
                self.cancel_token,
            )
        routing_table_path = self.crawling_routing_table_path
        if routing_table_path is not None:
            assert (
                platform.python_implementation() == "PyPy"
            ), "pytho3.6 on linux doesn't support pickling module objects"
            self.discovery = CrawlingService(discovery_proto,
                                             self.port,
                                             token=self.cancel_token)
            # hack: replace routing table
            if (os.path.exists(routing_table_path)
                    and os.path.getsize(routing_table_path) > 0):
                with open(routing_table_path, "rb") as f:
                    discovery_proto.routing = pickle.load(f)
                    discovery_proto.routing.this_node = discovery_proto.this_node
                    assert isinstance(discovery_proto.routing, RoutingTable)

            def persist_routing_table(_):
                with open(routing_table_path, "wb") as f:
                    pickle.dump(discovery_proto.routing, f)

            self.discovery.add_finished_callback(persist_routing_table)
            # no need to run peer pool as daemon
        else:
            self.discovery = DiscoveryService(discovery_proto,
                                              self.peer_pool,
                                              self.port,
                                              token=self.cancel_token)
            self.run_daemon(self.peer_pool)

        self.run_daemon(self.discovery)
        if self.upnp_service:
            # UPNP service is still experimental and not essential, so we don't use run_daemon() for
            # it as that means if it crashes we'd be terminated as well.
            self.run_child_service(self.upnp_service)
        self.syncer = self._make_syncer()
        await self.cancel_token.wait()