Skip to content

Commit ecaa88c

Browse files
committed
Unit test for UpdateCoins UTXO creation.
Add a unit test (together with the necessary framework around) for UTXO creation from UpdateCoins, based on the UTXO hasher (rather than the txid directly).
1 parent 1964748 commit ecaa88c

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

divi/src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ BITCOIN_TESTS =\
6363
test/MockBlockIncentivesPopulator.h \
6464
test/MockBlockSubsidyProvider.h \
6565
test/MockPoSStakeModifierService.h \
66+
test/MockUtxoHasher.cpp \
6667
test/MockVaultManagerDatabase.h \
6768
test/Monthlywalletbackupcreator_tests.cpp \
6869
test/ActiveMasternode_tests.cpp \
@@ -102,6 +103,7 @@ BITCOIN_TESTS =\
102103
test/PoSStakeModifierService_tests.cpp \
103104
test/LegacyPoSStakeModifierService_tests.cpp \
104105
test/LotteryWinnersCalculatorTests.cpp \
106+
test/UtxoCheckingAndUpdating_tests.cpp \
105107
test/VaultManager_tests.cpp \
106108
test/multi_wallet_tests.cpp \
107109
test/MockSignatureSizeEstimator.h \

divi/src/test/MockUtxoHasher.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "MockUtxoHasher.h"
2+
3+
#include "hash.h"
4+
#include "primitives/transaction.h"
5+
6+
uint256 MockUtxoHasher::Add(const CTransaction& tx)
7+
{
8+
++cnt;
9+
const uint256 value = Hash(&cnt, (&cnt) + 1);
10+
customHashes.emplace(tx.GetHash(), value);
11+
return value;
12+
}
13+
14+
uint256 MockUtxoHasher::GetUtxoHash(const CTransaction& tx) const
15+
{
16+
const uint256 txid = tx.GetHash();
17+
const auto mit = customHashes.find(txid);
18+
if (mit != customHashes.end())
19+
return mit->second;
20+
return txid;
21+
}

divi/src/test/MockUtxoHasher.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef MOCKUTXOHASHER_H
2+
#define MOCKUTXOHASHER_H
3+
4+
#include "UtxoCheckingAndUpdating.h"
5+
6+
#include "uint256.h"
7+
8+
#include <map>
9+
10+
class CTransaction;
11+
12+
/** A TransactionUtxoHasher instance that returns normal txid's (as per before
13+
* segwit-light) by default, but can be instructed to return custom hashes
14+
* for certain transactions. */
15+
class MockUtxoHasher : public TransactionUtxoHasher
16+
{
17+
18+
private:
19+
20+
/** Custom hashes to return for given txid's. */
21+
std::map<uint256, uint256> customHashes;
22+
23+
/** Internal counter to produce unique fake hashes. */
24+
unsigned cnt = 0;
25+
26+
public:
27+
28+
MockUtxoHasher()
29+
{}
30+
31+
/** Marks the given transaction for returning a custom hash. The hash
32+
* is generated uniquely and returned from the function. */
33+
uint256 Add(const CTransaction& tx);
34+
35+
uint256 GetUtxoHash(const CTransaction& tx) const override;
36+
37+
};
38+
39+
#endif // MOCKUTXOHASHER_H
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <test_only.h>
2+
#include <UtxoCheckingAndUpdating.h>
3+
4+
#include "coins.h"
5+
#include "MockUtxoHasher.h"
6+
#include "primitives/transaction.h"
7+
8+
namespace
9+
{
10+
11+
class UpdateCoinsTestFixture
12+
{
13+
14+
private:
15+
16+
/** Empty coins used as base in the cached view. */
17+
CCoinsView dummyCoins;
18+
19+
protected:
20+
21+
CCoinsViewCache coins;
22+
MockUtxoHasher utxoHasher;
23+
24+
UpdateCoinsTestFixture()
25+
: coins(&dummyCoins)
26+
{}
27+
28+
};
29+
30+
BOOST_FIXTURE_TEST_SUITE(UpdateCoins_tests, UpdateCoinsTestFixture)
31+
32+
BOOST_AUTO_TEST_CASE(addsCorrectOutputs)
33+
{
34+
CMutableTransaction mtx;
35+
mtx.vout.emplace_back(1, CScript() << OP_TRUE);
36+
const CTransaction tx1(mtx);
37+
38+
mtx.vout.clear();
39+
mtx.vout.emplace_back(2, CScript() << OP_TRUE);
40+
const CTransaction tx2(mtx);
41+
const auto id2 = utxoHasher.Add(tx2);
42+
43+
CTxUndo txundo;
44+
UpdateCoins(tx1, coins, txundo, utxoHasher, 101);
45+
UpdateCoins(tx2, coins, txundo, utxoHasher, 102);
46+
47+
BOOST_CHECK(coins.HaveCoins(tx1.GetHash()));
48+
BOOST_CHECK(!coins.HaveCoins(tx2.GetHash()));
49+
BOOST_CHECK(coins.HaveCoins(id2));
50+
}
51+
52+
BOOST_AUTO_TEST_SUITE_END()
53+
54+
} // anonymous namespace

0 commit comments

Comments
 (0)