From b59e0490a2abe4611d912e57f1d3085471e18327 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 7 Sep 2021 19:27:45 -0400 Subject: [PATCH] renormalization --- .../server/db/elasticsearch/constants.py | 2 +- lbry/wallet/server/db/elasticsearch/search.py | 11 ++-- .../blockchain/test_resolve_command.py | 62 ++++++++++--------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/lbry/wallet/server/db/elasticsearch/constants.py b/lbry/wallet/server/db/elasticsearch/constants.py index a3792c5e2..d7eedd454 100644 --- a/lbry/wallet/server/db/elasticsearch/constants.py +++ b/lbry/wallet/server/db/elasticsearch/constants.py @@ -30,7 +30,7 @@ INDEX_DEFAULT_SETTINGS = { "height": {"type": "integer"}, "claim_type": {"type": "byte"}, "censor_type": {"type": "byte"}, - "trending_score": {"type": "float"}, + "trending_score": {"type": "double"}, "release_time": {"type": "long"} } } diff --git a/lbry/wallet/server/db/elasticsearch/search.py b/lbry/wallet/server/db/elasticsearch/search.py index bc97bc79c..e98fdfb9f 100644 --- a/lbry/wallet/server/db/elasticsearch/search.py +++ b/lbry/wallet/server/db/elasticsearch/search.py @@ -162,7 +162,7 @@ class SearchIndex: update_trending_score_script = """ double softenLBC(double lbc) { Math.pow(lbc, 1.0f / 3.0f) } double inflateUnits(int height) { - int renormalizationPeriod = 300000; + int renormalizationPeriod = 100000; double doublingRate = 400.0f; Math.pow(2.0, (height % renormalizationPeriod) / doublingRate) } @@ -186,17 +186,18 @@ class SearchIndex: } } for (i in params.src.changes) { + double units = inflateUnits(i.height); if (i.added) { if (ctx._source.trending_score == null) { - ctx._source.trending_score = spikeMass(i.prev_amount, i.prev_amount + i.new_amount); + ctx._source.trending_score = (units * spikeMass(i.prev_amount, i.prev_amount + i.new_amount)); } else { - ctx._source.trending_score += spikeMass(i.prev_amount, i.prev_amount + i.new_amount); + ctx._source.trending_score += (units * spikeMass(i.prev_amount, i.prev_amount + i.new_amount)); } } else { if (ctx._source.trending_score == null) { - ctx._source.trending_score = spikeMass(i.prev_amount, i.prev_amount - i.new_amount); + ctx._source.trending_score = (units * spikeMass(i.prev_amount, i.prev_amount - i.new_amount)); } else { - ctx._source.trending_score += spikeMass(i.prev_amount, i.prev_amount - i.new_amount); + ctx._source.trending_score += (units * spikeMass(i.prev_amount, i.prev_amount - i.new_amount)); } } } diff --git a/tests/integration/blockchain/test_resolve_command.py b/tests/integration/blockchain/test_resolve_command.py index 899e6ce43..f91c44940 100644 --- a/tests/integration/blockchain/test_resolve_command.py +++ b/tests/integration/blockchain/test_resolve_command.py @@ -983,42 +983,48 @@ class ResolveClaimTakeovers(BaseResolveTestCase): claim_id=claim_id ))[0][0]['trending_score'] - claim_id1 = (await self.stream_create('derp', '2.0'))['outputs'][0]['claim_id'] - claim_id2 = (await self.stream_create('derp', '2.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] - claim_id3 = (await self.stream_create('derp', '2.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] - claim_id4 = (await self.stream_create('derp', '2.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] + claim_id1 = (await self.stream_create('derp', '1.0'))['outputs'][0]['claim_id'] + claim_id2 = (await self.stream_create('derp', '1.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] + claim_id3 = (await self.stream_create('derp', '1.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] + claim_id4 = (await self.stream_create('derp', '1.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] + claim_id5 = (await self.stream_create('derp', '1.0', allow_duplicate_name=True))['outputs'][0]['claim_id'] - COIN = 100_000_000 + COIN = 1E9 - for height in range(2000): - self.conductor.spv_node.server.bp._add_claim_activation_change_notification( - claim_id1, height + 100_000, True, 1_000_000 * COIN * (height + 1), 1_000_000 * COIN - ) - self.conductor.spv_node.server.bp._add_claim_activation_change_notification( - claim_id2, height + 100_000, True, 100_000 * COIN * (height + 1), 100_000 * COIN - ) - self.conductor.spv_node.server.bp._add_claim_activation_change_notification( - claim_id2, height + 100_000, False, 100_000 * COIN * (height + 1), 100_000 * COIN - ) - self.conductor.spv_node.server.bp._add_claim_activation_change_notification( - claim_id3, height + 100_000, True, 1_000 * COIN * (height + 1), 1_000 * COIN - ) - await self.generate(1) - - self.assertEqual(1093.0813885726313, await get_trending_score(claim_id1)) - self.assertEqual(-20.84548486028665, await get_trending_score(claim_id2)) - self.assertEqual(109.83445454475519, await get_trending_score(claim_id3)) - self.assertEqual(0.5848035382925417, await get_trending_score(claim_id4)) + height = 99000 self.conductor.spv_node.server.bp._add_claim_activation_change_notification( - claim_id4, 200_000, True, 2 * COIN, 10 * COIN + claim_id1, height, True, 1 * COIN, 1_000_000 * COIN + ) + self.conductor.spv_node.server.bp._add_claim_activation_change_notification( + claim_id2, height, True, 1 * COIN, 100_000 * COIN + ) + self.conductor.spv_node.server.bp._add_claim_activation_change_notification( + claim_id2, height + 1, False, 100_001 * COIN, 100_000 * COIN + ) + self.conductor.spv_node.server.bp._add_claim_activation_change_notification( + claim_id3, height, True, 1 * COIN, 1_000 * COIN + ) + self.conductor.spv_node.server.bp._add_claim_activation_change_notification( + claim_id4, height, True, 1 * COIN, 10 * COIN ) await self.generate(1) - self.assertEqual(1.2760741313418618, await get_trending_score(claim_id4)) + + self.assertEqual(3.1711298570548195e+76, await get_trending_score(claim_id1)) + self.assertEqual(-1.369652719234026e+74, await get_trending_score(claim_id2)) + self.assertEqual(2.925275298842502e+75, await get_trending_score(claim_id3)) + self.assertEqual(5.193711055804491e+74, await get_trending_score(claim_id4)) + self.assertEqual(0.6690521635580086, await get_trending_score(claim_id5)) + + self.conductor.spv_node.server.bp._add_claim_activation_change_notification( + claim_id5, height + 100, True, 2 * COIN, 10 * COIN + ) + await self.generate(1) + self.assertEqual(5.664516565750028e+74, await get_trending_score(claim_id5)) search_results = (await self.conductor.spv_node.server.bp.db.search_index.search(claim_name="derp"))[0] - self.assertEqual(4, len(search_results)) - self.assertListEqual([claim_id1, claim_id3, claim_id2, claim_id4], [c['claim_id'] for c in search_results]) + self.assertEqual(5, len(search_results)) + self.assertListEqual([claim_id1, claim_id3, claim_id4, claim_id2, claim_id5], [c['claim_id'] for c in search_results]) class ResolveAfterReorg(BaseResolveTestCase):