Skip to content

Commit fa72f09

Browse files
author
MarcoFalke
committed
Remove CHashWriter type
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.
1 parent fa4a9c0 commit fa72f09

File tree

6 files changed

+8
-19
lines changed

6 files changed

+8
-19
lines changed

src/hash.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,11 @@ class HashWriter
149149
class CHashWriter : public HashWriter
150150
{
151151
private:
152-
const int nType;
153152
const int nVersion;
154153

155154
public:
156-
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
155+
CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}
157156

158-
int GetType() const { return nType; }
159157
int GetVersion() const { return nVersion; }
160158

161159
template<typename T>
@@ -223,15 +221,6 @@ class HashedSourceWriter : public HashWriter
223221
}
224222
};
225223

226-
/** Compute the 256-bit hash of an object's serialization. */
227-
template<typename T>
228-
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
229-
{
230-
CHashWriter ss(nType, nVersion);
231-
ss << obj;
232-
return ss.GetHash();
233-
}
234-
235224
/** Single-SHA256 a 32-byte input (represented as uint256). */
236225
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
237226

src/primitives/block.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
uint256 CBlockHeader::GetHash() const
1212
{
13-
return SerializeHash(*this);
13+
return (CHashWriter{PROTOCOL_VERSION} << *this).GetHash();
1414
}
1515

1616
std::string CBlock::ToString() const

src/primitives/transaction.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin),
6767

6868
uint256 CMutableTransaction::GetHash() const
6969
{
70-
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
70+
return (CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
7171
}
7272

7373
uint256 CTransaction::ComputeHash() const
7474
{
75-
return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
75+
return (CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash();
7676
}
7777

7878
uint256 CTransaction::ComputeWitnessHash() const
7979
{
8080
if (!HasWitness()) {
8181
return hash;
8282
}
83-
return SerializeHash(*this, SER_GETHASH, 0);
83+
return (CHashWriter{0} << *this).GetHash();
8484
}
8585

8686
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}

src/test/hash_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE(siphash)
122122
(uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56));
123123
}
124124

125-
CHashWriter ss(SER_DISK, CLIENT_VERSION);
125+
CHashWriter ss{CLIENT_VERSION};
126126
CMutableTransaction tx;
127127
// Note these tests were originally written with tx.nVersion=1
128128
// and the test would be affected by default tx version bumps if not fixed.

src/test/serialize_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ BOOST_AUTO_TEST_CASE(vector_bool)
176176
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};
177177

178178
BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
179-
BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
179+
BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
180180
}
181181

182182
BOOST_AUTO_TEST_CASE(noncanonical)

src/test/sighash_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
7878
}
7979

8080
// Serialize and hash
81-
CHashWriter ss(SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
81+
CHashWriter ss{SERIALIZE_TRANSACTION_NO_WITNESS};
8282
ss << txTmp << nHashType;
8383
return ss.GetHash();
8484
}

0 commit comments

Comments
 (0)