broadcast_transaction: introduce async variant

This commit is contained in:
SomberNight 2018-09-10 18:39:10 +02:00
parent b279d351d8
commit e2338581eb
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
6 changed files with 12 additions and 8 deletions

View file

@ -255,7 +255,7 @@ class Commands:
def broadcast(self, tx): def broadcast(self, tx):
"""Broadcast a transaction to the network. """ """Broadcast a transaction to the network. """
tx = Transaction(tx) tx = Transaction(tx)
return self.network.broadcast_transaction(tx) return self.network.broadcast_transaction_from_non_network_thread(tx)
@command('') @command('')
def createmultisig(self, num, pubkeys): def createmultisig(self, num, pubkeys):

View file

@ -880,7 +880,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.network.broadcast_transaction(tx) ok, txid = self.network.broadcast_transaction_from_non_network_thread(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

@ -1616,7 +1616,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
if pr and pr.has_expired(): if pr and 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.network.broadcast_transaction(tx) status, msg = self.network.broadcast_transaction_from_non_network_thread(tx)
if pr and status is True: if pr and status is True:
self.invoices.set_paid(pr, tx.txid()) self.invoices.set_paid(pr, tx.txid())
self.invoices.save() self.invoices.save()

View file

@ -200,7 +200,7 @@ class ElectrumGui:
self.wallet.labels[tx.txid()] = self.str_description self.wallet.labels[tx.txid()] = self.str_description
print(_("Please wait...")) print(_("Please wait..."))
status, msg = self.network.broadcast_transaction(tx) status, msg = self.network.broadcast_transaction_from_non_network_thread(tx)
if status: if status:
print(_('Payment sent.')) print(_('Payment sent.'))

View file

@ -354,7 +354,7 @@ class ElectrumGui:
self.wallet.labels[tx.txid()] = self.str_description self.wallet.labels[tx.txid()] = self.str_description
self.show_message(_("Please wait..."), getchar=False) self.show_message(_("Please wait..."), getchar=False)
status, msg = self.network.broadcast_transaction(tx) status, msg = self.network.broadcast_transaction_from_non_network_thread(tx)
if status: if status:
self.show_message(_('Payment sent.')) self.show_message(_('Payment sent.'))

View file

@ -671,10 +671,14 @@ class Network(PrintError):
async def get_merkle_for_transaction(self, tx_hash, tx_height): async def get_merkle_for_transaction(self, tx_hash, tx_height):
return await self.interface.session.send_request('blockchain.transaction.get_merkle', [tx_hash, tx_height]) return await self.interface.session.send_request('blockchain.transaction.get_merkle', [tx_hash, tx_height])
def broadcast_transaction(self, tx, timeout=5): def broadcast_transaction_from_non_network_thread(self, tx, timeout=10):
fut = asyncio.run_coroutine_threadsafe(self.interface.session.send_request('blockchain.transaction.broadcast', [str(tx)]), self.asyncio_loop) # note: calling this from the network thread will deadlock it
fut = asyncio.run_coroutine_threadsafe(self.broadcast_transaction(tx, timeout=timeout), self.asyncio_loop)
return fut.result()
async def broadcast_transaction(self, tx, timeout=10):
try: try:
out = fut.result(timeout) out = await self.interface.session.send_request('blockchain.transaction.broadcast', [str(tx)], timeout=timeout)
except asyncio.TimeoutError as e: except asyncio.TimeoutError as e:
return False, "error: operation timed out" return False, "error: operation timed out"
except Exception as e: except Exception as e: