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/docs/CHANGES.md b/docs/CHANGES.md index a8925e653..f8d836f33 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, 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 4a485ef7a..41af292d1 100644 --- a/docs/consensus-diagnostics.md +++ b/docs/consensus-diagnostics.md @@ -191,3 +191,68 @@ 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 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 +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 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 + 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. +- `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 +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. 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..b5b94efc4 100644 --- a/solver/include/solver/solvercore.hpp +++ b/solver/include/solver/solvercore.hpp @@ -378,6 +378,18 @@ 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; 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; + cs::PublicKey key{}; + cs::StageOne stage; + }; + std::optional sentStageOne_; std::unique_ptr pVal_; std::string kLogPrefix_; }; diff --git a/solver/include/solver/stage.hpp b/solver/include/solver/stage.hpp index 7628bf8ec..ad75bf235 100644 --- a/solver/include/solver/stage.hpp +++ b/solver/include/solver/stage.hpp @@ -160,6 +160,35 @@ 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), +// Unreadable (the stored stamp does not parse as a number). +enum class StageOneResend : uint8_t { + Resend, + HashChanged, + NotAfterLastBlock, + TooOld, + Unreadable +}; + +// 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/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/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..1ed25659e 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,65 @@ 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::Unreadable; + } + 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"; + case StageOneResend::Unreadable: + return "unreadable"; + } + 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 145db2cab..c30a7a6bf 100644 --- a/solver/src/states/trustedstage1state.cpp +++ b/solver/src/states/trustedstage1state.cpp @@ -15,6 +15,7 @@ #include #include +#include #include namespace cs { @@ -99,7 +100,54 @@ 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 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 = 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); + // 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() << "." + << static_cast(context.subRound()) << " T" << static_cast(stage.sender) << " ts " << sentStamp + << " -> " << stage.roundTimeStamp << ", content " << (differs.empty() ? "same" : "differs (" + differs + ")") + << ", reason " << cs::toString(decision); + } context.add_stage1(stage, true); } } diff --git a/tests/csnode/stageoneresend_tests.cpp b/tests/csnode/stageoneresend_tests.cpp new file mode 100644 index 000000000..ac9bb3369 --- /dev/null +++ b/tests/csnode/stageoneresend_tests.cpp @@ -0,0 +1,155 @@ +// 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::Unreadable); + sent.roundTimeStamp = "x"; + EXPECT_EQ(cs::stageOneResend(sent, sent.hash, kLast, kLast + 1500), cs::StageOneResend::Unreadable); +} + +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, 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); + 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); +}