diff --git a/.github/workflows/robustness.yml b/.github/workflows/robustness.yml index a71c11778..4fb0d3b59 100644 --- a/.github/workflows/robustness.yml +++ b/.github/workflows/robustness.yml @@ -246,6 +246,9 @@ jobs: # slow nodes: 5% of one CPU for two trusted nodes - name: slow-nodes env: SLOW_NODES=3,6 SLOW_CPU=5 + # snapshot sharing announced in host_data: n3 and n5 serve, a stopped node lists them + - name: snapshot-capability + env: SNAPSHOT_SERVE_NODES=3,5 # random datagrams to every node's p2p port - name: garbage-datagrams env: GARBAGE_AT=150 GARBAGE_RATE=300 diff --git a/ci/testnet/run.sh b/ci/testnet/run.sh index a0a00bb1f..e09468b90 100755 --- a/ci/testnet/run.sh +++ b/ci/testnet/run.sh @@ -67,6 +67,9 @@ # snapshot with --import-snapshot into its own backend and joins; with SNAPSHOT_CORRUPT=1 the # blocks file is truncated first, so the restore must fail and that node syncs from block 0. # SNAPSHOT_FORMAT=1: packaging/docker/snapshot-make.sh and snapshot-restore.sh instead +# SNAPSHOT_SERVE_NODES (optional, comma separated): those nodes run with [snapshot] serve=on; +# at the end one other node of this build is stopped and its known_peers.json must list each +# of them with the snapshot capability, and no other node with it # STORAGE_SWITCH_AT (default 0 = off, needs PRUNE_NODE=6 and DB_BACKEND=lmdb): at that sequence # node 5 is restarted as a pruned node and node 6 as a full one # CONVERT_AT (default 0 = off, needs DB_BACKEND=berkeleydb): at that sequence n4 is converted to @@ -199,6 +202,7 @@ JOIN_PRUNED_AT=${JOIN_PRUNED_AT:-0} SNAPSHOT_AT=${SNAPSHOT_AT:-0} SNAPSHOT_FORMAT=${SNAPSHOT_FORMAT:-2} SNAPSHOT_CORRUPT=${SNAPSHOT_CORRUPT:-0} +SNAPSHOT_SERVE_NODES=${SNAPSHOT_SERVE_NODES:-} STORAGE_SWITCH_AT=${STORAGE_SWITCH_AT:-0} CONVERT_AT=${CONVERT_AT:-0} INDEX_LOSS_AT=${INDEX_LOSS_AT:-0} @@ -384,6 +388,7 @@ $(api_settings "$i") ajax_port=0 diag_port=0 $(storage_section "$i") +$([[ ",$SNAPSHOT_SERVE_NODES," == *",$i,"* ]] && printf "\n[snapshot]\nserve=on\n") [Core] Filter="%Severity% >= info" @@ -1310,4 +1315,33 @@ if [ "$LOW_STAKE_NODES" -gt 0 ]; then [ "$low_trusted" -eq 0 ] || { echo "FAIL: nodes below the minimum stake were elected trusted"; exit 1; } echo "stake: none of the $LOW_STAKE_NODES nodes below the minimum stake was trusted after round $from" fi +if [ -n "$SNAPSHOT_SERVE_NODES" ]; then + # known_peers.json is written at a clean stop: stop one node of this build that does not serve + observer=0 + for i in $(seq 1 "$NODES"); do + [[ ",$SNAPSHOT_SERVE_NODES," == *",$i,"* ]] && continue + [ -n "$BIN_ALT" ] && [ $((i % 2)) -eq 0 ] && continue + observer=$i; break + done + [ "$observer" -gt 0 ] || { echo "FAIL: no node left to read the capabilities from"; exit 1; } + kill -TERM "${PIDS[$observer]}" 2>/dev/null || true + for _ in $(seq 1 60); do kill -0 "${PIDS[$observer]}" 2>/dev/null || break; sleep 1; done + kill -0 "${PIDS[$observer]}" 2>/dev/null && { echo "FAIL: n$observer did not stop in 60 s"; exit 1; } + wait "${PIDS[$observer]}" 2>/dev/null || true + [ -f "$WORK/n$observer/known_peers.json" ] || { echo "FAIL: n$observer wrote no known_peers.json"; exit 1; } + serving="" + for i in ${SNAPSHOT_SERVE_NODES//,/ }; do serving="$serving ${KEYS[$((i - 1))]}"; done + python3 - "$WORK/n$observer/known_peers.json" $serving <<'PY' || { echo "FAIL: snapshot capability not announced as expected"; exit 1; } +import json, sys +peers = {p["id"]: p for p in json.load(open(sys.argv[1]))} +serving = set(sys.argv[2:]) +bad = [k for k in serving if peers.get(k, {}).get("capabilities") != 1] +extra = [k for k, p in peers.items() if k not in serving and p["capabilities"] != 0] +community = sum(1 for p in peers.values() if p["minor"] > 0) +print(f"capabilities: {len(peers)} known peers, {len(serving) - len(bad)} of {len(serving)} serving nodes announce snapshots, " + f"{len(extra)} others do, {community} announce a community version") +sys.exit(1 if bad or extra else 0) +PY + PIDS[$observer]="" +fi echo "PASS: $ALL_NODES nodes reached sequence $TARGET_SEQ with matching state digests" diff --git a/client/config/config.cpp b/client/config/config.cpp index c9fffbcf8..d753d8a14 100644 --- a/client/config/config.cpp +++ b/client/config/config.cpp @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include #include @@ -30,7 +32,8 @@ #endif const NodeVersion NODE_VERSION = 532; -const uint8_t MINOR_NODE_VERSION = 0; +// the community release (4.2.532-community.N); also announced to peers in host_data bits 32-47 +const uint8_t MINOR_NODE_VERSION = 13; const std::string BLOCK_NAME_PARAMS = "params"; const std::string BLOCK_NAME_HOST_INPUT = "host_input"; @@ -233,6 +236,27 @@ void Config::updateKnownHosts(std::vector& peers) const { for (auto& p : peers) { f << p.ip << ":" << p.port << " " << p.id << std::endl; } + + // the same peers with what they announce, next to known_hosts.txt + const auto json = boost::filesystem::path(hostsFileName_).parent_path() / "known_peers.json"; + std::ofstream j(json.string()); + if (j.is_open()) { + j << knownPeersJson(peers); + } +} + +std::string Config::knownPeersJson(const std::vector& peers) { + // ids are base58 and addresses are printed by asio: nothing to escape + std::ostringstream out; + out << "["; + for (size_t i = 0; i < peers.size(); ++i) { + const auto& p = peers[i]; + out << (i ? ",\n " : "\n ") << "{\"id\":\"" << p.id << "\",\"ip\":\"" << p.ip << "\",\"port\":" << p.port + << ",\"version\":" << p.version << ",\"minor\":" << p.minorVersion << ",\"platform\":" << int(p.platform) + << ",\"capabilities\":" << int(p.capabilities) << "}"; + } + out << (peers.empty() ? "]\n" : "\n]\n"); + return out.str(); } Config::Config(const ConveyerData& conveyerData) diff --git a/client/config/config.hpp b/client/config/config.hpp index 470434698..a55896fe4 100644 --- a/client/config/config.hpp +++ b/client/config/config.hpp @@ -355,6 +355,7 @@ class Config { void swap(Config& config); void updateKnownHosts(std::vector&) const; + static std::string knownPeersJson(const std::vector& peers); const EventsReportData& getEventsReportData() const { return eventsReport_; diff --git a/lib/include/lib/system/common.hpp b/lib/include/lib/system/common.hpp index 32251fd85..eacebe5d3 100644 --- a/lib/include/lib/system/common.hpp +++ b/lib/include/lib/system/common.hpp @@ -56,6 +56,9 @@ struct PeerData { uint16_t port = 0; int16_t version = 0; int8_t platform = OS_UNSPECIFIED; + // community nodes only (host_data bits 24-47), 0 on official nodes + uint8_t capabilities = 0; + uint16_t minorVersion = 0; }; using ConfidantSignature = std::pair; diff --git a/net/include/net/hostdata.hpp b/net/include/net/hostdata.hpp new file mode 100644 index 000000000..c37af77b3 --- /dev/null +++ b/net/include/net/hostdata.hpp @@ -0,0 +1,35 @@ +#ifndef NET_HOSTDATA_HPP +#define NET_HOSTDATA_HPP + +#include + +// The 64-bit host_data a node announces in its p2p datagrams. Bits 0-23 are the official layout +// (platform, node version); bits 24-31 are capability flags and bits 32-47 the minor (community) +// version, both 0 on official nodes; 48-63 stay 0. +namespace cs::hostdata { + +constexpr uint8_t kSnapshotProtocolV1 = 1; // bit 24: answers snapshot requests (MsgTypes 40-43) + +inline uint64_t encode(uint8_t platform, uint16_t version, uint8_t capabilities, uint16_t minorVersion) { + return uint64_t(platform) | (uint64_t(version) << 8) | (uint64_t(capabilities) << 24) | (uint64_t(minorVersion) << 32); +} + +inline uint8_t platform(uint64_t data) { + return static_cast(data); +} + +inline uint16_t version(uint64_t data) { + return static_cast(data >> 8); +} + +inline uint8_t capabilities(uint64_t data) { + return static_cast(data >> 24); +} + +inline uint16_t minorVersion(uint64_t data) { + return static_cast(data >> 32); +} + +} // namespace cs::hostdata + +#endif // NET_HOSTDATA_HPP diff --git a/net/include/net/transport.hpp b/net/include/net/transport.hpp index 262b41239..a29c6a699 100644 --- a/net/include/net/transport.hpp +++ b/net/include/net/transport.hpp @@ -73,6 +73,8 @@ class Transport : public net::HostEventHandler { void addToNeighbours(const std::set&); void getKnownPeers(std::vector&); + // the capability byte a peer of the routing table announces (cs::hostdata); false if unknown + bool peerCapabilities(const cs::PublicKey& key, uint8_t& capabilities); // from neigbours // @param added - true if new neighbour adder, false if removed diff --git a/net/src/transport.cpp b/net/src/transport.cpp index 793fba813..4305892aa 100644 --- a/net/src/transport.cpp +++ b/net/src/transport.cpp @@ -15,6 +15,8 @@ #include #include +#include + #include namespace { @@ -33,21 +35,13 @@ net::NodeId toNodeId(const cs::PublicKey& key) { } uint64_t getHostData() { + const auto& config = *cs::ConfigHolder::instance().config(); auto platform = static_cast(csconnector::connector::platform()); - auto node_version = static_cast(cs::ConfigHolder::instance().config()->getNodeVersion()); - - uint64_t user_data = 0; - user_data |= platform; - user_data |= (uint64_t(node_version) << 8); - return user_data; -} - -uint8_t getPlatform(uint64_t user_data) { - return static_cast(user_data); -} - -uint16_t getVersion(uint64_t user_data) { - return static_cast(user_data >> 8); + auto node_version = static_cast(config.getNodeVersion()); + // fixed for the life of the process: a node started with serve=off is found at its next start + uint8_t capabilities = config.getSnapshotSettings().serve ? cs::hostdata::kSnapshotProtocolV1 : 0; + auto minor_version = static_cast(config.getMinorNodeVersion()); + return cs::hostdata::encode(platform, node_version, capabilities, minor_version); } net::Config createNetConfig(bool& good) { @@ -448,8 +442,10 @@ void Transport::getKnownPeers(std::vector& result) { peerData.ip = p.address.to_string(); peerData.port = p.udp_port; if (p.user_data) { - peerData.version = static_cast(getVersion(p.user_data)); - peerData.platform = static_cast(getPlatform(p.user_data)); + peerData.version = static_cast(cs::hostdata::version(p.user_data)); + peerData.platform = static_cast(cs::hostdata::platform(p.user_data)); + peerData.capabilities = cs::hostdata::capabilities(p.user_data); + peerData.minorVersion = cs::hostdata::minorVersion(p.user_data); } if (uniqueIds.insert(peerData.id).second) { @@ -467,6 +463,19 @@ void Transport::getKnownPeers(std::vector& result) { } } +bool Transport::peerCapabilities(const cs::PublicKey& key, uint8_t& capabilities) { + std::vector knownPeers; + host_.GetKnownNodes(knownPeers); + const auto id = toNodeId(key); + for (const auto& p : knownPeers) { + if (p.id == id) { + capabilities = cs::hostdata::capabilities(p.user_data); + return true; + } + } + return false; +} + void Transport::addToNeighbours(const std::set& keys) { neighbourhood_.add(keys); } diff --git a/tests/csnode/hostdata_tests.cpp b/tests/csnode/hostdata_tests.cpp new file mode 100644 index 000000000..ca10da751 --- /dev/null +++ b/tests/csnode/hostdata_tests.cpp @@ -0,0 +1,59 @@ +#include + +#include +#include + +#include +#include + +TEST(HostData, OfficialFieldsUnchanged) { + const uint64_t data = cs::hostdata::encode(3, 532, cs::hostdata::kSnapshotProtocolV1, 13); + // the official decoders read bits 0-7 and 8-23 only + EXPECT_EQ(static_cast(data), 3); + EXPECT_EQ(static_cast(data >> 8), 532); + EXPECT_EQ(cs::hostdata::platform(data), 3); + EXPECT_EQ(cs::hostdata::version(data), 532); + EXPECT_EQ(data & 0xffffffu, cs::hostdata::encode(3, 532, 0, 0)); +} + +TEST(HostData, CapabilityAndMinorVersion) { + const uint64_t data = cs::hostdata::encode(0, 532, cs::hostdata::kSnapshotProtocolV1, 13); + EXPECT_EQ(cs::hostdata::capabilities(data), cs::hostdata::kSnapshotProtocolV1); + EXPECT_EQ(cs::hostdata::minorVersion(data), 13); + EXPECT_EQ((data >> 24) & 0xff, 1u); + EXPECT_EQ(data >> 48, 0u); + + const uint64_t full = cs::hostdata::encode(0xff, 0xffff, 0xff, 0xffff); + EXPECT_EQ(full, 0x0000ffffffffffffull); + EXPECT_EQ(cs::hostdata::minorVersion(full), 0xffff); +} + +TEST(HostData, OfficialNodeAnnouncesNothing) { + const uint64_t official = cs::hostdata::encode(1, 532, 0, 0); + EXPECT_EQ(cs::hostdata::capabilities(official), 0); + EXPECT_EQ(cs::hostdata::minorVersion(official), 0); +} + +TEST(HostData, KnownPeersJson) { + EXPECT_EQ(Config::knownPeersJson({}), "[]\n"); + + cs::PeerData a; + a.id = "3rUevsW5xfob6qDxWMDFwwTQCq39SYhzstuyfUGSDvF2"; + a.ip = "10.0.0.1"; + a.port = 6000; + a.version = 532; + a.platform = 1; + a.capabilities = 1; + a.minorVersion = 13; + cs::PeerData b; + b.id = "ESWr7ZThCmP2DVNNgrcsCvZZ2rrhDnFRW8oUFEpKpgdD"; + b.ip = "10.0.0.2"; + b.port = 6001; + EXPECT_EQ(Config::knownPeersJson({a, b}), + "[\n" + " {\"id\":\"3rUevsW5xfob6qDxWMDFwwTQCq39SYhzstuyfUGSDvF2\",\"ip\":\"10.0.0.1\",\"port\":6000,\"version\":532," + "\"minor\":13,\"platform\":1,\"capabilities\":1},\n" + " {\"id\":\"ESWr7ZThCmP2DVNNgrcsCvZZ2rrhDnFRW8oUFEpKpgdD\",\"ip\":\"10.0.0.2\",\"port\":6001,\"version\":0," + "\"minor\":0,\"platform\":-1,\"capabilities\":0}\n" + "]\n"); +}