Exemplo n.º 1
0
    def attach(self, laddr: Union[str, Tuple]) -> None:
        """Attaches to an already running RPC client subprocess.

        Args:
            laddr: Address that the client is listening at. Can be supplied as a
                   string "http://127.0.0.1:8545" or tuple ("127.0.0.1", 8545)"""
        if self.is_active():
            raise SystemError("RPC is already active.")
        if isinstance(laddr, str):
            o = urlparse(laddr)
            if not o.port:
                raise ValueError("No RPC port given")
            laddr = (o.hostname, o.port)

        ip = socket.gethostbyname(laddr[0])
        resolved_addr = (ip, laddr[1])
        pid = self._find_rpc_process_pid(resolved_addr)

        print(
            f"Attached to local RPC client listening at '{laddr[0]}:{laddr[1]}'..."
        )
        self.process = psutil.Process(pid)

        for key, module in ATTACH_BACKENDS.items():
            if web3.clientVersion.lower().startswith(key):
                self.backend = module
                break

        web3.reset_middlewares()
        self.backend.on_connection()
        chain._network_connected()
Exemplo n.º 2
0
    def launch(self, cmd: str, **kwargs: Dict) -> None:
        if self.is_active():
            raise SystemError("RPC is already active.")

        for key, module in LAUNCH_BACKENDS.items():
            if cmd.lower().startswith(key):
                self.backend = module
                break

        self.process = self.backend.launch(cmd, **kwargs)

        # check that web3 can connect
        if not web3.provider:
            chain._network_disconnected()
            return
        uri = web3.provider.endpoint_uri if web3.provider else None
        for i in range(100):
            if web3.isConnected():
                web3.reset_middlewares()
                self.backend.on_connection()
                chain._network_connected()
                return
            time.sleep(0.1)
            if isinstance(self.process, psutil.Popen):
                self.process.poll()
            if not self.process.is_running():
                self.kill(False)
                raise RPCProcessError(cmd, uri)
        self.kill(False)
        raise RPCConnectionError(cmd, self.process, uri)