do not store undo info during catch up
[electrum-server.git] / backends / bitcoind / blockchain_processor.py
index e907e85..784cd98 100644 (file)
@@ -394,7 +394,8 @@ class BlockchainProcessor(Processor):
         return eval(s)
 
     def write_undo_info(self, batch, height, undo_info):
-        batch.Put("undo%d"%(height%100), repr(undo_info))
+        if self.is_test or height > self.bitcoind_height - 100:
+            batch.Put("undo%d"%(height%100), repr(undo_info))
 
 
     def import_block(self, block, block_hash, block_height, sync, revert=False):
@@ -459,6 +460,7 @@ class BlockchainProcessor(Processor):
         # process
         t1 = time.time()
 
+        if revert: tx_hashes = tx_hashes[::-1]
         for txid in tx_hashes: # must be ordered
             tx = txdict[txid]
             if not revert:
@@ -482,7 +484,9 @@ class BlockchainProcessor(Processor):
                     i += 1
 
                     # read the history into batch list
-                    self.batch_list[prevout_addr] = self.db.Get(prevout_addr)
+                    if self.batch_list.get(prevout_addr) is None:
+                        self.batch_list[prevout_addr] = self.db.Get(prevout_addr)
+
                     # re-add them to the history
                     self.add_to_history( prevout_addr, x.get('prevout_hash'), x.get('prevout_n'), prevout_height)
                     print "new hist for", prevout_addr, self.deserialize(self.batch_list[prevout_addr])
@@ -660,8 +664,8 @@ class BlockchainProcessor(Processor):
 
             # are we done yet?
             info = self.bitcoind('getinfo')
-            bitcoind_height = info.get('blocks')
-            bitcoind_block_hash = self.bitcoind('getblockhash', [bitcoind_height])
+            self.bitcoind_height = info.get('blocks')
+            bitcoind_block_hash = self.bitcoind('getblockhash', [self.bitcoind_height])
             if self.last_hash == bitcoind_block_hash: 
                 self.up_to_date = True
                 break
@@ -671,7 +675,7 @@ class BlockchainProcessor(Processor):
             next_block_hash = self.bitcoind('getblockhash', [self.height+1])
             next_block = self.bitcoind('getblock', [next_block_hash, 1])
 
-            revert = (random.randint(1, 100)==1) if self.is_test else False
+            revert = (random.randint(1, 10)==1) if self.is_test else False
             if (next_block.get('previousblockhash') == self.last_hash) and not revert:
 
                 self.import_block(next_block, next_block_hash, self.height+1, sync)