3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2014 Thomas Voegtlin
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from util import print_error
24 from transaction import Transaction
27 class WalletSynchronizer(threading.Thread):
29 def __init__(self, wallet, network):
30 threading.Thread.__init__(self)
33 self.network = network
34 self.was_updated = True
36 self.lock = threading.Lock()
37 self.queue = Queue.Queue()
40 with self.lock: self.running = False
43 with self.lock: return self.running
46 def subscribe_to_addresses(self, addresses):
48 for addr in addresses:
49 messages.append(('blockchain.address.subscribe', [addr]))
50 self.network.subscribe( messages, lambda i,r: self.queue.put(r))
57 while self.is_running():
59 if not self.network.is_connected():
60 self.network.wait_until_connected()
65 def run_interface(self):
67 print_error("synchronizer: connected to", self.network.main_server())
71 requested_histories = {}
73 # request any missing transactions
74 for history in self.wallet.history.values():
75 if history == ['*']: continue
76 for tx_hash, tx_height in history:
77 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
78 missing_tx.append( (tx_hash, tx_height) )
81 print_error("missing tx", missing_tx)
84 self.subscribe_to_addresses(self.wallet.addresses(True))
86 while self.is_running():
87 # 1. create new addresses
88 new_addresses = self.wallet.synchronize()
90 # request missing addresses
92 self.subscribe_to_addresses(new_addresses)
94 # request missing transactions
95 for tx_hash, tx_height in missing_tx:
96 if (tx_hash, tx_height) not in requested_tx:
97 self.network.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
98 requested_tx.append( (tx_hash, tx_height) )
101 # detect if situation has changed
102 if self.network.is_up_to_date() and self.queue.empty():
103 if not self.wallet.is_up_to_date():
104 self.wallet.set_up_to_date(True)
105 self.was_updated = True
107 if self.wallet.is_up_to_date():
108 self.wallet.set_up_to_date(False)
109 self.was_updated = True
112 self.network.trigger_callback('updated')
113 self.was_updated = False
117 r = self.queue.get(block=True, timeout=1)
122 #if interface != self.network.interface:
131 result = r.get('result')
132 error = r.get('error')
137 if method == 'blockchain.address.subscribe':
139 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
140 if requested_histories.get(addr) is None:
141 self.network.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
142 requested_histories[addr] = result
144 elif method == 'blockchain.address.get_history':
146 print_error("receiving history", addr, result)
148 assert requested_histories.pop(addr) == '*'
149 self.wallet.receive_history_callback(addr, result)
152 # check that txids are unique
155 tx_hash = item['tx_hash']
156 if tx_hash not in txids:
157 txids.append(tx_hash)
158 hist.append( (tx_hash, item['height']) )
160 if len(hist) != len(result):
161 raise Exception("error: server sent history with non-unique txid", result)
163 # check that the status corresponds to what was announced
164 rs = requested_histories.pop(addr)
165 if self.wallet.get_status(hist) != rs:
166 raise Exception("error: status mismatch: %s"%addr)
168 # store received history
169 self.wallet.receive_history_callback(addr, hist)
171 # request transactions that we don't have
172 for tx_hash, tx_height in hist:
173 if self.wallet.transactions.get(tx_hash) is None:
174 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
175 missing_tx.append( (tx_hash, tx_height) )
177 elif method == 'blockchain.transaction.get':
179 tx_height = params[1]
180 assert tx_hash == bitcoin.hash_encode(bitcoin.Hash(result.decode('hex')))
181 tx = Transaction(result)
182 self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
183 self.was_updated = True
184 requested_tx.remove( (tx_hash, tx_height) )
185 print_error("received tx:", tx_hash, len(tx.raw))
188 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
190 if self.was_updated and not requested_tx:
191 self.network.trigger_callback('updated')
192 # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
193 self.network.trigger_callback("new_transaction")
194 self.was_updated = False