Browse Source

fix asyncio.iscoroutine usage

add_frost_channel_encryption
zebra-lucky 2 months ago
parent
commit
1b9eb8b1b9
  1. 23
      src/jmclient/client_protocol.py
  2. 76
      src/jmclient/taker.py
  3. 29
      src/jmclient/wallet.py
  4. 29
      src/jmclient/wallet_service.py
  5. 10
      src/jmclient/wallet_utils.py
  6. 8
      src/jmclient/yieldgenerator.py

23
src/jmclient/client_protocol.py

@ -143,10 +143,9 @@ class BIP78ClientProtocol(BaseClientProtocol):
@commands.BIP78SenderReceiveProposal.responder @commands.BIP78SenderReceiveProposal.responder
async def on_BIP78_SENDER_RECEIVE_PROPOSAL(self, psbt): async def on_BIP78_SENDER_RECEIVE_PROPOSAL(self, psbt):
if asyncio.iscoroutine(self.success_callback): cb_res = self.success_callback(psbt, self.manager)
await self.success_callback(psbt, self.manager) if asyncio.iscoroutine(cb_res):
else: cb_res = await cb_res
self.success_callback(psbt, self.manager)
return {"accepted": True} return {"accepted": True}
@commands.BIP78SenderReceiveError.responder @commands.BIP78SenderReceiveError.responder
@ -954,8 +953,8 @@ class JMTakerClientProtocol(JMClientProtocol):
jlog.info("Stall detected. Retrying transaction if possible ...") jlog.info("Stall detected. Retrying transaction if possible ...")
finished_cb_res = self.client.on_finished_callback( finished_cb_res = self.client.on_finished_callback(
False, True, 0.0) False, True, 0.0)
if asyncio.iscoroutine(self.client.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
else: else:
#This shouldn't really happen; if the tx confirmed, #This shouldn't really happen; if the tx confirmed,
#the finished callback should already be called. #the finished callback should already be called.
@ -1008,8 +1007,8 @@ class JMTakerClientProtocol(JMClientProtocol):
# the logic here is the same. # the logic here is the same.
finished_cb_res = self.client.on_finished_callback( finished_cb_res = self.client.on_finished_callback(
False, False, 0.0) False, False, 0.0)
if asyncio.iscoroutine(self.client.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
return {'accepted': False} return {'accepted': False}
else: else:
nick_list, tx = retval[1:] nick_list, tx = retval[1:]
@ -1034,16 +1033,16 @@ class JMTakerClientProtocol(JMClientProtocol):
#but is not the functionality desired in general (tumbler). #but is not the functionality desired in general (tumbler).
finished_cb_res = self.client.on_finished_callback( finished_cb_res = self.client.on_finished_callback(
False, False, 0.0) False, False, 0.0)
if asyncio.iscoroutine(self.client.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
return {'accepted': True} return {'accepted': True}
elif retval[0] == "commitment-failure": elif retval[0] == "commitment-failure":
#This case occurs if we cannot find any utxos for reasons #This case occurs if we cannot find any utxos for reasons
#other than age, which is a permanent failure #other than age, which is a permanent failure
finished_cb_res = self.client.on_finished_callback( finished_cb_res = self.client.on_finished_callback(
False, False, 0.0) False, False, 0.0)
if asyncio.iscoroutine(self.client.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
return {'accepted': True} return {'accepted': True}
amt, cmt, rev, foffers = retval[1:] amt, cmt, rev, foffers = retval[1:]
d = self.callRemote(commands.JMFill, d = self.callRemote(commands.JMFill,

76
src/jmclient/taker.py

@ -184,18 +184,18 @@ class Taker(object):
return (False,) return (False,)
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Received offers from joinmarket pit") "INFO", "Received offers from joinmarket pit")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
#choose the next item in the schedule #choose the next item in the schedule
self.schedule_index += 1 self.schedule_index += 1
if self.schedule_index == len(self.schedule): if self.schedule_index == len(self.schedule):
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Finished all scheduled transactions") "INFO", "Finished all scheduled transactions")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
finished_cb_res = self.on_finished_callback(True) finished_cb_res = self.on_finished_callback(True)
if asyncio.iscoroutine(self.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
return (False,) return (False,)
else: else:
#read the settings from the schedule entry #read the settings from the schedule entry
@ -256,8 +256,8 @@ class Taker(object):
#choose coins to spend #choose coins to spend
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Preparing bitcoin data..") "INFO", "Preparing bitcoin data..")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
if not await self.prepare_my_bitcoin_data(): if not await self.prepare_my_bitcoin_data():
return (False,) return (False,)
#Prepare a commitment #Prepare a commitment
@ -270,18 +270,18 @@ class Taker(object):
#(TODO, it's possible for user to dynamically add more coins, #(TODO, it's possible for user to dynamically add more coins,
#consider if this option means we should stay alive). #consider if this option means we should stay alive).
info_cb_res = self.taker_info_callback("ABORT", errmsg) info_cb_res = self.taker_info_callback("ABORT", errmsg)
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return ("commitment-failure",) return ("commitment-failure",)
else: else:
info_cb_res = self.taker_info_callback("INFO", errmsg) info_cb_res = self.taker_info_callback("INFO", errmsg)
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return (False,) return (False,)
else: else:
info_cb_res = self.taker_info_callback("INFO", errmsg) info_cb_res = self.taker_info_callback("INFO", errmsg)
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
#Initialization has been successful. We must set the nonrespondants #Initialization has been successful. We must set the nonrespondants
#now to keep track of what changed when we receive the utxo data #now to keep track of what changed when we receive the utxo data
@ -325,7 +325,7 @@ class Taker(object):
accepted = self.filter_orders_callback([self.orderbook, accepted = self.filter_orders_callback([self.orderbook,
self.total_cj_fee], self.total_cj_fee],
self.cjamount) self.cjamount)
if asyncio.iscoroutine(self.filter_orders_callback): if asyncio.iscoroutine(accepted):
accepted = await accepted accepted = await accepted
if accepted == "retry": if accepted == "retry":
#Special condition if Taker is "determined to continue" #Special condition if Taker is "determined to continue"
@ -361,8 +361,8 @@ class Taker(object):
except: except:
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"ABORT", "Failed to get a change address") "ABORT", "Failed to get a change address")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return False return False
#adjust the required amount upwards to anticipate an increase in #adjust the required amount upwards to anticipate an increase in
#transaction fees after re-estimation; this is sufficiently conservative #transaction fees after re-estimation; this is sufficiently conservative
@ -381,8 +381,8 @@ class Taker(object):
except Exception as e: except Exception as e:
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"ABORT", "Unable to select sufficient coins: " + repr(e)) "ABORT", "Unable to select sufficient coins: " + repr(e))
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return False return False
else: else:
#sweep #sweep
@ -423,14 +423,14 @@ class Taker(object):
if not self.orderbook: if not self.orderbook:
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"ABORT", "Could not find orders to complete transaction") "ABORT", "Could not find orders to complete transaction")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return False return False
if self.filter_orders_callback: if self.filter_orders_callback:
accepted = self.filter_orders_callback((self.orderbook, accepted = self.filter_orders_callback((self.orderbook,
self.total_cj_fee), self.total_cj_fee),
self.cjamount) self.cjamount)
if asyncio.iscoroutine(self.filter_orders_callback): if asyncio.iscoroutine(accepted):
accepted = await accepted accepted = await accepted
if not accepted: if not accepted:
return False return False
@ -482,15 +482,15 @@ class Taker(object):
"POLICY", "minimum_makers"): "POLICY", "minimum_makers"):
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Not enough counterparties, aborting.") "INFO", "Not enough counterparties, aborting.")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return (False, return (False,
"Not enough counterparties responded to fill, giving up") "Not enough counterparties responded to fill, giving up")
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Got all parts, enough to build a tx") "INFO", "Got all parts, enough to build a tx")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
#The list self.nonrespondants is now reset and #The list self.nonrespondants is now reset and
#used to track return of signatures for phase 2 #used to track return of signatures for phase 2
@ -583,8 +583,8 @@ class Taker(object):
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Built tx, sending to counterparties.") "INFO", "Built tx, sending to counterparties.")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
return (True, list(self.maker_utxo_data.keys()), return (True, list(self.maker_utxo_data.keys()),
self.latest_tx.serialize()) self.latest_tx.serialize())
@ -846,8 +846,8 @@ class Taker(object):
jlog.info('all makers have sent their signatures') jlog.info('all makers have sent their signatures')
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"INFO", "Transaction is valid, signing..") "INFO", "Transaction is valid, signing..")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
jlog.debug("schedule item was: " + str(self.schedule[self.schedule_index])) jlog.debug("schedule item was: " + str(self.schedule[self.schedule_index]))
return await self.self_sign_and_push() return await self.self_sign_and_push()
@ -1001,8 +1001,8 @@ class Taker(object):
"node. The transaction is NOT broadcast.") "node. The transaction is NOT broadcast.")
info_cb_res = self.taker_info_callback( info_cb_res = self.taker_info_callback(
"ABORT", warnmsg + "\nSee log for details.") "ABORT", warnmsg + "\nSee log for details.")
if asyncio.iscoroutine(self.taker_info_callback): if asyncio.iscoroutine(info_cb_res):
await info_cb_res info_cb_res = await info_cb_res
# warning is arguably not correct but it will stand out more: # warning is arguably not correct but it will stand out more:
jlog.warn(warnmsg) jlog.warn(warnmsg)
jlog.info(btc.human_readable_transaction(tx)) jlog.info(btc.human_readable_transaction(tx))
@ -1059,8 +1059,8 @@ class Taker(object):
pushed = self.push_ourselves() pushed = self.push_ourselves()
if not pushed: if not pushed:
finished_cb_res = self.on_finished_callback(False, fromtx=True) finished_cb_res = self.on_finished_callback(False, fromtx=True)
if asyncio.iscoroutine(self.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
else: else:
if nick_to_use: if nick_to_use:
return (nick_to_use, self.latest_tx.serialize()) return (nick_to_use, self.latest_tx.serialize())
@ -1084,8 +1084,8 @@ class Taker(object):
jlog.info("Transaction seen on network, waiting for confirmation") jlog.info("Transaction seen on network, waiting for confirmation")
#To allow client to mark transaction as "done" (e.g. by persisting state) #To allow client to mark transaction as "done" (e.g. by persisting state)
finished_cb_res= self.on_finished_callback(True, fromtx="unconfirmed") finished_cb_res= self.on_finished_callback(True, fromtx="unconfirmed")
if asyncio.iscoroutine(self.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
self.waiting_for_conf = True self.waiting_for_conf = True
confirm_timeout_sec = float(jm_single().config.get( confirm_timeout_sec = float(jm_single().config.get(
"TIMEOUT", "confirm_timeout_hours")) * 3600 "TIMEOUT", "confirm_timeout_hours")) * 3600
@ -1109,8 +1109,8 @@ class Taker(object):
waittime = self.schedule[self.schedule_index][4] waittime = self.schedule[self.schedule_index][4]
finished_cb_res = self.on_finished_callback( finished_cb_res = self.on_finished_callback(
True, fromtx=fromtx, waittime=waittime, txdetails=(txd, txid)) True, fromtx=fromtx, waittime=waittime, txdetails=(txd, txid))
if asyncio.iscoroutine(self.on_finished_callback): if asyncio.iscoroutine(finished_cb_res):
await finished_cb_res finished_cb_res = await finished_cb_res
return True return True
def _is_our_input(self, tx_input): def _is_our_input(self, tx_input):

29
src/jmclient/wallet.py

@ -2334,24 +2334,17 @@ class SNICKERWalletMixin(object):
assert unsigned_index != -1 assert unsigned_index != -1
# All validation checks passed. We now check whether the # All validation checks passed. We now check whether the
#transaction is acceptable according to the caller: #transaction is acceptable according to the caller:
if asyncio.iscoroutine(acceptance_callback): cb_res = acceptance_callback(
if not await acceptance_callback( [utx.vin[unsigned_index]],
[utx.vin[unsigned_index]], [x for i, x in enumerate(utx.vin)
[x for i, x in enumerate(utx.vin) if i != unsigned_index],
if i != unsigned_index], [utx.vout[our_output_index]],
[utx.vout[our_output_index]], [x for i, x in enumerate(utx.vout)
[x for i, x in enumerate(utx.vout) if i != our_output_index])
if i != our_output_index]): if asyncio.iscoroutine(cb_res):
return None, "Caller rejected transaction for signing." cb_res = await cb_res
else: if not cb_res:
if not acceptance_callback( return None, "Caller rejected transaction for signing."
[utx.vin[unsigned_index]],
[x for i, x in enumerate(utx.vin)
if i != unsigned_index],
[utx.vout[our_output_index]],
[x for i, x in enumerate(utx.vout)
if i != our_output_index]):
return None, "Caller rejected transaction for signing."
# Acceptance passed, prepare the deserialized tx for signing by us: # Acceptance passed, prepare the deserialized tx for signing by us:
signresult_and_signedpsbt, err = await self.sign_psbt( signresult_and_signedpsbt, err = await self.sign_psbt(

29
src/jmclient/wallet_service.py

@ -391,10 +391,9 @@ class WalletService(Service):
for f in self.callbacks["all"]: for f in self.callbacks["all"]:
# note we need no return value as we will never # note we need no return value as we will never
# remove these from the list # remove these from the list
if asyncio.iscoroutine(f): cb_res = f(txd, txid)
await f(txd, txid) if asyncio.iscoroutine(cb_res):
else: cb_res = await cb_res
f(txd, txid)
# txid is not always available at the time of callback registration. # txid is not always available at the time of callback registration.
# Migrate any callbacks registered under the provisional key, and # Migrate any callbacks registered under the provisional key, and
@ -422,12 +421,11 @@ class WalletService(Service):
if confs == 0: if confs == 0:
callbacks = [] callbacks = []
for f in self.callbacks["unconfirmed"].pop(txid, []): for f in self.callbacks["unconfirmed"].pop(txid, []):
if asyncio.iscoroutine(f): cb_res = f(txd, txid)
if not await f(txd, txid): if asyncio.iscoroutine(cb_res):
callbacks.append(f) cb_res = await cb_res
else: if not cb_res:
if not f(txd, txid): callbacks.append(f)
callbacks.append(f)
if callbacks: if callbacks:
self.callbacks["unconfirmed"][txid] = callbacks self.callbacks["unconfirmed"][txid] = callbacks
else: else:
@ -440,12 +438,11 @@ class WalletService(Service):
elif confs > 0: elif confs > 0:
callbacks = [] callbacks = []
for f in self.callbacks["confirmed"].pop(txid, []): for f in self.callbacks["confirmed"].pop(txid, []):
if asyncio.iscoroutine(f): cb_res = f(txd, txid, confs)
if not await f(txd, txid, confs): if asyncio.iscoroutine(cb_res):
callbacks.append(f) cb_res = await cb_res
else: if not cb_res:
if not f(txd, txid, confs): callbacks.append(f)
callbacks.append(f)
if callbacks: if callbacks:
self.callbacks["confirmed"][txid] = callbacks self.callbacks["confirmed"][txid] = callbacks
else: else:

10
src/jmclient/wallet_utils.py

@ -1390,12 +1390,10 @@ async def wallet_freezeutxo(wallet_service, md,
info_callback("The mixdepth: " + str(md) + \ info_callback("The mixdepth: " + str(md) + \
" contains no utxos to freeze/unfreeze.", "error") " contains no utxos to freeze/unfreeze.", "error")
return "Failed" return "Failed"
if asyncio.iscoroutine(display_callback): display_ret = display_callback(
display_ret = await display_callback(wallet_service, wallet_service, utxos_enabled, utxos_disabled)
utxos_enabled, utxos_disabled) if asyncio.iscoroutine(display_ret):
else: display_ret = await display_ret
display_ret = display_callback(wallet_service,
utxos_enabled, utxos_disabled)
if display_ret is None: if display_ret is None:
break break
if display_ret == "all": if display_ret == "all":

8
src/jmclient/yieldgenerator.py

@ -312,10 +312,10 @@ class YieldGeneratorService(Service):
# we do not catch Exceptions in setup, # we do not catch Exceptions in setup,
# deliberately; this must be caught and distinguished # deliberately; this must be caught and distinguished
# by whoever started the service. # by whoever started the service.
if asyncio.iscoroutine(setup): setup_res = setup()
raise NotImplementedError() # FIXME if asyncio.iscoroutine(setup_res):
else: raise Exception('YieldGeneratorService can not have '
setup() 'asyncio setup functions')
# TODO genericise to any YG class: # TODO genericise to any YG class:
self.yieldgen = YieldGeneratorBasic(self.wallet_service, self.yg_config) self.yieldgen = YieldGeneratorBasic(self.wallet_service, self.yg_config)

Loading…
Cancel
Save