From 09e1aba567d0bc6f0946d19e0d76a691d0c3fa48 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 8 Sep 2022 11:38:55 -0400 Subject: [PATCH] use effective amount index for faster future amount calculations --- hub/db/db.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/hub/db/db.py b/hub/db/db.py index e2d37f5..d78ee1e 100644 --- a/hub/db/db.py +++ b/hub/db/db.py @@ -494,13 +494,22 @@ class SecondaryDB: return self.prefix_db.claim_to_txo.get(claim_hash) def _get_active_amount(self, claim_hash: bytes, txo_type: int, height: int) -> int: - if height == self.db_height + 1: + if height >= self.db_height + 1: v = self.prefix_db.effective_amount.get(claim_hash) if not v: - return 0 - if txo_type is ACTIVATED_SUPPORT_TXO_TYPE: - return v.support_sum - return v.effective_amount - v.support_sum + amount = 0 + elif txo_type is ACTIVATED_SUPPORT_TXO_TYPE: + amount = v.support_sum + else: + amount = v.effective_amount - v.support_sum + if height == self.db_height + 1: + return amount + return amount + sum( + v.amount for v in self.prefix_db.active_amount.iterate( + start=(claim_hash, txo_type, self.db_height + 1), + stop=(claim_hash, txo_type, height), include_key=False + ) + ) return sum( v.amount for v in self.prefix_db.active_amount.iterate( start=(claim_hash, txo_type, 0), stop=(claim_hash, txo_type, height), include_key=False