Skip to content

Commit 56fdd43

Browse files
committed
Update unit tests for OutputHash.
Update also the unit tests for the OutputHash type. This is a separate commit to make the review easier (as reviewing the main part of the change in production code is separate).
1 parent 9974c7d commit 56fdd43

20 files changed

+90
-90
lines changed

divi/src/test/ActiveMasternode_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ BOOST_AUTO_TEST_CASE(willNotEnableMasternodeOnEmptyConfigurations)
6060

6161
BOOST_AUTO_TEST_CASE(willEnableMasternodeOnMatchingUTXO)
6262
{
63-
uint256 dummyHash = GetRandHash();
63+
const OutputHash dummyHash(GetRandHash());
6464
uint32_t out = 0;
6565
CTxIn validTxIn (dummyHash, out);
6666
CService service;
@@ -74,10 +74,10 @@ BOOST_AUTO_TEST_CASE(willNotEnableMasternodeOnMismatchedUTXO)
7474
{
7575
uint32_t out = 0;
7676

77-
uint256 correctDummyHash = GetRandHash();
77+
const OutputHash correctDummyHash(GetRandHash());
7878
CTxIn validTxIn (correctDummyHash, out);
7979

80-
uint256 wrongDummyHash = GetRandHash();
80+
const OutputHash wrongDummyHash(GetRandHash());
8181
CTxIn wrongTxIn (wrongDummyHash, out);
8282

8383

@@ -98,4 +98,4 @@ BOOST_AUTO_TEST_CASE(willSetMatchingPubkeyForPrivateKey)
9898
BOOST_CHECK(activeMasternode_->pubKeyMasternode == expectedPubkey);
9999
}
100100

101-
BOOST_AUTO_TEST_SUITE_END()
101+
BOOST_AUTO_TEST_SUITE_END()

divi/src/test/BareTxid_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class BareTxidTestFixture
3535
mtx.nVersion = 1;
3636
mtx.nLockTime = 1234567890;
3737

38-
mtx.vin.emplace_back (HashStr ("in 1"), 4, CScript () << OP_TRUE << OP_FALSE);
39-
mtx.vin.emplace_back (HashStr ("in 2"), 2, CScript () << OP_DUP);
38+
mtx.vin.emplace_back (OutputHash (HashStr ("in 1")), 4, CScript () << OP_TRUE << OP_FALSE);
39+
mtx.vin.emplace_back (OutputHash (HashStr ("in 2")), 2, CScript () << OP_DUP);
4040

4141
mtx.vout.emplace_back (1 * COIN, CScript () << OP_TRUE);
4242
mtx.vout.emplace_back (0, CScript () << OP_META << HashToVec ("data"));

divi/src/test/BlockSignature_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class BlockSignatureTestFixture
7878
CScript outputScript = (isP2SH)? ConvertToP2SH(redeemScript):redeemScript;
7979
vaultKeyStore.AddCScript(redeemScript);
8080

81-
coinstake.vin.emplace_back(uint256S("0x25"), 0, createDummyVaultScriptSig(redeemScript));
81+
coinstake.vin.emplace_back(OutputHash(uint256S("0x25")), 0, createDummyVaultScriptSig(redeemScript));
8282
coinstake.vout.emplace_back(0, outputScript);
8383

8484
block.vtx.emplace_back();

divi/src/test/DoS_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
139139
CMutableTransaction tx;
140140
tx.vin.resize(1);
141141
tx.vin[0].prevout.n = 0;
142-
tx.vin[0].prevout.hash = GetRandHash();
142+
tx.vin[0].prevout.hash = OutputHash(GetRandHash());
143143
tx.vin[0].scriptSig << OP_1;
144144
tx.vout.resize(1);
145145
tx.vout[0].nValue = 1*CENT;
@@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
156156
CMutableTransaction tx;
157157
tx.vin.resize(1);
158158
tx.vin[0].prevout.n = 0;
159-
tx.vin[0].prevout.hash = txPrev.GetHash();
159+
tx.vin[0].prevout.hash = OutputHash(txPrev.GetHash());
160160
tx.vout.resize(1);
161161
tx.vout[0].nValue = 1*CENT;
162162
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
@@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
178178
for (unsigned int j = 0; j < tx.vin.size(); j++)
179179
{
180180
tx.vin[j].prevout.n = j;
181-
tx.vin[j].prevout.hash = txPrev.GetHash();
181+
tx.vin[j].prevout.hash = OutputHash(txPrev.GetHash());
182182
}
183183
SignSignature(keystore, txPrev, tx, 0);
184184
// Re-use same signature for other inputs

divi/src/test/FakeWallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ CMutableTransaction createDefaultTransaction(const CScript& defaultScript, unsig
7171
tx.vout[index].scriptPubKey = defaultScript;
7272
tx.vin.resize(1);
7373
// Avoid flagging as a coinbase tx
74-
tx.vin[0].prevout = COutPoint(GetRandHash(),static_cast<uint32_t>(GetRand(100)) );
74+
tx.vin[0].prevout = COutPoint(OutputHash(GetRandHash()), static_cast<uint32_t>(GetRand(100)) );
7575

7676
return tx;
7777
}

divi/src/test/LotteryWinnersCalculatorTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class LotteryWinnersCalculatorTestFixture
7878
{// Avoid flagging as a coinbase tx
7979
const uint256 randomHash = uint256S("4f5e1dcf6b28438ecb4f92c37f72bc430055fc91651f3dbc22050eb93164c579");
8080
constexpr uint32_t randomIndex = 42;
81-
tx.vin[0].prevout = COutPoint(randomHash,randomIndex);
81+
tx.vin[0].prevout = COutPoint(OutputHash(randomHash), randomIndex);
8282
}
8383
assert(CTransaction(tx).IsCoinStake());
8484
return tx;
@@ -231,4 +231,4 @@ BOOST_AUTO_TEST_CASE(willRemoveLowestScoringDuplicateIfNewWinnersAreAvailable)
231231
}
232232
}
233233
}
234-
BOOST_AUTO_TEST_SUITE_END()
234+
BOOST_AUTO_TEST_SUITE_END()

