From d323edd2525bc0edd605d9fe9eb5577a4e1494f7 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 6 Apr 2022 22:00:30 -0400 Subject: [PATCH] _get_compactify_hashX_history_ops --- scribe/blockchain/service.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/scribe/blockchain/service.py b/scribe/blockchain/service.py index e6da88d..8642505 100644 --- a/scribe/blockchain/service.py +++ b/scribe/blockchain/service.py @@ -1271,6 +1271,23 @@ class BlockchainProcessorService(BlockchainService): status = sha256(history.encode()) self.db.prefix_db.hashX_mempool_status.stage_put((hashX,), (status,)) + def _get_compactify_hashX_history_ops(self, height: int, hashX: bytes): + if height > self.env.reorg_limit: # compactify existing history + hist_txs = b'' + # accumulate and delete all of the tx histories between height 1 and current - reorg_limit + for k, hist in self.db.prefix_db.hashX_history.iterate( + start=(hashX, 1), stop=(hashX, height - self.env.reorg_limit), + deserialize_key=False, deserialize_value=False): + hist_txs += hist + self.db.prefix_db.stage_raw_delete(k, hist) + if hist_txs: + # add the accumulated histories onto the existing compacted history at height 0 + key = self.db.prefix_db.hashX_history.pack_key(hashX, 0) + existing = self.db.prefix_db.get(key) + if existing is not None: + self.db.prefix_db.stage_raw_delete(key, existing) + self.db.prefix_db.stage_raw_put(key, (existing or b'') + hist_txs) + def advance_block(self, block: Block): height = self.height + 1 # print("advance ", height) @@ -1364,24 +1381,11 @@ class BlockchainProcessorService(BlockchainService): self.db.prefix_db.stage_raw_delete(k, v) for hashX, new_history in self.hashXs_by_tx.items(): + # TODO: combine this with compaction so that we only read the history once self._get_update_hashX_status_ops( hashX, [(self.pending_transactions[tx_num], height) for tx_num in new_history] ) - if height > self.env.reorg_limit: # compactify existing history - hist_txs = b'' - # accumulate and delete all of the tx histories between height 1 and current - reorg_limit - for k, hist in self.db.prefix_db.hashX_history.iterate( - start=(hashX, 1), stop=(hashX, height - self.env.reorg_limit), - deserialize_key=False, deserialize_value=False): - hist_txs += hist - self.db.prefix_db.stage_raw_delete(k, hist) - if hist_txs: - # add the accumulated histories onto the existing compacted history at height 0 - key = self.db.prefix_db.hashX_history.pack_key(hashX, 0) - existing = self.db.prefix_db.get(key) - if existing is not None: - self.db.prefix_db.stage_raw_delete(key, existing) - self.db.prefix_db.stage_raw_put(key, (existing or b'') + hist_txs) + self._get_compactify_hashX_history_ops(height, hashX) if not new_history: continue self.db.prefix_db.hashX_history.stage_put(key_args=(hashX, height), value_args=(new_history,))