diff --git a/server/db.py b/server/db.py index 3af0225..97d558c 100644 --- a/server/db.py +++ b/server/db.py @@ -119,9 +119,14 @@ class UTXOCache(object): hash168s.add(hash168) key = tx_hash + pack('H', key[-2:]) + if flush_id > self.utxo_flush_count: + keys.append(key) + + self.logger.info('deleting {:,d} history entries'.format(len(keys))) + with db.write_batch(transaction=True) as batch: + for key in keys: + db.delete(key) + self.utxo_flush_count = self.flush_count + self.flush_state(batch) + self.logger.info('deletion complete') def flush_state(self, batch): '''Flush chain state to the batch.''' @@ -375,8 +410,8 @@ class DB(object): self.last_flush = now state = { 'genesis': self.coin.GENESIS_HASH, - 'height': self.height, - 'tx_count': self.tx_count, + 'height': self.db_height, + 'tx_count': self.db_tx_count, 'tip': self.tip, 'flush_count': self.flush_count, 'utxo_flush_count': self.utxo_flush_count, @@ -405,18 +440,16 @@ class DB(object): self.logger.info('commencing history flush') with self.db.write_batch(transaction=True) as batch: + # History first - fast and frees memory + self.flush_history(batch) if flush_utxos: self.utxo_cache.flush(batch) - self.utxo_flush_count = self.flush_count + 1 - self.flush_history(batch) + self.utxo_flush_count = self.flush_count + self.db_tx_count = self.tx_count + self.db_height = self.height self.flush_state(batch) self.logger.info('committing transaction...') - # The flush succeeded, so update our record of DB state - if flush_utxos: - self.db_tx_count = self.tx_count - self.db_height = self.height - # Update and put the wall time again - otherwise we drop the # time it took leveldb to commit the batch self.flush_state(self.db) @@ -531,7 +564,7 @@ class DB(object): self.logger.info('cache stats at height {:,d} daemon height: {:,d}' .format(self.height, daemon_height)) self.logger.info(' entries: UTXO: {:,d} DB: {:,d} ' - 'hist count: {:,d} hist size: {:,d}' + 'hist addrs: {:,d} hist size: {:,d}' .format(len(self.utxo_cache.cache), len(self.utxo_cache.db_cache), len(self.history),