From e082c3c45135620c1f84703acdbe35abe728ed70 Mon Sep 17 00:00:00 2001 From: undeath Date: Sun, 27 Jun 2021 18:21:32 +0200 Subject: [PATCH] remove unneeded hex encoding/decoding from sent_tx + push_tx --- jmbase/jmbase/commands.py | 4 ++-- jmbase/test/test_commands.py | 6 +++--- jmclient/jmclient/client_protocol.py | 17 ++++++++--------- jmclient/jmclient/taker.py | 4 ++-- jmclient/test/test_client_protocol.py | 7 ++++--- jmclient/test/test_coinjoin.py | 6 +++--- jmdaemon/jmdaemon/daemon_protocol.py | 8 ++++---- jmdaemon/jmdaemon/message_channel.py | 17 ++++++++--------- jmdaemon/test/test_daemon_protocol.py | 6 +++--- jmdaemon/test/test_message_channel.py | 8 ++++---- 10 files changed, 41 insertions(+), 42 deletions(-) diff --git a/jmbase/jmbase/commands.py b/jmbase/jmbase/commands.py index 2eed32e..2d8eced 100644 --- a/jmbase/jmbase/commands.py +++ b/jmbase/jmbase/commands.py @@ -91,14 +91,14 @@ class JMMakeTx(JMCommand): to a set of counterparties """ arguments = [(b'nick_list', ListOf(Unicode())), - (b'txhex', Unicode())] + (b'tx', String())] class JMPushTx(JMCommand): """Pass a raw hex transaction to a specific counterparty (maker) for pushing (anonymity feature in JM) """ arguments = [(b'nick', Unicode()), - (b'txhex', Unicode())] + (b'tx', String())] """MAKER specific commands """ diff --git a/jmbase/test/test_commands.py b/jmbase/test/test_commands.py index d3d2863..83a5423 100644 --- a/jmbase/test/test_commands.py +++ b/jmbase/test/test_commands.py @@ -89,8 +89,8 @@ class JMTestServerProtocol(JMBaseProtocol): return {'accepted': True} @JMMakeTx.responder - def on_JM_MAKE_TX(self, nick_list, txhex): - show_receipt("JMMAKETX", nick_list, txhex) + def on_JM_MAKE_TX(self, nick_list, tx): + show_receipt("JMMAKETX", nick_list, tx) d = self.callRemote(JMSigReceived, nick="dummynick", sig="xxxsig") @@ -175,7 +175,7 @@ class JMTestClientProtocol(JMBaseProtocol): show_receipt("JMFILLRESPONSE", success, ioauth_data) d = self.callRemote(JMMakeTx, nick_list=['nick1', 'nick2', 'nick3'], - txhex="deadbeef") + tx=b"deadbeef") self.defaultCallbacks(d) return {'accepted': True} diff --git a/jmclient/jmclient/client_protocol.py b/jmclient/jmclient/client_protocol.py index 9969b73..10062b9 100644 --- a/jmclient/jmclient/client_protocol.py +++ b/jmclient/jmclient/client_protocol.py @@ -360,10 +360,10 @@ class JMClientProtocol(BaseClientProtocol): self.defaultCallbacks(d) return {'accepted': True} - def make_tx(self, nick_list, txhex): + def make_tx(self, nick_list, tx): d = self.callRemote(commands.JMMakeTx, nick_list=nick_list, - txhex=txhex) + tx=tx) self.defaultCallbacks(d) class JMMakerClientProtocol(JMClientProtocol): @@ -677,8 +677,8 @@ class JMTakerClientProtocol(JMClientProtocol): self.client.on_finished_callback(False, False, 0.0) return {'accepted': False} else: - nick_list, txhex = retval[1:] - reactor.callLater(0, self.make_tx, nick_list, txhex) + nick_list, tx = retval[1:] + reactor.callLater(0, self.make_tx, nick_list, tx) return {'accepted': True} @commands.JMOffers.responder @@ -716,17 +716,16 @@ class JMTakerClientProtocol(JMClientProtocol): def on_JM_SIG_RECEIVED(self, nick, sig): retval = self.client.on_sig(nick, sig) if retval: - nick_to_use, txhex = retval - self.push_tx(nick_to_use, txhex) + nick_to_use, tx = retval + self.push_tx(nick_to_use, tx) return {'accepted': True} def get_offers(self): d = self.callRemote(commands.JMRequestOffers) self.defaultCallbacks(d) - def push_tx(self, nick_to_push, txhex_to_push): - d = self.callRemote(commands.JMPushTx, nick=str(nick_to_push), - txhex=str(txhex_to_push)) + def push_tx(self, nick_to_push, tx): + d = self.callRemote(commands.JMPushTx, nick=str(nick_to_push), tx=tx) self.defaultCallbacks(d) class SNICKERClientProtocolFactory(protocol.ClientFactory): diff --git a/jmclient/jmclient/taker.py b/jmclient/jmclient/taker.py index a4eb2df..67af570 100644 --- a/jmclient/jmclient/taker.py +++ b/jmclient/jmclient/taker.py @@ -524,7 +524,7 @@ class Taker(object): self.taker_info_callback("INFO", "Built tx, sending to counterparties.") return (True, list(self.maker_utxo_data.keys()), - bintohex(self.latest_tx.serialize())) + self.latest_tx.serialize()) def _verify_ioauth_data(self, ioauth_data): verified_data = [] @@ -943,7 +943,7 @@ class Taker(object): self.on_finished_callback(False, fromtx=True) else: if nick_to_use: - return (nick_to_use, bintohex(self.latest_tx.serialize())) + return (nick_to_use, self.latest_tx.serialize()) #if push was not successful, return None def self_sign_and_push(self): diff --git a/jmclient/test/test_client_protocol.py b/jmclient/test/test_client_protocol.py index cb37a16..f77f446 100644 --- a/jmclient/test/test_client_protocol.py +++ b/jmclient/test/test_client_protocol.py @@ -65,7 +65,8 @@ class DummyTaker(Taker): if self.failutxos: return (False, "dummyreason") else: - return (True, [x*64 + ":01" for x in ["a", "b", "c"]], t_raw_signed_tx) + return (True, [x*64 + ":01" for x in ["a", "b", "c"]], + base64.b16decode(t_raw_signed_tx, casefold=True)) def on_sig(self, nick, sigb64): @@ -214,8 +215,8 @@ class JMTestServerProtocol(JMBaseProtocol): return {'accepted': True} @JMMakeTx.responder - def on_JM_MAKE_TX(self, nick_list, txhex): - show_receipt("JMMAKETX", nick_list, txhex) + def on_JM_MAKE_TX(self, nick_list, tx): + show_receipt("JMMAKETX", nick_list, tx) d = self.callRemote(JMSigReceived, nick="dummynick", sig="xxxsig") diff --git a/jmclient/test/test_coinjoin.py b/jmclient/test/test_coinjoin.py index 431699f..516c68b 100644 --- a/jmclient/test/test_coinjoin.py +++ b/jmclient/test/test_coinjoin.py @@ -9,7 +9,7 @@ import pytest import copy from twisted.internet import reactor -from jmbase import get_log, hextobin +from jmbase import get_log from jmclient import load_test_config, jm_single,\ YieldGeneratorBasic, Taker, LegacyWallet, SegwitLegacyWallet, SegwitWallet,\ NO_ROUNDING @@ -206,7 +206,7 @@ def test_coinjoin_mixdepth_wrap_taker(monkeypatch, tmpdir, setup_cj): taker_final_result = do_tx_signing(taker, makers, active_orders, txdata) assert taker_final_result is not False - tx = btc.CMutableTransaction.deserialize(hextobin(txdata[2])) + tx = btc.CMutableTransaction.deserialize(txdata[2]) wallet_service = wallet_services[-1] # TODO change for new tx monitoring: @@ -261,7 +261,7 @@ def test_coinjoin_mixdepth_wrap_maker(monkeypatch, tmpdir, setup_cj): taker_final_result = do_tx_signing(taker, makers, active_orders, txdata) assert taker_final_result is not False - tx = btc.CMutableTransaction.deserialize(hextobin(txdata[2])) + tx = btc.CMutableTransaction.deserialize(txdata[2]) for i in range(MAKER_NUM): wallet_service = wallet_services[i] diff --git a/jmdaemon/jmdaemon/daemon_protocol.py b/jmdaemon/jmdaemon/daemon_protocol.py index 64d3f1c..2430cd3 100644 --- a/jmdaemon/jmdaemon/daemon_protocol.py +++ b/jmdaemon/jmdaemon/daemon_protocol.py @@ -629,19 +629,19 @@ class JMDaemonServerProtocol(amp.AMP, OrderbookWatch): return {'accepted': True} @JMMakeTx.responder - def on_JM_MAKE_TX(self, nick_list, txhex): + def on_JM_MAKE_TX(self, nick_list, tx): """Taker sends the prepared unsigned transaction to all the Makers in nick_list """ if not self.jm_state == 4: log.msg("Make tx was called in wrong state, rejecting") return {'accepted': False} - self.mcc.send_tx(nick_list, txhex) + self.mcc.send_tx(nick_list, tx) return {'accepted': True} @JMPushTx.responder - def on_JM_PushTx(self, nick, txhex): - self.mcc.push_tx(nick, txhex) + def on_JM_PushTx(self, nick, tx): + self.mcc.push_tx(nick, tx) return {'accepted': True} """Maker specific responders diff --git a/jmdaemon/jmdaemon/message_channel.py b/jmdaemon/jmdaemon/message_channel.py index aa786f2..277abc0 100644 --- a/jmdaemon/jmdaemon/message_channel.py +++ b/jmdaemon/jmdaemon/message_channel.py @@ -1,7 +1,6 @@ #! /usr/bin/env python import abc import base64 -import binascii import threading from twisted.internet import reactor from jmdaemon import encrypt_encode, decode_decrypt, COMMAND_PREFIX,\ @@ -351,14 +350,14 @@ class MessageChannelCollection(object): self.prepare_privmsg(nick, "error", errormsg) @check_privmsg - def push_tx(self, nick, txhex): + def push_tx(self, nick, tx): #TODO supporting sending to arbitrary nicks #adds quite a bit of complexity, not supported #initially; will fail if nick is not part of TX - txb64 = base64.b64encode(binascii.unhexlify(txhex)).decode('ascii') + txb64 = base64.b64encode(tx).decode('ascii') self.prepare_privmsg(nick, "push", txb64) - def send_tx(self, nick_list, txhex): + def send_tx(self, nick_list, tx): """Push out the transaction to nicks in groups by their message channel. """ @@ -376,10 +375,10 @@ class MessageChannelCollection(object): else: tx_nick_sets[self.active_channels[nick]].append(nick) for mc, nl in tx_nick_sets.items(): - self.prepare_send_tx(mc, nl, txhex) + self.prepare_send_tx(mc, nl, tx) - def prepare_send_tx(self, mc, nick_list, txhex): - txb64 = base64.b64encode(binascii.unhexlify(txhex)).decode('ascii') + def prepare_send_tx(self, mc, nick_list, tx): + txb64 = base64.b64encode(tx).decode('ascii') for nick in nick_list: self.prepare_privmsg(nick, "tx", txb64, mc=mc) @@ -853,10 +852,10 @@ class MessageChannel(object): msg += ' ' + commitment self.privmsg(c, 'fill', msg) - def push_tx(self, nick, txhex): + def push_tx(self, nick, tx): #Note: not currently used; will require prepare_privmsg call so #not in this class (see send_error) - txb64 = base64.b64encode(binascii.unhexlify(txhex)).decode('ascii') + txb64 = base64.b64encode(tx).decode('ascii') self.privmsg(nick, 'push', txb64) def send_error(self, nick, errormsg): diff --git a/jmdaemon/test/test_daemon_protocol.py b/jmdaemon/test/test_daemon_protocol.py index 059cb4a..002e517 100644 --- a/jmdaemon/test/test_daemon_protocol.py +++ b/jmdaemon/test/test_daemon_protocol.py @@ -106,7 +106,7 @@ class JMTestClientProtocol(JMBaseProtocol): nl = list(ioauth_data) d = self.callRemote(JMMakeTx, nick_list=nl, - txhex="deadbeef") + tx=b"deadbeef") self.defaultCallbacks(d) @JMOffers.responder @@ -258,10 +258,10 @@ class JMDaemonTestServerProtocol(JMDaemonServerProtocol): return super().on_JM_FILL(amount, commitment, revelation, filled_offers) @JMMakeTx.responder - def on_JM_MAKE_TX(self, nick_list, txhex): + def on_JM_MAKE_TX(self, nick_list, tx): for n in nick_list: reactor.callLater(1, self.on_sig, n, "dummytxsig") - return super().on_JM_MAKE_TX(nick_list, txhex) + return super().on_JM_MAKE_TX(nick_list, tx) diff --git a/jmdaemon/test/test_message_channel.py b/jmdaemon/test/test_message_channel.py index 684128e..a9a8bfb 100644 --- a/jmdaemon/test/test_message_channel.py +++ b/jmdaemon/test/test_message_channel.py @@ -160,7 +160,7 @@ def test_setup_mc(): mcc.privmsg(cp1+"XXX", "fill", "0") #trigger check_privmsg decorator mcc.send_error(cp1, "errormsg") - mcc.push_tx(cp1, "deadbeef") + mcc.push_tx(cp1, b"deadbeef") #kill the chan on which the cp is marked active; #note dummychannel has no actual shutdown (call it anyway), #so change its status manually. @@ -215,13 +215,13 @@ def test_setup_mc(): mcc.fill_orders(new_offers, 1000, "dummypubkey", "dummycommit") #now send a dummy transaction to this same set. #first fails with no crypto box. - mcc.send_tx(cps, "deadbeef") + mcc.send_tx(cps, b"deadbeef") #Now initialize the boxes for c in cps: dummydaemon.crypto_boxes[c] = ["a", DummyBox()] - mcc.send_tx(cps, "deadbeef") + mcc.send_tx(cps, b"deadbeef") #try to send the transaction to a wrong cp: - mcc.send_tx(["notrealcp"], "deadbeef") + mcc.send_tx(["notrealcp"], b"deadbeef") #At this stage, dmcs0,2 should be "up" and 1 should be "down": assert mcc.mc_status[dmcs[0]] == 1