move code that is specific to abe
[electrum-server.git] / backends / abe / __init__.py
index d34cf28..61ecf44 100644 (file)
@@ -26,7 +26,7 @@ class AbeStore(Datastore_class):
 
         Datastore_class.__init__(self,args)
 
-        self.sql_limit = config.get('database','limit')
+        self.sql_limit = int( config.get('database','limit') )
 
         self.tx_cache = {}
         self.bitcoind_url = 'http://%s:%s@%s:%s/' % ( config.get('bitcoind','user'), config.get('bitcoind','password'), config.get('bitcoind','host'), config.get('bitcoind','port'))
@@ -34,6 +34,14 @@ class AbeStore(Datastore_class):
         self.address_queue = Queue()
 
         self.dblock = thread.allocate_lock()
+        self.last_tx_id = 0
+
+    
+    def import_tx(self, tx, is_coinbase):
+        tx_id = super(AbeStore, self).import_tx(tx, is_coinbase)
+        self.last_tx_id = tx_id
+        return tx_id
+        
 
 
 
@@ -87,6 +95,7 @@ class AbeStore(Datastore_class):
             ret = self.selectall(sql,params)
         except:
             error = True
+            traceback.print_exc(file=sys.stdout)
         finally:
             if lock: self.dblock.release()
 
@@ -267,21 +276,19 @@ class AbeStore(Datastore_class):
         rows += self.get_address_out_rows_memorypool( dbhash )
         address_has_mempool = False
 
-        current_id = self.new_id("tx")
-
         for row in rows:
             is_in, tx_hash, tx_id, pos, value = row
             tx_hash = self.hashout_hex(tx_hash)
             if tx_hash in known_tx:
                 continue
 
-            # this means that pending transactions were added to the db, even if they are not returned by getmemorypool
-            address_has_mempool = True
-
-            # fixme: we need to detect transactions that became invalid
-            if current_id - tx_id > 10000:
+            # discard transactions that are too old
+            if self.last_tx_id - tx_id > 10000:
+                print "discarding tx id", tx_id
                 continue
 
+            # this means that pending transactions were added to the db, even if they are not returned by getmemorypool
+            address_has_mempool = True
 
             #print "mempool", tx_hash
             txpoint = {
@@ -334,6 +341,8 @@ class AbeStore(Datastore_class):
                 if row:
                     if not row[4]: txpoint['raw_output_script'] = row[1]
 
+            txpoint.pop('tx_id')
+
         # cache result
         # do not cache mempool results because statuses are ambiguous
         if not address_has_mempool:
@@ -380,6 +389,7 @@ class AbeStore(Datastore_class):
             else:
                 tx_id = store.import_tx(tx, False)
                 store.update_tx_cache(tx_id)
+                #print tx_hash
     
         store.commit()
 
@@ -397,22 +407,13 @@ class AbeStore(Datastore_class):
 
 
     def main_iteration(store):
-        try:
-            store.dblock.acquire()
+        with store.dblock:
             store.catch_up()
             store.memorypool_update()
             block_number = store.get_block_number(1)
+            return block_number
 
-        except IOError:
-            print "IOError: cannot reach bitcoind"
-            block_number = 0
-        except:
-            traceback.print_exc(file=sys.stdout)
-            block_number = 0
-        finally:
-            store.dblock.release()
 
-        return block_number
 
 
     def catch_up(store):
@@ -437,6 +438,11 @@ class BlockchainProcessor(Processor):
         self.store = AbeStore(config)
         self.block_number = -1
         self.watched_addresses = []
+
+        # catch_up first
+        n = self.store.main_iteration()
+        print "blockchain: %d blocks"%n
+
         threading.Timer(10, self.run_store_iteration).start()
 
     def process(self, request):
@@ -491,11 +497,18 @@ class BlockchainProcessor(Processor):
 
 
     def run_store_iteration(self):
+        
+        try:
+            block_number = self.store.main_iteration()
+        except:
+            traceback.print_exc(file=sys.stdout)
+            print "terminating"
+            self.shared.stop()
+
         if self.shared.stopped(): 
             print "exit timer"
             return
-        
-        block_number = self.store.main_iteration()
+
         if self.block_number != block_number:
             self.block_number = block_number
             print "block number:", self.block_number