2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2019-08-28 15:58:53 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2022-02-22 14:48:57 -05:00
|
|
|
#include <chainparams.h>
|
2019-08-28 15:58:53 -04:00
|
|
|
#include <index/coinstatsindex.h>
|
2022-01-13 07:57:54 -05:00
|
|
|
#include <interfaces/chain.h>
|
2022-05-26 17:06:07 +02:00
|
|
|
#include <kernel/coinstats.h>
|
2019-08-28 15:58:53 -04:00
|
|
|
#include <test/util/setup_common.h>
|
2022-02-22 14:48:57 -05:00
|
|
|
#include <test/util/validation.h>
|
2019-08-28 15:58:53 -04:00
|
|
|
#include <util/time.h>
|
|
|
|
#include <validation.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
|
|
|
|
|
2022-02-22 14:48:57 -05:00
|
|
|
static void IndexWaitSynced(BaseIndex& index)
|
|
|
|
{
|
|
|
|
// Allow the CoinStatsIndex to catch up with the block index that is syncing
|
|
|
|
// in a background thread.
|
|
|
|
const auto timeout = GetTime<std::chrono::seconds>() + 120s;
|
|
|
|
while (!index.BlockUntilSyncedToCurrentChain()) {
|
|
|
|
BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
|
|
|
|
UninterruptibleSleep(100ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:58:53 -04:00
|
|
|
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
|
|
|
|
{
|
2022-01-13 07:57:54 -05:00
|
|
|
CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1 << 20, true};
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
const CBlockIndex* block_index;
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-09-09 16:15:15 -04:00
|
|
|
block_index = m_node.chainman->ActiveChain().Tip();
|
2019-08-28 15:58:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CoinStatsIndex should not be found before it is started.
|
2022-05-26 17:06:07 +02:00
|
|
|
BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
|
|
|
|
// is started.
|
|
|
|
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
|
|
|
|
|
2022-01-13 07:57:54 -05:00
|
|
|
BOOST_REQUIRE(coin_stats_index.Start());
|
2019-08-28 15:58:53 -04:00
|
|
|
|
2022-02-22 14:48:57 -05:00
|
|
|
IndexWaitSynced(coin_stats_index);
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
// Check that CoinStatsIndex works for genesis block.
|
|
|
|
const CBlockIndex* genesis_block_index;
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-09-09 16:15:15 -04:00
|
|
|
genesis_block_index = m_node.chainman->ActiveChain().Genesis();
|
2019-08-28 15:58:53 -04:00
|
|
|
}
|
2022-05-26 17:06:07 +02:00
|
|
|
BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
// Check that CoinStatsIndex updates with new blocks.
|
2022-05-26 17:06:07 +02:00
|
|
|
BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
|
|
|
|
std::vector<CMutableTransaction> noTxns;
|
|
|
|
CreateAndProcessBlock(noTxns, script_pub_key);
|
|
|
|
|
|
|
|
// Let the CoinStatsIndex to catch up again.
|
|
|
|
BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
|
|
|
|
|
|
|
|
const CBlockIndex* new_block_index;
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2020-09-09 16:15:15 -04:00
|
|
|
new_block_index = m_node.chainman->ActiveChain().Tip();
|
2019-08-28 15:58:53 -04:00
|
|
|
}
|
2022-05-26 17:06:07 +02:00
|
|
|
BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
|
2019-08-28 15:58:53 -04:00
|
|
|
|
|
|
|
BOOST_CHECK(block_index != new_block_index);
|
|
|
|
|
2022-09-27 18:02:22 +02:00
|
|
|
// It is not safe to stop and destroy the index until it finishes handling
|
|
|
|
// the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
|
|
|
|
// call above is sufficient to ensure this, but the
|
|
|
|
// SyncWithValidationInterfaceQueue() call below is also needed to ensure
|
|
|
|
// TSAN always sees the test thread waiting for the notification thread, and
|
|
|
|
// avoid potential false positive reports.
|
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
|
2019-08-28 15:58:53 -04:00
|
|
|
// Shutdown sequence (c.f. Shutdown() in init.cpp)
|
|
|
|
coin_stats_index.Stop();
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:48:57 -05:00
|
|
|
// Test shutdown between BlockConnected and ChainStateFlushed notifications,
|
|
|
|
// make sure index is not corrupted and is able to reload.
|
|
|
|
BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
|
|
|
|
{
|
2022-03-09 12:37:19 -05:00
|
|
|
Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
|
2022-02-22 14:48:57 -05:00
|
|
|
const CChainParams& params = Params();
|
|
|
|
{
|
2022-01-13 07:57:54 -05:00
|
|
|
CoinStatsIndex index{interfaces::MakeChain(m_node), 1 << 20};
|
|
|
|
BOOST_REQUIRE(index.Start());
|
2022-02-22 14:48:57 -05:00
|
|
|
IndexWaitSynced(index);
|
|
|
|
std::shared_ptr<const CBlock> new_block;
|
|
|
|
CBlockIndex* new_block_index = nullptr;
|
|
|
|
{
|
|
|
|
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
|
|
|
|
const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
|
|
|
|
|
|
|
|
new_block = std::make_shared<CBlock>(block);
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
BlockValidationState state;
|
|
|
|
BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
|
2022-08-02 16:48:57 -04:00
|
|
|
BOOST_CHECK(chainstate.AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
|
2022-02-22 14:48:57 -05:00
|
|
|
CCoinsViewCache view(&chainstate.CoinsTip());
|
|
|
|
BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
|
|
|
|
}
|
|
|
|
// Send block connected notification, then stop the index without
|
|
|
|
// sending a chainstate flushed notification. Prior to #24138, this
|
|
|
|
// would cause the index to be corrupted and fail to reload.
|
|
|
|
ValidationInterfaceTest::BlockConnected(index, new_block, new_block_index);
|
|
|
|
index.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-01-13 07:57:54 -05:00
|
|
|
CoinStatsIndex index{interfaces::MakeChain(m_node), 1 << 20};
|
2022-02-22 14:48:57 -05:00
|
|
|
// Make sure the index can be loaded.
|
2022-01-13 07:57:54 -05:00
|
|
|
BOOST_REQUIRE(index.Start());
|
2022-02-22 14:48:57 -05:00
|
|
|
index.Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:58:53 -04:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|