Skip to content

Commit 980fbfb

Browse files
committed
Type-safe OutputHash.
This introduces a new, different type OutputHash for hashes of outputs, e.g. in COutPoint and all related places in the code. This type is functionally equivalent to uint256, but it enables the compiler to ensure that all places that should be using an UTXO hasher are actually using it.
1 parent 92c00f6 commit 980fbfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+210
-102
lines changed

divi/src/BlockMemoryPoolTransactionCollector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <defaultValues.h>
1212
#include <Logging.h>
1313
#include <TransactionOpCounting.h>
14+
#include <OutputHash.h>
1415
#include <UtxoCheckingAndUpdating.h>
1516

1617
#include <Settings.h>
@@ -53,7 +54,7 @@ class COrphan
5354
{
5455
public:
5556
const CTransaction* ptx;
56-
std::set<uint256> setDependsOn;
57+
std::set<OutputHash> setDependsOn;
5758
CFeeRate feeRate;
5859
double dPriority;
5960

@@ -141,7 +142,7 @@ void BlockMemoryPoolTransactionCollector::ComputeTransactionPriority (
141142

142143
void BlockMemoryPoolTransactionCollector::AddDependingTransactionsToPriorityQueue (
143144
DependingTransactionsMap& dependentTransactions,
144-
const uint256& hash,
145+
const OutputHash& hash,
145146
std::vector<TxPriority>& vecPriority,
146147
TxPriorityCompare& comparer) const
147148
{

divi/src/BlockMemoryPoolTransactionCollector.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CTxMemPool;
2626
class CBlockTemplate;
2727
class CBlockHeader;
2828
class CFeeRate;
29+
class OutputHash;
2930
class Settings;
3031

3132
template <typename MutexObj>
@@ -51,7 +52,7 @@ class CChain;
5152
class BlockMemoryPoolTransactionCollector: public I_BlockTransactionCollector
5253
{
5354
private:
54-
using DependingTransactionsMap = std::map<uint256, std::vector<std::shared_ptr<COrphan>>>;
55+
using DependingTransactionsMap = std::map<OutputHash, std::vector<std::shared_ptr<COrphan>>>;
5556

5657
CCoinsViewCache* baseCoinsViewCache_;
5758
const CChain& activeChain_;
@@ -80,7 +81,7 @@ class BlockMemoryPoolTransactionCollector: public I_BlockTransactionCollector
8081
const CTransaction* mempoolTx) const;
8182
void AddDependingTransactionsToPriorityQueue (
8283
DependingTransactionsMap& mapDependers,
83-
const uint256& hash,
84+
const OutputHash& hash,
8485
std::vector<TxPriority>& vecPriority,
8586
TxPriorityCompare& comparer) const;
8687

divi/src/IndexDatabaseUpdates.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vector>
44
#include <utility>
55
#include <addressindex.h>
6+
#include <OutputHash.h>
67
#include <spentindex.h>
78
#include <uint256.h>
89

@@ -35,7 +36,7 @@ struct IndexDatabaseUpdates
3536
struct TransactionLocationReference
3637
{
3738
uint256 hash;
38-
uint256 utxoHash;
39+
OutputHash utxoHash;
3940
unsigned blockHeight;
4041
int transactionIndex;
4142

divi/src/Logging.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <DataDirectory.h>
1111
#include <chainparamsbase.h>
12+
#include <OutputHash.h>
1213
#include <uint256.h>
1314
#include <serialize.h>
1415
#include <Settings.h>
@@ -22,6 +23,7 @@ bool fLogIPs = false;
2223

2324
extern Settings& settings;
2425

26+
LOG_FORMAT_WITH_TOSTRING(OutputHash)
2527
LOG_FORMAT_WITH_TOSTRING(uint256)
2628

2729
namespace

divi/src/Logging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ template <typename T>
5050
} \
5151
};
5252
LOG_WITH_CONVERSION(CLockLocation)
53+
LOG_WITH_CONVERSION(OutputHash)
5354
LOG_WITH_CONVERSION(uint256)
5455

5556
/* Defined in Logging-common.cpp. */

divi/src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ BITCOIN_CORE_H = \
246246
BlockMemoryPoolTransactionCollector.h \
247247
CoinMinter.h \
248248
CoinMintingModule.h \
249+
OutputHash.h \
249250
PoSTransactionCreator.h \
250251
PeerNotificationOfMintService.h \
251252
MonthlyWalletBackupCreator.h \

divi/src/MasternodeModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ void LockUpMasternodeCollateral(const Settings& settings, std::function<void(con
434434
{
435435
LogPrintf(" %s %s\n", mne.getTxHash(), mne.getOutputIndex());
436436
mnTxHash.SetHex(mne.getTxHash());
437-
COutPoint outpoint(mnTxHash, boost::lexical_cast<unsigned int>(mne.getOutputIndex()));
437+
COutPoint outpoint(OutputHash(mnTxHash), boost::lexical_cast<unsigned int>(mne.getOutputIndex()));
438438
walletUtxoLockingFunction(outpoint);
439439
}
440440
}

divi/src/OrphanTransactions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ struct COrphanTx {
1717
NodeId fromPeer;
1818
};
1919
std::map<uint256, COrphanTx> mapOrphanTransactions;
20-
std::map<uint256, std::set<uint256> > mapOrphanTransactionsByPrev;
20+
std::map<OutputHash, std::set<uint256> > mapOrphanTransactionsByPrev;
2121

2222

2323
//////////////////////////////////////////////////////////////////////////////
2424
//
2525
// mapOrphanTransactions
2626
//
27-
const std::set<uint256>& GetOrphanSpendingTransactionIds(const uint256& txHash)
27+
const std::set<uint256>& GetOrphanSpendingTransactionIds(const OutputHash& txHash)
2828
{
2929
static std::set<uint256> emptySet;
3030
const auto it = mapOrphanTransactionsByPrev.find(txHash);

divi/src/OrphanTransactions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#ifndef ORPHAN_TRANSACTIONS_H
22
#define ORPHAN_TRANSACTIONS_H
33
#include <NodeId.h>
4+
#include <OutputHash.h>
45
#include <uint256.h>
56
#include <set>
67
class CTransaction;
7-
const std::set<uint256>& GetOrphanSpendingTransactionIds(const uint256& txHash);
8+
const std::set<uint256>& GetOrphanSpendingTransactionIds(const OutputHash& txHash);
89
const CTransaction& GetOrphanTransaction(const uint256& txHash, NodeId& peer);
910
bool OrphanTransactionIsKnown(const uint256& hash);
1011
bool AddOrphanTx(const CTransaction& tx, NodeId peer);

divi/src/OutputHash.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef OUTPUT_HASH_H
2+
#define OUTPUT_HASH_H
3+
4+
#include "serialize.h"
5+
#include "uint256.h"
6+
7+
/** This class is "equivalent" to a uint256, but semantically it represents
8+
* a specific use, namely the hash used for an outpoint in the UTXO set
9+
* and referred to by following transactions. This is the normal txid
10+
* originally, but may be changed in the future e.g. with segwit-light. */
11+
class OutputHash
12+
{
13+
14+
private:
15+
16+
/** The actual hash value. */
17+
uint256 value;
18+
19+
public:
20+
21+
OutputHash () = default;
22+
OutputHash (const OutputHash&) = default;
23+
OutputHash& operator= (const OutputHash&) = default;
24+
25+
explicit OutputHash (const uint256& val)
26+
: value(val)
27+
{}
28+
29+
ADD_SERIALIZE_METHODS;
30+
31+
template<typename Stream, typename Operation>
32+
inline void SerializationOp (Stream& s, Operation ser_action,
33+
int nType, int nVersion)
34+
{
35+
READWRITE (value);
36+
}
37+
38+
inline const uint256& GetValue () const
39+
{
40+
return value;
41+
}
42+
43+
inline void SetNull ()
44+
{
45+
value.SetNull ();
46+
}
47+
48+
inline bool IsNull () const
49+
{
50+
return value.IsNull ();
51+
}
52+
53+
inline std::string ToString () const
54+
{
55+
return value.ToString ();
56+
}
57+
58+
inline std::string GetHex () const
59+
{
60+
return value.GetHex ();
61+
}
62+
63+
inline friend bool operator== (const OutputHash& a, const OutputHash& b)
64+
{
65+
return a.value == b.value;
66+
}
67+
68+
inline friend bool operator!= (const OutputHash& a, const OutputHash& b)
69+
{
70+
return !(a == b);
71+
}
72+
73+
inline friend bool operator< (const OutputHash& a, const OutputHash& b)
74+
{
75+
return a.value < b.value;
76+
}
77+
78+
};
79+
80+
#endif // OUTPUT_HASH_H

0 commit comments

Comments
 (0)