transaction: TxInput: store addr/spk/value_sats, instead of re-calc
We were re-calculating txin.address from the prevtx-utxo on every call, e.g. in electrum/gui/qt/utxo_list.py#L103 (for every utxo in the wallet). For a wallet with ~1300 utxos, UTXOList.update() took ~3.2 sec before this, now it's ~0.8 sec.
This commit is contained in:
@@ -265,6 +265,9 @@ class TxInput:
|
|||||||
self.spent_height = None # type: Optional[int] # height at which the TXO got spent
|
self.spent_height = None # type: Optional[int] # height at which the TXO got spent
|
||||||
self.spent_txid = None # type: Optional[str] # txid of the spender
|
self.spent_txid = None # type: Optional[str] # txid of the spender
|
||||||
self._utxo = None # type: Optional[Transaction]
|
self._utxo = None # type: Optional[Transaction]
|
||||||
|
self.__scriptpubkey = None # type: Optional[bytes]
|
||||||
|
self.__address = None # type: Optional[str]
|
||||||
|
self.__value_sats = None # type: Optional[int]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def short_id(self):
|
def short_id(self):
|
||||||
@@ -289,6 +292,11 @@ class TxInput:
|
|||||||
return
|
return
|
||||||
self.validate_data(utxo=tx)
|
self.validate_data(utxo=tx)
|
||||||
self._utxo = tx
|
self._utxo = tx
|
||||||
|
# update derived fields
|
||||||
|
out_idx = self.prevout.out_idx
|
||||||
|
self.__scriptpubkey = self._utxo.outputs()[out_idx].scriptpubkey
|
||||||
|
self.__address = get_address_from_output_script(self.__scriptpubkey)
|
||||||
|
self.__value_sats = self._utxo.outputs()[out_idx].value
|
||||||
|
|
||||||
def validate_data(self, *, utxo: Optional['Transaction'] = None, **kwargs) -> None:
|
def validate_data(self, *, utxo: Optional['Transaction'] = None, **kwargs) -> None:
|
||||||
utxo = utxo or self.utxo
|
utxo = utxo or self.utxo
|
||||||
@@ -308,23 +316,15 @@ class TxInput:
|
|||||||
return self._is_coinbase_output
|
return self._is_coinbase_output
|
||||||
|
|
||||||
def value_sats(self) -> Optional[int]:
|
def value_sats(self) -> Optional[int]:
|
||||||
if self.utxo:
|
return self.__value_sats
|
||||||
out_idx = self.prevout.out_idx
|
|
||||||
return self.utxo.outputs()[out_idx].value
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def address(self) -> Optional[str]:
|
def address(self) -> Optional[str]:
|
||||||
if self.scriptpubkey:
|
return self.__address
|
||||||
return get_address_from_output_script(self.scriptpubkey)
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scriptpubkey(self) -> Optional[bytes]:
|
def scriptpubkey(self) -> Optional[bytes]:
|
||||||
if self.utxo:
|
return self.__scriptpubkey
|
||||||
out_idx = self.prevout.out_idx
|
|
||||||
return self.utxo.outputs()[out_idx].scriptpubkey
|
|
||||||
return None
|
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
d = {
|
d = {
|
||||||
@@ -1560,6 +1560,8 @@ class PartialTxInput(TxInput, PSBTSection):
|
|||||||
return addr
|
return addr
|
||||||
if self._trusted_address is not None:
|
if self._trusted_address is not None:
|
||||||
return self._trusted_address
|
return self._trusted_address
|
||||||
|
if self.witness_utxo:
|
||||||
|
return self.witness_utxo.address
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
Reference in New Issue
Block a user