1
0

Register loaded wallets in daemon, to prevent a wallet from being opened twice.

Simplify the wizard logic.
This commit is contained in:
ThomasV
2016-01-05 13:51:03 +01:00
parent 3d9f321cae
commit 62868ab29a
10 changed files with 78 additions and 75 deletions

View File

@@ -61,16 +61,13 @@ class RequestHandler(SimpleJSONRPCRequestHandler):
class Daemon(DaemonThread):
def __init__(self, config, network, gui=None):
def __init__(self, config, network):
DaemonThread.__init__(self)
self.config = config
self.network = network
self.gui = gui
self.gui = None
self.wallets = {}
if gui is None:
self.wallet = self.load_wallet(config)
else:
self.wallet = None
self.wallet = None
self.cmd_runner = Commands(self.config, self.wallet, self.network)
host = config.get('rpchost', 'localhost')
port = config.get('rpcport', 0)
@@ -123,22 +120,26 @@ class Daemon(DaemonThread):
response = "Error: Electrum is running in daemon mode. Please stop the daemon first."
return response
def load_wallet(self, config):
path = config.get_wallet_path()
def load_wallet(self, path, wizard=None):
if path in self.wallets:
wallet = self.wallets[path]
else:
storage = WalletStorage(path)
wallet = Wallet(storage)
wallet.start_threads(self.network)
self.wallets[path] = wallet
if wizard:
wallet = wizard.open_wallet(self.network, path)
else:
storage = WalletStorage(path)
wallet = Wallet(storage)
wallet.start_threads(self.network)
if wallet:
self.wallets[path] = wallet
return wallet
def run_cmdline(self, config_options):
config = SimpleConfig(config_options)
cmdname = config.get('cmd')
cmd = known_commands[cmdname]
wallet = self.load_wallet(config) if cmd.requires_wallet else None
path = config.get_wallet_path()
wallet = self.load_wallet(path) if cmd.requires_wallet else None
# arguments passed to function
args = map(lambda x: config.get(x), cmd.params)
# decode json arguments

View File

@@ -126,43 +126,9 @@ class WizardBase(PrintError):
"""Choose a server if one is not set in the config anyway."""
raise NotImplementedError
def open_existing_wallet(self, storage, network):
wallet = Wallet(storage)
self.update_wallet_format(wallet)
self.run_wallet_actions(wallet)
wallet.start_threads(network)
return wallet, None
def create_new_wallet(self, storage, network):
action, wallet = self.create_or_restore(storage)
self.run_wallet_actions(wallet)
if network:
self.choose_server(network)
else:
self.show_warning(_('You are offline'))
def task():
# Synchronize before starting the threads
wallet.synchronize()
wallet.start_threads(network)
# FIXME
# if action == 'create':
# msg = _('Wallet addresses generated.')
# else:
# wallet.wait_until_synchronized()
# if network:
# if wallet.is_found():
# msg = _("Recovery successful")
# else:
# msg = _("No transactions found for this seed")
# else:
# msg = _("This wallet was restored offline. It may "
# "contain more addresses than displayed.")
# self.show_message(msg)
return wallet, (MSG_GENERATING_WAIT, task)
def show_restore(self, wallet, network, action):
"""Show restore result"""
pass
def open_wallet(self, network, filename):
'''The main entry point of the wizard. Open a wallet from the given
@@ -170,22 +136,43 @@ class WizardBase(PrintError):
install wizard proper.'''
storage = WalletStorage(filename)
if storage.file_exists:
return self.open_existing_wallet(storage, network)
wallet = Wallet(storage)
self.update_wallet_format(wallet)
task = None
else:
return self.create_new_wallet(storage, network)
cr, wallet = self.create_or_restore(storage)
if not wallet:
return
task = lambda: self.show_restore(wallet, network, cr)
def run_wallet_actions(self, wallet):
if not wallet:
return
action = orig_action = wallet.get_action()
action = wallet.get_action()
requires_action = action is not None
while action:
self.run_wallet_action(wallet, action)
action = wallet.get_action()
# Save the wallet after successful completion of actions.
# It will be saved again once synchronized.
if orig_action:
if requires_action:
wallet.storage.write()
if network:
self.choose_server(network)
else:
self.show_warning(_('You are offline'))
# start wallet threads
if network:
wallet.start_threads(network)
else:
wallet.synchronize()
if task:
task()
return wallet
def run_wallet_action(self, wallet, action):
self.print_error("action %s on %s" % (action, wallet.basename()))
# Run the action on the wallet plugin, if any, then the