Skip to content

Commit 4d8f4dc

Browse files
committed
validation: pass ChainstateRole for validationinterface calls
This allows consumers to decide how to handle events from background or assumedvalid chainstates.
1 parent 1e59acd commit 4d8f4dc

19 files changed

+66
-42
lines changed

src/bench/wallet_create_tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void generateFakeBlock(const CChainParams& params,
7070

7171
// notify wallet
7272
const auto& pindex = WITH_LOCK(::cs_main, return context.chainman->ActiveChain().Tip());
73-
wallet.blockConnected(kernel::MakeBlockInfo(pindex, &block));
73+
wallet.blockConnected(ChainstateRole::NORMAL, kernel::MakeBlockInfo(pindex, &block));
7474
}
7575

7676
struct PreSelectInputs {

src/index/base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
250250
return true;
251251
}
252252

253-
void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
253+
void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
254254
{
255255
if (!m_synced) {
256256
return;
@@ -296,7 +296,7 @@ void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const
296296
}
297297
}
298298

299-
void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
299+
void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator)
300300
{
301301
if (!m_synced) {
302302
return;

src/index/base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ class BaseIndex : public CValidationInterface
102102
Chainstate* m_chainstate{nullptr};
103103
const std::string m_name;
104104

105-
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
105+
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
106106

107-
void ChainStateFlushed(const CBlockLocator& locator) override;
107+
void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;
108108

109109
/// Initialize internal state from the database and block index.
110110
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }

src/interfaces/chain.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Coin;
2727
class uint256;
2828
enum class MemPoolRemovalReason;
2929
enum class RBFTransactionState;
30+
enum class ChainstateRole;
3031
struct bilingual_str;
3132
struct CBlockLocator;
3233
struct FeeCalculation;
@@ -310,10 +311,10 @@ class Chain
310311
virtual ~Notifications() {}
311312
virtual void transactionAddedToMempool(const CTransactionRef& tx) {}
312313
virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) {}
313-
virtual void blockConnected(const BlockInfo& block) {}
314+
virtual void blockConnected(ChainstateRole role, const BlockInfo& block) {}
314315
virtual void blockDisconnected(const BlockInfo& block) {}
315316
virtual void updatedBlockTip() {}
316-
virtual void chainStateFlushed(const CBlockLocator& locator) {}
317+
virtual void chainStateFlushed(ChainstateRole role, const CBlockLocator& locator) {}
317318
};
318319

319320
//! Register handler for notifications.

src/net_processing.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <index/blockfilterindex.h>
1919
#include <kernel/mempool_entry.h>
2020
#include <logging.h>
21+
#include <kernel/chain.h>
2122
#include <merkleblock.h>
2223
#include <netbase.h>
2324
#include <netmessagemaker.h>
@@ -483,7 +484,7 @@ class PeerManagerImpl final : public PeerManager
483484
CTxMemPool& pool, Options opts);
484485

485486
/** Overridden from CValidationInterface. */
486-
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
487+
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
487488
EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex);
488489
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
489490
EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex);
@@ -1911,7 +1912,10 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
19111912
* announcements for them. Also save the time of the last tip update and
19121913
* possibly reduce dynamic block stalling timeout.
19131914
*/
1914-
void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
1915+
void PeerManagerImpl::BlockConnected(
1916+
ChainstateRole role,
1917+
const std::shared_ptr<const CBlock>& pblock,
1918+
const CBlockIndex* pindex)
19151919
{
19161920
m_orphanage.EraseForBlock(*pblock);
19171921
m_last_tip_update = GetTime<std::chrono::seconds>();

src/node/interfaces.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ class NotificationsProxy : public CValidationInterface
434434
{
435435
m_notifications->transactionRemovedFromMempool(tx, reason);
436436
}
437-
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
437+
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
438438
{
439-
m_notifications->blockConnected(kernel::MakeBlockInfo(index, block.get()));
439+
m_notifications->blockConnected(role, kernel::MakeBlockInfo(index, block.get()));
440440
}
441441
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
442442
{
@@ -446,7 +446,9 @@ class NotificationsProxy : public CValidationInterface
446446
{
447447
m_notifications->updatedBlockTip();
448448
}
449-
void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->chainStateFlushed(locator); }
449+
void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override {
450+
m_notifications->chainStateFlushed(role, locator);
451+
}
450452
std::shared_ptr<Chain::Notifications> m_notifications;
451453
};
452454

src/test/coinstatsindex_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
105105
// Send block connected notification, then stop the index without
106106
// sending a chainstate flushed notification. Prior to #24138, this
107107
// would cause the index to be corrupted and fail to reload.
108-
ValidationInterfaceTest::BlockConnected(index, new_block, new_block_index);
108+
ValidationInterfaceTest::BlockConnected(ChainstateRole::NORMAL, index, new_block, new_block_index);
109109
index.Stop();
110110
}
111111

src/test/util/validation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ void TestChainstateManager::JumpOutOfIbd()
2222
Assert(!IsInitialBlockDownload());
2323
}
2424

25-
void ValidationInterfaceTest::BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
25+
void ValidationInterfaceTest::BlockConnected(
26+
ChainstateRole role,
27+
CValidationInterface& obj,
28+
const std::shared_ptr<const CBlock>& block,
29+
const CBlockIndex* pindex)
2630
{
27-
obj.BlockConnected(block, pindex);
31+
obj.BlockConnected(role, block, pindex);
2832
}

src/test/util/validation.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ struct TestChainstateManager : public ChainstateManager {
1919
class ValidationInterfaceTest
2020
{
2121
public:
22-
static void BlockConnected(CValidationInterface& obj, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex);
22+
static void BlockConnected(
23+
ChainstateRole role,
24+
CValidationInterface& obj,
25+
const std::shared_ptr<const CBlock>& block,
26+
const CBlockIndex* pindex);
2327
};
2428

2529
#endif // BITCOIN_TEST_UTIL_VALIDATION_H

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct TestSubscriber final : public CValidationInterface {
4343
BOOST_CHECK_EQUAL(m_expected_tip, pindexNew->GetBlockHash());
4444
}
4545

46-
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
46+
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
4747
{
4848
BOOST_CHECK_EQUAL(m_expected_tip, block->hashPrevBlock);
4949
BOOST_CHECK_EQUAL(m_expected_tip, pindex->pprev->GetBlockHash());

0 commit comments

Comments
 (0)