From 0d86717a9a07f1cb86eec0c71532ab647462fc5f Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 26 Jul 2021 13:25:50 -0400 Subject: [PATCH] faster _cached_get_active_amount for claims -remove dead code --- lbry/wallet/server/block_processor.py | 19 ++++++++----------- lbry/wallet/server/leveldb.py | 8 ++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index 52a5bc4f6..e2d2088ad 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -673,19 +673,16 @@ class BlockProcessor: def _cached_get_active_amount(self, claim_hash: bytes, txo_type: int, height: int) -> int: if (claim_hash, txo_type, height) in self.amount_cache: return self.amount_cache[(claim_hash, txo_type, height)] - self.amount_cache[(claim_hash, txo_type, height)] = amount = self.db._get_active_amount( - claim_hash, txo_type, height - ) + if txo_type == ACTIVATED_CLAIM_TXO_TYPE: + self.amount_cache[(claim_hash, txo_type, height)] = amount = self.db.get_active_amount_as_of_height( + claim_hash, height + ) + else: + self.amount_cache[(claim_hash, txo_type, height)] = amount = self.db._get_active_amount( + claim_hash, txo_type, height + ) return amount - def _cached_get_effective_amount(self, claim_hash: bytes, support_only=False) -> int: - support_amount = self._cached_get_active_amount(claim_hash, ACTIVATED_SUPPORT_TXO_TYPE, self.db.db_height + 1) - if support_only: - return support_only - return support_amount + self._cached_get_active_amount( - claim_hash, ACTIVATED_CLAIM_TXO_TYPE, self.db.db_height + 1 - ) - def _get_pending_claim_amount(self, name: str, claim_hash: bytes, height=None) -> int: if (name, claim_hash) in self.activated_claim_amount_by_name_and_hash: return self.activated_claim_amount_by_name_and_hash[(name, claim_hash)] diff --git a/lbry/wallet/server/leveldb.py b/lbry/wallet/server/leveldb.py index 9963ed943..8d7342cde 100644 --- a/lbry/wallet/server/leveldb.py +++ b/lbry/wallet/server/leveldb.py @@ -402,6 +402,14 @@ class LevelDB: claim_hash, txo_type, height), include_key=False) ) + def get_active_amount_as_of_height(self, claim_hash: bytes, height: int) -> int: + for v in self.db.iterator( + start=Prefixes.active_amount.pack_partial_key(claim_hash, ACTIVATED_CLAIM_TXO_TYPE, 0), + stop=Prefixes.active_amount.pack_partial_key(claim_hash, ACTIVATED_CLAIM_TXO_TYPE, height), + include_key=False, reverse=True): + return Prefixes.active_amount.unpack_value(v).amount + return 0 + def get_effective_amount(self, claim_hash: bytes, support_only=False) -> int: support_amount = self._get_active_amount(claim_hash, ACTIVATED_SUPPORT_TXO_TYPE, self.db_height + 1) if support_only: