document public methods of verifier
[electrum-nvc.git] / lib / verifier.py
index a23a88d..5bbcf00 100644 (file)
@@ -25,13 +25,14 @@ from bitcoin import *
 
 
 class WalletVerifier(threading.Thread):
+    """ Simple Verification Protocol """
 
-    def __init__(self, interface, config, get_transactions):
+    def __init__(self, interface, config):
         threading.Thread.__init__(self)
         self.daemon = True
         self.config = config
         self.interface = interface
-        self.get_transactions = get_transactions
+        self.transactions    = []                                 # monitored transactions
         self.interface.register_channel('verifier')
         self.verified_tx     = config.get('verified_tx',{})
         self.merkle_roots    = config.get('merkle_roots',{})      # hashed by me
@@ -43,7 +44,16 @@ class WalletVerifier(threading.Thread):
         self.set_local_height()
 
     def get_confirmations(self, tx):
-        return (self.local_height - self.verified_tx[tx] + 1) if tx in self.verified_tx else 0
+        """ return the number of confirmations of a monitored transaction. """
+        with self.lock:
+            assert tx in self.transactions
+            return (self.local_height - self.verified_tx[tx] + 1) if tx in self.verified_tx else 0
+
+    def add(self, tx):
+        """ add a transaction to the list of monitored transactions. """
+        with self.lock:
+            if tx not in self.transactions:
+                self.transactions.append(tx)
 
     def run(self):
         requested_merkle = []
@@ -75,8 +85,7 @@ class WalletVerifier(threading.Thread):
                         requested_headers.append(i)
             
             # request missing tx merkle
-            txlist = self.get_transactions()
-            for tx in txlist:
+            for tx in self.transactions:
                 if tx not in self.verified_tx:
                     if tx not in requested_merkle:
                         requested_merkle.append(tx)
@@ -126,6 +135,7 @@ class WalletVerifier(threading.Thread):
                         break
                 pending_headers_changed = False
 
+            self.interface.trigger_callback('updated')
 
 
     def request_merkle(self, tx_hash):
@@ -277,7 +287,6 @@ class WalletVerifier(threading.Thread):
             h = os.path.getsize(name)/80 - 1
             if self.local_height != h:
                 self.local_height = h
-                self.interface.trigger_callback('updated')
 
 
     def read_header(self, block_height):