divi/src/test/MockUtxoHasher.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
#include "hash.h"
44
#include "primitives/transaction.h"
55

6-
uint256 MockUtxoHasher::Add(const CTransaction& tx)
6+
OutputHash MockUtxoHasher::Add(const CTransaction& tx)
77
{
88
++cnt;
9-
const uint256 value = Hash(&cnt, (&cnt) + 1);
9+
const OutputHash value(Hash(&cnt, (&cnt) + 1));
1010
customHashes.emplace(tx.GetHash(), value);
1111
return value;
1212
}
1313

1414
void MockUtxoHasher::UseBareTxid(const CTransaction& tx)
1515
{
16-
customHashes.emplace(tx.GetHash(), tx.GetBareTxid());
16+
customHashes.emplace(tx.GetHash(), OutputHash(tx.GetBareTxid()));
1717
}
1818

19-
uint256 MockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
19+
OutputHash MockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
2020
{
2121
const uint256 txid = tx.GetHash();
2222
const auto mit = customHashes.find(txid);
2323
if (mit != customHashes.end())
2424
return mit->second;
25-
return txid;
25+
return OutputHash(txid);
2626
}

divi/src/test/MockUtxoHasher.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MockUtxoHasher : public TransactionUtxoHasher
1818
private:
1919

2020
/** Custom hashes to return for given txid's. */
21-
std::map<uint256, uint256> customHashes;
21+
std::map<uint256, OutputHash> customHashes;
2222

2323
/** Internal counter to produce unique fake hashes. */
2424
unsigned cnt = 0;
@@ -30,13 +30,13 @@ class MockUtxoHasher : public TransactionUtxoHasher
3030

3131
/** Marks the given transaction for returning a custom hash. The hash
3232
* is generated uniquely and returned from the function. */
33-
uint256 Add(const CTransaction& tx);
33+
OutputHash Add(const CTransaction& tx);
3434

3535
/** Marks the given transaction for using the bare txid rather than
3636
* the normal txid. */
3737
void UseBareTxid(const CTransaction& tx);
3838

