Browse Source

Merge pull request #4049 from SomberNight/cache_coin_price

speed-up wallet.get_full_history: cache coin_price
3.2.x
ThomasV 7 years ago
committed by GitHub
parent
commit
646044b431
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      lib/wallet.py

11
lib/wallet.py

@ -229,6 +229,8 @@ class Abstract_Wallet(PrintError):
self.invoices = InvoiceStore(self.storage) self.invoices = InvoiceStore(self.storage)
self.contacts = Contacts(self.storage) self.contacts = Contacts(self.storage)
self.coin_price_cache = {}
def diagnostic_name(self): def diagnostic_name(self):
return self.basename() return self.basename()
@ -1757,8 +1759,14 @@ class Abstract_Wallet(PrintError):
Acquisition price of a coin. Acquisition price of a coin.
This assumes that either all inputs are mine, or no input is mine. This assumes that either all inputs are mine, or no input is mine.
""" """
cache_key = "{}:{}:{}".format(str(txid), str(ccy), str(txin_value))
result = self.coin_price_cache.get(cache_key, None)
if result is not None:
return result
if self.txi.get(txid, {}) != {}: if self.txi.get(txid, {}) != {}:
return self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN) result = self.average_price(txid, price_func, ccy) * txin_value/Decimal(COIN)
self.coin_price_cache[cache_key] = result
return result
else: else:
fiat_value = self.get_fiat_value(txid, ccy) fiat_value = self.get_fiat_value(txid, ccy)
if fiat_value is not None: if fiat_value is not None:
@ -1767,6 +1775,7 @@ class Abstract_Wallet(PrintError):
p = self.price_at_timestamp(txid, price_func) p = self.price_at_timestamp(txid, price_func)
return p * txin_value/Decimal(COIN) return p * txin_value/Decimal(COIN)
class Simple_Wallet(Abstract_Wallet): class Simple_Wallet(Abstract_Wallet):
# wallet with a single keystore # wallet with a single keystore

Loading…
Cancel
Save