delete claims above reorg height from the database

This commit is contained in:
Jack Robison 2020-03-31 10:14:20 -04:00
parent 130d36acd9
commit 64f7f837e7
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 13 additions and 3 deletions

View file

@ -2,7 +2,7 @@ import time
import asyncio import asyncio
from struct import pack, unpack from struct import pack, unpack
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
from typing import Optional
import lbry import lbry
from lbry.schema.claim import Claim from lbry.schema.claim import Claim
from lbry.wallet.server.db.writer import SQLDB from lbry.wallet.server.db.writer import SQLDB
@ -219,7 +219,7 @@ class BlockProcessor:
'resetting the prefetcher') 'resetting the prefetcher')
await self.prefetcher.reset_height(self.height) 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. """Handle a chain reorganisation.
Count is the number of blocks to simulate a reorg, or None for 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(self.backup_blocks, raw_blocks)
await self.run_in_thread_with_lock(flush_backup) await self.run_in_thread_with_lock(flush_backup)
last -= len(raw_blocks) last -= len(raw_blocks)
self.db.sql.delete_claims_above_height(self.height)
await self.prefetcher.reset_height(self.height) await self.prefetcher.reset_height(self.height)
async def reorg_hashes(self, count): async def reorg_hashes(self, count):
@ -270,7 +271,7 @@ class BlockProcessor:
return start, last, await self.db.fs_block_hashes(start, count) 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""" """Calculate the reorg range"""
def diff_pos(hashes1, hashes2): def diff_pos(hashes1, hashes2):

View file

@ -433,6 +433,15 @@ class SQLDB:
return {r.channel_hash for r in affected_channels} return {r.channel_hash for r in affected_channels}
return set() 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]): def _clear_claim_metadata(self, claim_hashes: Set[bytes]):
if claim_hashes: if claim_hashes:
for table in ('tag',): # 'language', 'location', etc for table in ('tag',): # 'language', 'location', etc