39-
uint256 GetUtxoHash(const CTransaction& tx) const override;
39+
OutputHash GetUtxoHash(const CTransaction& tx) const override;
4040

4141
};
4242

divi/src/test/ProofOfStake_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(onlyHashesAFixedNumberOfTimesIfCalculatorRepeatedlyFails)
4242
unsigned chainTipDifficulty = 0x1000001;
4343
unsigned transactionTimeStart = blockTimeOfFirstUTXOConfirmation + 60*60*60;
4444
unsigned transactionTime = transactionTimeStart;
45-
COutPoint utxo(GetRandHash(),GetRandInt(10));
45+
COutPoint utxo(OutputHash(GetRandHash()), GetRandInt(10));
4646
uint256 blockHash = GetRandHash();
4747
StakingData stakingData(chainTipDifficulty, blockTimeOfFirstUTXOConfirmation, blockHash, utxo, 0*COIN, 0);
4848

@@ -105,7 +105,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
105105
unsigned(470026099),
106106
unsigned(1538645320),
107107
uint256S("967b03e3c1daf39633ed73ffb29abfcab9ae5b384dc5b95dabee0890bf8b4546"),
108-
COutPoint(uint256S("4266403b499375917920311b1af704805d3fa2d6d6f4e3217026618028423607"), 1),
108+
COutPoint(OutputHash(uint256S("4266403b499375917920311b1af704805d3fa2d6d6f4e3217026618028423607")), 1),
109109
CAmount(62542750000000),
110110
uint256("acf49c06030a7a76059a25b174dc7adcdc5f4ad36c91b564c585743af4829f7a")
111111
},
@@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
117117
unsigned(469856526),
118118
unsigned(1539089488),
119119
uint256S("5cf4c9c2cf5b7ce030d39bb8b322f4b7f60bfe3408c48071e88cbe82d1b0c05e"),
120-
COutPoint(uint256S("9e6dea178ac919781836e19fd773fda9788a59b3ff0fc7daddaccaa880f55396"),1),
120+
COutPoint(OutputHash(uint256S("9e6dea178ac919781836e19fd773fda9788a59b3ff0fc7daddaccaa880f55396")), 1),
121121
CAmount(9647175000000),
122122
uint256S("d4504de09669961a5d483070a0b4a0bd2231eda531b360ea5992c3cedaf096a0")
123123
},
@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
129129
unsigned(469821893),
130130
unsigned(1539779674),
131131
uint256S("854dfc8fca75d2ff38b1d7eaa3d049d14f5c2756ae9e385faeef7b616aebfcb8"),
132-
COutPoint(uint256S("91d6a2ecdc9d7afeec1b9ac711046188ea15022a439a5ae4b95b8ce725e45144"),2),
132+
COutPoint(OutputHash(uint256S("91d6a2ecdc9d7afeec1b9ac711046188ea15022a439a5ae4b95b8ce725e45144")), 2),
133133
CAmount(7536149207500),
134134
uint256S("170781f60c8e5a7705a55d42272263b017df80d8c12a6eb7af6d74f5ba139cac")
135135
},
@@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
141141
unsigned(453551099),
142142
unsigned(1580666056),
143143
uint256S("dec4fd2e49eda04dae3bb36c4eea0c57f677a930bb7dcbea8d9957a90b11464c"),
144-
COutPoint(uint256S("01dfa586a601481b66309b574bed91eccb0921c6faf908538b935e677a338b90"),1),
144+
COutPoint(OutputHash(uint256S("01dfa586a601481b66309b574bed91eccb0921c6faf908538b935e677a338b90")), 1),
145145
CAmount(9126800000000),
146146
uint256S("d397d5a4e387246ba3bc1e8c22c0dcf7d00da3dc8e78e63648ee31de88e83b0c")
147147
},
@@ -153,7 +153,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
153153
unsigned(453485185),
154154
unsigned(1586034307),
155155
uint256S("9cc08403bfeac16eb293b848d619639f52a693d13f00f0188c1efeb85fff332c"),
156-
COutPoint(uint256S("47b3248311ada187ad01b25c4274289224fc16e185fa4427fa0dc62cde426913"),1),
156+
COutPoint(OutputHash(uint256S("47b3248311ada187ad01b25c4274289224fc16e185fa4427fa0dc62cde426913")), 1),
157157
CAmount(5846400000000),
158158
uint256S("79808b1228f217c55ab46b52b7c5b120afeed598cb86bd8d7062bb6b1d90da4e")
159159
},
@@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
165165
unsigned(453338064),
166166
unsigned(1598487374),
167167
uint256S("e5fd3874ca56174d611c8925785a0dda728a4160b59ab777644e7a17500576d4"),
168-
COutPoint(uint256S("d17d0226b20b1853b6ad50e73f132a1bd1ce1b5fa08db17c0cbbc93b82619da1"),1),
168+
COutPoint(OutputHash(uint256S("d17d0226b20b1853b6ad50e73f132a1bd1ce1b5fa08db17c0cbbc93b82619da1")), 1),
169169
CAmount(1445296875000),
170170
uint256S("25f7f482cbf34cd7da9d5db0e3b633c8c0abe54e0de1ef96e97ba15e8713e984")
171171
},
@@ -177,7 +177,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
177177
unsigned(453369958),
178178
unsigned(1603300997),
179179
uint256S("1f41f84a8aa151b900c66ba4c9423f7eef93bd076c030af54fa1dae1d238cfa0"),
180-
COutPoint(uint256S("b5071db8b245780b77ecc6d4423ed5e8465b14065d19c394ec14ff0b5fde4d3a"),1),
180+
COutPoint(OutputHash(uint256S("b5071db8b245780b77ecc6d4423ed5e8465b14065d19c394ec14ff0b5fde4d3a")), 1),
181181
CAmount(5557600000000),
182182
uint256S("bd04a024f2f3ad91e694d4c70c6a983f1e73bb7803333d0406e9c314d6c265b7")
183183
},
@@ -189,7 +189,7 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
189189
unsigned(453347746),
190190
unsigned(1599860645),
191191
uint256S("4d2597aa8ff30a0f0f82466e1dfd7603d8f928e08c8887597e0e0524ae293e5c"),
192-
COutPoint(uint256S("1f5d59023369ac9f32d25de4645cd4a1d101911a01514a1a854a6518795f4805"),1),
192+
COutPoint(OutputHash(uint256S("1f5d59023369ac9f32d25de4645cd4a1d101911a01514a1a854a6518795f4805")), 1),
193193
CAmount(1699450000000),
194194
uint256S("2dfcd9caa2558c41148a13b67117197289863a887480825646acf5225bcc0156")
195195
},
@@ -216,4 +216,4 @@ BOOST_AUTO_TEST_CASE(willEnsureBackwardCompatibilityWithMainnetHashproofs)
216216
}
217217
}
218218

