catch exception raised when mempool has changed
[electrum-server.git] / backends / bitcoind / blockchain_processor.py
index ef41793..5dfecb8 100644 (file)
@@ -81,7 +81,12 @@ class BlockchainProcessor(Processor):
 
     def bitcoind(self, method, params=[]):
         postdata = dumps({"method": method, 'params': params, 'id':'jsonrpc'})
-        respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
+        try:
+            respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
+        except:
+            traceback.print_exc(file=sys.stdout)
+            self.shared.stop()
+
         r = loads(respdata)
         if r['error'] != None:
             raise BaseException(r['error'])
@@ -212,11 +217,15 @@ class BlockchainProcessor(Processor):
         return chunk
 
 
-    def get_transaction(self, txid, block_height=-1, is_coinbase = False):
-        raw_tx = self.bitcoind('getrawtransaction', [txid, 0, block_height])
+    def get_mempool_transaction(self, txid):
+        try:
+            raw_tx = self.bitcoind('getrawtransaction', [txid, 0, -1])
+        except:
+            return None
+
         vds = deserialize.BCDataStream()
         vds.write(raw_tx.decode('hex'))
-        out = deserialize.parse_Transaction(vds, is_coinbase)
+        out = deserialize.parse_Transaction(vds, is_coinbase = False)
         return out
 
 
@@ -460,7 +469,7 @@ class BlockchainProcessor(Processor):
 
                     # re-add them to the history
                     self.add_to_history( prevout_addr, x.get('prevout_hash'), x.get('prevout_n'), prevout_height)
-                    print_log( "new hist for", hash_160_to_bc_address(prevout_addr), self.deserialize(self.batch_list[prevout_addr]) )
+                    # print_log( "new hist for", hash_160_to_bc_address(prevout_addr), self.deserialize(self.batch_list[prevout_addr]) )
 
         # write
         max_len = 0
@@ -591,9 +600,13 @@ class BlockchainProcessor(Processor):
                     print_log( "error:", error)
 
         elif method == 'blockchain.transaction.broadcast':
-            txo = self.bitcoind('sendrawtransaction', params)
-            print_log( "sent tx:", txo )
-            result = txo 
+            try:
+                txo = self.bitcoind('sendrawtransaction', params)
+                print_log( "sent tx:", txo )
+                result = txo 
+            except BaseException, e:
+                result = str(e) # do not send an error
+                print_log( "error:", str(e), params )
 
         elif method == 'blockchain.transaction.get_merkle':
             if cache_only:
@@ -696,7 +709,7 @@ class BlockchainProcessor(Processor):
         for tx_hash in mempool_hashes:
             if tx_hash in self.mempool_hashes: continue
 
-            tx = self.get_transaction(tx_hash)
+            tx = self.get_mempool_transaction(tx_hash)
             if not tx: continue
 
             for x in tx.get('inputs'):