Permalink
Browse files

Refactor GetTxid() into UpdateTxid() to match coding style of hash me…

…mber variable.

UpdateTxid() is called alongside UpdateHash() when a CTransaction is
deserialized or constructed.  GetTxid() now returns a const reference.
  • Loading branch information...
bitcartel committed Jul 27, 2016
1 parent 2930626 commit 07e6d5b02533d829e6333ac4cbac671dff3104b9
Showing with 13 additions and 7 deletions.
  1. +4 −3 src/primitives/transaction.cpp
  2. +9 −4 src/primitives/transaction.h
@@ -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<uint256*>(&joinSplitPubKey) = tx.joinSplitPubKey;
*const_cast<joinsplit_sig_t*>(&joinSplitSig) = tx.joinSplitSig;
*const_cast<uint256*>(&hash) = tx.hash;
*const_cast<uint256*>(&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<uint256*>(&txid) = tx.GetSerializeHash();
}


// Return a txid which is non-malleable.
uint256 CMutableTransaction::GetTxid() const
{
@@ -288,6 +288,8 @@ class CTransaction
/** Memory only. */
const uint256 hash;
void UpdateHash() const;
uint256 txid;
void UpdateTxid() const;

public:
typedef boost::array<unsigned char, 64> joinsplit_sig_t;
@@ -331,8 +333,10 @@ class CTransaction
READWRITE(*const_cast<joinsplit_sig_t*>(&joinSplitSig));
}
}
if (ser_action.ForRead())
if (ser_action.ForRead()) {
UpdateHash();
UpdateTxid();
}
}

bool IsNull() const {
@@ -374,9 +378,10 @@ class CTransaction

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. */

0 comments on commit 07e6d5b

Please sign in to comment.