workaround
[electrum-server.git] / backends / abe / __init__.py
index eec391a..8a9c73b 100644 (file)
@@ -1,4 +1,4 @@
-from Abe.abe import hash_to_address, decode_check_address
+from Abe.util import hash_to_address, decode_check_address
 from Abe.DataStore import DataStore as Datastore_class
 from Abe import DataStore, readconf, BCDataStream,  deserialize, util, base58
 
@@ -53,6 +53,10 @@ class AbeStore(Datastore_class):
         inrows = self.get_tx_inputs(txid, False)
         for row in inrows:
             _hash = self.binout(row[6])
+            if not _hash:
+                print "WARNING: missing tx_in for tx", txid
+                continue
+
             address = hash_to_address(chr(0), _hash)
             if self.tx_cache.has_key(address):
                 print "cache: invalidating", address
@@ -62,6 +66,10 @@ class AbeStore(Datastore_class):
         outrows = self.get_tx_outputs(txid, False)
         for row in outrows:
             _hash = self.binout(row[6])
+            if not _hash:
+                print "WARNING: missing tx_out for tx", txid
+                continue
+
             address = hash_to_address(chr(0), _hash)
             if self.tx_cache.has_key(address):
                 print "cache: invalidating", address
@@ -72,11 +80,14 @@ class AbeStore(Datastore_class):
         try:
             if lock: self.dblock.acquire()
             ret = self.selectall(sql,params)
-            if lock: self.dblock.release()
-            return ret
         except:
             print "sql error", sql
-            return []
+            ret = []
+        finally:
+            if lock: self.dblock.release()
+
+        return ret
+            
 
     def get_tx_outputs(self, tx_id, lock=True):
         return self.safe_sql("""SELECT
@@ -344,7 +355,8 @@ class AbeStore(Datastore_class):
         respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
         r = loads(respdata)
         if r['error'] != None:
-            out = "error: transaction rejected by memorypool\n"+tx
+            msg = r['error'].get('message')
+            out = "error: transaction rejected by memorypool: " + msg + "\n" + tx
         else:
             out = r['result']
         return out
@@ -369,6 +381,19 @@ class AbeStore(Datastore_class):
         return block_number
 
 
+    def catch_up(store):
+        # if there is an exception, do rollback and then re-raise the exception
+        for dircfg in store.datadirs:
+            try:
+                store.catch_up_dir(dircfg)
+            except Exception, e:
+                store.log.exception("Failed to catch up %s", dircfg)
+                store.rollback()
+                raise e
+
+
+
+
 from processor import Processor
 
 class BlockchainProcessor(Processor):
@@ -402,7 +427,7 @@ class BlockchainProcessor(Processor):
             print "unknown method", request
 
         if result != '':
-            response = { 'id':message_id, 'method':method, 'params':params, 'result':result }
+            response = { 'id':message_id, 'result':result }
             self.push_response(response)
 
 
@@ -419,7 +444,8 @@ class BlockchainProcessor(Processor):
 
             if self.block_number != old_block_number:
                 old_block_number = self.block_number
-                self.push_response({ 'method':'blockchain.numblocks.subscribe', 'result':self.block_number })
+                print "block number:", self.block_number
+                self.push_response({ 'method':'blockchain.numblocks.subscribe', 'params':[self.block_number] })
 
             while True:
                 try:
@@ -428,7 +454,7 @@ class BlockchainProcessor(Processor):
                     break
                 if addr in self.watched_addresses:
                     status = self.store.get_status( addr )
-                    self.push_response({ 'method':'blockchain.address.subscribe', 'params':[addr], 'result':status })
+                    self.push_response({ 'method':'blockchain.address.subscribe', 'params':[addr, status] })
 
             time.sleep(10)