From 6965971613c6b358b21d2267e05904caf1a31882 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 4 Jun 2019 18:59:25 -0400 Subject: [PATCH] delete claims and re-calculate winner when claim is expired --- lbrynet/wallet/server/db.py | 16 ++++++++++++++- tests/integration/test_resolve_command.py | 1 - tests/unit/wallet/server/test_sqldb.py | 24 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lbrynet/wallet/server/db.py b/lbrynet/wallet/server/db.py index 76af0024b..2bd0f446d 100644 --- a/lbrynet/wallet/server/db.py +++ b/lbrynet/wallet/server/db.py @@ -622,6 +622,11 @@ class SQLDB: r(self._update_effective_amount, height) r(self._perform_overtake, height, [], []) + def get_expiring(self, height): + return self.execute( + f"SELECT claim_hash, normalized FROM claim WHERE expiration_height = {height}" + ) + def advance_txs(self, height, all_txs, header, daemon_height, timer): insert_claims = [] update_claims = [] @@ -642,8 +647,8 @@ class SQLDB: ) body_timer.start() delete_claim_hashes.update({r['claim_hash'] for r in spent_claims}) - delete_support_txo_hashes.update({r['txo_hash'] for r in spent_supports}) deleted_claim_names.update({r['normalized'] for r in spent_claims}) + delete_support_txo_hashes.update({r['txo_hash'] for r in spent_supports}) recalculate_claim_hashes.update({r['claim_hash'] for r in spent_supports}) delete_others.update(spent_others) # Outputs @@ -661,12 +666,21 @@ class SQLDB: delete_claim_hashes.discard(claim_hash) delete_others.discard(output.ref.hash) # claim insertion and update occurring in the same block body_timer.stop() + skip_claim_timer = timer.add_timer('skip insertion of abandoned claims') skip_claim_timer.start() for new_claim in list(insert_claims): if new_claim.ref.hash in delete_others: insert_claims.remove(new_claim) skip_claim_timer.stop() + + expire_timer = timer.add_timer('recording expired claims') + expire_timer.start() + for expired in self.get_expiring(height): + delete_claim_hashes.add(expired['claim_hash']) + deleted_claim_names.add(expired['normalized']) + expire_timer.stop() + r = timer.run r(self.delete_claims, delete_claim_hashes) r(self.delete_supports, delete_support_txo_hashes) diff --git a/tests/integration/test_resolve_command.py b/tests/integration/test_resolve_command.py index 430a0f6c3..061160ca8 100644 --- a/tests/integration/test_resolve_command.py +++ b/tests/integration/test_resolve_command.py @@ -134,7 +134,6 @@ class ResolveCommand(CommandTestCase): channel_id = self.get_claim_id( await self.channel_create('@abc', '1.1', allow_duplicate_name=True)) await self.assertResolvesToClaimId(f'@abc', channel_id) - await self.assertResolvesToClaimId(f'@abc#{channel_id[0]}', channel_id) await self.assertResolvesToClaimId(f'@abc#{channel_id[:10]}', channel_id) await self.assertResolvesToClaimId(f'@abc#{channel_id}', channel_id) channel = (await self.claim_search(claim_id=channel_id))[0] diff --git a/tests/unit/wallet/server/test_sqldb.py b/tests/unit/wallet/server/test_sqldb.py index 09148f3b3..c564f9e4c 100644 --- a/tests/unit/wallet/server/test_sqldb.py +++ b/tests/unit/wallet/server/test_sqldb.py @@ -140,7 +140,7 @@ class TestSQLDB(unittest.TestCase): return [otx[0].tx.outputs[0] for otx in txs] def state(self, controlling=None, active=None, accepted=None): - self.assertEqual(controlling or [], self.get_controlling()) + self.assertEqual(controlling, self.get_controlling()) self.assertEqual(active or [], self.get_active()) self.assertEqual(accepted or [], self.get_accepted()) @@ -288,6 +288,28 @@ class TestSQLDB(unittest.TestCase): accepted=[] ) + def test_winning_claim_expires_and_another_takes_over(self): + advance, state = self.advance, self.state + advance(10, [self.get_stream('Claim A', 11*COIN)]) + advance(20, [self.get_stream('Claim B', 10*COIN)]) + state( + controlling=('Claim A', 11*COIN, 11*COIN, 10), + active=[('Claim B', 10*COIN, 10*COIN, 20)], + accepted=[] + ) + advance(262984, []) + state( + controlling=('Claim B', 10*COIN, 10*COIN, 20), + active=[], + accepted=[] + ) + advance(262994, []) + state( + controlling=None, + active=[], + accepted=[] + ) + def test_trending(self): advance, state = self.advance, self.state no_trend = self.get_stream('Claim A', COIN)