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

divi/src/SpentOutputTracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void SpentOutputTracker::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::it
4646
* Outpoint is spent if any non-conflicted transaction
4747
* spends it:
4848
*/
49-
bool SpentOutputTracker::IsSpent(const uint256& hash, unsigned int n) const
49+
bool SpentOutputTracker::IsSpent(const OutputHash& hash, unsigned int n) const
5050
{
5151
const COutPoint outpoint(hash, n);
5252
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

1313
class SpentOutputTracker
@@ -33,7 +33,7 @@ class SpentOutputTracker
3333
const CWalletTx& newlyAddedTransaction,
3434
int64_t orderedTransactionIndex=0,
3535
bool loadedFromDisk=false);
36-
bool IsSpent(const uint256& hash, unsigned int n) const;
36+
bool IsSpent(const OutputHash& hash, unsigned int n) const;
3737
std::set<uint256> GetConflictingTxHashes(const CWalletTx& tx) const;
3838
};
3939
#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 = false);
11+
bool GetTransaction(const OutputHash& hash, CTransaction& tx, uint256& hashBlock, bool fAllowSlow = false);
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
@@ -85,7 +85,7 @@ UnspentOutputs VaultManager::getUTXOs() const
8585
for(const auto& hashAndTransaction: walletTxRecord_->mapWallet)
8686
{
8787
const CWalletTx& tx = hashAndTransaction.second;
88-
const uint256 hash = utxoHasher_.GetUtxoHash(tx);
88+
const OutputHash hash(utxoHasher_.GetUtxoHash(tx));
8989
if(tx.GetNumberOfBlockConfirmations()<1) continue;
9090
if((tx.IsCoinBase() || tx.IsCoinStake()) && tx.GetBlocksToMaturity() > 0) continue;
9191
for(unsigned outputIndex = 0; outputIndex < tx.vout.size(); ++outputIndex)

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;

divi/src/bloom.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
141141
if (data.size() != 0 && contains(data)) {
142142
fFound = true;
143143
if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
144-
insert(COutPoint(hash, i));
144+
insert(COutPoint(OutputHash(hash), i));
145145
else if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY) {
146146
txnouttype type;
147147
std::vector<std::vector<unsigned char> > vSolutions;
148148
if (ExtractScriptPubKeyFormat(txout.scriptPubKey, type, vSolutions) &&
149149
(type == TX_PUBKEY || type == TX_MULTISIG))
150-
insert(COutPoint(hash, i));
150+
insert(COutPoint(OutputHash(hash), i));
151151
}
152152
break;
153153
}

divi/src/coincontrol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CCoinControl
4949
return (setSelected.size() > 0);
5050
}
5151

52-
bool IsSelected(const uint256& hash, unsigned int n) const
52+
bool IsSelected(const OutputHash& hash, unsigned int n) const
5353
{
5454
COutPoint outpt(hash, n);
5555
return (setSelected.count(outpt) > 0);

0 commit comments

Comments
 (0)