1
0

TrezorClient: should be in a separate thread

First steps; get show_address working.
Client is not responsible for showing exceptions.
Suppress uninteresting exceptions.
This commit is contained in:
Neil Booth
2016-01-17 19:38:32 +09:00
parent c99f0acfba
commit 06c262d0dc
5 changed files with 33 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
from sys import stderr
from electrum.i18n import _
from electrum.util import PrintError
from electrum.util import PrintError, SilentException
class GuiMixin(object):
@@ -20,6 +20,16 @@ class GuiMixin(object):
'passphrase': _("Confirm on %s device to continue"),
}
def callback_Failure(self, msg):
# BaseClient's unfortunate call() implementation forces us to
# raise exceptions on failure in order to unwind the stack.
# However, making the user acknowledge they cancelled
# gets old very quickly, so we suppress those.
if msg.code in (self.types.Failure_PinCancelled,
self.types.Failure_ActionCancelled):
raise SilentException()
raise RuntimeError(msg.message)
def callback_ButtonRequest(self, msg):
msg_code = self.msg_code_override or msg.code
message = self.messages.get(msg_code, self.messages['default'])
@@ -65,6 +75,7 @@ class TrezorClientBase(GuiMixin, PrintError):
self.handler = handler
self.hid_id_ = hid_id
self.tx_api = plugin
self.types = plugin.types
self.msg_code_override = None
def __str__(self):
@@ -172,9 +183,6 @@ class TrezorClientBase(GuiMixin, PrintError):
def wrapped(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except BaseException as e:
self.handler.show_error(str(e))
raise e
finally:
self.handler.finished()

View File

@@ -1,5 +1,6 @@
import base64
import re
import threading
import time
from binascii import unhexlify
@@ -172,6 +173,7 @@ class TrezorCompatiblePlugin(BasePlugin, ThreadJob):
def __init__(self, parent, config, name):
BasePlugin.__init__(self, parent, config, name)
self.main_thread = threading.current_thread()
self.device = self.wallet_class.device
self.wallet_class.plugin = self
self.prevent_timeout = time.time() + 3600 * 24 * 365
@@ -216,6 +218,8 @@ class TrezorCompatiblePlugin(BasePlugin, ThreadJob):
return self.client_class(transport, handler, self, hid_id)
def get_client(self, wallet, force_pair=True, check_firmware=True):
assert self.main_thread != threading.current_thread()
'''check_firmware is ignored unless force_pair is True.'''
client = self.device_manager().get_client(wallet, force_pair)

View File

@@ -231,7 +231,7 @@ def qt_plugin_class(base_plugin_class):
window.statusBar().addPermanentWidget(window.tzb)
wallet.handler = self.create_handler(window)
# Trigger a pairing
self.get_client(wallet)
wallet.thread.add(partial(self.get_client, wallet))
def on_create_wallet(self, wallet, wizard):
assert type(wallet) == self.wallet_class
@@ -242,8 +242,9 @@ def qt_plugin_class(base_plugin_class):
@hook
def receive_menu(self, menu, addrs, wallet):
if type(wallet) == self.wallet_class and len(addrs) == 1:
menu.addAction(_("Show on %s") % self.device,
lambda: self.show_address(wallet, addrs[0]))
def show_address():
wallet.thread.add(partial(self.show_address, wallet, addrs[0]))
menu.addAction(_("Show on %s") % self.device, show_address)
def settings_dialog(self, window):
hid_id = self.choose_device(window)