219-
BOOST_AUTO_TEST_SUITE_END()
219+
BOOST_AUTO_TEST_SUITE_END()

divi/src/test/SignatureSizeEstimation_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SignatureSizeTestFixture
2626
CMutableTransaction getSampleTransaction()
2727
{
2828
CMutableTransaction sampleTransaction;
29-
sampleTransaction.vin.emplace_back(uint256S("0x8b4bdd6fd8220ca956938d214cbd4635bfaacc663f53ad8bda5e434b9dc647fe"),1);
29+
sampleTransaction.vin.emplace_back(OutputHash(uint256S("0x8b4bdd6fd8220ca956938d214cbd4635bfaacc663f53ad8bda5e434b9dc647fe")), 1);
3030
return sampleTransaction;
3131
}
3232
void createKeys(unsigned numberOfKeys, bool compressedKey = true)
@@ -254,4 +254,4 @@ BOOST_AUTO_TEST_CASE(willRecoverCorrectSignatureSizeForP2SHScriptsWhenScriptsAre
254254
}
255255

256256

257-
BOOST_AUTO_TEST_SUITE_END()
257+
BOOST_AUTO_TEST_SUITE_END()

divi/src/test/UtxoCheckingAndUpdating_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ BOOST_AUTO_TEST_CASE(addsCorrectOutputs)
4444
UpdateCoinsWithTransaction(tx1, coins, txundo, utxoHasher, 101);
4545
UpdateCoinsWithTransaction(tx2, coins, txundo, utxoHasher, 102);
4646

