merge bitcoin#24138: Commit MuHash and best block together for coinstatsindex

Continuation of c21f23db from dash#5501
This commit is contained in:
Kittywhiskers Van Gogh 2024-10-12 05:57:17 +00:00
parent b218f123b7
commit 1caaa85716
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 57 additions and 0 deletions

View File

@ -2,9 +2,11 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
#include <index/coinstatsindex.h> #include <index/coinstatsindex.h>
#include <test/util/index.h> #include <test/util/index.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <util/time.h> #include <util/time.h>
#include <validation.h> #include <validation.h>
@ -74,4 +76,44 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
coin_stats_index.Stop(); coin_stats_index.Stop();
} }
// 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)
{
CChainState& chainstate = Assert(m_node.chainman)->ActiveChainstate();
const CChainParams& params = Params();
{
CoinStatsIndex index{1 << 20};
BOOST_REQUIRE(index.Start(chainstate));
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()));
BOOST_CHECK(chainstate.AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr));
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();
}
{
CoinStatsIndex index{1 << 20};
// Make sure the index can be loaded.
BOOST_REQUIRE(index.Start(chainstate));
index.Stop();
}
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -7,6 +7,7 @@
#include <util/check.h> #include <util/check.h>
#include <util/time.h> #include <util/time.h>
#include <validation.h> #include <validation.h>
#include <validationinterface.h>
void TestChainState::ResetIbd() void TestChainState::ResetIbd()
{ {
@ -20,3 +21,8 @@ void TestChainState::JumpOutOfIbd()
m_cached_finished_ibd = true; m_cached_finished_ibd = true;
Assert(!IsInitialBlockDownload()); Assert(!IsInitialBlockDownload());
} }
void ValidationInterfaceTest::BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
{
obj.BlockConnected(block, pindex);
}

View File

@ -7,6 +7,8 @@
#include <validation.h> #include <validation.h>
class CValidationInterface;
struct TestChainState : public CChainState { struct TestChainState : public CChainState {
/** Reset the ibd cache to its initial state */ /** Reset the ibd cache to its initial state */
void ResetIbd(); void ResetIbd();
@ -14,4 +16,10 @@ struct TestChainState : public CChainState {
void JumpOutOfIbd(); void JumpOutOfIbd();
}; };
class ValidationInterfaceTest
{
public:
static void BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex);
};
#endif // BITCOIN_TEST_UTIL_VALIDATION_H #endif // BITCOIN_TEST_UTIL_VALIDATION_H

View File

@ -200,6 +200,7 @@ protected:
* has been received and connected to the headers tree, though not validated yet */ * has been received and connected to the headers tree, though not validated yet */
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {}; virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
friend class CMainSignals; friend class CMainSignals;
friend class ValidationInterfaceTest;
}; };
struct MainSignalsInstance; struct MainSignalsInstance;