Bitcoin Core 30.99.0
P2P Digital Currency
chainstate_write_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 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
6#include <validation.h>
8
9#include <boost/test/unit_test.hpp>
10
12
13// Taken from validation.cpp
14static constexpr auto DATABASE_WRITE_INTERVAL_MIN{50min};
15static constexpr auto DATABASE_WRITE_INTERVAL_MAX{70min};
16
17BOOST_AUTO_TEST_SUITE(chainstate_write_tests)
18
19BOOST_FIXTURE_TEST_CASE(chainstate_write_interval, TestingSetup)
20{
22 bool m_did_flush{false};
23 void ChainStateFlushed(const ChainstateRole&, const CBlockLocator&) override
24 {
25 m_did_flush = true;
26 }
27 };
28
29 const auto sub{std::make_shared<TestSubscriber>()};
30 m_node.validation_signals->RegisterSharedValidationInterface(sub);
31 auto& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
32 BlockValidationState state_dummy{};
33
34 // The first periodic flush sets m_next_write and does not flush
35 chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
36 m_node.validation_signals->SyncWithValidationInterfaceQueue();
37 BOOST_CHECK(!sub->m_did_flush);
38
39 // The periodic flush interval is between 50 and 70 minutes (inclusive)
40 SetMockTime(GetTime<std::chrono::minutes>() + DATABASE_WRITE_INTERVAL_MIN - 1min);
41 chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
42 m_node.validation_signals->SyncWithValidationInterfaceQueue();
43 BOOST_CHECK(!sub->m_did_flush);
44
45 SetMockTime(GetTime<std::chrono::minutes>() + DATABASE_WRITE_INTERVAL_MAX);
46 chainstate.FlushStateToDisk(state_dummy, FlushStateMode::PERIODIC);
47 m_node.validation_signals->SyncWithValidationInterfaceQueue();
48 BOOST_CHECK(sub->m_did_flush);
49}
50
51// Test that we do PERIODIC flushes inside ActivateBestChain.
52// This is necessary for reindex-chainstate to be able to periodically flush
53// before reaching chain tip.
54BOOST_FIXTURE_TEST_CASE(write_during_multiblock_activation, TestChain100Setup)
55{
57 {
58 const CBlockIndex* m_tip{nullptr};
59 const CBlockIndex* m_flushed_at_block{nullptr};
60 void ChainStateFlushed(const ChainstateRole&, const CBlockLocator&) override
61 {
62 m_flushed_at_block = m_tip;
63 }
64 void UpdatedBlockTip(const CBlockIndex* block_index, const CBlockIndex*, bool) override {
65 m_tip = block_index;
66 }
67 };
68
69 auto& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
70 BlockValidationState state_dummy{};
71
72 // Pop two blocks from the tip
73 const CBlockIndex* tip{chainstate.m_chain.Tip()};
74 CBlockIndex* second_from_tip{tip->pprev};
75
76 {
77 LOCK2(m_node.chainman->GetMutex(), chainstate.MempoolMutex());
78 chainstate.DisconnectTip(state_dummy, nullptr);
79 chainstate.DisconnectTip(state_dummy, nullptr);
80 }
81
82 BOOST_CHECK_EQUAL(second_from_tip->pprev, chainstate.m_chain.Tip());
83
84 // Set m_next_write to current time
85 chainstate.FlushStateToDisk(state_dummy, FlushStateMode::ALWAYS);
86 m_node.validation_signals->SyncWithValidationInterfaceQueue();
87 // The periodic flush interval is between 50 and 70 minutes (inclusive)
88 // The next call to a PERIODIC write will flush
90
91 const auto sub{std::make_shared<TestSubscriber>()};
92 m_node.validation_signals->RegisterSharedValidationInterface(sub);
93
94 // ActivateBestChain back to tip
95 chainstate.ActivateBestChain(state_dummy, nullptr);
96 BOOST_CHECK_EQUAL(tip, chainstate.m_chain.Tip());
97 // Check that we flushed inside ActivateBestChain while we were at the
98 // second block from tip, since FlushStateToDisk is called with PERIODIC
99 // inside the outer loop.
100 m_node.validation_signals->SyncWithValidationInterfaceQueue();
101 BOOST_CHECK_EQUAL(sub->m_flushed_at_block, second_from_tip);
102}
103
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
BOOST_FIXTURE_TEST_CASE(chainstate_write_interval, TestingSetup)
static constexpr auto DATABASE_WRITE_INTERVAL_MAX
static constexpr auto DATABASE_WRITE_INTERVAL_MIN
#define Assert(val)
Identity function.
Definition: check.h:113
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:95
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:101
Implement this to subscribe to events generated in validation and mempool.
virtual void ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:124
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:146
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
Testing setup that configures a complete environment.
Definition: setup_common.h:121
Information about chainstate that notifications are sent from.
Definition: types.h:18
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define LOCK2(cs1, cs2)
Definition: sync.h:260
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:48
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40