fix for notifications
[electrum-nvc.git] / lib / wallet.py
index cfce210..0047452 100644 (file)
@@ -381,7 +381,8 @@ class Wallet:
                 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')
@@ -409,6 +410,7 @@ class Wallet:
     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])
@@ -443,7 +445,8 @@ class Wallet:
 
         for addr in domain:
             h = self.history.get(addr, [])
-            for tx_hash, tx_height, in h:
+            if h == ['*']: continue
+            for tx_hash, tx_height in h:
                 tx = self.transactions.get(tx_hash)
                 for output in tx.get('outputs'):
                     if output.get('address') != addr: continue
@@ -456,7 +459,8 @@ class Wallet:
 
         for addr in self.prioritized_addresses:
             h = self.history.get(addr, [])
-            for tx_hash, tx_height, in h:
+            if h == ['*']: continue
+            for tx_hash, tx_height in h:
                 for output in tx.get('outputs'):
                     if output.get('address') != addr: continue
                     key = tx_hash + ":%d" % output.get('index')
@@ -525,10 +529,13 @@ class Wallet:
             return s
 
 
-    def get_status(self, address):
+    def get_history(self, address):
         with self.lock:
-            h = self.history.get(address)
+            return self.history.get(address)
+
+    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
@@ -536,23 +543,43 @@ class Wallet:
 
 
 
-    def receive_tx_callback(self, tx_hash, d):
-        #print "updating history for", addr
-        #with self.lock:
-        self.transactions[tx_hash] = d
+    def receive_tx_callback(self, tx_hash, tx):
+
+        if not self.check_new_tx(tx_hash, tx):
+            raise BaseException("error: received transaction is not consistent with history"%tx_hash)
+
+        with self.lock:
+            self.transactions[tx_hash] = tx
+
+        tx_height = tx.get('height')
+        if tx_height>0: self.verifier.add(tx_hash, tx_height)
+
         self.update_tx_outputs(tx_hash)
 
         self.save()
 
 
     def receive_history_callback(self, addr, hist):
-        #print "updating history for", 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)
+                    # add it in case it was previously unconfirmed
+                    self.verifier.add(tx_hash, tx_height)
+                    # set the height in case it changed
+                    tx = self.transactions.get(tx_hash)
+                    if tx:
+                        if tx.get('height') != tx_height:
+                            print_error( "changing height for tx", tx_hash )
+                            tx['height'] = tx_height
 
 
     def get_tx_history(self):
@@ -873,18 +900,37 @@ class Wallet:
 
     def set_verifier(self, verifier):
         self.verifier = verifier
+
+        # review transactions (they might not all be in history)
+        for tx_hash, tx in self.transactions.items():
+            tx_height = tx.get('height')
+            if tx_height <1:
+                print_error( "skipping", tx_hash, tx_height )
+                continue
             
-        # set the timestamp for transactions that need it
-        for hist in self.history.values():
-            for tx_hash, tx_height in hist:
-                tx = self.transactions.get(tx_hash)
-                if tx and not tx.get('timestamp'):
-                    timestamp = self.verifier.get_timestamp(tx_height)
-                    if timestamp:
-                        self.set_tx_timestamp(tx_hash, timestamp)
+            if tx_height>0:
+                self.verifier.add(tx_hash, tx_height)
 
+            # set the timestamp for transactions that need it
+            if tx and not tx.get('timestamp'):
+                timestamp = self.verifier.get_timestamp(tx_height)
+                if timestamp:
+                    self.set_tx_timestamp(tx_hash, timestamp)
+
+
+        # review existing history
+        for addr, hist in self.history.items():
+            if hist == ['*']: continue
+            for tx_hash, tx_height in hist:
                 if tx_height>0:
-                    self.verifier.add(tx_hash)
+                    # add it in case it was previously unconfirmed
+                    self.verifier.add(tx_hash, tx_height)
+                    # set the height in case it changed
+                    tx = self.transactions.get(tx_hash)
+                    if tx:
+                        if tx.get('height') != tx_height:
+                            print_error( "changing height for tx", tx_hash )
+                            tx['height'] = tx_height
 
 
 
@@ -894,6 +940,54 @@ class Wallet:
 
 
 
+    def is_addr_in_tx(self, addr, tx):
+        found = False
+        for txin in tx.get('inputs'):
+            if addr == txin.get('address'): 
+                found = True
+                break
+        for txout in tx.get('outputs'):
+            if addr == txout.get('address'): 
+                found = True
+                break
+        return found
+
+
+    def check_new_history(self, addr, hist):
+        # - check that all tx in hist are relevant
+        for tx_hash, height in hist:
+            tx = self.transactions.get(tx_hash)
+            if not tx: continue
+            if not self.is_addr_in_tx(addr,tx):
+                return False
+
+        # todo: check that we are not "orphaning" a transaction
+        # if we are, reject tx if unconfirmed, else reject the server
+
+        return True
+
+
+
+    def check_new_tx(self, tx_hash, tx):
+        # 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)
+
+        if not addresses:
+            return False
+
+        # 2 check that referencing addresses are in the tx
+        for addr in addresses:
+            if not self.is_addr_in_tx(addr, tx):
+                return False
+
+        return True
+
+
 
 
 class WalletSynchronizer(threading.Thread):
@@ -913,19 +1007,20 @@ class WalletSynchronizer(threading.Thread):
         new_addresses = self.wallet.synchronize()
         if new_addresses:
             self.subscribe_to_addresses(new_addresses)
+            self.wallet.up_to_date = False
+            return
             
-        if self.interface.is_up_to_date('synchronizer'):
-            if not self.wallet.up_to_date:
-                self.wallet.up_to_date = True
-                self.was_updated = True
-                self.wallet.up_to_date_event.set()
-        else:
+        if not self.interface.is_up_to_date('synchronizer'):
             if self.wallet.up_to_date:
                 self.wallet.up_to_date = False
                 self.was_updated = True
+            return
 
+        self.wallet.up_to_date = True
+        self.was_updated = True
+        self.wallet.up_to_date_event.set()
 
-
+    
     def subscribe_to_addresses(self, addresses):
         messages = []
         for addr in addresses:
@@ -940,6 +1035,7 @@ class WalletSynchronizer(threading.Thread):
 
         # 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) )
@@ -977,29 +1073,53 @@ class WalletSynchronizer(threading.Thread):
             # 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]
-                if self.wallet.get_status(addr) != result:
+                if self.wallet.get_status(self.wallet.get_history(addr)) != result:
                     self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
                     requested_histories[addr] = result
 
             elif method == 'blockchain.address.get_history':
                 addr = params[0]
-                hist = []
-                # in the new protocol, we will receive a list of (tx_hash, height)
-                for item in result: hist.append( (item['tx_hash'], item['height']) )
-                # store it
-                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)
+                print_error("receiving history", addr, result)
+                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", result)
+
+                    # 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)
 
             elif method == 'blockchain.transaction.get':
                 tx_hash = params[0]
@@ -1033,6 +1153,7 @@ class WalletSynchronizer(threading.Thread):
         vds = deserialize.BCDataStream()
         vds.write(raw_tx.decode('hex'))
         d = deserialize.parse_Transaction(vds)
+        d['height'] = tx_height
         d['tx_hash'] = tx_hash
         d['timestamp'] = self.wallet.verifier.get_timestamp(tx_height)
         return d