faster get_future_activated

This commit is contained in:
Jack Robison 2021-07-24 14:25:37 -04:00 committed by Victor Shyba
parent 05b6bdb8f6
commit c81c0d9480
2 changed files with 11 additions and 9 deletions

View file

@ -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 # upon the delayed activation of B, we need to detect to activate C and make it take over early instead
claim_exists = {} 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 # uses the pending effective amount for the future activation height, not the current height
future_amount = self._get_pending_claim_amount( 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: if activated.claim_hash not in claim_exists:
claim_exists[activated.claim_hash] = activated.claim_hash in self.claim_hash_to_txo or ( 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) 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: 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 future_activations[activated.name][activated.claim_hash] = v
for name, future_activated in activate_in_future.items(): for name, future_activated in activate_in_future.items():

View file

@ -652,15 +652,17 @@ class LevelDB:
activated[v].append(k) activated[v].append(k)
return activated return activated
def get_future_activated(self, height: int) -> DefaultDict[PendingActivationValue, List[PendingActivationKey]]: def get_future_activated(self, height: int) -> typing.Generator[
activated = defaultdict(list) Tuple[PendingActivationValue, PendingActivationKey], None, None]:
yielded = set()
start_prefix = Prefixes.pending_activation.pack_partial_key(height + 1) start_prefix = Prefixes.pending_activation.pack_partial_key(height + 1)
stop_prefix = Prefixes.pending_activation.pack_partial_key(height + 1 + self.coin.maxTakeoverDelay) 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): for _k, _v in self.db.iterator(start=start_prefix, stop=stop_prefix, reverse=True):
k = Prefixes.pending_activation.unpack_key(_k)
v = Prefixes.pending_activation.unpack_value(_v) v = Prefixes.pending_activation.unpack_value(_v)
activated[v].append(k) if v not in yielded:
return activated yielded.add(v)
k = Prefixes.pending_activation.unpack_key(_k)
yield v, k
async def _read_tx_counts(self): async def _read_tx_counts(self):
if self.tx_counts is not None: if self.tx_counts is not None: