From 1f4a231e3c66ab99ea6bb659f8c3dc824fb8863a Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 9 Nov 2026 12:31:58 +0100 Subject: [PATCH 1/5] solver: keep the own signed stage 1 across a same-round package replacement; log a re-signed stage 1 (diagnostics) The stage 1 this node signs is kept with its round, sub-round and seat key and survives nextRound(true). When stage 1 is signed again for the same round and sub-round, an info line shows both timestamps and which content fields differ. The replacement of a round package by a fuller one is logged at info level. Nothing sent or computed changes. Co-Authored-By: Claude Opus 5.5 --- csnode/src/node.cpp | 4 +- solver/include/solver/solvercontext.hpp | 7 +++ solver/include/solver/solvercore.hpp | 11 +++++ .../solver/states/trustedstage1state.hpp | 2 + solver/src/solvercontext.cpp | 23 ++++++++++ solver/src/states/trustedstage1state.cpp | 44 +++++++++++++++++++ 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/csnode/src/node.cpp b/csnode/src/node.cpp index ae499ca7f..f903688e5 100644 --- a/csnode/src/node.cpp +++ b/csnode/src/node.cpp @@ -4044,7 +4044,7 @@ void Node::getRoundTable(const uint8_t* data, const size_t size, const cs::Round if (rPackage.roundTable().round == roundPackageCache_.back().roundTable().round && !stageThreeSent_) { auto mask = roundPackageCache_.back().poolMetaInfo().realTrustedMask; if (cs::TrustedMask::trustedSize(rPackage.poolMetaInfo().realTrustedMask) > cs::TrustedMask::trustedSize(mask)) { - csdebug() << "Current Roundpackage of " << rNum << " will be replaced by new one"; + cslog() << "NODE> Current Roundpackage of " << rNum << " will be replaced by new one"; auto it = roundPackageCache_.end(); if (!roundPackageCache_.empty()) { --it; @@ -4081,7 +4081,7 @@ void Node::getRoundTable(const uint8_t* data, const size_t size, const cs::Round && getBlockChain().hasDeferredBlock(own.sequenceNumber); const bool verifies = deferred && getBlockChain().packageBlockVerifies(rPackage); if (moreTrusted) { // official rule - csdebug() << "Current Roundpackage of " << rNum << " will be replaced by new one"; + cslog() << "NODE> Current Roundpackage of " << rNum << " will be replaced by new one"; roundPackageCache_.push_back(rPackage); } else if (verifies) { diff --git a/solver/include/solver/solvercontext.hpp b/solver/include/solver/solvercontext.hpp index 2d5c5e20d..4b9a70b2b 100644 --- a/solver/include/solver/solvercontext.hpp +++ b/solver/include/solver/solvercontext.hpp @@ -249,6 +249,13 @@ class SolverContext { void add_stage1(cs::StageOne& stage, bool send); + // the stage 1 this node already signed and sent for the current round and sub-round from its + // current seat (nullptr if none): kept across a same-round round-package replacement + const cs::StageOne* sent_stage1() const; + + // remembers the stage 1 just signed and sent, for sent_stage1() + void remember_sent_stage1(const cs::StageOne& stage); + void add_stage2(cs::StageTwo& stage, bool send); void add_stage3(cs::StageThree& stage); diff --git a/solver/include/solver/solvercore.hpp b/solver/include/solver/solvercore.hpp index cff674365..aec84878d 100644 --- a/solver/include/solver/solvercore.hpp +++ b/solver/include/solver/solvercore.hpp @@ -378,6 +378,17 @@ class SolverCore { cs::RoundNumber lastGrayUpdated_ = 0; RoundPackage justCreatedRoundPackage_; SentSignatures lastSentSignatures_; + + // the stage 1 this node last signed and sent, with the round, sub-round and seat key it was + // signed for; unlike stageOneStorage it survives nextRound(true), the re-entry of the same + // round after a fuller round package replaced the first one + struct SentStageOne { + cs::RoundNumber round = 0; + uint8_t subRound = 0; + cs::PublicKey key{}; + cs::StageOne stage; + }; + std::optional sentStageOne_; std::unique_ptr pVal_; std::string kLogPrefix_; }; diff --git a/solver/include/solver/states/trustedstage1state.hpp b/solver/include/solver/states/trustedstage1state.hpp index e6b10e9e2..ec322f2d8 100644 --- a/solver/include/solver/states/trustedstage1state.hpp +++ b/solver/include/solver/states/trustedstage1state.hpp @@ -51,6 +51,8 @@ class TrustedStage1State : public DefaultStateBehavior { bool min_time_expired{false}; void finalizeStage(SolverContext& context); + static std::string stage1ContentDiff(const cs::StageOne& a, const cs::StageOne& b); + // TimeoutTracking min_time_tracking; cs::StageOne stage; size_t likeMineHashes; diff --git a/solver/src/solvercontext.cpp b/solver/src/solvercontext.cpp index 5cde93beb..1a8e02eb5 100644 --- a/solver/src/solvercontext.cpp +++ b/solver/src/solvercontext.cpp @@ -50,6 +50,29 @@ void SolverContext::add_stage1(cs::StageOne& stage, bool send) { core.gotStageOne(stage); } +const cs::StageOne* SolverContext::sent_stage1() const { + const auto& sent = core.sentStageOne_; + if (!sent.has_value()) { + return nullptr; + } + const cs::Conveyer& conveyer = cs::Conveyer::instance(); + const uint8_t seat = own_conf_number(); + if (sent->round != conveyer.currentRoundNumber() || sent->subRound != core.subRound() || sent->stage.sender != seat || + !conveyer.isConfidantExists(seat) || conveyer.confidantByIndex(seat) != sent->key) { + return nullptr; + } + return &sent->stage; +} + +void SolverContext::remember_sent_stage1(const cs::StageOne& stage) { + const cs::Conveyer& conveyer = cs::Conveyer::instance(); + if (!conveyer.isConfidantExists(stage.sender)) { + core.sentStageOne_.reset(); + return; + } + core.sentStageOne_ = SolverCore::SentStageOne{conveyer.currentRoundNumber(), core.subRound(), conveyer.confidantByIndex(stage.sender), stage}; +} + void SolverContext::add_stage2(cs::StageTwo& stage, bool send) { // core.stageTwoStorage.push_back(stage); diff --git a/solver/src/states/trustedstage1state.cpp b/solver/src/states/trustedstage1state.cpp index 145db2cab..962bc146b 100644 --- a/solver/src/states/trustedstage1state.cpp +++ b/solver/src/states/trustedstage1state.cpp @@ -15,6 +15,7 @@ #include #include +#include #include namespace cs { @@ -99,9 +100,52 @@ void TrustedStage1State::off(SolverContext& context) { //} csdebug() << name() << ": --> stage-1 [" << static_cast(stage.sender) << "]"; if (min_time_expired && transactions_checked && enough_hashes) { + // a stage 1 already signed and sent for this round and sub-round: the round was entered + // again after a fuller round package replaced the first one (SolverCore::nextRound(true)) + const cs::StageOne* sent = context.sent_stage1(); + std::string sentStamp; + std::string differs; + if (sent != nullptr) { + sentStamp = sent->roundTimeStamp; + differs = stage1ContentDiff(*sent, stage); + } finalizeStage(context); + if (sent != nullptr) { + // diagnostics only: the receivers now hold two signed stage-1 messages of this seat + cslog() << name() << ": stage1 signed again: R-" << cs::Conveyer::instance().currentRoundNumber() << "." + << static_cast(context.subRound()) << " T" << static_cast(stage.sender) << " ts " << sentStamp + << " -> " << stage.roundTimeStamp << ", content " << (differs.empty() ? "same" : "differs (" + differs + ")"); + } context.add_stage1(stage, true); + context.remember_sent_stage1(stage); + } +} + +// the fields of two stage-1 messages of one seat that differ, timestamp and signature aside +// (empty: the same content); candidates compare as sets, their order is the arrival order +std::string TrustedStage1State::stage1ContentDiff(const cs::StageOne& a, const cs::StageOne& b) { + std::string diff; + auto add = [&diff](const char* field) { + diff += diff.empty() ? field : std::string(",") + field; + }; + if (a.sender != b.sender) { + add("sender"); + } + if (a.hash != b.hash) { + add("hash"); + } + auto sameSet = [](auto x, auto y) { + std::sort(x.begin(), x.end()); + std::sort(y.begin(), y.end()); + return x == y; + }; + if (!sameSet(a.trustedCandidates, b.trustedCandidates)) { + add("candidates"); + } + if (a.hashesCandidates != b.hashesCandidates) { + add("packets"); } + return diff; } Result TrustedStage1State::onSyncTransactions(SolverContext& context, cs::RoundNumber round) { From ed88167cf59aad9ad3708d74161043d707c0a15c Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 9 Nov 2026 18:15:11 +0100 Subject: [PATCH 2/5] solver: re-send the signed stage 1 instead of signing a second one in the same round After a fuller round package of the same round replaced the first one, a confidant enters stage 1 again (SolverCore::nextRound(true)) and used to sign a second stage 1 with a new timestamp. The confidants could then hold different stage-1 copies of that seat, and so compute different block timestamps or see two hash-signature pairs of the seat at stage 2. Now, when this node already signed a stage 1 for the same round, sub-round and seat, it sends the stored signed message again, byte for byte. Its trusted candidates and packet hashes may lack the ones that came in later; these are only this node's votes for the next round. A new stage 1 is still signed when the stored one would be refused or would make the seat look faulty (cs::stageOneResend): the characteristic hash changed, the stored timestamp is not after the last block's, or it is more than DefaultTimeStampRange old (the own stage-2 window is measured from the own stamp). Only what this node sends changes; receivers verify the message as any stage 1. The stored stage 1 is cleared by nextRound(false) and at the start of every bootstrap round, and sent_stage1() still checks round, sub-round and seat key. The "stage1 signed again" line now gives the reason; the content comparison moved to cs::stageOneContentDiff. Unit tests cover the decision and that the resent bytes verify at a receiver in the same round and sub-round only. Co-Authored-By: Claude Opus 5.5 --- solver/include/solver/solvercore.hpp | 3 +- solver/include/solver/stage.hpp | 27 ++++ .../solver/states/trustedstage1state.hpp | 2 - solver/src/solverinterface.cpp | 6 + solver/src/stage.cpp | 63 ++++++++ solver/src/states/trustedstage1state.cpp | 69 ++++---- tests/csnode/stageoneresend_tests.cpp | 147 ++++++++++++++++++ 7 files changed, 281 insertions(+), 36 deletions(-) create mode 100644 tests/csnode/stageoneresend_tests.cpp diff --git a/solver/include/solver/solvercore.hpp b/solver/include/solver/solvercore.hpp index aec84878d..b5b94efc4 100644 --- a/solver/include/solver/solvercore.hpp +++ b/solver/include/solver/solvercore.hpp @@ -381,7 +381,8 @@ class SolverCore { // the stage 1 this node last signed and sent, with the round, sub-round and seat key it was // signed for; unlike stageOneStorage it survives nextRound(true), the re-entry of the same - // round after a fuller round package replaced the first one + // round after a fuller round package replaced the first one; nextRound(false) and the start of + // a bootstrap round clear it, and SolverContext::sent_stage1() checks round, sub-round and seat struct SentStageOne { cs::RoundNumber round = 0; uint8_t subRound = 0; diff --git a/solver/include/solver/stage.hpp b/solver/include/solver/stage.hpp index 7628bf8ec..fa5e0f63f 100644 --- a/solver/include/solver/stage.hpp +++ b/solver/include/solver/stage.hpp @@ -160,6 +160,33 @@ struct StageHash { cs::RoundNumber round; }; +// What a confidant does with its stage 1 when it enters stage 1 again in the same round and +// sub-round from the same seat (SolverCore::nextRound(true), after a fuller round package of the +// round replaced the first one) and finds the stage 1 it already signed and sent: +// - Resend: the stored signed message is sent again byte for byte, so every confidant holds the +// same stage 1 of this seat; +// - otherwise a new stage 1 is signed as before, because the stored one would be refused or +// would make this seat look faulty: HashChanged (the characteristic hash must match the +// majority's), NotAfterLastBlock (the stamp must be after the last block's), TooOld (the stored +// stamp is more than Consensus::DefaultTimeStampRange old, so fresh stamps of the others could +// fall outside this node's own stage-2 window, which is measured from its own stamp). +enum class StageOneResend : uint8_t { + Resend, + HashChanged, + NotAfterLastBlock, + TooOld +}; + +// sent: the stored signed stage 1; freshHash: the characteristic hash computed now; +// lastBlockStamp: the last block's timestamp; now: the current time (ms) +StageOneResend stageOneResend(const StageOne& sent, const Hash& freshHash, uint64_t lastBlockStamp, uint64_t now); + +const char* toString(StageOneResend decision); + +// the content fields of two stage-1 messages of one seat that differ, timestamp and signature +// aside, comma separated (empty: the same content); trusted candidates compare as sets +std::string stageOneContentDiff(const StageOne& a, const StageOne& b); + } // namespace cs #endif // STAGE_HPP diff --git a/solver/include/solver/states/trustedstage1state.hpp b/solver/include/solver/states/trustedstage1state.hpp index ec322f2d8..e6b10e9e2 100644 --- a/solver/include/solver/states/trustedstage1state.hpp +++ b/solver/include/solver/states/trustedstage1state.hpp @@ -51,8 +51,6 @@ class TrustedStage1State : public DefaultStateBehavior { bool min_time_expired{false}; void finalizeStage(SolverContext& context); - static std::string stage1ContentDiff(const cs::StageOne& a, const cs::StageOne& b); - // TimeoutTracking min_time_tracking; cs::StageOne stage; size_t likeMineHashes; diff --git a/solver/src/solverinterface.cpp b/solver/src/solverinterface.cpp index 676508837..787a88338 100644 --- a/solver/src/solverinterface.cpp +++ b/solver/src/solverinterface.cpp @@ -199,6 +199,12 @@ void SolverCore::nextRound(bool updateRound) { if (!updateRound) { recv_hash.clear(); } + // the own signed stage 1 is kept only across the re-entry of the same round after a fuller + // round package replaced the first one; a new round and every bootstrap round start without it + // (bootstrap tables start rounds with updateRound = true too) + if (!updateRound || pnode->isBootstrapRound()) { + sentStageOne_.reset(); + } deferredBlock_ = csdb::Pool{}; stageOneStorage.clear(); stageTwoStorage.clear(); diff --git a/solver/src/stage.cpp b/solver/src/stage.cpp index 29f8ae41e..679d81024 100644 --- a/solver/src/stage.cpp +++ b/solver/src/stage.cpp @@ -6,6 +6,10 @@ #include #include #include + +#include +#include + namespace cs { TrustedMask::TrustedMask() {} @@ -239,4 +243,63 @@ namespace cs { Stage::Stage() { sender = cs::ConfidantConsts::InvalidConfidantIndex; } + + StageOneResend stageOneResend(const StageOne& sent, const Hash& freshHash, uint64_t lastBlockStamp, uint64_t now) { + if (sent.hash != freshHash) { + return StageOneResend::HashChanged; + } + uint64_t sentStamp = 0; + try { + sentStamp = std::stoull(sent.roundTimeStamp); + } + catch (...) { + return StageOneResend::NotAfterLastBlock; + } + if (sentStamp <= lastBlockStamp) { + return StageOneResend::NotAfterLastBlock; + } + if (now > sentStamp && now - sentStamp > Consensus::DefaultTimeStampRange) { + return StageOneResend::TooOld; + } + return StageOneResend::Resend; + } + + const char* toString(StageOneResend decision) { + switch (decision) { + case StageOneResend::Resend: + return "resend"; + case StageOneResend::HashChanged: + return "hash-changed"; + case StageOneResend::NotAfterLastBlock: + return "not-after-last-block"; + case StageOneResend::TooOld: + return "too-old"; + } + return "?"; + } + + std::string stageOneContentDiff(const StageOne& a, const StageOne& b) { + std::string diff; + auto add = [&diff](const char* field) { + diff += diff.empty() ? field : std::string(",") + field; + }; + if (a.sender != b.sender) { + add("sender"); + } + if (a.hash != b.hash) { + add("hash"); + } + auto sameSet = [](auto x, auto y) { + std::sort(x.begin(), x.end()); + std::sort(y.begin(), y.end()); + return x == y; + }; + if (!sameSet(a.trustedCandidates, b.trustedCandidates)) { + add("candidates"); + } + if (a.hashesCandidates != b.hashesCandidates) { + add("packets"); + } + return diff; + } } diff --git a/solver/src/states/trustedstage1state.cpp b/solver/src/states/trustedstage1state.cpp index 962bc146b..e808d0aaa 100644 --- a/solver/src/states/trustedstage1state.cpp +++ b/solver/src/states/trustedstage1state.cpp @@ -15,7 +15,7 @@ #include #include -#include +#include #include namespace cs { @@ -100,54 +100,57 @@ void TrustedStage1State::off(SolverContext& context) { //} csdebug() << name() << ": --> stage-1 [" << static_cast(stage.sender) << "]"; if (min_time_expired && transactions_checked && enough_hashes) { - // a stage 1 already signed and sent for this round and sub-round: the round was entered - // again after a fuller round package replaced the first one (SolverCore::nextRound(true)) + // a stage 1 already signed and sent for this round and sub-round from this seat: the round + // was entered again after a fuller round package replaced the first one (SolverCore::nextRound(true)) const cs::StageOne* sent = context.sent_stage1(); std::string sentStamp; std::string differs; + cs::StageOneResend decision = cs::StageOneResend::Resend; if (sent != nullptr) { sentStamp = sent->roundTimeStamp; - differs = stage1ContentDiff(*sent, stage); + differs = cs::stageOneContentDiff(*sent, stage); + // Do not sign a second stage 1 for the same round and sub-round: send the same signed + // bytes again, as gotStageOneRequest() does. A second signed message of the seat makes + // the confidants hold different stage-1 copies (a receiver keeps the later stamp only if + // it gets both): they choose different block timestamps, or see two hash-signature pairs + // of this seat at stage 2 and exclude it. Receivers verify the message as any other (the + // signature covers this round, sub-round and message hash); one that already holds it + // ignores it (same timestamp). + // What the stored message may lack is harmless: the trusted candidates and the packet + // hashes are this node's votes for the next round, and the ones that came in after the + // first stage 1 only miss one vote, as if the replacement had not happened; the others + // already hold and accept that message. A new stage 1 is signed as before when the + // stored one would be refused or make this seat look faulty: see cs::stageOneResend(). + uint64_t lastStamp = 0; + try { + lastStamp = std::stoull(context.blockchain().getLastTimeStamp()); + } + catch (...) { + lastStamp = std::numeric_limits::max(); // unknown: sign a new one + } + decision = cs::stageOneResend(*sent, stage.hash, lastStamp, cs::Utils::currentTimestamp()); + if (decision == cs::StageOneResend::Resend) { + stage = *sent; + cslog() << name() << ": stage1 resent unchanged: R-" << cs::Conveyer::instance().currentRoundNumber() << "." + << static_cast(context.subRound()) << " T" << static_cast(stage.sender) << " ts " << sentStamp + << (differs.empty() ? std::string{} : ", a new one would differ in " + differs); + context.add_stage1(stage, true); + return; + } } finalizeStage(context); if (sent != nullptr) { - // diagnostics only: the receivers now hold two signed stage-1 messages of this seat + // the receivers may now hold two signed stage-1 messages of this seat cslog() << name() << ": stage1 signed again: R-" << cs::Conveyer::instance().currentRoundNumber() << "." << static_cast(context.subRound()) << " T" << static_cast(stage.sender) << " ts " << sentStamp - << " -> " << stage.roundTimeStamp << ", content " << (differs.empty() ? "same" : "differs (" + differs + ")"); + << " -> " << stage.roundTimeStamp << ", content " << (differs.empty() ? "same" : "differs (" + differs + ")") + << ", reason " << cs::toString(decision); } context.add_stage1(stage, true); context.remember_sent_stage1(stage); } } -// the fields of two stage-1 messages of one seat that differ, timestamp and signature aside -// (empty: the same content); candidates compare as sets, their order is the arrival order -std::string TrustedStage1State::stage1ContentDiff(const cs::StageOne& a, const cs::StageOne& b) { - std::string diff; - auto add = [&diff](const char* field) { - diff += diff.empty() ? field : std::string(",") + field; - }; - if (a.sender != b.sender) { - add("sender"); - } - if (a.hash != b.hash) { - add("hash"); - } - auto sameSet = [](auto x, auto y) { - std::sort(x.begin(), x.end()); - std::sort(y.begin(), y.end()); - return x == y; - }; - if (!sameSet(a.trustedCandidates, b.trustedCandidates)) { - add("candidates"); - } - if (a.hashesCandidates != b.hashesCandidates) { - add("packets"); - } - return diff; -} - Result TrustedStage1State::onSyncTransactions(SolverContext& context, cs::RoundNumber round) { cs::Conveyer& conveyer = cs::Conveyer::instance(); if (round < conveyer.currentRoundNumber()) { diff --git a/tests/csnode/stageoneresend_tests.cpp b/tests/csnode/stageoneresend_tests.cpp new file mode 100644 index 000000000..747c205ca --- /dev/null +++ b/tests/csnode/stageoneresend_tests.cpp @@ -0,0 +1,147 @@ +// What a confidant does with the stage 1 it already signed when it enters stage 1 again in the +// same round and sub-round (cs::stageOneResend), and that the message it then sends again verifies +// at a receiver exactly as the first copy did (the check of Node::getStageOne). + +#include + +#include +#include +#include + +#include + +#include "gtest/gtest.h" + +namespace { + +cs::Hash hashOf(uint8_t b) { + cs::Hash h; + h.fill(b); + return h; +} + +cs::PublicKey keyOf(uint8_t b) { + cs::PublicKey k; + k.fill(b); + return k; +} + +cs::StageOne stageOne(uint64_t stamp) { + cs::StageOne st; + st.sender = 2; + st.hash = hashOf(7); + st.trustedCandidates = {keyOf(1), keyOf(2), keyOf(3)}; + st.roundTimeStamp = std::to_string(stamp); + return st; +} + +// the bytes a receiver checks the signature against: round, sub-round, hash of the message +cs::Bytes signedPart(cs::RoundNumber round, uint8_t subRound, const cs::Bytes& message) { + const cs::Hash messageHash = cscrypto::calculateHash(message.data(), message.size()); + cs::Bytes out; + cs::ODataStream stream(out); + stream << round; + stream << subRound; + stream << messageHash; + return out; +} + +constexpr uint64_t kLast = 1'700'000'000'000ULL; // last block's stamp + +} // namespace + +TEST(StageOneResend, ResendsWhenOnlyTheVotesDiffer) { + const cs::StageOne sent = stageOne(kLast + 500); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::Resend); +} + +TEST(StageOneResend, SignsAgainWhenTheCharacteristicHashChanged) { + const cs::StageOne sent = stageOne(kLast + 500); + EXPECT_EQ(cs::stageOneResend(sent, hashOf(8), kLast, kLast + 1500), cs::StageOneResend::HashChanged); +} + +TEST(StageOneResend, SignsAgainWhenTheStampIsNotAfterTheLastBlock) { + const cs::StageOne sent = stageOne(kLast + 500); + // the replacement package carried a later last block + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast + 500, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast + 900, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast + 499, kLast + 1500), cs::StageOneResend::Resend); +} + +TEST(StageOneResend, SignsAgainWhenTheStampIsUnreadable) { + cs::StageOne sent = stageOne(kLast + 500); + sent.roundTimeStamp = ""; + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); + sent.roundTimeStamp = "x"; + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); +} + +TEST(StageOneResend, SignsAgainWhenTheStampIsTooOld) { + const uint64_t stamp = kLast + 500; + const cs::StageOne sent = stageOne(stamp); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, stamp + Consensus::DefaultTimeStampRange), cs::StageOneResend::Resend); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, stamp + Consensus::DefaultTimeStampRange + 1), cs::StageOneResend::TooOld); + // a clock behind the stored stamp is not "too old" + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, stamp - 100), cs::StageOneResend::Resend); +} + +TEST(StageOneResend, HashCheckComesFirst) { + cs::StageOne sent = stageOne(kLast); // not after the last block either + EXPECT_EQ(cs::stageOneResend(sent, hashOf(9), kLast, kLast + 1), cs::StageOneResend::HashChanged); +} + +TEST(StageOneResend, ContentDiff) { + const cs::StageOne a = stageOne(kLast + 1); + cs::StageOne b = stageOne(kLast + 2); // only the stamp differs + EXPECT_EQ(cs::stageOneContentDiff(a, b), ""); + + // candidates compare as a set: the order is the arrival order + b.trustedCandidates = {keyOf(3), keyOf(1), keyOf(2)}; + EXPECT_EQ(cs::stageOneContentDiff(a, b), ""); + + b.trustedCandidates.push_back(keyOf(4)); + EXPECT_EQ(cs::stageOneContentDiff(a, b), "candidates"); + + b.hashesCandidates.push_back(cs::TransactionsPacketHash{}); + EXPECT_EQ(cs::stageOneContentDiff(a, b), "candidates,packets"); + + b.hash = hashOf(1); + EXPECT_EQ(cs::stageOneContentDiff(a, b), "hash,candidates,packets"); +} + +TEST(StageOneResend, ResentBytesVerifyAsTheFirstCopy) { + cs::PublicKey pub; + const auto priv = cscrypto::generateKeyPair(pub); + const cs::RoundNumber round = 1234; + const uint8_t subRound = 0; + + // signed as TrustedStage1State::finalizeStage() does + cs::StageOne first = stageOne(kLast + 500); + first.toBytes(); + first.messageHash = cscrypto::calculateHash(first.message.data(), first.message.size()); + const cs::Bytes part = signedPart(round, subRound, first.message); + first.signature = cscrypto::generateSignature(priv, part.data(), part.size()); + + // the re-entered stage 1 resends the stored message unchanged + ASSERT_EQ(cs::stageOneResend(first, first.hash, kLast, kLast + 1500), cs::StageOneResend::Resend); + const cs::StageOne resent = first; + EXPECT_EQ(resent.message, first.message); + EXPECT_EQ(resent.signature, first.signature); + + // a receiver in the same round and sub-round verifies it (Node::getStageOne) + const cs::Bytes atReceiver = signedPart(round, subRound, resent.message); + EXPECT_TRUE(cscrypto::verifySignature(resent.signature, pub, atReceiver.data(), atReceiver.size())); + // and refuses it in another round or sub-round, as for any stage 1 + const cs::Bytes otherRound = signedPart(round + 1, subRound, resent.message); + EXPECT_FALSE(cscrypto::verifySignature(resent.signature, pub, otherRound.data(), otherRound.size())); + const cs::Bytes otherSub = signedPart(round, subRound + 1, resent.message); + EXPECT_FALSE(cscrypto::verifySignature(resent.signature, pub, otherSub.data(), otherSub.size())); + + // the stamp decoded from the resent message is the first one, so a receiver that already + // holds the first copy ignores the resent one (SolverCore::gotStageOne keeps a later stamp only) + cs::StageOne decoded; + cs::IDataStream stream(resent.message.data(), resent.message.size()); + stream >> decoded.sender >> decoded.hash >> decoded.trustedCandidates >> decoded.hashesCandidates >> decoded.roundTimeStamp; + ASSERT_TRUE(stream.isValid()); + EXPECT_EQ(decoded.roundTimeStamp, first.roundTimeStamp); +} From 6532db028506851a2185cc6b97236286c6c74d17 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 9 Nov 2026 18:15:49 +0100 Subject: [PATCH 3/5] docs: the stage1 resent/signed again lines; CHANGES row for #178 Co-Authored-By: Claude Opus 5.5 --- docs/CHANGES.md | 4 ++- docs/consensus-diagnostics.md | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/docs/CHANGES.md b/docs/CHANGES.md index a8925e653..f68c6f4d6 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -42,7 +42,9 @@ lines under the new release. ## Not released yet -Nothing yet. +| PR | Change | +|---|---| +| #178 | solver: when a confidant enters stage 1 again in the same round after a fuller round package replaced the first one, it sends the stage 1 it already signed again instead of signing a second one with a new timestamp; it signs a new one when the characteristic hash changed, the stored timestamp is not after the last block's, or it is more than 30 s old; new info lines `stage1 resent unchanged` and `stage1 signed again ... reason`, and the package replacement is logged at info level (`docs/consensus-diagnostics.md`, section 5) | ## community.18 — 2026-11-09 diff --git a/docs/consensus-diagnostics.md b/docs/consensus-diagnostics.md index 4a485ef7a..4b65b092f 100644 --- a/docs/consensus-diagnostics.md +++ b/docs/consensus-diagnostics.md @@ -191,3 +191,58 @@ the larger group. - `the signatures of the block are insufficient or incorrect: # hash prev ` (warning, was debug): a received block failed the signature check and is dropped; its full hash and previous hash identify which block it was. + +## 5. Stage 1 entered again in the same round: `stage1 resent unchanged` / `stage1 signed again` + +A confidant can receive a second round package for the round it is in, with more real trusted +signatures than the first. It then replaces the first package and enters stage 1 again +(`SolverCore::nextRound(true)`): + +``` +info NODE> Current Roundpackage of 1205 will be replaced by new one +``` + +(this line was at debug level before). If this node already signed and sent its stage 1 for that +round and sub-round from the same seat, it now sends the same signed message again instead of +signing a second one with a new timestamp: + +``` +info TrustedStage1: stage1 resent unchanged: R-1205.0 T3 ts 1793783441147, a new one would differ in candidates,packets +``` + +`a new one would differ in` lists the fields a new stage 1 would have changed (`candidates`: the +trusted candidates as a set, `packets`: the packet hashes); it is absent when nothing differs. +Both are this node's votes for the next round, and the stored message the other confidants +already hold stays valid. Receivers check the resent message like any stage 1. A receiver that +already holds it ignores it, because its timestamp is the same. + +Two signed stage-1 messages from one seat with different timestamps can leave the confidants +holding different copies. They then compute different block timestamps, or see two +hash-signature pairs for that seat at stage 2. In both cases the seat, or the block, can fail the +stage-3 check. + +The node signs a new stage 1, as before this change, when the stored one would be refused or +would make the seat look faulty: + +``` +info TrustedStage1: stage1 signed again: R-1205.0 T3 ts 1793783441147 -> 1793783442010, content differs (hash), reason hash-changed +``` + +- `hash-changed`: the characteristic hash changed, because the new package changed the last + block. This hash must match the majority's. +- `not-after-last-block`: the stored timestamp is not after the last block's, so the receivers + would refuse it. +- `too-old`: the stored timestamp is more than `DefaultTimeStampRange` (30 s) old, so fresh + timestamps from the other confidants could fall outside this node's stage-2 window, which is + measured from its own timestamp. + +`content` compares the old and the new stage 1 apart from the timestamp: `same`, or `differs` +with the fields (`hash`, `candidates`, `packets`). The stored stage 1 is kept only while the node +is in that round and sub-round on the same seat. A new round, a new sub-round and every bootstrap +round start without it. + +The replacement itself also logs `fatal ConveyerBase> Meta round currently in conveyer`. This line +is older than this change and the official node logs it too, on every replacement of a round +package by one for the same round. The conveyer already holds a round table for that round, so it +keeps that table and does not store the second one. Despite its `fatal` level, the node keeps +running. From 3c8fa99239f6525d964c78e45fffc98a69181a91 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 9 Nov 2026 19:35:03 +0100 Subject: [PATCH 4/5] docs: consensus-diagnostics: the state name as logged (Trusted-1) Co-Authored-By: Claude Opus 5.5 --- docs/consensus-diagnostics.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/consensus-diagnostics.md b/docs/consensus-diagnostics.md index 4b65b092f..b45ec62f8 100644 --- a/docs/consensus-diagnostics.md +++ b/docs/consensus-diagnostics.md @@ -207,7 +207,7 @@ round and sub-round from the same seat, it now sends the same signed message aga signing a second one with a new timestamp: ``` -info TrustedStage1: stage1 resent unchanged: R-1205.0 T3 ts 1793783441147, a new one would differ in candidates,packets +info Trusted-1: stage1 resent unchanged: R-1205.0 T3 ts 1793783441147, a new one would differ in candidates,packets ``` `a new one would differ in` lists the fields a new stage 1 would have changed (`candidates`: the @@ -225,7 +225,7 @@ The node signs a new stage 1, as before this change, when the stored one would b would make the seat look faulty: ``` -info TrustedStage1: stage1 signed again: R-1205.0 T3 ts 1793783441147 -> 1793783442010, content differs (hash), reason hash-changed +info Trusted-1: stage1 signed again: R-1205.0 T3 ts 1793783441147 -> 1793783442010, content differs (hash), reason hash-changed ``` - `hash-changed`: the characteristic hash changed, because the new package changed the last From 8a83fd4541499feb220d04d0245bcde7c0ebeac7 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 9 Nov 2026 20:09:08 +0100 Subject: [PATCH 5/5] solver: review follow-ups for the stage-1 resend - the own signed stage 1 is stored before add_stage1(), which already runs the state's onStage1() handlers, so the store never depends on that order; - a stored stamp that cannot be parsed is its own reason, "unreadable", instead of "not-after-last-block"; unit tests updated, reason names tested; - docs: the two cases that still sign a second stage 1, as before (a writer-side replacement, which starts the round with updateRound = false, and a replacement inside a bootstrap round). Co-Authored-By: Claude Opus 5.5 --- docs/CHANGES.md | 2 +- docs/consensus-diagnostics.md | 10 ++++++++++ solver/include/solver/stage.hpp | 6 ++++-- solver/src/stage.cpp | 4 +++- solver/src/states/trustedstage1state.cpp | 3 ++- tests/csnode/stageoneresend_tests.cpp | 12 ++++++++++-- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/CHANGES.md b/docs/CHANGES.md index f68c6f4d6..f8d836f33 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -44,7 +44,7 @@ lines under the new release. | PR | Change | |---|---| -| #178 | solver: when a confidant enters stage 1 again in the same round after a fuller round package replaced the first one, it sends the stage 1 it already signed again instead of signing a second one with a new timestamp; it signs a new one when the characteristic hash changed, the stored timestamp is not after the last block's, or it is more than 30 s old; new info lines `stage1 resent unchanged` and `stage1 signed again ... reason`, and the package replacement is logged at info level (`docs/consensus-diagnostics.md`, section 5) | +| #178 | solver: when a confidant enters stage 1 again in the same round after a fuller round package replaced the first one, it sends the stage 1 it already signed again instead of signing a second one with a new timestamp; it signs a new one when the characteristic hash changed, the stored timestamp is not after the last block's, is more than 30 s old or cannot be read; a writer-side replacement and a replacement inside a bootstrap round still sign a second stage 1, as before; new info lines `stage1 resent unchanged` and `stage1 signed again ... reason`, and the package replacement is logged at info level (`docs/consensus-diagnostics.md`, section 5) | ## community.18 — 2026-11-09 diff --git a/docs/consensus-diagnostics.md b/docs/consensus-diagnostics.md index b45ec62f8..41af292d1 100644 --- a/docs/consensus-diagnostics.md +++ b/docs/consensus-diagnostics.md @@ -235,6 +235,16 @@ info Trusted-1: stage1 signed again: R-1205.0 T3 ts 1793783441147 -> 17937834420 - `too-old`: the stored timestamp is more than `DefaultTimeStampRange` (30 s) old, so fresh timestamps from the other confidants could fall outside this node's stage-2 window, which is measured from its own timestamp. +- `unreadable`: the stored timestamp cannot be read as a number. + +Two cases still sign a second stage 1 for the same round, as before this change, and log no +`stage1` line: + +- a fuller package that reaches the writer of the round package (the `moreTrusted` branch of + `Node::getRoundTable` for a node that holds its own package): that path starts the round + again with `updateRound = false`, which clears the stored stage 1; +- a fuller package that replaces the first one inside a bootstrap round: the start of a bootstrap + round clears the stored stage 1. `content` compares the old and the new stage 1 apart from the timestamp: `same`, or `differs` with the fields (`hash`, `candidates`, `packets`). The stored stage 1 is kept only while the node diff --git a/solver/include/solver/stage.hpp b/solver/include/solver/stage.hpp index fa5e0f63f..ad75bf235 100644 --- a/solver/include/solver/stage.hpp +++ b/solver/include/solver/stage.hpp @@ -169,12 +169,14 @@ struct StageHash { // would make this seat look faulty: HashChanged (the characteristic hash must match the // majority's), NotAfterLastBlock (the stamp must be after the last block's), TooOld (the stored // stamp is more than Consensus::DefaultTimeStampRange old, so fresh stamps of the others could -// fall outside this node's own stage-2 window, which is measured from its own stamp). +// fall outside this node's own stage-2 window, which is measured from its own stamp), +// Unreadable (the stored stamp does not parse as a number). enum class StageOneResend : uint8_t { Resend, HashChanged, NotAfterLastBlock, - TooOld + TooOld, + Unreadable }; // sent: the stored signed stage 1; freshHash: the characteristic hash computed now; diff --git a/solver/src/stage.cpp b/solver/src/stage.cpp index 679d81024..1ed25659e 100644 --- a/solver/src/stage.cpp +++ b/solver/src/stage.cpp @@ -253,7 +253,7 @@ namespace cs { sentStamp = std::stoull(sent.roundTimeStamp); } catch (...) { - return StageOneResend::NotAfterLastBlock; + return StageOneResend::Unreadable; } if (sentStamp <= lastBlockStamp) { return StageOneResend::NotAfterLastBlock; @@ -274,6 +274,8 @@ namespace cs { return "not-after-last-block"; case StageOneResend::TooOld: return "too-old"; + case StageOneResend::Unreadable: + return "unreadable"; } return "?"; } diff --git a/solver/src/states/trustedstage1state.cpp b/solver/src/states/trustedstage1state.cpp index e808d0aaa..c30a7a6bf 100644 --- a/solver/src/states/trustedstage1state.cpp +++ b/solver/src/states/trustedstage1state.cpp @@ -139,6 +139,8 @@ void TrustedStage1State::off(SolverContext& context) { } } finalizeStage(context); + // stored before add_stage1(): that call already runs the state's onStage1() handlers + context.remember_sent_stage1(stage); if (sent != nullptr) { // the receivers may now hold two signed stage-1 messages of this seat cslog() << name() << ": stage1 signed again: R-" << cs::Conveyer::instance().currentRoundNumber() << "." @@ -147,7 +149,6 @@ void TrustedStage1State::off(SolverContext& context) { << ", reason " << cs::toString(decision); } context.add_stage1(stage, true); - context.remember_sent_stage1(stage); } } diff --git a/tests/csnode/stageoneresend_tests.cpp b/tests/csnode/stageoneresend_tests.cpp index 747c205ca..ac9bb3369 100644 --- a/tests/csnode/stageoneresend_tests.cpp +++ b/tests/csnode/stageoneresend_tests.cpp @@ -71,9 +71,9 @@ TEST(StageOneResend, SignsAgainWhenTheStampIsNotAfterTheLastBlock) { TEST(StageOneResend, SignsAgainWhenTheStampIsUnreadable) { cs::StageOne sent = stageOne(kLast + 500); sent.roundTimeStamp = ""; - EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::Unreadable); sent.roundTimeStamp = "x"; - EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::NotAfterLastBlock); + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::Unreadable); } TEST(StageOneResend, SignsAgainWhenTheStampIsTooOld) { @@ -109,6 +109,14 @@ TEST(StageOneResend, ContentDiff) { EXPECT_EQ(cs::stageOneContentDiff(a, b), "hash,candidates,packets"); } +TEST(StageOneResend, ReasonNames) { + EXPECT_STREQ(cs::toString(cs::StageOneResend::Resend), "resend"); + EXPECT_STREQ(cs::toString(cs::StageOneResend::HashChanged), "hash-changed"); + EXPECT_STREQ(cs::toString(cs::StageOneResend::NotAfterLastBlock), "not-after-last-block"); + EXPECT_STREQ(cs::toString(cs::StageOneResend::TooOld), "too-old"); + EXPECT_STREQ(cs::toString(cs::StageOneResend::Unreadable), "unreadable"); +} + TEST(StageOneResend, ResentBytesVerifyAsTheFirstCopy) { cs::PublicKey pub; const auto priv = cscrypto::generateKeyPair(pub);