47-
BOOST_CHECK(coins.HaveCoins(tx1.GetHash()));
48-
BOOST_CHECK(!coins.HaveCoins(tx2.GetHash()));
47+
BOOST_CHECK(coins.HaveCoins(OutputHash(tx1.GetHash())));
48+
BOOST_CHECK(!coins.HaveCoins(OutputHash(tx2.GetHash())));
4949
BOOST_CHECK(coins.HaveCoins(id2));
5050
}
5151

divi/src/test/coins_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ namespace
1919
class CCoinsViewTest : public CCoinsView
2020
{
2121
uint256 hashBestBlock_;
22-
std::map<uint256, CCoins> map_;
22+
std::map<OutputHash, CCoins> map_;
2323

2424
public:
25-
bool GetCoins(const uint256& txid, CCoins& coins) const override
25+
bool GetCoins(const OutputHash& txid, CCoins& coins) const override
2626
{
2727
auto it = map_.find(txid);
2828
if (it == map_.end()) {
@@ -36,7 +36,7 @@ class CCoinsViewTest : public CCoinsView
3636
return true;
3737
}
3838

39-
bool HaveCoins(const uint256& txid) const override
39+
bool HaveCoins(const OutputHash& txid) const override
4040
{
4141
CCoins coins;
4242
return GetCoins(txid, coins);
@@ -88,24 +88,24 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
8888
bool missed_an_entry = false;
8989

9090
// A simple map to track what we expect the cache stack to represent.
91-
std::map<uint256, CCoins> result;
91+
std::map<OutputHash, CCoins> result;
9292

9393
// The cache stack.
9494
CCoinsViewTest base; // A CCoinsViewTest at the bottom.
9595
std::vector<CCoinsViewCache*> stack; // A stack of CCoinsViewCaches on top.
9696
stack.push_back(new CCoinsViewCache(&base)); // Start with one cache.
9797

9898
// Use a limited set of random transaction ids, so we do test overwriting entries.
99-
std::vector<uint256> txids;
99+
std::vector<OutputHash> txids;
100100
txids.resize(NUM_SIMULATION_ITERATIONS / 8);
101101
for (unsigned int i = 0; i < txids.size(); i++) {
102-
txids[i] = GetRandHash();
102+
txids[i] = OutputHash(GetRandHash());
103103
}
104104

105105
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
106106
// Do a random modification.
107107
{
108-
uint256 txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration.
108+
OutputHash txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration.
109109
CCoins& coins = result[txid];
110110
CCoinsModifier entry = stack.back()->ModifyCoins(txid);
111111
BOOST_CHECK(coins == *entry);

divi/src/test/kernel_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class CheckCoinstakeForVaultsTestFixture
6565
dummyTxNonVault.vout[i].scriptPubKey = scriptNonVault;
6666
}
6767

68-
coins.ModifyCoins(dummyTxVault.GetHash())->FromTx(dummyTxVault, 0);
69-
coins.ModifyCoins(dummyTxNonVault.GetHash())->FromTx(dummyTxNonVault, 0);
68+
coins.ModifyCoins(OutputHash(dummyTxVault.GetHash()))->FromTx(dummyTxVault, 0);
69+
coins.ModifyCoins(OutputHash(dummyTxNonVault.GetHash()))->FromTx(dummyTxNonVault, 0);
7070

7171
for (unsigned i = 0; i < dummyTxVault.vout.size(); ++i) {
72-
vaultCoins.emplace_back(dummyTxVault.GetHash(), i);
73-
nonVaultCoins.emplace_back(dummyTxNonVault.GetHash(), i);
72+
vaultCoins.emplace_back(OutputHash(dummyTxVault.GetHash()), i);
73+
nonVaultCoins.emplace_back(OutputHash(dummyTxNonVault.GetHash()), i);
7474
}
7575
}
7676

0 commit comments

Comments
 (0)