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
3 changes: 3 additions & 0 deletions .github/workflows/robustness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions ci/testnet/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
26 changes: 25 additions & 1 deletion client/config/config.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <algorithm>
#include <iostream>
#include <fstream>
#include <regex>
#include <sstream>
#include <set>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -233,6 +236,27 @@ void Config::updateKnownHosts(std::vector<cs::PeerData>& 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<cs::PeerData>& 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)
Expand Down
1 change: 1 addition & 0 deletions client/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ class Config {
void swap(Config& config);

void updateKnownHosts(std::vector<cs::PeerData>&) const;
static std::string knownPeersJson(const std::vector<cs::PeerData>& peers);

const EventsReportData& getEventsReportData() const {
return eventsReport_;
Expand Down
3 changes: 3 additions & 0 deletions lib/include/lib/system/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Byte, Signature>;
Expand Down
35 changes: 35 additions & 0 deletions net/include/net/hostdata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef NET_HOSTDATA_HPP
#define NET_HOSTDATA_HPP

#include <cstdint>

// 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<uint8_t>(data);
}

inline uint16_t version(uint64_t data) {
return static_cast<uint16_t>(data >> 8);
}

inline uint8_t capabilities(uint64_t data) {
return static_cast<uint8_t>(data >> 24);
}

inline uint16_t minorVersion(uint64_t data) {
return static_cast<uint16_t>(data >> 32);
}

} // namespace cs::hostdata

#endif // NET_HOSTDATA_HPP
2 changes: 2 additions & 0 deletions net/include/net/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Transport : public net::HostEventHandler {
void addToNeighbours(const std::set<cs::PublicKey>&);

void getKnownPeers(std::vector<cs::PeerData>&);
// 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
Expand Down
41 changes: 25 additions & 16 deletions net/src/transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <lib/system/structures.hpp>
#include <lib/system/utils.hpp>

#include <net/hostdata.hpp>

#include <packetvalidator.hpp>

namespace {
Expand All @@ -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<uint8_t>(csconnector::connector::platform());
auto node_version = static_cast<uint16_t>(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<uint8_t>(user_data);
}

uint16_t getVersion(uint64_t user_data) {
return static_cast<uint16_t>(user_data >> 8);
auto node_version = static_cast<uint16_t>(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<uint16_t>(config.getMinorNodeVersion());
return cs::hostdata::encode(platform, node_version, capabilities, minor_version);
}

net::Config createNetConfig(bool& good) {
Expand Down Expand Up @@ -448,8 +442,10 @@ void Transport::getKnownPeers(std::vector<cs::PeerData>& result) {
peerData.ip = p.address.to_string();
peerData.port = p.udp_port;
if (p.user_data) {
peerData.version = static_cast<decltype(peerData.version)>(getVersion(p.user_data));
peerData.platform = static_cast<decltype(peerData.platform)>(getPlatform(p.user_data));
peerData.version = static_cast<decltype(peerData.version)>(cs::hostdata::version(p.user_data));
peerData.platform = static_cast<decltype(peerData.platform)>(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) {
Expand All @@ -467,6 +463,19 @@ void Transport::getKnownPeers(std::vector<cs::PeerData>& result) {
}
}

bool Transport::peerCapabilities(const cs::PublicKey& key, uint8_t& capabilities) {
std::vector<net::NodeEntrance> 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<cs::PublicKey>& keys) {
neighbourhood_.add(keys);
}
59 changes: 59 additions & 0 deletions tests/csnode/hostdata_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <gtest/gtest.h>

#include <string>
#include <vector>

#include <config.hpp>
#include <net/hostdata.hpp>

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<uint8_t>(data), 3);
EXPECT_EQ(static_cast<uint16_t>(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");
}
Loading