diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index 9c6f10682..bdb3d014d 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -2,7 +2,7 @@ import time import asyncio from struct import pack, unpack from concurrent.futures.thread import ThreadPoolExecutor - +from typing import Optional import lbry from lbry.schema.claim import Claim from lbry.wallet.server.db.writer import SQLDB @@ -219,7 +219,7 @@ class BlockProcessor: 'resetting the prefetcher') await self.prefetcher.reset_height(self.height) - async def reorg_chain(self, count=None): + async def reorg_chain(self, count: Optional[int] = None): """Handle a chain reorganisation. Count is the number of blocks to simulate a reorg, or None for @@ -253,6 +253,7 @@ class BlockProcessor: await self.run_in_thread_with_lock(self.backup_blocks, raw_blocks) await self.run_in_thread_with_lock(flush_backup) last -= len(raw_blocks) + self.db.sql.delete_claims_above_height(self.height) await self.prefetcher.reset_height(self.height) async def reorg_hashes(self, count): @@ -270,7 +271,7 @@ class BlockProcessor: return start, last, await self.db.fs_block_hashes(start, count) - async def calc_reorg_range(self, count): + async def calc_reorg_range(self, count: Optional[int]): """Calculate the reorg range""" def diff_pos(hashes1, hashes2): diff --git a/lbry/wallet/server/db/writer.py b/lbry/wallet/server/db/writer.py index cb9ad07ea..07ba36d81 100644 --- a/lbry/wallet/server/db/writer.py +++ b/lbry/wallet/server/db/writer.py @@ -433,6 +433,15 @@ class SQLDB: return {r.channel_hash for r in affected_channels} return set() + def delete_claims_above_height(self, height: int): + claim_hashes = [x[0] for x in self.execute( + "SELECT claim_hash FROM claim WHERE height>=?", (height, ) + ).fetchall()] + while claim_hashes: + batch = set(claim_hashes[:500]) + claim_hashes = claim_hashes[500:] + self.delete_claims(batch) + def _clear_claim_metadata(self, claim_hashes: Set[bytes]): if claim_hashes: for table in ('tag',): # 'language', 'location', etc