self.daemon = True
self.config = config
self.interface = interface
- self.transactions = [] # monitored transactions
+ self.transactions = {} # monitored transactions
self.interface.register_channel('verifier')
self.verified_tx = config.get('verified_tx',{}) # height of verified tx
def get_confirmations(self, tx):
""" return the number of confirmations of a monitored transaction. """
with self.lock:
- if tx in self.transactions:
+ if tx in self.transactions.keys():
return (self.local_height - self.verified_tx[tx] + 1) if tx in self.verified_tx else 0
else:
return 0
- def add(self, tx_hash):
+ def add(self, tx_hash, tx_height):
""" add a transaction to the list of monitored transactions. """
with self.lock:
- if tx_hash not in self.transactions:
- self.transactions.append(tx_hash)
+ if tx_hash not in self.transactions.keys():
+ self.transactions[tx_hash] = tx_height
def run(self):
requested_merkle = []
# request missing tx
if all_chunks:
- for tx_hash in self.transactions:
+ for tx_hash, tx_height in self.transactions.items():
if tx_hash not in self.verified_tx:
if self.merkle_roots.get(tx_hash) is None and tx_hash not in requested_merkle:
print_error('requesting merkle', tx_hash)
- self.interface.send([ ('blockchain.transaction.get_merkle',[tx_hash]) ], 'verifier')
+ self.interface.send([ ('blockchain.transaction.get_merkle',[tx_hash, tx_height]) ], 'verifier')
requested_merkle.append(tx_hash)
# process pending headers
addr = item.get('address')
if addr in addresses:
key = item['prevout_hash'] + ':%d'%item['prevout_n']
- value = self.prevout_values[ key ]
+ value = self.prevout_values.get( key )
+ if value is None: continue
v -= value
for item in d.get('outputs'):
addr = item.get('address')
def get_addr_balance(self, addr):
assert self.is_mine(addr)
h = self.history.get(addr,[])
+ if h == ['*']: return 0,0
c = u = 0
for tx_hash, tx_height in h:
v = self.get_tx_value(tx_hash, [addr])
def get_status(self, h):
if not h: return None
+ if h == ['*']: return '*'
status = ''
for tx_hash, height in h:
status += tx_hash + ':%d:' % height
def receive_history_callback(self, addr, hist):
- if not self.check_new_history(addr, hist):
- raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
+ if hist != ['*']:
+ if not self.check_new_history(addr, hist):
+ raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
with self.lock:
self.history[addr] = hist
self.save()
+
+ if hist != ['*']:
for tx_hash, tx_height in hist:
if tx_height>0:
- self.verifier.add(tx_hash)
-
+ self.verifier.add(tx_hash, tx_height)
def get_tx_history(self):
# set the timestamp for transactions that need it
for hist in self.history.values():
+ if hist == ['*']: continue
for tx_hash, tx_height in hist:
tx = self.transactions.get(tx_hash)
if tx and not tx.get('timestamp'):
self.set_tx_timestamp(tx_hash, timestamp)
if tx_height>0:
- self.verifier.add(tx_hash)
+ self.verifier.add(tx_hash, tx_height)
# 1 check that tx is referenced in addr_history.
addresses = []
for addr, hist in self.history.items():
+ if hist == ['*']:continue
for txh, height in hist:
if txh == tx_hash:
addresses.append(addr)
# request any missing transactions
for history in self.wallet.history.values():
+ if history == ['*']: continue
for tx_hash, tx_height in history:
if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
missing_tx.append( (tx_hash, tx_height) )
# 3. handle response
method = r['method']
params = r['params']
- result = r['result']
+ result = r.get('result')
+ error = r.get('error')
+ if error:
+ print "error", r
+ continue
if method == 'blockchain.address.subscribe':
addr = params[0]
elif method == 'blockchain.address.get_history':
addr = params[0]
- hist = []
-
- # check that txids are unique
- txids = []
- for item in result:
- tx_hash = item['tx_hash']
- if tx_hash not in txids:
- txids.append(tx_hash)
- hist.append( (tx_hash, item['height']) )
-
- if len(hist) != len(result):
- raise BaseException("error: server sent history with non-unique txid")
-
- # check that the status corresponds to what was announced
- if self.wallet.get_status(hist) != requested_histories.pop(addr):
- raise BaseException("error: status mismatch: %s"%addr)
+ if result == ['*']:
+ assert requested_histories.pop(addr) == '*'
+ self.wallet.receive_history_callback(addr, result)
+ else:
+ hist = []
+ # check that txids are unique
+ txids = []
+ for item in result:
+ tx_hash = item['tx_hash']
+ if tx_hash not in txids:
+ txids.append(tx_hash)
+ hist.append( (tx_hash, item['height']) )
+
+ if len(hist) != len(result):
+ raise BaseException("error: server sent history with non-unique txid")
+
+ # check that the status corresponds to what was announced
+ rs = requested_histories.pop(addr)
+ if self.wallet.get_status(hist) != rs:
+ raise BaseException("error: status mismatch: %s"%addr)
- # store received history
- self.wallet.receive_history_callback(addr, hist)
-
- # request transactions that we don't have
- for tx_hash, tx_height in hist:
- if self.wallet.transactions.get(tx_hash) is None:
- if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
- missing_tx.append( (tx_hash, tx_height) )
- else:
- timestamp = self.wallet.verifier.get_timestamp(tx_height)
- self.wallet.set_tx_timestamp(tx_hash, timestamp)
+ # store received history
+ self.wallet.receive_history_callback(addr, hist)
+
+ # request transactions that we don't have
+ for tx_hash, tx_height in hist:
+ if self.wallet.transactions.get(tx_hash) is None:
+ if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
+ missing_tx.append( (tx_hash, tx_height) )
+ else:
+ timestamp = self.wallet.verifier.get_timestamp(tx_height)
+ self.wallet.set_tx_timestamp(tx_hash, timestamp)
elif method == 'blockchain.transaction.get':
tx_hash = params[0]