Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions csnode/include/csnode/caches_serialization_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cs::Bytes(cs::Sequence)>& chainHashAt);

class CachesSerializationManager {
public:
CachesSerializationManager();
Expand Down
9 changes: 8 additions & 1 deletion csnode/src/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down
8 changes: 8 additions & 0 deletions csnode/src/caches_serialization_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,14 @@ bool CachesSerializationManager::save(size_t version, const CheckpointHead& head
return true;
}

bool isCheckpointOnChain(const CheckpointHead& head, const std::function<cs::Bytes(cs::Sequence)>& 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{});
}
Expand Down
32 changes: 32 additions & 0 deletions tests/csnode/checkpoint_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <gtest/gtest.h>

#include <csnode/caches_serialization_manager.hpp>

// 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; }));
}
Loading