terminate if bitcoind is dead
[electrum-server.git] / backends / bitcoind / blockchain_processor.py
index 5e7901a..9620401 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'])
@@ -182,6 +187,11 @@ class BlockchainProcessor(Processor):
         if sync or len(self.headers_data) > 40*100:
             self.flush_headers()
 
+        with self.cache_lock:
+            chunk_index = header.get('block_height')/2016
+            if self.chunk_cache.get(chunk_index):
+                self.chunk_cache.pop(chunk_index)
+
     def pop_header(self):
         # we need to do this only if we have not flushed
         if self.headers_data:
@@ -198,10 +208,12 @@ class BlockchainProcessor(Processor):
 
     def get_chunk(self, i):
         # store them on disk; store the current chunk in memory
-        chunk = self.chunk_cache.get(i)
-        if not chunk:
-            chunk = self.read_chunk(i)
-            self.chunk_cache[i] = chunk
+        with self.cache_lock:
+            chunk = self.chunk_cache.get(i)
+            if not chunk:
+                chunk = self.read_chunk(i)
+                self.chunk_cache[i] = chunk
+
         return chunk
 
 
@@ -453,7 +465,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
@@ -584,9 +596,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: