Skip to content

Commit ed77dd6

Browse files
author
Jim Posen
committed
[test] Simple unit test for TxIndex.
1 parent 6d772a3 commit ed77dd6

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ BITCOIN_TESTS =\
8383
test/timedata_tests.cpp \
8484
test/torcontrol_tests.cpp \
8585
test/transaction_tests.cpp \
86+
test/txindex_tests.cpp \
8687
test/txvalidation_tests.cpp \
8788
test/txvalidationcache_tests.cpp \
8889
test/versionbits_tests.cpp \

src/test/txindex_tests.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) 2017-2018 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/txindex.h>
6+
#include <script/standard.h>
7+
#include <test/test_bitcoin.h>
8+
#include <util.h>
9+
#include <utiltime.h>
10+
#include <validation.h>
11+
12+
#include <boost/test/unit_test.hpp>
13+
14+
BOOST_AUTO_TEST_SUITE(txindex_tests)
15+
16+
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
17+
{
18+
TxIndex txindex(MakeUnique<TxIndexDB>(1 << 20, true));
19+
20+
CTransactionRef tx_disk;
21+
uint256 block_hash;
22+
23+
// Transaction should not be found in the index before it is started.
24+
for (const auto& txn : m_coinbase_txns) {
25+
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
26+
}
27+
28+
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
29+
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
30+
31+
txindex.Start();
32+
33+
// Allow tx index to catch up with the block index.
34+
constexpr int64_t timeout_ms = 10 * 1000;
35+
int64_t time_start = GetTimeMillis();
36+
while (!txindex.BlockUntilSyncedToCurrentChain()) {
37+
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
38+
MilliSleep(100);
39+
}
40+
41+
// Check that txindex has all txs that were in the chain before it started.
42+
for (const auto& txn : m_coinbase_txns) {
43+
if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
44+
BOOST_ERROR("FindTx failed");
45+
} else if (tx_disk->GetHash() != txn->GetHash()) {
46+
BOOST_ERROR("Read incorrect tx");
47+
}
48+
}
49+
50+
// Check that new transactions in new blocks make it into the index.
51+
for (int i = 0; i < 10; i++) {
52+
CScript coinbase_script_pub_key = GetScriptForDestination(coinbaseKey.GetPubKey().GetID());
53+
std::vector<CMutableTransaction> no_txns;
54+
const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
55+
const CTransaction& txn = *block.vtx[0];
56+
57+
BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
58+
if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
59+
BOOST_ERROR("FindTx failed");
60+
} else if (tx_disk->GetHash() != txn.GetHash()) {
61+
BOOST_ERROR("Read incorrect tx");
62+
}
63+
}
64+
}
65+
66+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)