From 1a5912877e2ab6dbb5e5724b15edbd9a5f4ce051 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Sat, 24 Jul 2021 14:25:37 -0400 Subject: [PATCH] faster get_future_activated --- lbry/wallet/server/block_processor.py | 6 +++--- lbry/wallet/server/leveldb.py | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index 5d8fb7599..18bb33755 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -895,16 +895,16 @@ class BlockProcessor: # upon the delayed activation of B, we need to detect to activate C and make it take over early instead claim_exists = {} - for activated, activated_txos in self.db.get_future_activated(height).items(): + for activated, activated_claim_txo in self.db.get_future_activated(height): # uses the pending effective amount for the future activation height, not the current height future_amount = self._get_pending_claim_amount( - activated.name, activated.claim_hash, activated_txos[-1].height + 1 + activated.name, activated.claim_hash, activated_claim_txo.height + 1 ) if activated.claim_hash not in claim_exists: claim_exists[activated.claim_hash] = activated.claim_hash in self.claim_hash_to_txo or ( self.db.get_claim_txo(activated.claim_hash) is not None) if claim_exists[activated.claim_hash] and activated.claim_hash not in self.abandoned_claims: - v = future_amount, activated, activated_txos[-1] + v = future_amount, activated, activated_claim_txo future_activations[activated.name][activated.claim_hash] = v for name, future_activated in activate_in_future.items(): diff --git a/lbry/wallet/server/leveldb.py b/lbry/wallet/server/leveldb.py index 7ba1347a4..b0bde437f 100644 --- a/lbry/wallet/server/leveldb.py +++ b/lbry/wallet/server/leveldb.py @@ -652,15 +652,17 @@ class LevelDB: activated[v].append(k) return activated - def get_future_activated(self, height: int) -> DefaultDict[PendingActivationValue, List[PendingActivationKey]]: - activated = defaultdict(list) + def get_future_activated(self, height: int) -> typing.Generator[ + Tuple[PendingActivationValue, PendingActivationKey], None, None]: + yielded = set() start_prefix = Prefixes.pending_activation.pack_partial_key(height + 1) stop_prefix = Prefixes.pending_activation.pack_partial_key(height + 1 + self.coin.maxTakeoverDelay) - for _k, _v in self.db.iterator(start=start_prefix, stop=stop_prefix): - k = Prefixes.pending_activation.unpack_key(_k) + for _k, _v in self.db.iterator(start=start_prefix, stop=stop_prefix, reverse=True): v = Prefixes.pending_activation.unpack_value(_v) - activated[v].append(k) - return activated + if v not in yielded: + yielded.add(v) + k = Prefixes.pending_activation.unpack_key(_k) + yield v, k async def _read_tx_counts(self): if self.tx_counts is not None: