diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index 393215207..114aaf372 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -132,6 +132,7 @@ CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion joinSplitPubKey(tx.joinSplitPubKey), joinSplitSig(tx.joinSplitSig) { UpdateHash(); + UpdateTxid(); } CTransaction& CTransaction::operator=(const CTransaction &tx) { @@ -143,6 +144,7 @@ CTransaction& CTransaction::operator=(const CTransaction &tx) { *const_cast(&joinSplitPubKey) = tx.joinSplitPubKey; *const_cast(&joinSplitSig) = tx.joinSplitSig; *const_cast(&hash) = tx.hash; + *const_cast(&txid) = tx.txid; return *this; } @@ -227,7 +229,7 @@ std::string CTransaction::ToString() const // Return a txid which is non-malleable. // Signature data is cleared before the transaction is serialized and hashed. -uint256 CTransaction::GetTxid() const +void CTransaction::UpdateTxid() const { // Create a deep copy of this transaction CMutableTransaction tx(*this); @@ -241,10 +243,9 @@ uint256 CTransaction::GetTxid() const tx.joinSplitSig.assign(0); // Return double SHA256 hash - return tx.GetSerializeHash(); + *const_cast(&txid) = tx.GetSerializeHash(); } - // Return a txid which is non-malleable. uint256 CMutableTransaction::GetTxid() const { diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index ed11e5c30..ab3f60883 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -288,6 +288,8 @@ private: /** Memory only. */ const uint256 hash; void UpdateHash() const; + uint256 txid; + void UpdateTxid() const; public: typedef boost::array joinsplit_sig_t; @@ -331,8 +333,10 @@ public: READWRITE(*const_cast(&joinSplitSig)); } } - if (ser_action.ForRead()) + if (ser_action.ForRead()) { UpdateHash(); + UpdateTxid(); + } } bool IsNull() const { @@ -374,9 +378,10 @@ public: std::string ToString() const; - // Return the txid which is the double SHA256 hash of the transaction. - uint256 GetTxid() const; - + // Return the txid, which is the double SHA256 hash over portions of the transaction. + const uint256& GetTxid() const { + return txid; + } }; /** A mutable version of CTransaction. */