diff --git a/csnode/include/csnode/bootstrapweight.hpp b/csnode/include/csnode/bootstrapweight.hpp new file mode 100644 index 000000000..b7674c275 --- /dev/null +++ b/csnode/include/csnode/bootstrapweight.hpp @@ -0,0 +1,54 @@ +#ifndef BOOTSTRAPWEIGHT_HPP +#define BOOTSTRAPWEIGHT_HPP + +#include +#include +#include + +#include + +namespace cs { + +// The weight a node gives a bootstrap round table: it takes a table only if it weighs more than the +// one it took last. A table signed with the blockchain key beats every unsigned one. +using BootstrapWeight = uint32_t; +constexpr BootstrapWeight kSignedBootstrapWeight = std::numeric_limits::max(); +// the largest table the official node weighs (in a uint8_t); it drops larger ones +constexpr size_t kOfficialBootstrapTableLimit = 8; +constexpr size_t kMaxBootstrapTable = 25; // Consensus::MaxTrustedNodes + +// Up to 8 keys, exactly the official node's value on x86-64, so that both pick the same table when a +// stalled network bootstraps again: the table is matched against the initial confidants in their +// (sorted) order, and the confidant with index k adds 2^(n-1-k) for a table of n keys. In the official +// code an index k >= n shifts by a wrapped size_t, which x86 masks to 31..8 and the uint8_t sum then +// truncates to 0: it adds nothing, as here. From 9 to 25 keys, where the official node returns 0 and +// drops the table, 2^n on top, so every larger table beats every smaller one, but only for a table +// the starter builds: initial confidants in their order, each once. Any other table of 9 or more keys +// (repeated or unordered keys, which an unsigned table could otherwise use to outweigh an honest one) +// weighs 0 and is dropped, as by the official node. +inline BootstrapWeight bootstrapWeight(const std::set& initial, const PublicKeys& table) { + const size_t n = table.size(); + if (n == 0 || n > kMaxBootstrapTable) { + return 0; + } + BootstrapWeight weight = n > kOfficialBootstrapTableLimit ? (BootstrapWeight(1) << n) : 0; + size_t matched = 0; + size_t index = 0; + for (const auto& confidant : initial) { + if (matched < n && table[matched] == confidant) { + if (index < n) { + weight += BootstrapWeight(1) << (n - 1 - index); + } + ++matched; + } + ++index; + } + if (n > kOfficialBootstrapTableLimit && matched != n) { + return 0; + } + return weight; +} + +} // namespace cs + +#endif // BOOTSTRAPWEIGHT_HPP diff --git a/csnode/include/csnode/node.hpp b/csnode/include/csnode/node.hpp index 95d24d17d..1c95b20c6 100644 --- a/csnode/include/csnode/node.hpp +++ b/csnode/include/csnode/node.hpp @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -192,7 +193,7 @@ class Node { void accountInitiationRequest(uint64_t& aTime, cs::PublicKey key); void cleanConfirmationList(cs::RoundNumber rNum); - uint8_t calculateBootStrapWeight(cs::PublicKeys& confidants); + cs::BootstrapWeight calculateBootStrapWeight(const cs::PublicKeys& confidants) const; // state syncro functions void sendStateRequest(const csdb::Address& contract_abs_addr, const cs::PublicKeys& confidants); @@ -553,7 +554,7 @@ public slots: std::map changeableParams_; cs::PublicKey globalPublicKey_; cs::NodeVersionChange nVersionChange_; - uint8_t bootStrapWeight_ = 0U; + cs::BootstrapWeight bootStrapWeight_ = 0U; std::set initialConfidants_; bool isBootstrapRound_ = false; diff --git a/csnode/src/node.cpp b/csnode/src/node.cpp index 4e5469175..26433e03b 100644 --- a/csnode/src/node.cpp +++ b/csnode/src/node.cpp @@ -507,23 +507,12 @@ void Node::getUtilityMessage(const uint8_t* data, const size_t size) { } } -uint8_t Node::calculateBootStrapWeight(cs::PublicKeys& confidants) { - size_t confSize = confidants.size(); - uint8_t currentWeight = 0U; - if (confSize > 8) { - return currentWeight; - } - //auto it = initialConfidants_.cbegin(); - auto itConfidants = confidants.cbegin(); - --confSize; - for (auto it : initialConfidants_) { - if (std::equal(it.cbegin(), it.cend(), itConfidants->cbegin())) { - currentWeight += 1 << confSize; - ++itConfidants; - } - --confSize; - } - return currentWeight; +static_assert(cs::kMaxBootstrapTable == Consensus::MaxTrustedNodes, "a bootstrap table holds at most MaxTrustedNodes keys"); + +// the official node weighs tables of up to 8 keys only and silently drops larger ones, so a network +// of 9 or more initial confidants never bootstraps: see cs::bootstrapWeight +cs::BootstrapWeight Node::calculateBootStrapWeight(const cs::PublicKeys& confidants) const { + return cs::bootstrapWeight(initialConfidants_, confidants); } @@ -558,7 +547,7 @@ void Node::getBootstrapTable(const uint8_t* data, const size_t size, const cs::R } } - uint8_t currentWeight = calculateBootStrapWeight(confidants); + cs::BootstrapWeight currentWeight = calculateBootStrapWeight(confidants); if (unknown) { cs::Signature sig; @@ -578,9 +567,11 @@ void Node::getBootstrapTable(const uint8_t* data, const size_t size, const cs::R cswarning() << kLogPrefix_ << "failed to test bootstrap signature, drop"; return; } - currentWeight = 255U; + currentWeight = cs::kSignedBootstrapWeight; } if (currentWeight <= bootStrapWeight_) { + cslog() << kLogPrefix_ << "bootstrap table #" << rNum << " with " << confidants.size() << " confidants weighs " + << currentWeight << ", not above " << bootStrapWeight_ << ", drop"; return; } @@ -592,7 +583,7 @@ void Node::getBootstrapTable(const uint8_t* data, const size_t size, const cs::R if (!isBootstrapRound_) { isBootstrapRound_ = true; - cslog() << "NODE> Bootstrap on"; + cslog() << "NODE> Bootstrap on, table of " << confidants.size() << " confidants, weight " << currentWeight; } roundTable.confidants = std::move(confidants); @@ -4642,7 +4633,7 @@ void Node::onRoundTimeElapsed() { for (auto it : actualConfidants) { confs.push_back(it); } - uint8_t currentWeight = calculateBootStrapWeight(confs); + cs::BootstrapWeight currentWeight = calculateBootStrapWeight(confs); if (currentWeight > bootStrapWeight_) { bootStrapWeight_ = currentWeight; } @@ -4708,7 +4699,7 @@ bool Node::bootstrap(const cs::Bytes& bytes, cs::RoundNumber round) { auto& conveyer = cs::Conveyer::instance(); conveyer.updateRoundTable(roundPackageCache_.back().roundTable().round, roundPackageCache_.back().roundTable()); - uint8_t currentWeight = calculateBootStrapWeight(rt.confidants); + cs::BootstrapWeight currentWeight = calculateBootStrapWeight(rt.confidants); if (currentWeight > bootStrapWeight_) { bootStrapWeight_ = currentWeight; } diff --git a/docs/CHANGES.md b/docs/CHANGES.md index a508fa165..4b5513943 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -35,6 +35,7 @@ lines under the new release. | PR | Change | |---|---| | #67 | csdb, ci, docs: conversion follow-ups from the review of #65 (contract-records message, keep a copy of `db/` before converting from berkeleydb, sturdier db-convert scenario) | +| #70 | csnode: bootstrap tables of 9 to 25 confidants are accepted (the official node silently drops them, so a network of 9 or more initial confidants never starts); the same choice as the official node up to 8 | ## community.9 — 2026-10-22 diff --git a/docs/differences.md b/docs/differences.md index dc240fb08..c4589a3f4 100644 --- a/docs/differences.md +++ b/docs/differences.md @@ -66,6 +66,12 @@ quick-starts after a conversion. start; - it rebuilds the block hash databases (`blockHashes: rebuilding from the stored blocks ...`) when they fail their check. +- **Bootstrap with more than 8 confidants.** The official node gives a bootstrap round table a + weight that fits in 8 bits and silently drops tables of 9 or more confidants, so a network whose + initial confidants (or the bootstrap nodes set by special order 2) are 9 or more never starts or + restarts. The community node accepts tables of up to 25 confidants (`MaxTrustedNodes`). Up to 8 it + gives exactly the official weights, so both pick the same table when a stalled network starts + again; a dropped table is now logged. - **State kept across a quick start.** Pending consensus-settings changes, special orders and deployed smart contracts survive a quick start. The official node can lose them, or apply them differently, after a quick start. diff --git a/tests/csnode/bootstrapweight_tests.cpp b/tests/csnode/bootstrapweight_tests.cpp new file mode 100644 index 000000000..483021879 --- /dev/null +++ b/tests/csnode/bootstrapweight_tests.cpp @@ -0,0 +1,186 @@ +#include "gtest/gtest.h" + +#include +#include +#include + +#include + +using cs::BootstrapWeight; +using cs::bootstrapWeight; + +namespace { +// keys whose set order is their index +cs::PublicKey key(int i) { + cs::PublicKey k{}; + k[0] = static_cast(i); + k[31] = static_cast(0xA0 + i); + return k; +} + +std::set initial(int count) { + std::set s; + for (int i = 0; i < count; ++i) { + s.insert(key(i)); + } + return s; +} + +cs::PublicKeys table(std::initializer_list indices) { + cs::PublicKeys t; + for (int i : indices) { + t.push_back(key(i)); + } + return t; +} + +cs::PublicKeys range(int from, int to) { + cs::PublicKeys t; + for (int i = from; i <= to; ++i) { + t.push_back(key(i)); + } + return t; +} + +// the official Node::calculateBootStrapWeight as it behaves on x86-64: a shift count wrapped below 0 +// is masked to 5 bits, and the uint8_t sum keeps the low byte, so those terms add nothing. The past-the- +// end read of the official loop is replaced by a bounds check (it can only add such a term) +uint8_t official(const std::set& init, const cs::PublicKeys& t) { + size_t confSize = t.size(); + uint8_t weight = 0; + if (confSize > 8) { + return weight; + } + auto it = t.cbegin(); + --confSize; + for (const auto& c : init) { + if (it != t.cend() && std::equal(c.cbegin(), c.cend(), it->cbegin())) { + weight = static_cast(weight + (uint32_t(1) << (confSize & 31))); + ++it; + } + --confSize; + } + return weight; +} +} // namespace + +// the values the official node gives: a community node must choose the same table +TEST(BootstrapWeight, OfficialValuesUpToEightConfidants) { + struct Case { + int init; + cs::PublicKeys table; + BootstrapWeight weight; + }; + const Case cases[] = { + {4, range(0, 3), 15}, + {5, range(0, 4), 31}, + {5, range(0, 3), 15}, + {5, range(1, 4), 7}, + {6, range(0, 5), 63}, + {6, range(0, 4), 31}, + {6, range(1, 5), 15}, + {6, range(0, 3), 15}, + {6, range(2, 5), 3}, + {7, range(0, 6), 127}, + {7, range(0, 5), 63}, + {7, range(1, 6), 31}, + {7, range(0, 4), 31}, + {7, range(2, 6), 7}, + {7, table({0, 2, 4, 6}), 10}, + {7, table({1, 3, 5}), 2}, + {7, range(3, 6), 1}, + {7, range(0, 3), 15}, + {7, table({0, 1, 2, 5, 6}), 28}, + {7, table({1, 0, 2, 3, 4}), 8}, + {7, table({6, 5, 4, 3, 2}), 0}, + {8, range(0, 7), 255}, + {8, range(0, 6), 127}, + {8, range(1, 7), 63}, + {8, range(4, 7), 0}, + {8, range(0, 4), 31}, + {8, range(3, 7), 3}, + {9, range(0, 7), 255}, + {9, range(1, 8), 127}, + {12, range(0, 3), 15}, + {12, range(8, 11), 0}, + }; + for (const auto& c : cases) { + EXPECT_EQ(bootstrapWeight(initial(c.init), c.table), c.weight) << c.init << " initial, " << c.table.size() << " keys"; + } +} + +// every table of up to 8 keys taken from up to 12 initial confidants, in order and reversed +TEST(BootstrapWeight, SameAsTheOfficialNodeForEveryTableUpToEightKeys) { + for (int m = 1; m <= 12; ++m) { + const auto init = initial(m); + for (uint32_t mask = 1; mask < (1u << m); ++mask) { + cs::PublicKeys t; + for (int i = 0; i < m; ++i) { + if (mask >> i & 1) { + t.push_back(key(i)); + } + } + if (t.size() > 8) { + continue; + } + ASSERT_EQ(bootstrapWeight(init, t), official(init, t)) << m << " initial, mask " << mask; + std::reverse(t.begin(), t.end()); + ASSERT_EQ(bootstrapWeight(init, t), official(init, t)) << m << " initial, reversed mask " << mask; + } + } +} + +// a stall recovery on mainnet (7 initial confidants) with only some of them ready +TEST(BootstrapWeight, PartialTablesOfSeven) { + const auto init = initial(7); + EXPECT_EQ(bootstrapWeight(init, range(0, 4)), 31u); + EXPECT_EQ(bootstrapWeight(init, range(2, 6)), 7u); + EXPECT_EQ(bootstrapWeight(init, range(0, 5)), 63u); + EXPECT_EQ(bootstrapWeight(init, range(1, 6)), 31u); +} + +// the official node returns 0 from 9 keys on and drops the table: 9 initial confidants never bootstrap +TEST(BootstrapWeight, LargerTablesArePositiveAndGrowWithTheirSize) { + BootstrapWeight previous = bootstrapWeight(initial(8), range(0, 7)); + for (int m = 9; m <= 25; ++m) { + const auto full = bootstrapWeight(initial(m), range(0, m - 1)); + EXPECT_EQ(full, (BootstrapWeight(1) << (m + 1)) - 1) << m; + EXPECT_GT(full, previous) << m; + previous = full; + // the lightest table of 9 keys still beats every table of 8 + EXPECT_GT(bootstrapWeight(initial(m), range(m - 9, m - 1)), 255u) << m; + } + EXPECT_GT(bootstrapWeight(initial(9), range(0, 8)), bootstrapWeight(initial(9), range(0, 7))); +} + +// an unsigned table of 9 or more known keys counts only as the starter builds it: initial confidants +// in their order, each once; otherwise it would outweigh an honest table of up to 8 +TEST(BootstrapWeight, LargerTablesOnlyAsTheStarterBuildsThem) { + cs::PublicKeys repeated(9, key(0)); + EXPECT_EQ(bootstrapWeight(initial(7), repeated), 0u); + EXPECT_EQ(bootstrapWeight(initial(25), repeated), 0u); + auto padded = range(0, 6); + padded.push_back(key(6)); + padded.push_back(key(6)); + EXPECT_EQ(bootstrapWeight(initial(7), padded), 0u); + auto unordered = range(0, 8); + std::swap(unordered[0], unordered[8]); + EXPECT_EQ(bootstrapWeight(initial(9), unordered), 0u); + auto foreign = range(0, 8); + foreign[4] = key(200); + EXPECT_EQ(bootstrapWeight(initial(9), foreign), 0u); + EXPECT_GT(bootstrapWeight(initial(9), range(0, 8)), 255u); +} + +TEST(BootstrapWeight, SignedBeatsEverything) { + EXPECT_GT(cs::kSignedBootstrapWeight, bootstrapWeight(initial(25), range(0, 24))); +} + +TEST(BootstrapWeight, EdgeCases) { + EXPECT_EQ(bootstrapWeight(initial(7), cs::PublicKeys{}), 0u); + EXPECT_EQ(bootstrapWeight(initial(26), range(0, 25)), 0u); // more than MaxTrustedNodes + // a key that is not an initial confidant is skipped, as by the official node + auto t = range(0, 3); + t.insert(t.begin() + 2, key(200)); + EXPECT_EQ(bootstrapWeight(initial(7), t), official(initial(7), t)); +}