From 9838229448088336615d6e22d7099d0a993fe065 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 07:40:12 +0200 Subject: [PATCH] csnode: check quick-start checkpoints against the chain before loading them PR #71 added a chain binding to checkpoints (head.bin: sequence and head hash) and a ChainVerifier hook to CachesSerializationManager::load(), but BlockChain::tryQuickStart() calls load() without a verifier. A checkpoint saved for a block the node no longer has at that sequence (after a rollback, a fork, or caches copied from another node) is therefore loaded as is. Its wallet balances and consensus settings then silently differ from the chain the node actually holds. Pass a verifier that compares the checkpoint head hash with the block hash the node has at that sequence (BlockHashes, persisted independently of the blocks DB). On a mismatch the checkpoint is quarantined and the next older one is tried, falling back to a slow start; this is the existing behaviour for any rejected checkpoint. When the check is not possible (legacy checkpoint without head, or hash unknown locally), the checkpoint is accepted as before, so healthy nodes are never forced into a resync. The decision is in a pure helper, cs::isCheckpointOnChain(), covered by tests/csnode/checkpoint_tests.cpp (4 cases). nodetests: 123/123 pass. Co-Authored-By: Claude Opus 5.5 --- .../csnode/caches_serialization_manager.hpp | 4 +++ csnode/src/blockchain.cpp | 9 +++++- csnode/src/caches_serialization_manager.cpp | 8 +++++ tests/csnode/checkpoint_tests.cpp | 32 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/csnode/checkpoint_tests.cpp diff --git a/csnode/include/csnode/caches_serialization_manager.hpp b/csnode/include/csnode/caches_serialization_manager.hpp index 7645b8dc0..41ab4333f 100644 --- a/csnode/include/csnode/caches_serialization_manager.hpp +++ b/csnode/include/csnode/caches_serialization_manager.hpp @@ -32,6 +32,10 @@ struct CheckpointHead { cs::Bytes prev_hash; }; +// True when the checkpoint head is the block the chain holds at that sequence, or when this +// cannot be checked (checkpoint without head, or hash not known locally). +bool isCheckpointOnChain(const CheckpointHead& head, const std::function& chainHashAt); + class CachesSerializationManager { public: CachesSerializationManager(); diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index a1b06da46..19790f216 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -90,7 +90,14 @@ bool BlockChain::tryQuickStart( for (auto it : initialConfidants) { reserveConf.insert(it); } - bool ok = serializationManPtr_->load(); + // trust the caches only if their head is still the block this node has at that sequence + bool ok = serializationManPtr_->load([this](const cs::CheckpointHead& head) { + if (cs::isCheckpointOnChain(head, [this](cs::Sequence seq) { return blockHashes_->find(seq).to_binary(); })) { + return true; + } + cswarning() << "QUICK START: checkpoint head #" << head.sequence << " is not on this chain, skip it"; + return false; + }); if (ok) { cslog() << "Caches for QUICK START loaded successfully!"; diff --git a/csnode/src/caches_serialization_manager.cpp b/csnode/src/caches_serialization_manager.cpp index 81dca51ae..0964b35ab 100644 --- a/csnode/src/caches_serialization_manager.cpp +++ b/csnode/src/caches_serialization_manager.cpp @@ -630,6 +630,14 @@ bool CachesSerializationManager::save(size_t version, const CheckpointHead& head return true; } +bool isCheckpointOnChain(const CheckpointHead& head, const std::function& chainHashAt) { + if (head.sequence == 0 || head.head_hash.empty()) { + return true; + } + const cs::Bytes onChain = chainHashAt(head.sequence); + return onChain.empty() || onChain == head.head_hash; +} + bool CachesSerializationManager::load() { return load(ChainVerifier{}); } diff --git a/tests/csnode/checkpoint_tests.cpp b/tests/csnode/checkpoint_tests.cpp new file mode 100644 index 000000000..c8077b4dc --- /dev/null +++ b/tests/csnode/checkpoint_tests.cpp @@ -0,0 +1,32 @@ +#include + +#include + +// quick start must not trust caches saved for a block the chain no longer has at that sequence +namespace { +const cs::Bytes kHashA{ 1, 2, 3 }; +const cs::Bytes kHashB{ 4, 5, 6 }; + +cs::CheckpointHead head(cs::Sequence seq, const cs::Bytes& hash) { + cs::CheckpointHead h; + h.sequence = seq; + h.head_hash = hash; + return h; +} +} // namespace + +TEST(Checkpoint, AcceptedWhenHeadMatchesChain) { + ASSERT_TRUE(cs::isCheckpointOnChain(head(100, kHashA), [](cs::Sequence) { return kHashA; })); +} + +TEST(Checkpoint, RejectedWhenChainHasAnotherBlockAtHead) { + ASSERT_FALSE(cs::isCheckpointOnChain(head(100, kHashA), [](cs::Sequence) { return kHashB; })); +} + +TEST(Checkpoint, AcceptedWhenChainHashUnknown) { + ASSERT_TRUE(cs::isCheckpointOnChain(head(100, kHashA), [](cs::Sequence) { return cs::Bytes{}; })); +} + +TEST(Checkpoint, AcceptedWhenCheckpointHasNoHead) { + ASSERT_TRUE(cs::isCheckpointOnChain(head(0, cs::Bytes{}), [](cs::Sequence) { return kHashB; })); +}