Skip to content

Commit ce3fe42

Browse files
committed
WIP: OutputHash
1 parent 3677f7c commit ce3fe42

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

+221
-105
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

@@ -139,7 +140,7 @@ void BlockMemoryPoolTransactionCollector::ComputeTransactionPriority (
139140

140141
void BlockMemoryPoolTransactionCollector::AddDependingTransactionsToPriorityQueue (
141142
DependingTransactionsMap& dependentTransactions,
142-
const uint256& hash,
143+
const OutputHash& hash,
143144
std::vector<TxPriority>& vecPriority,
144145
TxPriorityCompare& comparer) const
145146
{

divi/src/BlockMemoryPoolTransactionCollector.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CTxMemPool;
2525
class CBlockTemplate;
2626
class CBlockHeader;
2727
class CFeeRate;
28+
class OutputHash;
2829
class Settings;
2930

3031
template <typename MutexObj>
@@ -50,7 +51,7 @@ class CChain;
5051
class BlockMemoryPoolTransactionCollector: public I_BlockTransactionCollector
5152
{
5253
private:
53-
using DependingTransactionsMap = std::map<uint256, std::vector<std::shared_ptr<COrphan>>>;
54+
using DependingTransactionsMap = std::map<OutputHash, std::vector<std::shared_ptr<COrphan>>>;
5455

5556
CCoinsViewCache* baseCoinsViewCache_;
5657
const CChain& activeChain_;
@@ -78,7 +79,7 @@ class BlockMemoryPoolTransactionCollector: public I_BlockTransactionCollector
7879
const CTransaction* mempoolTx) const;
7980
void AddDependingTransactionsToPriorityQueue (
8081
DependingTransactionsMap& mapDependers,
81-
const uint256& hash,
82+
const OutputHash& hash,
8283
std::vector<TxPriority>& vecPriority,
8384
TxPriorityCompare& comparer) const;
8485

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/MasternodeBroadcastFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static bool GetVinAndKeysFromOutput(const CKeyStore& walletKeyStore, CScript pub
4343

4444
static bool ParseInputReference(std::string strTxHash, std::string strOutputIndex, CTxIn& txinRet)
4545
{
46-
uint256 txHash = uint256S(strTxHash);
46+
const OutputHash txHash(uint256S(strTxHash));
4747

4848
int nOutputIndex;
4949
try {

divi/src/MasternodeModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ void LockUpMasternodeCollateral(const Settings& settings, std::function<void(con
421421
{
422422
LogPrintf(" %s %s\n", mne.getTxHash(), mne.getOutputIndex());
423423
mnTxHash.SetHex(mne.getTxHash());
424-
COutPoint outpoint(mnTxHash, boost::lexical_cast<unsigned int>(mne.getOutputIndex()));
424+
COutPoint outpoint(OutputHash(mnTxHash), boost::lexical_cast<unsigned int>(mne.getOutputIndex()));
425425
walletUtxoLockingFunction(outpoint);
426426
}
427427
}

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
@@ -14,9 +14,9 @@
1414

1515
extern BlockMap mapBlockIndex;
1616

17-
uint256 BlockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
17+
OutputHash BlockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
1818
{
19-
return tx.GetHash();
19+
return OutputHash(tx.GetHash());
2020
}
2121

2222
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>
@@ -37,7 +39,7 @@ class TransactionUtxoHasher
3739
TransactionUtxoHasher(const TransactionUtxoHasher&) = delete;
3840
void operator=(const TransactionUtxoHasher&) = delete;
3941

40-
virtual uint256 GetUtxoHash(const CTransaction& tx) const = 0;
42+
virtual OutputHash GetUtxoHash(const CTransaction& tx) const = 0;
4143

4244
};
4345

@@ -55,7 +57,7 @@ class BlockUtxoHasher : public TransactionUtxoHasher
5557

5658
public:
5759

58-
uint256 GetUtxoHash(const CTransaction& tx) const override;
60+
OutputHash GetUtxoHash(const CTransaction& tx) const override;
5961

6062
};
6163

divi/src/VaultManager.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#include <WalletTx.h>
55
#include <WalletTransactionRecord.h>
66
#include <SpentOutputTracker.h>
7+
#include <UtxoCheckingAndUpdating.h>
78
#include <I_VaultManagerDatabase.h>
89

910
VaultManager::VaultManager(
1011
const CChain& activeChain,
11-
const BlockMap& blockIndicesByHash
12+
const BlockMap& blockIndicesByHash,
13+
const TransactionUtxoHasher& utxoHasher
1214
): activeChain_(activeChain)
1315
, blockIndicesByHash_(blockIndicesByHash)
16+
, utxoHasher_(utxoHasher)
1417
, cs_vaultManager_()
1518
, transactionOrderingIndex_(0)
1619
, walletTxRecord_(new WalletTransactionRecord(cs_vaultManager_))
@@ -23,8 +26,9 @@ VaultManager::VaultManager(
2326
VaultManager::VaultManager(
2427
const CChain& activeChain,
2528
const BlockMap& blockIndicesByHash,
29+
const TransactionUtxoHasher& utxoHasher,
2630
I_VaultManagerDatabase& vaultManagerDB
27-
): VaultManager(activeChain,blockIndicesByHash)
31+
): VaultManager(activeChain,blockIndicesByHash,utxoHasher)
2832
{
2933
LOCK(cs_vaultManager_);
3034
vaultManagerDB.ReadManagedScripts(managedScriptsLimits_);
@@ -80,8 +84,8 @@ UnspentOutputs VaultManager::getUTXOs() const
8084
auto managedScriptsLimitsCopy = managedScriptsLimits_;
8185
for(const auto& hashAndTransaction: walletTxRecord_->mapWallet)
8286
{
83-
uint256 hash = hashAndTransaction.first;
8487
const CWalletTx& tx = hashAndTransaction.second;
88+
const OutputHash hash(utxoHasher_.GetUtxoHash(tx));
8589
if(tx.GetNumberOfBlockConfirmations()<1) continue;
8690
if((tx.IsCoinBase() || tx.IsCoinStake()) && tx.GetBlocksToMaturity() > 0) continue;
8791
for(unsigned outputIndex = 0; outputIndex < tx.vout.size(); ++outputIndex)

divi/src/VaultManager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using UnspentOutputs = std::set<COutPoint>;
1313
class CTransaction;
1414
class CBlock;
1515
class CWalletTx;
16+
class TransactionUtxoHasher;
1617
class uint256;
1718

1819
class I_VaultManagerDatabase;
@@ -27,16 +28,19 @@ class VaultManager
2728
const BlockMap& blockIndicesByHash_;
2829
mutable CCriticalSection cs_vaultManager_;
2930
uint64_t transactionOrderingIndex_;
31+
const TransactionUtxoHasher& utxoHasher_;
3032
std::unique_ptr<WalletTransactionRecord> walletTxRecord_;
3133
std::unique_ptr<SpentOutputTracker> outputTracker_;
3234
ManagedScripts managedScriptsLimits_;
3335
public:
3436
VaultManager(
3537
const CChain& activeChain,
36-
const BlockMap& blockIndicesByHash);
38+
const BlockMap& blockIndicesByHash,
39+
const TransactionUtxoHasher& utxoHasher);
3740
VaultManager(
3841
const CChain& activeChain,
3942
const BlockMap& blockIndicesByHash,
43+
const TransactionUtxoHasher& utxoHasher,
4044
I_VaultManagerDatabase& vaultManagerDB);
4145
~VaultManager();
4246
void SyncTransaction(const CTransaction& tx, const CBlock *pblock);

0 commit comments

Comments
 (0)