class WalletRPCManager(ProcessManager): def __init__(self, resources_path, wallet_file_path, wallet_password, app, log_level=2): self.user_agent = str(uuid4().hex) wallet_log_path = os.path.join(os.path.dirname(wallet_file_path), "electroneum-wallet-rpc.log") wallet_rpc_args = u'%s/bin/electroneum-wallet-rpc --disable-rpc-login --wallet-file %s --log-file %s --rpc-bind-port %d --log-level %d --daemon-port %d --password %s' % ( resources_path, wallet_file_path, wallet_log_path, RPC_DAEMON_PORT + 2, log_level, RPC_DAEMON_PORT, wallet_password) ProcessManager.__init__(self, wallet_rpc_args, "electroneum-wallet-rpc") self.rpc_request = WalletRPCRequest(app, self.user_agent) self.ready = False self.block_hex = None self.block_height = 0 self.is_password_invalid = Event() def run(self): is_ready_str = "Run net_service loop" err_str = "ERROR" invalid_password = "******" height_regex = re.compile( r"Processed block: \<([a-z0-9]+)\>, height (\d+)") for line in iter(self.proc.stdout.readline, b''): if not self.ready and is_ready_str in line: self.ready = True log("Wallet RPC ready!", LEVEL_INFO, self.proc_name) elif err_str in line: self.last_error = line.rstrip() log("[%s]>>> %s" % (self.proc_name, line.rstrip()), LEVEL_ERROR, self.proc_name) if not self.is_password_invalid.is_set( ) and invalid_password in self.last_error: self.is_password_invalid.set() else: log("[%s]>>> %s" % (self.proc_name, line.rstrip()), LEVEL_DEBUG, self.proc_name) m_height = height_regex.search(line) if m_height: self.block_hex = m_height.group(1) self.block_height = m_height.group(2) if not self.proc.stdout.closed: self.proc.stdout.close() def is_ready(self): return self.ready def is_invalid_password(self): return self.is_password_invalid.is_set() def stop(self): self.rpc_request.stop_wallet() if self.is_proc_running(): counter = 0 while True: if self.is_proc_running(): if counter < 5: sleep(1) counter += 1 else: self.proc.kill() log("[%s] killed" % self.proc_name, LEVEL_INFO, self.proc_name) break else: break self.ready = False log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)
class WalletRPCManager(ProcessManager): def __init__(self, resources_path, wallet_dir_path, app, log_level=1): self.rpc_user_name = str(uuid4().hex)[:12] self.rpc_password = str(uuid4().hex)[:12] wallet_log_dir_path = os.path.abspath(os.path.join(wallet_dir_path, ".." , "logs", "sumo-wallet-rpc-bin.log")) exec_path = u'%s/bin/sumo-wallet-rpc' % resources_path wallet_rpc_args = u'--rpc-bind-port=19738 --wallet-dir=%s --log-file=%s --rpc-login=%s:%s --log-level=%d' \ % (wallet_dir_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level) super(WalletRPCManager, self).__init__(exec_path, wallet_rpc_args, "sumo-wallet-rpc") self.rpc_request = WalletRPCRequest(app, self.rpc_user_name, self.rpc_password) self._ready = False self._block_height = 0 self._block_hash = None self._stopped = False def run(self): rpc_ready_strs = ["Binding on 127.0.0.1", "Starting wallet rpc server", "Run net_service loop", "Refresh done"] err_str = "ERROR" height_regex = re.compile(r"Processed block: \<([a-z0-9]+)\>, height (\d+)") for line in iter(self.proc.stdout.readline, b''): m_height = height_regex.search(line) if m_height: self._block_hash = m_height.group(1) self._block_height = m_height.group(2) self._ready = False if not self._ready and any(s in line for s in rpc_ready_strs): self._ready = True log("[%s] Ready!" % self.proc_name, LEVEL_INFO, self.proc_name) if err_str in line: self.last_error = line.rstrip() log("[%s] %s" % (self.proc_name, line.rstrip()), LEVEL_ERROR, self.proc_name) elif m_height: log("[%s] %s" % (self.proc_name, line.rstrip()), LEVEL_DEBUG, self.proc_name) else: log("[%s] %s" % (self.proc_name, line.rstrip()), LEVEL_DEBUG, self.proc_name) if self._stopped: break # if not self.proc.stdout.closed: # self.proc.stdout.close() def is_ready(self): return self._ready def set_ready(self, status): self._ready = status def get_block_height(self): return int(self._block_height) def get_block_hash(self): return self._block_hash def reset_block_height(self): self._block_height = 0 def stop(self): self._stopped = True self.rpc_request.stop_wallet(no_wait=True) sleep(2) counter = 1 while self.is_proc_running(): try: if counter < 30: if counter % 5 == 0: if hasattr(signal, 'CTRL_C_EVENT'): try: os.kill(self.proc.pid, signal.CTRL_C_EVENT) except KeyboardInterrupt: pass else: os.kill(self.proc.pid, signal.SIGINT) sleep(1) counter += 1 else: self.proc.kill() log("[%s] killed" % self.proc_name, LEVEL_INFO, self.proc_name) break except: break log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)
class WalletRPCManager(ProcessManager): def __init__(self, resources_path, wallet_file_path, wallet_password, app, log_level=1, enable_ssl=False): self.user_agent = str(uuid4().hex) wallet_log_path = os.path.join(os.path.dirname(wallet_file_path), "solace-wallet-rpc.log") if enable_ssl: wallet_rpc_args = u'%s/bin/solace-wallet-rpc --daemon-address %s --wallet-file %s --log-file %s --rpc-bind-port %d --user-agent %s --log-level %d --enable-ssl --cacerts-path %s' \ % (resources_path, REMOTE_DAEMON_SSL_ADDRESS, wallet_file_path, wallet_log_path, WALLET_RPC_PORT_SSL, self.user_agent, log_level, CA_CERTS_PATH) else: wallet_rpc_args = u'%s/bin/solace-wallet-rpc --daemon-address %s --wallet-file %s --log-file %s --rpc-bind-port %d --user-agent %s --log-level %d' \ % (resources_path, REMOTE_DAEMON_ADDRESS, wallet_file_path, wallet_log_path, WALLET_RPC_PORT, self.user_agent, log_level) ProcessManager.__init__(self, wallet_rpc_args, "solace-wallet-rpc") sleep(0.2) self.send_command(wallet_password) self.rpc_request = WalletRPCRequest(app, self.user_agent, enable_ssl) # self.rpc_request.start() self._stopped = False self._ready = Event() self.block_height = 0 self.is_password_invalid = Event() self.last_log_lines = [] self.last_error = "" def run(self): rpc_ready_strs = ["Binding on 127.0.0.1:%d" % WALLET_RPC_PORT, "Starting wallet rpc server", "Run net_service loop", "Refresh done"] err_str = "ERROR" invalid_password_str = "invalid password" height_regex = re.compile(r"Processed block: \<([a-z0-9]+)\>, height (\d+)") height_regex2 = re.compile(r"Skipped block by height: (\d+)") height_regex3 = re.compile(r"Skipped block by timestamp, height: (\d+)") for line in iter(self.proc.stdout.readline, b''): if self._stopped: break m_height = height_regex.search(line) if m_height: self.block_height = m_height.group(2) if not m_height: m_height = height_regex2.search(line) if m_height: self.block_height = m_height.group(1) if not m_height: m_height = height_regex3.search(line) if m_height: self.block_height = m_height.group(1) if not self._ready.is_set() and any(s in line for s in rpc_ready_strs): self._ready.set() log("RPC server ready!", LEVEL_INFO, self.proc_name) if err_str in line: self.last_error = line.rstrip() if not self.is_password_invalid.is_set() and invalid_password_str in line: self.is_password_invalid.set() log("ERROR: Invalid wallet password", LEVEL_ERROR, self.proc_name) else: log(self.last_error, LEVEL_ERROR, self.proc_name) elif m_height: log(line.rstrip(), LEVEL_INFO, self.proc_name) else: log(line.rstrip(), LEVEL_DEBUG, self.proc_name) if len(self.last_log_lines) > 1: self.last_log_lines.pop(0) self.last_log_lines.append(line[:120]) if not self.proc.stdout.closed: self.proc.stdout.close() def is_ready(self): return self._ready.is_set() def is_invalid_password(self): return self.is_password_invalid.is_set() def stop(self, force=False): if not force: self.rpc_request.stop_wallet() if self.is_proc_running(): counter = 0 while True: if self.is_proc_running(): if counter < 60: sleep(1) counter += 1 else: self.proc.kill() log("[%s] killed" % self.proc_name, LEVEL_INFO, self.proc_name) break else: break self._stopped = True self._ready = Event() self.block_height = 0 self.is_password_invalid = Event() self.last_log_lines = [] self.last_error = "" log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)
class WalletRPCManager(ProcessManager): def __init__(self, resources_path, wallet_file_path, wallet_password, app, log_level=1): self.rpc_user_name = str(uuid4().hex)[:12] self.rpc_password = str(uuid4().hex)[:12] wallet_log_dir_path = os.path.join(os.path.dirname(wallet_file_path), "..", "logs", "sumo-wallet-rpc-bin.log") wallet_rpc_args = u'%s/bin/sumo-wallet-rpc --wallet-file %s --log-file %s --rpc-bind-port 19738 --rpc-login %s:%s --prompt-for-password --log-level %d' \ % (resources_path, wallet_file_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level) ProcessManager.__init__(self, wallet_rpc_args, "sumo-wallet-rpc") sleep(0.2) self.send_command(wallet_password) self.rpc_request = WalletRPCRequest(app, self.rpc_user_name, self.rpc_password) # self.rpc_request.start() self._ready = Event() self.block_height = 0 self.is_password_invalid = Event() self.last_log_lines = [] def run(self): rpc_ready_strs = [ "Binding on 127.0.0.1", "Starting wallet rpc server", "Run net_service loop", "Refresh done" ] err_str = "ERROR" invalid_password_str = "invalid password" height_regex = re.compile( r"Processed block: \<([a-z0-9]+)\>, height (\d+)") height_regex2 = re.compile(r"Skipped block by height: (\d+)") height_regex3 = re.compile( r"Skipped block by timestamp, height: (\d+)") for line in iter(self.proc.stdout.readline, b''): m_height = height_regex.search(line) if m_height: self.block_height = m_height.group(2) if not m_height: m_height = height_regex2.search(line) if m_height: self.block_height = m_height.group(1) if not m_height: m_height = height_regex3.search(line) if m_height: self.block_height = m_height.group(1) if not self._ready.is_set() and any(s in line for s in rpc_ready_strs): self._ready.set() log("RPC server ready!", LEVEL_INFO, self.proc_name) if err_str in line: self.last_error = line.rstrip() log(line.rstrip(), LEVEL_ERROR, self.proc_name) if not self.is_password_invalid.is_set( ) and invalid_password_str in self.last_error: self.is_password_invalid.set() elif m_height: log(line.rstrip(), LEVEL_INFO, self.proc_name) else: log(line.rstrip(), LEVEL_DEBUG, self.proc_name) if len(self.last_log_lines) > 1: self.last_log_lines.pop(0) self.last_log_lines.append(line.rstrip()) if not self.proc.stdout.closed: self.proc.stdout.close() def is_ready(self): return self._ready.is_set() def is_invalid_password(self): return self.is_password_invalid.is_set() def stop(self): self.rpc_request.stop_wallet() if self.is_proc_running(): counter = 0 while True: if self.is_proc_running(): if counter < 5: sleep(1) counter += 1 else: self.proc.kill() log("[%s] killed" % self.proc_name, LEVEL_INFO, self.proc_name) break else: break self._ready = Event() log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)
class WalletRPCManager(ProcessManager): def __init__(self, resources_path, wallet_file_path, wallet_password, app, log_level=1): self.user_agent = str(uuid4().hex) wallet_log_path = os.path.join(os.path.dirname(wallet_file_path), "wownero-wallet-rpc.log") wallet_rpc_args = [ r'%s/bin/wownero-wallet-rpc' % resources_path, '--wallet-file', wallet_file_path, '--log-file', wallet_log_path, '--disable-rpc-login', '--trusted-daemon', '--rpc-bind-port', '34578', '--prompt-for-password', '--log-level', str(log_level), ] ProcessManager.__init__(self, wallet_rpc_args, "wownero-wallet-rpc") sleep(0.2) self.send_command(wallet_password) self.rpc_request = WalletRPCRequest(app, self.user_agent) # self.rpc_request.start() self._ready = False self.block_height = 0 self.is_password_invalid = Event() self.last_log_lines = [] def run(self): is_ready_str = "Starting wallet RPC server" err_str = "ERROR" invalid_password = "******" height_regex = re.compile( r"Processed block: \<([a-z0-9]+)\>, height (\d+)") height_regex2 = re.compile(r"Skipped block by height: (\d+)") height_regex3 = re.compile( r"Skipped block by timestamp, height: (\d+)") for line in iter(self.proc.stdout.readline, b''): m_height = height_regex.search(line) if m_height: self.block_height = m_height.group(2) if not m_height: m_height = height_regex2.search(line) if m_height: self.block_height = m_height.group(1) if not m_height: m_height = height_regex3.search(line) if m_height: self.block_height = m_height.group(1) if not self._ready and is_ready_str in line: self._ready = True log(line.rstrip(), LEVEL_INFO, self.proc_name) elif err_str in line: self.last_error = line.rstrip() log(line.rstrip(), LEVEL_ERROR, self.proc_name) if not self.is_password_invalid.is_set( ) and invalid_password in self.last_error: self.is_password_invalid.set() elif m_height: log(line.rstrip(), LEVEL_INFO, self.proc_name) else: log(line.rstrip(), LEVEL_DEBUG, self.proc_name) if len(self.last_log_lines) > 1: self.last_log_lines.pop(0) self.last_log_lines.append(line.rstrip()) if not self.proc.stdout.closed: self.proc.stdout.close() def is_ready(self): return self._ready def is_invalid_password(self): return self.is_password_invalid.is_set() def stop(self): self.rpc_request.stop_wallet() if self.is_proc_running(): counter = 0 while True: if self.is_proc_running(): if counter < 5: sleep(1) counter += 1 else: self.proc.kill() log("[%s] killed" % self.proc_name, LEVEL_INFO, self.proc_name) break else: break self._ready = False log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)