make sure the broadcast tx thread always times out

This commit is contained in:
ThomasV 2016-02-25 08:55:06 +01:00
parent 463818b12d
commit 96f144eb03
6 changed files with 28 additions and 49 deletions

View File

@ -698,7 +698,7 @@ class ElectrumWindow(App):
Clock.schedule_once(lambda dt: on_success(tx)) Clock.schedule_once(lambda dt: on_success(tx))
def _broadcast_thread(self, tx, on_complete): def _broadcast_thread(self, tx, on_complete):
ok, txid = self.wallet.sendtx(tx) ok, txid = self.network.broadcast(tx)
Clock.schedule_once(lambda dt: on_complete(ok, txid)) Clock.schedule_once(lambda dt: on_complete(ok, txid))
def broadcast(self, tx, pr=None): def broadcast(self, tx, pr=None):

View File

@ -1321,21 +1321,18 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
def broadcast_thread(): def broadcast_thread():
# non-GUI thread # non-GUI thread
pr = self.payment_request pr = self.payment_request
if pr is None: if pr and pr.has_expired():
return self.wallet.sendtx(tx)
if pr.has_expired():
self.payment_request = None self.payment_request = None
return False, _("Payment request has expired") return False, _("Payment request has expired")
status, msg = self.wallet.sendtx(tx) status, msg = self.network.broadcast(tx)
if not status: if pr and status is True:
return False, msg pr.set_paid(tx.hash())
pr.set_paid(tx.hash()) self.invoices.save()
self.invoices.save() self.payment_request = None
self.payment_request = None refund_address = self.wallet.addresses()[0]
refund_address = self.wallet.addresses()[0] ack_status, ack_msg = pr.send_ack(str(tx), refund_address)
ack_status, ack_msg = pr.send_ack(str(tx), refund_address) if ack_status:
if ack_status: msg = ack_msg
msg = ack_msg
return status, msg return status, msg
# Capture current TL window; override might be removed on return # Capture current TL window; override might be removed on return

View File

@ -195,10 +195,8 @@ class ElectrumGui:
if self.str_description: if self.str_description:
self.wallet.labels[tx.hash()] = self.str_description self.wallet.labels[tx.hash()] = self.str_description
h = self.wallet.send_tx(tx)
print(_("Please wait...")) print(_("Please wait..."))
self.wallet.tx_event.wait() status, msg = self.network.broadcast(tx)
status, msg = self.wallet.receive_tx( h, tx )
if status: if status:
print(_('Payment sent.')) print(_('Payment sent.'))

View File

@ -337,10 +337,8 @@ class ElectrumGui:
if self.str_description: if self.str_description:
self.wallet.labels[tx.hash()] = self.str_description self.wallet.labels[tx.hash()] = self.str_description
h = self.wallet.send_tx(tx)
self.show_message(_("Please wait..."), getchar=False) self.show_message(_("Please wait..."), getchar=False)
self.wallet.tx_event.wait() status, msg = self.network.broadcast(tx)
status, msg = self.wallet.receive_tx( h, tx )
if status: if status:
self.show_message(_('Payment sent.')) self.show_message(_('Payment sent.'))

View File

@ -824,7 +824,21 @@ class Network(util.DaemonThread):
def synchronous_get(self, request, timeout=100000000): def synchronous_get(self, request, timeout=100000000):
queue = Queue.Queue() queue = Queue.Queue()
self.send([request], queue.put) self.send([request], queue.put)
r = queue.get(True, timeout) try:
r = queue.get(True, timeout)
except Queue.Empty:
raise BaseException('Server did not answer')
if r.get('error'): if r.get('error'):
raise BaseException(r.get('error')) raise BaseException(r.get('error'))
return r.get('result') return r.get('result')
def broadcast(self, tx, timeout=10):
tx_hash = tx.hash()
try:
r = self.synchronous_get(('blockchain.transaction.broadcast', [str(tx)]), timeout)
except BaseException as e:
return False, "error: " + str(e)
result = r.get('result')
if out != tx_hash:
return False, "error: " + out
return True, out

View File

@ -204,7 +204,6 @@ class Abstract_Wallet(PrintError):
self.up_to_date = False self.up_to_date = False
self.lock = threading.Lock() self.lock = threading.Lock()
self.transaction_lock = threading.Lock() self.transaction_lock = threading.Lock()
self.tx_event = threading.Event()
self.check_history() self.check_history()
@ -1038,33 +1037,6 @@ class Abstract_Wallet(PrintError):
if keypairs: if keypairs:
tx.sign(keypairs) tx.sign(keypairs)
def sendtx(self, tx):
# synchronous
h = self.send_tx(tx)
self.tx_event.wait()
return self.receive_tx(h, tx)
def send_tx(self, tx):
# asynchronous
self.tx_event.clear()
# fixme: this does not handle the case where server does not answer
if not self.network.interface:
raise BaseException("Not connected")
self.network.send([('blockchain.transaction.broadcast', [str(tx)])], self.on_broadcast)
return tx.hash()
def on_broadcast(self, r):
self.tx_result = r.get('result')
self.tx_event.set()
def receive_tx(self, tx_hash, tx):
out = self.tx_result
if out != tx_hash:
return False, "error: " + out
run_hook('receive_tx', tx, self)
return True, out
def update_password(self, old_password, new_password): def update_password(self, old_password, new_password):
if new_password == '': if new_password == '':
new_password = None new_password = None