From 8560930bac223e8bd0fa06bf469876129734a0fa Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 1 Mar 2020 09:57:59 +0100 Subject: [PATCH] transaction: (fix) invalidate ser cache when changing locktime/version we can keep the same API, using @property, instead of introducing getters and setters --- electrum/tests/test_transaction.py | 12 ++++++++++++ electrum/transaction.py | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/electrum/tests/test_transaction.py b/electrum/tests/test_transaction.py index 852d3fd86..c8346c7c1 100644 --- a/electrum/tests/test_transaction.py +++ b/electrum/tests/test_transaction.py @@ -89,6 +89,18 @@ class TestTransaction(ElectrumTestCase): tx.update_signatures(signed_blob_signatures) self.assertEqual(tx.serialize(), signed_blob) + def test_tx_setting_locktime_invalidates_ser_cache(self): + tx = tx_from_any("cHNidP8BAJICAAAAAdAEtnw/IOVkr4oexG2xYnm+Vevsn3J7nbZsGpiBWS8MAQAAAAD9////A2Q5AwAAAAAAF6kUF6jKG6BuNVhq1RilflIDCitepw6H/NEEAAAAAAAXqRQx9SsFxDAaaOWbLB2ely1ZoZ61DYeIbQoAAAAAABYAFItCjFDsC28Z1R3tFaoi//pcInvnI3AZAAABAR+weRIAAAAAABYAFEK0I6qyqoA/lXCEgysQNZvqokaQIgYC9tgRn6/8hlDLEvEg3lKD1HmNim0gGRYwt4x3aJURIq4MqAq7DwEAAAAUAAAAAAAAIgICXYdVjyDIufLQ3yeDA4M8016luFER2SWaGPk6UF8CbuQMqAq7DwEAAAAXAAAAAA==") + self.assertEqual("2774c819a05e44861a0555401d2741e6c03079cc4d892c69b910c0f52f407859", tx.txid()) + tx.locktime = 111222333 + self.assertEqual("3d33a69c3f7717840b266c24ae1a6d29486820249b47261232e93ee118a6565b", tx.txid()) + + def test_tx_setting_version_invalidates_ser_cache(self): + tx = tx_from_any("cHNidP8BAJICAAAAAdAEtnw/IOVkr4oexG2xYnm+Vevsn3J7nbZsGpiBWS8MAQAAAAD9////A2Q5AwAAAAAAF6kUF6jKG6BuNVhq1RilflIDCitepw6H/NEEAAAAAAAXqRQx9SsFxDAaaOWbLB2ely1ZoZ61DYeIbQoAAAAAABYAFItCjFDsC28Z1R3tFaoi//pcInvnI3AZAAABAR+weRIAAAAAABYAFEK0I6qyqoA/lXCEgysQNZvqokaQIgYC9tgRn6/8hlDLEvEg3lKD1HmNim0gGRYwt4x3aJURIq4MqAq7DwEAAAAUAAAAAAAAIgICXYdVjyDIufLQ3yeDA4M8016luFER2SWaGPk6UF8CbuQMqAq7DwEAAAAXAAAAAA==") + self.assertEqual("2774c819a05e44861a0555401d2741e6c03079cc4d892c69b910c0f52f407859", tx.txid()) + tx.version = 555 + self.assertEqual("8a9b89a1a7aac1995dd013069d9866197d77c14c22315958d612fc02fd4b596a", tx.txid()) + def test_tx_deserialize_for_signed_network_tx(self): tx = transaction.Transaction(signed_blob) tx.deserialize() diff --git a/electrum/transaction.py b/electrum/transaction.py index 041d5b1d6..cbca474e7 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -526,11 +526,29 @@ class Transaction: raise Exception(f"cannot initialize transaction from {raw}") self._inputs = None # type: List[TxInput] self._outputs = None # type: List[TxOutput] - self.locktime = 0 - self.version = 2 + self._locktime = 0 + self._version = 2 self._cached_txid = None # type: Optional[str] + @property + def locktime(self): + return self._locktime + + @locktime.setter + def locktime(self, value): + self._locktime = value + self.invalidate_ser_cache() + + @property + def version(self): + return self._version + + @version.setter + def version(self, value): + self._version = value + self.invalidate_ser_cache() + def to_json(self) -> dict: d = { 'version': self.version,