From 22c75605ee3c014fd5399ccfcde980cfff3242de Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 22 Jul 2021 16:09:18 -0400 Subject: [PATCH] fix getting block hash during reorg --- lbry/wallet/server/block_processor.py | 8 ++------ lbry/wallet/server/leveldb.py | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index 5801ddb1e..336b1972d 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -201,7 +201,6 @@ class BlockProcessor: self.touched = set() # Caches of unflushed items. - self.block_hashes = [] self.block_txs = [] self.undo_infos = [] @@ -336,7 +335,7 @@ class BlockProcessor: for height, block_hash in zip( reversed(range(min_start_height, min_start_height + self.coin.REORG_LIMIT)), reversed(block_hashes_from_lbrycrd)): - if self.block_hashes[height][::-1].hex() == block_hash: + if self.db.get_block_hash(height)[::-1].hex() == block_hash: break count += 1 self.logger.warning(f"blockchain reorg detected at {self.height}, unwinding last {count} blocks") @@ -373,8 +372,7 @@ class BlockProcessor: def flush_data(self): """The data for a flush. The lock must be taken.""" assert self.state_lock.locked() - return FlushData(self.height, self.tx_count, self.block_hashes, - self.block_txs, self.db_op_stack, self.tip) + return FlushData(self.height, self.tx_count, self.block_txs, self.db_op_stack, self.tip) async def flush(self): def flush(): @@ -1137,7 +1135,6 @@ class BlockProcessor: txs: List[Tuple[Tx, bytes]] = block.transactions block_hash = self.coin.header_hash(block.header) - self.block_hashes.append(block_hash) self.db_op_stack.append(RevertablePut(*Prefixes.block_hash.pack_item(height, block_hash))) tx_count = self.tx_count @@ -1298,7 +1295,6 @@ class BlockProcessor: self.removed_claims_to_send_es.update(touched_and_deleted.deleted_claims) self.db.headers.pop() - self.block_hashes.pop() self.db.tx_counts.pop() self.tip = self.coin.header_hash(self.db.headers[-1]) while len(self.db.total_transactions) > self.db.tx_counts[-1]: diff --git a/lbry/wallet/server/leveldb.py b/lbry/wallet/server/leveldb.py index 1c02208fb..49ed95a1d 100644 --- a/lbry/wallet/server/leveldb.py +++ b/lbry/wallet/server/leveldb.py @@ -65,7 +65,6 @@ TXO_STRUCT_pack = TXO_STRUCT.pack class FlushData: height = attr.ib() tx_count = attr.ib() - block_hashes = attr.ib() block_txs = attr.ib() put_and_delete_ops = attr.ib() tip = attr.ib() @@ -382,8 +381,11 @@ class LevelDB: def get_claim_txo_amount(self, claim_hash: bytes) -> Optional[int]: v = self.db.get(Prefixes.claim_to_txo.pack_key(claim_hash)) + + def get_block_hash(self, height: int) -> Optional[bytes]: + v = self.db.get(Prefixes.block_hash.pack_key(height)) if v: - return Prefixes.claim_to_txo.unpack_value(v).amount + return Prefixes.block_hash.unpack_value(v).block_hash def get_support_txo_amount(self, claim_hash: bytes, tx_num: int, position: int) -> Optional[int]: v = self.db.get(Prefixes.claim_to_support.pack_key(claim_hash, tx_num, position)) @@ -790,7 +792,6 @@ class LevelDB: assert flush_data.tx_count == self.fs_tx_count == self.db_tx_count assert flush_data.height == self.fs_height == self.db_height assert flush_data.tip == self.db_tip - assert not flush_data.block_txs assert not len(flush_data.put_and_delete_ops) def flush_dbs(self, flush_data: FlushData): @@ -840,7 +841,6 @@ class LevelDB: self.write_db_state(batch) def flush_backup(self, flush_data): - assert not flush_data.block_txs assert flush_data.height < self.db_height assert not self.hist_unflushed