From d0d39e534ef146639eb6c1e350fc08789ebd384c Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Thu, 17 May 2018 12:04:54 +0200 Subject: [PATCH 1/3] Make side effect explicit The removed method didn't allude to the fact it was adding the generated hash to a dictionary. This was problematic since the code base now seemingly had two methods for converting an address to a scripthash. One in the network module and one in the (logical) bitcoin module. --- lib/network.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/network.py b/lib/network.py index 10fbf2ee4..6477de82c 100644 --- a/lib/network.py +++ b/lib/network.py @@ -625,12 +625,6 @@ class Network(util.DaemonThread): # Response is now in canonical form self.process_response(interface, response, callbacks) - def addr_to_scripthash(self, addr): - h = bitcoin.address_to_scripthash(addr) - if h not in self.h2addr: - self.h2addr[h] = addr - return h - def overload_cb(self, callback): def cb2(x): x2 = x.copy() @@ -641,12 +635,14 @@ class Network(util.DaemonThread): return cb2 def subscribe_to_addresses(self, addresses, callback): - hashes = [self.addr_to_scripthash(addr) for addr in addresses] - msgs = [('blockchain.scripthash.subscribe', [x]) for x in hashes] + hash2address = {bitcoin.address_to_scripthash(address): address for address in addresses} + self.h2addr.update(hash2address) + msgs = [('blockchain.scripthash.subscribe', [x]) for x in hash2address.keys()] self.send(msgs, self.overload_cb(callback)) def request_address_history(self, address, callback): - h = self.addr_to_scripthash(address) + h = bitcoin.address_to_scripthash(address) + self.h2addr.update({h: address}) self.send([('blockchain.scripthash.get_history', [h])], self.overload_cb(callback)) def send(self, messages, callback): From 812d570ee28c3c01a4a5f1967607815d3ecb341d Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Thu, 17 May 2018 15:17:06 +0200 Subject: [PATCH 2/3] Rename method to reflect its goal Answer the question *what* problem the method solves. Not *how* it is solving it. --- lib/network.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/network.py b/lib/network.py index 6477de82c..688a3f5e1 100644 --- a/lib/network.py +++ b/lib/network.py @@ -625,7 +625,7 @@ class Network(util.DaemonThread): # Response is now in canonical form self.process_response(interface, response, callbacks) - def overload_cb(self, callback): + def map_scripthash_to_address(self, callback): def cb2(x): x2 = x.copy() p = x2.pop('params') @@ -638,12 +638,12 @@ class Network(util.DaemonThread): hash2address = {bitcoin.address_to_scripthash(address): address for address in addresses} self.h2addr.update(hash2address) msgs = [('blockchain.scripthash.subscribe', [x]) for x in hash2address.keys()] - self.send(msgs, self.overload_cb(callback)) + self.send(msgs, self.map_scripthash_to_address(callback)) def request_address_history(self, address, callback): h = bitcoin.address_to_scripthash(address) self.h2addr.update({h: address}) - self.send([('blockchain.scripthash.get_history', [h])], self.overload_cb(callback)) + self.send([('blockchain.scripthash.get_history', [h])], self.map_scripthash_to_address(callback)) def send(self, messages, callback): '''Messages is a list of (method, params) tuples''' From ab2f59590cdc7acc21224e14d88b4e37946a5138 Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Thu, 17 May 2018 15:20:22 +0200 Subject: [PATCH 3/3] Push network calls into network module Eventually all network calls should go through the network module without resorting to using raw protocol strings. This, then, makes adding an other backend easier. Please note that the behaviour slightly changed. Initially the caller received an unmodified response from the ElectrumX server. In it the scripthash is present. This change replaces that scripthash with the original address. --- lib/commands.py | 3 +-- lib/websockets.py | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/commands.py b/lib/commands.py index e9c6cf277..df66f2288 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -647,8 +647,7 @@ class Commands: util.print_error('Got Response for %s' % address) except BaseException as e: util.print_error(str(e)) - h = self.network.addr_to_scripthash(address) - self.network.send([('blockchain.scripthash.subscribe', [h])], callback) + self.network.subscribe_to_addresses([addr], callback) return True @command('wn') diff --git a/lib/websockets.py b/lib/websockets.py index 587119f97..5462dd161 100644 --- a/lib/websockets.py +++ b/lib/websockets.py @@ -84,9 +84,7 @@ class WsClientThread(util.DaemonThread): l = self.subscriptions.get(addr, []) l.append((ws, amount)) self.subscriptions[addr] = l - h = self.network.addr_to_scripthash(addr) - self.network.send([('blockchain.scripthash.subscribe', [h])], self.response_queue.put) - + self.network.subscribe_to_addresses([addr], self.response_queue.put) def run(self): threading.Thread(target=self.reading_thread).start()