From 71697afabd1a378d810154535c15c5c3b422237e Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 8 Feb 2023 00:40:46 +0000 Subject: [PATCH 1/3] invoices: get_outputs to use .outputs field if available It is wasteful to create new PartialTxOutput objects if we already have an outputs field. Btw apparently lightning invoices too have an outputs field. --- electrum/invoices.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/electrum/invoices.py b/electrum/invoices.py index e7b6c46de..52fbeb68f 100644 --- a/electrum/invoices.py +++ b/electrum/invoices.py @@ -1,5 +1,5 @@ import time -from typing import TYPE_CHECKING, List, Optional, Union, Dict, Any +from typing import TYPE_CHECKING, List, Optional, Union, Dict, Any, Sequence from decimal import Decimal import attr @@ -126,16 +126,13 @@ class Invoice(StoredObject): address = self._lnaddr.get_fallback_address() or None return address - def get_outputs(self): - if self.is_lightning(): + def get_outputs(self) -> Sequence[PartialTxOutput]: + outputs = self.outputs or [] + if not outputs: address = self.get_address() amount = self.get_amount_sat() if address and amount is not None: outputs = [PartialTxOutput.from_address_and_value(address, int(amount))] - else: - outputs = [] - else: - outputs = self.outputs return outputs def can_be_paid_onchain(self) -> bool: From ede9b2b37256992c83e35949c09f12b3ee795efa Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Sat, 29 May 2021 20:44:15 +0200 Subject: [PATCH 2/3] transaction: cache address determination from output script In order to avoid repeatedly calling get_addr_from_output_script() on every read of the "TxOutput.address" property, determine and cache it only whenever the output script is created/changed. --- electrum/transaction.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/electrum/transaction.py b/electrum/transaction.py index bcf44e8be..425ce9a80 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -148,9 +148,18 @@ class TxOutput: return cls(scriptpubkey=bfh(addr), value=val) raise Exception(f"unexpected legacy address type: {_type}") + @property + def scriptpubkey(self) -> bytes: + return self._scriptpubkey + + @scriptpubkey.setter + def scriptpubkey(self, scriptpubkey: bytes): + self._scriptpubkey = scriptpubkey + self._address = get_address_from_output_script(scriptpubkey) + @property def address(self) -> Optional[str]: - return get_address_from_output_script(self.scriptpubkey) # TODO cache this? + return self._address def get_ui_address_str(self) -> str: addr = self.address From 1e3f9b942f66f990d96a135fc1bee707a5222823 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 8 Feb 2023 00:45:18 +0000 Subject: [PATCH 3/3] qt: MyTreeView.refresh_all to use maybe_defer_update In particular, window.timer_actions() calls request_list.refresh_all() and invoice_list.refresh_all(), every 0.5 seconds. We avoid doing this at least when those lists are not visible anyway. --- electrum/gui/qt/util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py index 8d1e54c13..fce6f4474 100644 --- a/electrum/gui/qt/util.py +++ b/electrum/gui/qt/util.py @@ -813,6 +813,8 @@ class MyTreeView(QTreeView): return row def refresh_all(self): + if self.maybe_defer_update(): + return for row in range(0, self.std_model.rowCount()): item = self.std_model.item(row, 0) key = item.data(self.key_role)