electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 5e0427392f941b957f609875363c850d15f2d75c
parent 28b1569f28b2d21ebb2c428e5f1b35c6ca8a7c3b
Author: ThomasV <thomasv@electrum.org>
Date:   Thu, 15 Aug 2019 12:25:59 +0200

Daemon: Replace get_server with request.
This function performs a single RPC, and may raise a DaemonNotRunning exception.

Diffstat:
Melectrum/daemon.py | 48+++++++++++++++++++++++++++---------------------
Mrun_electrum | 23++++++++++-------------
2 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/electrum/daemon.py b/electrum/daemon.py @@ -53,6 +53,8 @@ from .logging import get_logger, Logger _logger = get_logger(__name__) +class DaemonNotRunning(Exception): + pass def get_lockfile(config: SimpleConfig): return os.path.join(config.path, 'daemon') @@ -74,35 +76,39 @@ def get_file_descriptor(config: SimpleConfig): return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644) except OSError: pass - server = get_server(config) - if server is not None: - return - # Couldn't connect; remove lockfile and try again. - remove_lockfile(lockfile) + try: + request(config, 'ping') + return None + except DaemonNotRunning: + # Couldn't connect; remove lockfile and try again. + remove_lockfile(lockfile) -def get_server(config: SimpleConfig) -> Optional[jsonrpclib.Server]: +def request(config: SimpleConfig, endpoint, *args, **kwargs): lockfile = get_lockfile(config) while True: create_time = None try: with open(lockfile) as f: (host, port), create_time = ast.literal_eval(f.read()) - rpc_user, rpc_password = get_rpc_credentials(config) - if rpc_password == '': - # authentication disabled - server_url = 'http://%s:%d' % (host, port) - else: - server_url = 'http://%s:%s@%s:%d' % ( - rpc_user, rpc_password, host, port) - server = jsonrpclib.Server(server_url) - # Test daemon is running - server.ping() - return server - except Exception as e: - _logger.info(f"failed to connect to JSON-RPC server: {e}") - if not create_time or create_time < time.time() - 1.0: - return None + rpc_user, rpc_password = get_rpc_credentials(config) + if rpc_password == '': + # authentication disabled + server_url = 'http://%s:%d' % (host, port) + else: + server_url = 'http://%s:%s@%s:%d' % ( + rpc_user, rpc_password, host, port) + except Exception: + raise DaemonNotRunning() + server = jsonrpclib.Server(server_url) + try: + # run request + f = getattr(server, endpoint) + return f(*args, **kwargs) + except ConnectionRefusedError: + _logger.info(f"failed to connect to JSON-RPC server") + if not create_time or create_time < time.time() - 1.0: + raise DaemonNotRunning() # Sleep a bit and try again; it might have just been started time.sleep(1.0) diff --git a/run_electrum b/run_electrum @@ -364,8 +364,7 @@ if __name__ == '__main__': d.init_gui(config, plugins) sys.exit(0) else: - server = daemon.get_server() - result = server.gui(config_options) + result = daemon.request(config, 'gui', config_options) elif cmdname == 'daemon': subcommand = config.get('subcommand') @@ -405,27 +404,25 @@ if __name__ == '__main__': d.join() sys.exit(0) else: - server = daemon.get_server(config) - result = server.daemon(config_options) + result = daemon.request(config, 'daemon', config_options) else: - server = daemon.get_server(config) - if server is not None: - result = server.daemon(config_options) - else: + try: + result = daemon.request(config, 'daemon', config_options) + except daemon.DaemonNotRunning: print_msg("Daemon not running") sys.exit(1) else: # command line - server = daemon.get_server(config) - init_cmdline(config_options, server) - if server is not None: - result = server.run_cmdline(config_options) - else: + try: + init_cmdline(config_options, True) + result = daemon.request(config, 'run_cmdline', config_options) + except daemon.DaemonNotRunning: cmd = known_commands[cmdname] if cmd.requires_network: print_msg("Daemon not running; try 'electrum daemon start'") sys.exit(1) else: + init_cmdline(config_options, False) plugins = init_plugins(config, 'cmdline') result = run_offline_command(config, config_options, plugins) # print result