Skip to content

Commit 850c366

Browse files
committed
test: Add unit test for UtxoSetHash
1 parent 29201d2 commit 850c366

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Makefile.test.include

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ BITCOIN_TESTS =\
148148
test/txvalidationcache_tests.cpp \
149149
test/uint256_tests.cpp \
150150
test/util_tests.cpp \
151+
test/utxosethash_tests.cpp \
151152
test/validation_block_tests.cpp \
152153
test/versionbits_tests.cpp
153154

src/test/utxosethash_tests.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <index/utxosethash.h>
6+
#include <test/setup_common.h>
7+
#include <validation.h>
8+
//
9+
#include <boost/test/unit_test.hpp>
10+
11+
BOOST_AUTO_TEST_SUITE(utxosethash_tests)
12+
13+
BOOST_FIXTURE_TEST_CASE(utxosethash_initial_sync, TestChain100Setup)
14+
{
15+
UtxoSetHash utxo_set_hash(0, false);
16+
17+
uint256 hash_digest;
18+
CBlockIndex* block_index = ::ChainActive().Tip();
19+
20+
// UTXO set hash should not be found before it is started.
21+
BOOST_CHECK(!utxo_set_hash.LookupHash(block_index, hash_digest));
22+
23+
// BlockUntilSyncedToCurrentChain should return false before utxo_set_hash is started.
24+
BOOST_CHECK(!utxo_set_hash.BlockUntilSyncedToCurrentChain());
25+
26+
utxo_set_hash.Start();
27+
28+
// Allow the UTXO set hash to catch up with the block index.
29+
constexpr int64_t timeout_ms = 10 * 1000;
30+
int64_t time_start = GetTimeMillis();
31+
while (!utxo_set_hash.BlockUntilSyncedToCurrentChain()) {
32+
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
33+
MilliSleep(100);
34+
}
35+
36+
// Check that UTXO set hash works for genesis block.
37+
CBlockIndex* genesis_block_index = ::ChainActive().Genesis();
38+
BOOST_CHECK(utxo_set_hash.LookupHash(genesis_block_index, hash_digest));
39+
40+
// Check that UTXO set hash updates with new blocks.
41+
block_index = ::ChainActive().Tip();
42+
utxo_set_hash.LookupHash(block_index, hash_digest);
43+
44+
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
45+
std::vector<CMutableTransaction> noTxns;
46+
CreateAndProcessBlock(noTxns, scriptPubKey);
47+
48+
time_start = GetTimeMillis();
49+
while (!utxo_set_hash.BlockUntilSyncedToCurrentChain()) {
50+
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
51+
MilliSleep(100);
52+
}
53+
54+
uint256 new_hash_digest;
55+
CBlockIndex* new_block_index = ::ChainActive().Tip();
56+
utxo_set_hash.LookupHash(new_block_index, new_hash_digest);
57+
58+
BOOST_CHECK(block_index != new_block_index);
59+
BOOST_CHECK(hash_digest != new_hash_digest);
60+
61+
// shutdown sequence (c.f. Shutdown() in init.cpp)
62+
utxo_set_hash.Stop();
63+
64+
threadGroup.interrupt_all();
65+
threadGroup.join_all();
66+
67+
// Rest of shutdown sequence and destructors happen in ~TestingSetup()
68+
}
69+
70+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)