Skip to content

Commit

Permalink
Remove CHashWriter type
Browse files Browse the repository at this point in the history
The type is only ever set, but never read via GetType(), so remove it.
Also, remove SerializeHash to avoid silent merge conflicts and use the
already existing GetHash() boilerplate consistently.
  • Loading branch information
MarcoFalke authored and HashEngineering committed Oct 11, 2023
1 parent b99e288 commit 6f09bf5
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 26 deletions.
13 changes: 1 addition & 12 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ class HashWriter
class CHashWriter : public HashWriter
{
private:
const int nType;
const int nVersion;

public:
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}

int GetType() const { return nType; }
int GetVersion() const { return nVersion; }

template<typename T>
Expand Down Expand Up @@ -225,15 +223,6 @@ class HashedSourceWriter : public HashWriter
}
};

/** Compute the 256-bit hash of an object's serialization. */
template<typename T>
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
{
CHashWriter ss(nType, nVersion);
ss << obj;
return ss.GetHash();
}

/** Single-SHA256 a 32-byte input (represented as uint256). */
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);

Expand Down
6 changes: 2 additions & 4 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include <primitives/block.h>

#include <hash.h>
#include <groestlcoin.h>
#include <tinyformat.h>

uint256 CBlockHeader::GetHash() const
{
XCoin::CGroestlHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); // GRS
ss << *this; // GRS
return ss.GetHash(); // GRS
// return SerializeHash(*this);
return (XCoin::CGroestlHashWriter{PROTOCOL_VERSION} << *this).GetHash();
}

std::string CBlock::ToString() const
Expand Down
11 changes: 4 additions & 7 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <consensus/amount.h>
#include <hash.h>
#include <groestlcoin.h>
#include <script/script.h>
#include <serialize.h>
#include <tinyformat.h>
Expand Down Expand Up @@ -67,24 +68,20 @@ CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin),

uint256 CMutableTransaction::GetHash() const
{
// GRS uses single SHA256
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
return (XCoin::CGroestlHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
}

uint256 CTransaction::ComputeHash() const
{
uint256 hash;
// GRS uses single SHA256
*const_cast<uint256*>(&hash) = SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
return hash;
return (XCoin::CGroestlHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
}

uint256 CTransaction::ComputeWitnessHash() const
{
if (!HasWitness()) {
return hash;
}
return SerializeHash(*this, SER_GETHASH, 0);
return (CHashWriter{0} << *this).GetHash();
}

CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/hash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE(siphash)
(uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56));
}

CHashWriter ss(SER_DISK, CLIENT_VERSION);
CHashWriter ss{CLIENT_VERSION};
CMutableTransaction tx;
// Note these tests were originally written with tx.nVersion=1
// and the test would be affected by default tx version bumps if not fixed.
Expand Down
2 changes: 1 addition & 1 deletion src/test/serialize_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ BOOST_AUTO_TEST_CASE(vector_bool)
std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};

BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
}

BOOST_AUTO_TEST_CASE(noncanonical)
Expand Down
2 changes: 1 addition & 1 deletion src/test/sighash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
}

// Serialize and hash
CHashWriter ss(SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
CHashWriter ss{SERIALIZE_TRANSACTION_NO_WITNESS};
ss << txTmp << nHashType;
return ss.GetHash();
}
Expand Down

0 comments on commit 6f09bf5

Please sign in to comment.