diff --git a/electrum/transaction.py b/electrum/transaction.py index cca06ff189db..e074235db196 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -137,6 +137,10 @@ def __init__(self, *, scriptpubkey: bytes, value: Union[int, str]): raise ValueError(f"bad txout value: {value!r}") self.value = value # int in satoshis; or spend-max-like str + + # Precompute hash for efficiency in __hash__, since TxOutput is likely immutable (and both scriptpubkey and value are immutable) + self._hash: int = hash((self.scriptpubkey, self.value)) + @classmethod def from_address_and_value(cls, address: str, value: Union[int, str]) -> Union['TxOutput', 'PartialTxOutput']: return cls(scriptpubkey=bitcoin.address_to_script(address), @@ -204,7 +208,8 @@ def __ne__(self, other): return not (self == other) def __hash__(self) -> int: - return hash((self.scriptpubkey, self.value)) + # Return precomputed hash value instead of recalculating each call. + return self._hash def to_json(self): d = { @@ -625,9 +630,11 @@ def read_uint64(self): return self._read_num('