From ede9b2b37256992c83e35949c09f12b3ee795efa Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Sat, 29 May 2021 20:44:15 +0200 Subject: [PATCH] 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