Skip to content

Commit 420f833

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 3be6b14 commit 420f833

Some content is hidden

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

44 files changed

+219
-104
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

@@ -142,7 +143,7 @@ void BlockMemoryPoolTransactionCollector::ComputeTransactionPriority (
142143

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

divi/src/BlockMemoryPoolTransactionCollector.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CTxMemPool;
2727
class CBlockTemplate;
2828
class CBlockHeader;
2929
class CFeeRate;
30+
class OutputHash;
3031
class Settings;
3132

3233
struct PrioritizedTransactionData
@@ -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/I_StakingCoinSelector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <I_KeypoolReserver.h>
77

88
class CMerkleTx;
9+
class OutputHash;
910

1011
class I_StakingCoinSelector
1112
{
@@ -22,6 +23,6 @@ class I_StakingWallet: public virtual CKeyStore, public I_StakingCoinSelector, p
2223

2324
/** Returns the UTXO hash that should be used for spending outputs
2425
* from the given transaction (which should be part of the wallet). */
25-
virtual uint256 GetUtxoHash(const CMerkleTx& tx) const = 0;
26+
virtual OutputHash GetUtxoHash(const CMerkleTx& tx) const = 0;
2627
};
2728
#endif// I_STAKING_COIN_SELECTOR_H

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
@@ -255,6 +255,7 @@ BITCOIN_CORE_H = \
255255
BlockMemoryPoolTransactionCollector.h \
256256
CoinMinter.h \
257257
CoinMintingModule.h \
258+
OutputHash.h \
258259
PoSTransactionCreator.h \
259260
PeerNotificationOfMintService.h \
260261
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

divi/src/SpentOutputTracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SpentOutputTracker::SpentOutputTracker(
1919
* Outpoint is spent if any non-conflicted transaction
2020
* spends it:
2121
*/
22-
bool SpentOutputTracker::IsSpent(const uint256& hash, unsigned int n) const
22+
bool SpentOutputTracker::IsSpent(const OutputHash& hash, unsigned int n) const
2323
{
2424
const COutPoint outpoint(hash, n);
2525
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;

divi/src/SpentOutputTracker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class WalletTransactionRecord;
99
class COutPoint;
10-
class uint256;
10+
class OutputHash;
1111
class CWalletTx;
1212
class I_MerkleTxConfirmationNumberCalculator;
1313

@@ -36,7 +36,7 @@ class SpentOutputTracker
3636
const CWalletTx& newlyAddedTransaction,
3737
int64_t orderedTransactionIndex=0,
3838
bool loadedFromDisk=false);
39-
bool IsSpent(const uint256& hash, unsigned int n) const;
39+
bool IsSpent(const OutputHash& hash, unsigned int n) const;
4040
std::set<uint256> GetConflictingTxHashes(const CWalletTx& tx) const;
4141
};
4242
#endif// SPENT_OUTPUT_TRACKER_H

divi/src/TransactionDiskAccessor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
5858
int nHeight = -1;
5959
{
6060
CCoinsViewCache& view = *pcoinsTip;
61-
const CCoins* coins = view.AccessCoins(hash);
61+
const CCoins* coins = view.AccessCoins(OutputHash(hash));
6262
if (coins)
6363
nHeight = coins->nHeight;
6464
}
@@ -83,6 +83,11 @@ bool GetTransaction(const uint256& hash, CTransaction& txOut, uint256& hashBlock
8383
return false;
8484
}
8585

86+
bool GetTransaction(const OutputHash& hash, CTransaction& txOut, uint256& hashBlock, bool fAllowSlow)
87+
{
88+
return GetTransaction(hash.GetValue(), txOut, hashBlock, fAllowSlow);
89+
}
90+
8691
bool CollateralIsExpectedAmount(const COutPoint &outpoint, int64_t expectedAmount)
8792
{
8893
CCoins coins;

divi/src/TransactionDiskAccessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
class uint256;
55
class CTransaction;
66
class COutPoint;
7+
class OutputHash;
78

89
/** Get transaction from mempool or disk **/
910
bool GetTransaction(const uint256& hash, CTransaction& tx, uint256& hashBlock, bool fAllowSlow);
11+
bool GetTransaction(const OutputHash& hash, CTransaction& tx, uint256& hashBlock, bool fAllowSlow);
1012
bool CollateralIsExpectedAmount(const COutPoint &outpoint, int64_t expectedAmount);
11-
#endif // TRANSACTION_DISK_ACCESSOR_H
13+
#endif // TRANSACTION_DISK_ACCESSOR_H

divi/src/UtxoCheckingAndUpdating.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include <chainparams.h>
1313
#include <defaultValues.h>
1414

15-
uint256 BlockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
15+
OutputHash BlockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
1616
{
17-
return tx.GetHash();
17+
return OutputHash(tx.GetHash());
1818
}
1919

2020
void UpdateCoinsWithTransaction(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo& txundo,

divi/src/UtxoCheckingAndUpdating.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#ifndef UTXO_CHECKING_AND_UPDATING_H
22
#define UTXO_CHECKING_AND_UPDATING_H
3+
4+
#include <OutputHash.h>
35
#include <vector>
46
#include <scriptCheck.h>
57
#include <amount.h>
@@ -38,7 +40,7 @@ class TransactionUtxoHasher
3840
TransactionUtxoHasher(const TransactionUtxoHasher&) = delete;
3941
void operator=(const TransactionUtxoHasher&) = delete;
4042

41-
virtual uint256 GetUtxoHash(const CTransaction& tx) const = 0;
43+
virtual OutputHash GetUtxoHash(const CTransaction& tx) const = 0;
4244

4345
};
4446

@@ -56,7 +58,7 @@ class BlockUtxoHasher : public TransactionUtxoHasher
5658

5759
public:
5860

59-
uint256 GetUtxoHash(const CTransaction& tx) const override;
61+
OutputHash GetUtxoHash(const CTransaction& tx) const override;
6062

6163
};
6264

divi/src/VaultManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ UnspentOutputs VaultManager::getManagedUTXOs(VaultUTXOFilters filter) const
238238
if( (filter & VaultUTXOFilters::MATURED) > 0 && blocksTillMaturity > 0 ) continue;
239239
if( (filter & VaultUTXOFilters::INMATURE) > 0 && blocksTillMaturity < 1 ) continue;
240240

241-
const uint256 hash = utxoHasher_.GetUtxoHash(tx);
241+
const OutputHash hash(utxoHasher_.GetUtxoHash(tx));
242242
for(unsigned outputIndex = 0; outputIndex < tx.vout.size(); ++outputIndex)
243243
{
244244
const CTxOut& output = tx.vout[outputIndex];

divi/src/WalletTransactionRecord.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ const CWalletTx* WalletTransactionRecord::GetWalletTx(const uint256& hash) const
4141
return nullptr;
4242
}
4343

44+
const CWalletTx* WalletTransactionRecord::GetWalletTx(const OutputHash& hash) const
45+
{
46+
return GetWalletTx(hash.GetValue());
47+
}
48+
4449
std::vector<const CWalletTx*> WalletTransactionRecord::GetWalletTransactionReferences() const
4550
{
4651
AssertLockHeld(cs_walletTxRecord);

divi/src/WalletTransactionRecord.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct WalletTransactionRecord
2121
WalletTransactionRecord(CCriticalSection& requiredWalletLock,const std::string& walletFilename);
2222
WalletTransactionRecord(CCriticalSection& requiredWalletLock);
2323
const CWalletTx* GetWalletTx(const uint256& hash) const;
24+
const CWalletTx* GetWalletTx(const OutputHash& hash) const;
2425

2526
/** Tries to look up a transaction in the wallet, either by hash (txid) or
2627
* the bare txid that is used after segwit-light to identify outputs. */

divi/src/addressindex.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ struct CMempoolAddressDelta
1515
{
1616
int64_t time;
1717
CAmount amount;
18-
uint256 prevhash;
18+
OutputHash prevhash;
1919
unsigned int prevout;
2020

21-
CMempoolAddressDelta(int64_t t, CAmount a, const uint256& hash, unsigned int out) {
21+
CMempoolAddressDelta(int64_t t, CAmount a, const OutputHash& hash, unsigned int out) {
2222
time = t;
2323
amount = a;
2424
prevhash = hash;
@@ -219,7 +219,7 @@ struct CAddressIndexIteratorHeightKey {
219219
struct CAddressUnspentKey {
220220
unsigned int type;
221221
uint160 hashBytes;
222-
uint256 txhash;
222+
OutputHash txhash;
223223
size_t index;
224224

225225
size_t GetSerializeSize(int nType, int nVersion) const {
@@ -240,7 +240,7 @@ struct CAddressUnspentKey {
240240
index = ser_readdata32(s);
241241
}
242242

243-
CAddressUnspentKey(unsigned int addressType, uint160 addressHash, const uint256& txid, size_t indexValue) {
243+
CAddressUnspentKey(unsigned int addressType, uint160 addressHash, const OutputHash& txid, size_t indexValue) {
244244
type = addressType;
245245
hashBytes = addressHash;
246246
txhash = txid;

0 commit comments

Comments
 (0)