diff --git a/.github/workflows/build-platforms.yml b/.github/workflows/build-platforms.yml index 8d4931d39..0cc333270 100644 --- a/.github/workflows/build-platforms.yml +++ b/.github/workflows/build-platforms.yml @@ -157,7 +157,7 @@ jobs: uses: actions/cache/restore@v4 with: path: ${{ env.BOOST_PREFIX }} - key: boost-${{ env.BOOST_VERSION }}-static-macos-14-v1 + key: boost-${{ env.BOOST_VERSION }}-static-cxx17-macos-14-v1 - name: Build Boost if: steps.cache-boost.outputs.cache-hit != 'true' @@ -168,8 +168,11 @@ jobs: tar xjf boost.tar.bz2 cd "boost_${v}" ./bootstrap.sh --with-libraries=system,filesystem,program_options,date_time,regex,log,iostreams,thread,serialization,atomic,chrono + # cxxstd=17: Apple clang compiles C++ as C++98 unless told otherwise; Boost built that way + # holds definitions of header templates (Asio in Boost.Log's syslog backend) that differ from + # the node's C++17 ones, and the linker keeps one of each ./b2 -j"$(sysctl -n hw.ncpu)" -d0 toolset=clang variant=release link=static threading=multi \ - cxxflags=-fPIC --layout=tagged -sNO_BZIP2=1 -sNO_ZSTD=1 -sNO_LZMA=1 \ + cxxstd=17 cxxflags=-fPIC --layout=tagged -sNO_BZIP2=1 -sNO_ZSTD=1 -sNO_LZMA=1 \ install --prefix="${BOOST_PREFIX}" - name: Save Boost diff --git a/.github/workflows/platform-sync.yml b/.github/workflows/platform-sync.yml index eb2256f96..1e1326a30 100644 --- a/.github/workflows/platform-sync.yml +++ b/.github/workflows/platform-sync.yml @@ -16,8 +16,8 @@ on: description: 'Id of a Release workflow run whose artifacts hold the Linux, Windows and macOS packages (empty = the latest successful one)' default: '' timeout_min: - description: 'Minutes each node has to reach the head' - default: '180' + description: 'Minutes each node has to reach the head (Linux needs about 5)' + default: '30' min_common: description: 'Blocks with a digest from all three systems needed to pass' default: '5' @@ -74,7 +74,7 @@ jobs: package: '*-macos-arm64.tar.gz' node: ./node runs-on: ${{ matrix.runner }} - timeout-minutes: 300 + timeout-minutes: 90 defaults: run: shell: bash @@ -123,23 +123,45 @@ jobs: - name: Restore the published snapshot working-directory: csnode - run: ${{ matrix.node }} --config-file config.ini --download-snapshot download.log 2>&1 || { tail -n 60 download.log | cut -c1-300; exit 1; } + grep -aE '^(download-snapshot|import-snapshot|snapshot)' download.log | cut -c1-250 + tail -n 3 download.log | cut -c1-250 - name: Sync to the head env: - TIMEOUT_MIN: ${{ inputs.timeout_min || '180' }} + TIMEOUT_MIN: ${{ inputs.timeout_min || '30' }} run: | cd csnode - ${{ matrix.node }} --config-file config.ini stdout.log 2>&1 & - echo "NODE_PID=$!" >> "$GITHUB_ENV" + # how the node ended, if it ends before the head + # (not "cmd; echo $?": with -e the subshell would end at the failing node) + ( rc=0; ${{ matrix.node }} --config-file config.ini stdout.log 2>&1 || rc=$?; echo "the node exited with status $rc" > node-exit.txt ) & + sleep 2 + pid= + [ "${{ matrix.os }}" = windows ] || pid=$(pgrep -n -f -- '--config-file config.ini' || true) + echo "NODE_PID=$pid" >> "$GITHUB_ENV" cd .. - python ci/snapshot/check_node.py wait-head --api 127.0.0.1:9090 --timeout-min "$TIMEOUT_MIN" + # Linux syncs this stretch at 30000+ blocks/min and reaches the head in about 5 minutes: a node + # that ends, has no API after 4 minutes or moves at less than a tenth of that speed is + # looked at at once, not after the timeout + python ci/snapshot/check_node.py wait-head --api 127.0.0.1:9090 --timeout-min "$TIMEOUT_MIN" \ + --exit-file csnode/node-exit.txt --api-grace-min 4 --min-rate 3000 --window-min 2 2>&1 | tee csnode/waithead.txt || { + echo "---- the end of the node's output"; tail -n 60 csnode/stdout.log + exit 1 + } - name: Stop and collect the digests if: always() env: OS: ${{ matrix.os }} run: | + mkdir -p result + # a node that never reached the head: where its threads are + if [ "$OS" = macos ] && [ -n "${NODE_PID:-}" ] && kill -0 "$NODE_PID" 2>/dev/null; then + sample "$NODE_PID" 5 -file result/sample-macos.txt >/dev/null 2>&1 || true + fi if [ "$OS" = windows ]; then taskkill //F //IM node.exe >/dev/null 2>&1 || true taskkill //F //IM java.exe >/dev/null 2>&1 || true @@ -148,10 +170,18 @@ jobs: for _ in $(seq 1 120); do [ -z "${NODE_PID:-}" ] || kill -0 "$NODE_PID" 2>/dev/null || break; sleep 1; done pkill -f '[c]ontract-executor[.]jar' || true fi - mkdir -p result cat csnode/log/log_*.txt 2>/dev/null | grep -a "STATE DIGEST #" > "result/digests-$OS.txt" || true cat csnode/log/log_*.txt 2>/dev/null | grep -aE "counted transactions is different|Couldn't save block|is not on this chain|but its caches already hold block|snapshot.verified|snapshot.mismatch" | head -50 > "result/problems-$OS.txt" || true tail -c 2000000 csnode/stdout.log > "result/stdout-$OS.log" 2>/dev/null || true + cat csnode/node-exit.txt 2>/dev/null | tee result/exit-$OS.txt || true + cp csnode/waithead.txt "result/waithead-$OS.txt" 2>/dev/null || true + cp csnode/download.log "result/download-$OS.log" 2>/dev/null || true + if [ "$OS" = macos ]; then + sleep 15 # the crash report is written a moment after the process ends + cp ~/Library/Logs/DiagnosticReports/node*.ips result/ 2>/dev/null || true + ls ~/Library/Logs/DiagnosticReports/ 2>/dev/null | head || true + fi + for f in csnode/log/*; do [ -f "$f" ] && tail -c 3000000 "$f" > "result/$OS-$(basename "$f")"; done 2>/dev/null || true echo "$(wc -l < "result/digests-$OS.txt") digest lines"; tail -3 "result/digests-$OS.txt" - name: Upload the digests diff --git a/ci/snapshot/check_node.py b/ci/snapshot/check_node.py index e33707865..e91ba37dc 100755 --- a/ci/snapshot/check_node.py +++ b/ci/snapshot/check_node.py @@ -59,19 +59,47 @@ def monitor(path): def wait_head(args): - node = None - deadline = time.time() + args.timeout_min * 60 + started = time.time() + deadline = started + args.timeout_min * 60 near_since = None + api_up_since = None + window = [] # (time, block) of the last --window-min minutes while time.time() < deadline: + # a node that ended is not waited for + if args.exit_file and os.path.exists(args.exit_file): + print(f"FAIL: {open(args.exit_file).read().strip()}", flush=True) + return 1 try: - # the node opens its API only once its database is loaded: connect (again) until it does - node = node or client(args.api) - s = node.SyncStateGet() + # a new connection for every poll: the node closes a connection idle for its + # server_receive_timeout (30 s), the interval of these polls, and a kept connection would + # race that close (lost on macOS every time, sometimes on Windows: "TSocket read 0 bytes"). + # The node opens its API only once its database is loaded: connect until it does. + node = client(args.api) + try: + s = node.SyncStateGet() + finally: + node.close() gap = s.currRound - s.lastBlock + api_up_since = api_up_since or time.time() except Exception as e: # the node may still be starting print(f"sync state unavailable: {e}", flush=True) - node = None gap = None + if api_up_since is None and args.api_grace_min and time.time() - started > args.api_grace_min * 60: + print(f"FAIL: the node's API is not up {args.api_grace_min} min after the start", flush=True) + return 1 + if gap is not None: + # the speed of the sync: a node far behind has to move, and at a speed in line with the + # others (Linux: 30000 blocks/min and more); near the head the chain sets the pace + now = time.time() + window.append((now, s.lastBlock)) + while window and now - window[0][0] > args.window_min * 60 + 1: + window.pop(0) + if args.min_rate and gap > args.near_gap and now - window[0][0] >= args.window_min * 60: + rate = (s.lastBlock - window[0][1]) / ((now - window[0][0]) / 60) + print(f"speed over the last {args.window_min} min: {rate:.0f} blocks/min (at least {args.min_rate} wanted)", flush=True) + if rate < args.min_rate: + print(f"FAIL: the sync is too slow: {rate:.0f} blocks/min, {gap} blocks behind", flush=True) + return 1 if gap is not None and s.lastBlock > 0 and gap <= 3: near_since = near_since or time.time() if time.time() - near_since >= 180: @@ -189,6 +217,11 @@ def main(): w = sub.add_parser("wait-head") w.add_argument("--api", required=True) w.add_argument("--timeout-min", type=int, default=90) + w.add_argument("--exit-file", help="a file the caller writes when the node process ends: the wait fails at once") + w.add_argument("--api-grace-min", type=int, default=0, help="minutes the node's API may take to come up (0 = no limit)") + w.add_argument("--min-rate", type=int, default=0, help="blocks per minute the sync must keep while far behind (0 = no check)") + w.add_argument("--window-min", type=int, default=2, help="minutes the speed is measured over") + w.add_argument("--near-gap", type=int, default=3000, help="no speed check this close to the head, in blocks") b = sub.add_parser("balances") b.add_argument("--api", required=True) b.add_argument("--count", type=int, default=50) diff --git a/csnode/include/csnode/archive_stream.hpp b/csnode/include/csnode/archive_stream.hpp new file mode 100644 index 000000000..8505358f2 --- /dev/null +++ b/csnode/include/csnode/archive_stream.hpp @@ -0,0 +1,191 @@ +#ifndef ARCHIVE_STREAM_HPP +#define ARCHIVE_STREAM_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cs { + +// The input of a boost binary_iarchive that reads a checkpoint written on another system. +// +// A binary_oarchive header records the native sizes of int, long, float and double, and +// binary_iarchive refuses a header whose sizes differ from its own: long is 8 bytes on Linux and +// macOS, 4 on Windows, so a checkpoint (or a snapshot made of one) from one system did not load on +// the other. No checkpoint field is a long: 64-bit fields are int64_t/uint64_t, written as 8 bytes +// on every system, and boost writes its own counts as 8-byte size_t on 64-bit systems. The size of +// long in the header is the only difference. Archives are written with 8 there on every system +// (ArchiveOutput), so only Windows has anything to change on read: it takes an 8 as its own 4. On +// Linux and macOS the input is passed through untouched, the plain read, and boost keeps refusing +// an archive of a 32-bit system. +// +// Header of boost 1.83: size_t length (22) | "serialization::archive" | uint16 library version | +// sizeof(int) | sizeof(long) | sizeof(float) | sizeof(double) | int 1 (byte order). Anything else is +// passed through unchanged, for binary_iarchive to accept or refuse as before. +class ArchiveInputBuf : public std::streambuf { +public: + explicit ArchiveInputBuf(std::streambuf* source) + : source_(source) { + head_.resize(kHeaderSize); + head_.resize(static_cast(source_->sgetn(head_.data(), static_cast(kHeaderSize)))); + patchHeader(); + setg(head_.data(), head_.data(), head_.data() + head_.size()); + } + + static constexpr size_t kSignatureSize = 22; + static constexpr size_t kLongSizeOffset = sizeof(uint64_t) + kSignatureSize + sizeof(uint16_t) + 1; + static constexpr size_t kHeaderSize = kLongSizeOffset + 3 + sizeof(int32_t); + +protected: + int_type underflow() override { + if (gptr() < egptr()) { + return traits_type::to_int_type(*gptr()); + } + buffer_.resize(kChunk); + const auto n = source_->sgetn(buffer_.data(), static_cast(buffer_.size())); + if (n <= 0) { + return traits_type::eof(); + } + setg(buffer_.data(), buffer_.data(), buffer_.data() + n); + return traits_type::to_int_type(*gptr()); + } + +private: + static constexpr size_t kChunk = 1 << 16; + + void patchHeader() { + if (head_.size() < kHeaderSize) { + return; + } + uint64_t length = 0; + for (size_t i = 0; i < sizeof(length); ++i) { + length |= static_cast(static_cast(head_[i])) << (8 * i); + } + static const char kSignature[] = "serialization::archive"; + if (length != kSignatureSize || std::memcmp(head_.data() + sizeof(uint64_t), kSignature, kSignatureSize) != 0) { + return; + } + auto byteAt = [this](size_t i) { return static_cast(head_[i]); }; + const bool sizesAsExpected = byteAt(kLongSizeOffset - 1) == sizeof(int32_t) && byteAt(kLongSizeOffset + 1) == sizeof(float) && + byteAt(kLongSizeOffset + 2) == sizeof(double); + if (sizesAsExpected && sizeof(long) == 4 && byteAt(kLongSizeOffset) == 8) { + head_[kLongSizeOffset] = static_cast(sizeof(long)); + } + } + + std::streambuf* source_; + std::vector head_; + std::vector buffer_; +}; + +// usage: std::ifstream ifs(path, std::ios::binary); cs::ArchiveInput in(ifs); boost::archive::binary_iarchive ia(in); +class ArchiveInput : public std::istream { +public: + explicit ArchiveInput(std::istream& source) + : std::istream(nullptr) + , buf_(source.rdbuf()) { + rdbuf(&buf_); + if (!source) { + setstate(std::ios::badbit); + } + } + +private: + ArchiveInputBuf buf_; +}; + +// The output of a boost binary_oarchive for a checkpoint: the header records long as 8 bytes on +// every system, so that a checkpoint (and a snapshot made of one) has the same bytes on Windows as +// on Linux and macOS, and nodes without ArchiveInput read it too. Only that header byte changes. +class ArchiveOutputBuf : public std::streambuf { +public: + explicit ArchiveOutputBuf(std::streambuf* target) + : target_(target) { + } + +protected: + int_type overflow(int_type c) override { + if (traits_type::eq_int_type(c, traits_type::eof())) { + return traits_type::not_eof(c); + } + const char ch = traits_type::to_char_type(c); + return xsputn(&ch, 1) == 1 ? c : traits_type::eof(); + } + + std::streamsize xsputn(const char* s, std::streamsize n) override { + if (n <= 0) { + return 0; + } + const auto count = static_cast(n); + if (written_ > ArchiveInputBuf::kLongSizeOffset) { + return put(s, n); + } + std::vector copy(s, s + count); + for (size_t i = 0; i < count && written_ + i <= ArchiveInputBuf::kLongSizeOffset; ++i) { + const auto at = written_ + i; + if (at < ArchiveInputBuf::kLongSizeOffset) { + head_[at] = copy[i]; + } + else if (signatureWritten() && (copy[i] == 4 || copy[i] == 8)) { + copy[i] = 8; + } + } + return put(copy.data(), n); + } + + int sync() override { + return target_->pubsync(); + } + +private: + std::streamsize put(const char* s, std::streamsize n) { + const auto done = target_->sputn(s, n); + written_ += static_cast(done > 0 ? done : 0); + return done; + } + + bool signatureWritten() const { + static const char kSignature[] = "serialization::archive"; + for (size_t i = 1; i < sizeof(uint64_t); ++i) { + if (head_[i] != 0) { + return false; + } + } + return static_cast(head_[0]) == ArchiveInputBuf::kSignatureSize && + std::memcmp(head_.data() + sizeof(uint64_t), kSignature, ArchiveInputBuf::kSignatureSize) == 0 && + static_cast(head_[ArchiveInputBuf::kLongSizeOffset - 1]) == sizeof(int32_t); + } + + std::streambuf* target_; + size_t written_ = 0; + std::array head_{}; +}; + +// usage: std::ofstream ofs(path, std::ios::binary); cs::ArchiveOutput out(ofs); boost::archive::binary_oarchive oa(out); +class ArchiveOutput : public std::ostream { +public: + explicit ArchiveOutput(std::ostream& target) + : std::ostream(nullptr) + , buf_(target.rdbuf()) { + rdbuf(&buf_); + if (!target) { + setstate(std::ios::badbit); + } + } + + ~ArchiveOutput() override { + flush(); + } + +private: + ArchiveOutputBuf buf_; +}; + +} // namespace cs + +#endif // ARCHIVE_STREAM_HPP diff --git a/csnode/include/csnode/serializers_helper.hpp b/csnode/include/csnode/serializers_helper.hpp index 55232131b..0800ad669 100644 --- a/csnode/include/csnode/serializers_helper.hpp +++ b/csnode/include/csnode/serializers_helper.hpp @@ -12,7 +12,7 @@ class SerializersHelper { static const size_t kBufSize = 1 << 27; std::vector buf(::cscrypto::kHashSize + kBufSize, 0); { - std::ifstream ifs(fileName); + std::ifstream ifs(fileName, std::ios::binary); // text mode on Windows would change the bytes hashed while (!ifs.eof()) { ifs.read( reinterpret_cast(buf.data() + ::cscrypto::kHashSize), diff --git a/csnode/src/apihandler_serializer.cpp b/csnode/src/apihandler_serializer.cpp index 2befc9258..16bc2fb5e 100644 --- a/csnode/src/apihandler_serializer.cpp +++ b/csnode/src/apihandler_serializer.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -28,7 +29,8 @@ void APIHandler_Serializer::bind(api::APIHandler& apih) { void APIHandler_Serializer::save(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); auto saveHelper = [&](auto& entity) { auto ref = lockedReference(entity); @@ -45,7 +47,8 @@ void APIHandler_Serializer::save(const std::filesystem::path& rootDir) { void APIHandler_Serializer::load(const std::filesystem::path& rootDir) { std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); auto loadHelper = [&](auto& entity) { auto ref = lockedReference(entity); diff --git a/csnode/src/blockchain_serializer.cpp b/csnode/src/blockchain_serializer.cpp index 7bd2da180..b9ac2b4d5 100644 --- a/csnode/src/blockchain_serializer.cpp +++ b/csnode/src/blockchain_serializer.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -69,7 +70,8 @@ void BlockChain_Serializer::clear(const std::filesystem::path& rootDir) { void BlockChain_Serializer::save(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); oa << *previousNonEmpty_; oa << *lastNonEmptyBlock_; oa << *totalTransactionsCount_; @@ -88,7 +90,8 @@ void BlockChain_Serializer::save(const std::filesystem::path& rootDir) { void BlockChain_Serializer::savePending(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kPendingFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); oa << kPendingFormat; oa << *pendingRound_; oa << *pendingStakingOn_ << *pendingMiningOn_; @@ -105,7 +108,8 @@ void BlockChain_Serializer::loadPending(const std::filesystem::path& rootDir) { return; } std::ifstream ifs(path, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); uint8_t format = 0; ia >> format; if (format != 1 && format != kPendingFormat) { @@ -146,7 +150,8 @@ ::cscrypto::Hash BlockChain_Serializer::hash() { void BlockChain_Serializer::load(const std::filesystem::path& rootDir) { std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); ia >> *previousNonEmpty_; ia >> *lastNonEmptyBlock_; ia >> *totalTransactionsCount_; diff --git a/csnode/src/caches_serialization_manager.cpp b/csnode/src/caches_serialization_manager.cpp index 7d36dec5d..68c1e85f0 100644 --- a/csnode/src/caches_serialization_manager.cpp +++ b/csnode/src/caches_serialization_manager.cpp @@ -212,7 +212,7 @@ struct CachesSerializationManager::Impl { } void saveHashes(const std::filesystem::path& dir) { - std::ofstream f(dir / kHashesFile); + std::ofstream f(dir / kHashesFile, std::ios::binary); f << getHashes(); } @@ -293,7 +293,8 @@ struct CachesSerializationManager::Impl { std::ifstream f( std::filesystem::path(kQuickStartRoot) / std::to_string(version) / - kHashesFile + kHashesFile, + std::ios::binary ); std::string writtenHashes; diff --git a/csnode/src/roundstat_serializer.cpp b/csnode/src/roundstat_serializer.cpp index 87f26f374..df4b0e878 100644 --- a/csnode/src/roundstat_serializer.cpp +++ b/csnode/src/roundstat_serializer.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -35,7 +36,8 @@ namespace cs { void RoundStat_Serializer::save(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); csdebug() << kLogPrefix << __func__; roundStat_->printClassInfo(); oa << roundStat_->serialize(); @@ -62,7 +64,8 @@ namespace cs { void RoundStat_Serializer::load(const std::filesystem::path& rootDir) { std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); csdebug() << kLogPrefix << __func__; Bytes data; ia >> data; diff --git a/csnode/src/smartcontracts_serializer.cpp b/csnode/src/smartcontracts_serializer.cpp index 4e1c613be..691803b50 100644 --- a/csnode/src/smartcontracts_serializer.cpp +++ b/csnode/src/smartcontracts_serializer.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -32,7 +33,8 @@ void SmartContracts_Serializer::clear(const std::filesystem::path& rootDir) { void SmartContracts_Serializer::save(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); csdebug() << kLogPrefix << __func__; contracts_->printClassInfo(); oa << contracts_->serialize(); @@ -60,7 +62,8 @@ ::cscrypto::Hash SmartContracts_Serializer::hash() { void SmartContracts_Serializer::load(const std::filesystem::path& rootDir) { std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); csdebug() << kLogPrefix << __func__; Bytes data; ia >> data; diff --git a/csnode/src/tokens_serializer.cpp b/csnode/src/tokens_serializer.cpp index 9ab9da1c2..39be7b973 100644 --- a/csnode/src/tokens_serializer.cpp +++ b/csnode/src/tokens_serializer.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -30,7 +31,7 @@ void TokensMaster_Serializer::clear(const std::filesystem::path& rootDir) { } void TokensMaster_Serializer::save(const std::filesystem::path& rootDir) { - std::ofstream ofs(rootDir / kDataFileName); + std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); boost::archive::text_oarchive oa(ofs); oa << *tokens_; oa << *holders_; @@ -38,7 +39,8 @@ void TokensMaster_Serializer::save(const std::filesystem::path& rootDir) { ::cscrypto::Hash TokensMaster_Serializer::hash() { { - std::ofstream ofs(kDataFileName); + std::ofstream ofs(kDataFileName, std::ios::binary); // text mode on Windows would write \r\n and change the hash + ofs.imbue(std::locale::classic()); // with no_codecvt the archive formats numbers in the stream's locale boost::archive::text_oarchive oa( ofs, boost::archive::no_header | boost::archive::no_codecvt @@ -60,7 +62,7 @@ ::cscrypto::Hash TokensMaster_Serializer::hash() { } void TokensMaster_Serializer::load(const std::filesystem::path& rootDir) { - std::ifstream ifs(rootDir / kDataFileName); + std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); boost::archive::text_iarchive ia(ifs); ia >> *tokens_; ia >> *holders_; diff --git a/csnode/src/walletscache_serializer.cpp b/csnode/src/walletscache_serializer.cpp index 18a6d347b..f3d199637 100644 --- a/csnode/src/walletscache_serializer.cpp +++ b/csnode/src/walletscache_serializer.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -63,7 +64,8 @@ void WalletsCache_Serializer::save(const std::filesystem::path& rootDir) { ::cscrypto::Hash WalletsCache_Serializer::hash() { { - std::ofstream ofs(kTmpDataFile); + std::ofstream ofs(kTmpDataFile, std::ios::binary); // text mode on Windows would write \r\n and change the hash + ofs.imbue(std::locale::classic()); // with no_codecvt the archive formats numbers in the stream's locale boost::archive::text_oarchive oa( ofs, boost::archive::no_header | boost::archive::no_codecvt diff --git a/csnode/src/walletsids_serializer.cpp b/csnode/src/walletsids_serializer.cpp index fc61467f6..c5827fe2d 100644 --- a/csnode/src/walletsids_serializer.cpp +++ b/csnode/src/walletsids_serializer.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -33,7 +34,8 @@ void WalletsIds_Serializer::clear(const std::filesystem::path& rootDir) { void WalletsIds_Serializer::save(const std::filesystem::path& rootDir) { std::ofstream ofs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_oarchive oa(ofs); + cs::ArchiveOutput out(ofs); // the same header bytes on every system + boost::archive::binary_oarchive oa(out); oa << *data_; oa << *nextId_; } @@ -65,7 +67,8 @@ ::cscrypto::Hash WalletsIds_Serializer::hash() { void WalletsIds_Serializer::load(const std::filesystem::path& rootDir) { std::ifstream ifs(rootDir / kDataFileName, std::ios::binary); - boost::archive::binary_iarchive ia(ifs); + cs::ArchiveInput in(ifs); // also a checkpoint written on another system + boost::archive::binary_iarchive ia(in); ia >> *data_; ia >> *nextId_; } diff --git a/docs/CHANGES.md b/docs/CHANGES.md index 86deeb078..eebd7bcc4 100644 --- a/docs/CHANGES.md +++ b/docs/CHANGES.md @@ -39,6 +39,7 @@ lines under the new release. |---|---| | #113 | packaging: one package per build for full and pruned storage (`config.ini` full, `config.pruned.ini` pruned on LMDB), half the release size; the workflows that take the latest package accept both layouts | | #114 | the node builds and passes its tests on Windows (MSVC, x64) and macOS (Apple Silicon): POSIX-only code behind `lib/system/platform.hpp`, the database lock now also on Windows, a new `build-platforms.yml` workflow; the release workflow also makes a Windows `.zip` and a macOS package (with Eclipse Temurin 11), kept as artifacts until a mainnet state-digest check on each system | +| #115 | Windows and macOS nodes load the checkpoints and snapshots of Linux and sync mainnet with the same state digests: the binary checkpoint header records `long` as 8 bytes on every system (`cs::ArchiveOutput`, read back by `cs::ArchiveInput`), checkpoint text files are read and written in binary mode, macOS Boost is built as C++17; `Transport` releases the routing table with the p2p host, fixing a use-after-free at exit (a crash on macOS); a platform consensus check in CI | ## community.13 — 2026-10-26 diff --git a/net/src/transport.cpp b/net/src/transport.cpp index c1b501ee3..85aa443d1 100644 --- a/net/src/transport.cpp +++ b/net/src/transport.cpp @@ -121,6 +121,13 @@ Transport::~Transport() { if (processorThread_.joinable()) { processorThread_.join(); } + // p2p's Network singleton keeps a reference to the host's routing table (Host::Run). Left there, + // the routing table (its UDP socket, pinger and explorer threads) would outlive host_ and its + // io_context and be destroyed at static destruction, against the io_context's freed reactor: + // SIGSEGV in kqueue_reactor::deregister_descriptor at exit on macOS, undefined behaviour on the + // other systems. Without it the routing table goes with host_ (just below, as host_ is the last + // member), after ~Host has stopped its io_context and before the io_context is destroyed. + net::Network::Instance().SetRoutingTable(nullptr); } void Transport::run() { diff --git a/tests/csnode/archive_stream_tests.cpp b/tests/csnode/archive_stream_tests.cpp new file mode 100644 index 000000000..9da944596 --- /dev/null +++ b/tests/csnode/archive_stream_tests.cpp @@ -0,0 +1,193 @@ +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { +struct Data { + uint64_t sequence = 0; + int64_t signedValue = 0; + std::map counts; + std::vector bytes; + + template + void serialize(Archive& ar, const unsigned int) { + ar & sequence; + ar & signedValue; + ar & counts; + ar & bytes; + } +}; + +Data sample() { + Data d; + d.sequence = 186600000; + d.signedValue = -1234567890123; + d.counts = {{"a", 1}, {"contract", 1ll << 40}}; + d.bytes.assign(200000, 0x5a); // more than one chunk of the reader + d.bytes[123456] = 7; + return d; +} + +// an archive written here, with the header the other kind of system writes +std::string archiveWithLongSize(unsigned char longSize) { + std::ostringstream out(std::ios::binary); + { + boost::archive::binary_oarchive oa(out); + const auto d = sample(); + oa << d; + } + auto bytes = out.str(); + EXPECT_EQ(static_cast(bytes[cs::ArchiveInputBuf::kLongSizeOffset]), sizeof(long)); + bytes[cs::ArchiveInputBuf::kLongSizeOffset] = static_cast(longSize); + return bytes; +} + +void expectSample(const Data& d) { + const auto want = sample(); + EXPECT_EQ(d.sequence, want.sequence); + EXPECT_EQ(d.signedValue, want.signedValue); + EXPECT_EQ(d.counts, want.counts); + EXPECT_EQ(d.bytes, want.bytes); +} +} // namespace + +// A checkpoint is written with a long of 8 bytes everywhere: Windows (long of 4) reads it, and +// Linux/macOS read it as they always did. A header with a 4 is refused where long is 8, as by the +// plain reader: nothing is rewritten there. +TEST(ArchiveInput, ReadsTheHeaderTheNodeWrites) { + for (unsigned char longSize : {4, 8}) { + std::istringstream source(archiveWithLongSize(longSize), std::ios::binary); + cs::ArchiveInput in(source); + const bool accepted = longSize == sizeof(long) || (sizeof(long) == 4 && longSize == 8); + if (!accepted) { + EXPECT_THROW(boost::archive::binary_iarchive ia(in), boost::archive::archive_exception) << int(longSize); + continue; + } + boost::archive::binary_iarchive ia(in); + Data d; + ia >> d; + expectSample(d); + } +} + +// where long is 8 bytes the input is the plain one: the same result for a header of any long size +TEST(ArchiveInput, PassesTheHeaderThroughWhereLongIsEightBytes) { + if (sizeof(long) != 8) { + GTEST_SKIP() << "long is 4 bytes here"; + } + for (unsigned char longSize : {1, 2, 4, 8, 16}) { + const auto bytes = archiveWithLongSize(longSize); + std::istringstream plainSource(bytes, std::ios::binary); + std::istringstream wrappedSource(bytes, std::ios::binary); + cs::ArchiveInput in(wrappedSource); + bool plainThrew = false, wrappedThrew = false; + try { boost::archive::binary_iarchive ia(plainSource); } catch (const boost::archive::archive_exception&) { plainThrew = true; } + try { boost::archive::binary_iarchive ia(in); } catch (const boost::archive::archive_exception&) { wrappedThrew = true; } + EXPECT_EQ(plainThrew, wrappedThrew) << int(longSize); + } +} + +TEST(ArchiveInput, PlainReaderRefusesTheOtherSystem) { + const unsigned char other = sizeof(long) == 8 ? 4 : 8; + std::istringstream source(archiveWithLongSize(other), std::ios::binary); + EXPECT_THROW(boost::archive::binary_iarchive ia(source), boost::archive::archive_exception); +} + +// other sizes than the two systems' are left for binary_iarchive to refuse +TEST(ArchiveInput, OtherHeadersPassUnchanged) { + std::istringstream source(archiveWithLongSize(2), std::ios::binary); + cs::ArchiveInput in(source); + EXPECT_THROW(boost::archive::binary_iarchive ia(in), boost::archive::archive_exception); + + std::istringstream garbage(std::string("not an archive at all, just bytes"), std::ios::binary); + cs::ArchiveInput in2(garbage); + EXPECT_ANY_THROW(boost::archive::binary_iarchive ia(in2)); // as without ArchiveInput + + std::istringstream empty(std::string(), std::ios::binary); + cs::ArchiveInput in3(empty); + EXPECT_ANY_THROW(boost::archive::binary_iarchive ia(in3)); +} + +// written through ArchiveOutput, the header says long is 8 bytes on every system and the rest is unchanged +TEST(ArchiveOutput, WritesTheSameHeaderOnEverySystem) { + std::ostringstream plain(std::ios::binary); + { + boost::archive::binary_oarchive oa(plain); + const auto d = sample(); + oa << d; + } + std::ostringstream canonical(std::ios::binary); + { + cs::ArchiveOutput out(canonical); + boost::archive::binary_oarchive oa(out); + const auto d = sample(); + oa << d; + } + auto expected = plain.str(); + expected[cs::ArchiveInputBuf::kLongSizeOffset] = 8; + EXPECT_EQ(canonical.str(), expected); + + std::istringstream source(canonical.str(), std::ios::binary); + cs::ArchiveInput in(source); + boost::archive::binary_iarchive ia(in); + Data d; + ia >> d; + expectSample(d); +} + +// the header of a Windows node (long of 4 bytes), written byte by byte or at once +TEST(ArchiveOutput, RewritesAFourByteLong) { + const auto windows = archiveWithLongSize(4); + for (bool byByte : {false, true}) { + std::ostringstream target(std::ios::binary); + { + cs::ArchiveOutput out(target); + if (byByte) { + for (char c : windows) { + out.put(c); + } + } + else { + out.write(windows.data(), static_cast(windows.size())); + } + } + auto expected = windows; + expected[cs::ArchiveInputBuf::kLongSizeOffset] = 8; + EXPECT_EQ(target.str(), expected); + } + // not an archive header: passed through + std::ostringstream target(std::ios::binary); + { + cs::ArchiveOutput out(target); + out << std::string(40, '\x04'); + } + EXPECT_EQ(target.str(), std::string(40, '\x04')); +} + +// The header the node writes, byte by byte: what every node, old ones included, reads. A change of +// the Boost version or of the format shows up here first. +TEST(ArchiveOutput, HeaderIsTheOneOfBoost183) { + std::ostringstream out(std::ios::binary); + { + cs::ArchiveOutput stream(out); + boost::archive::binary_oarchive oa(stream); + const uint32_t value = 1; + oa << value; + } + const std::string header = out.str().substr(0, cs::ArchiveInputBuf::kHeaderSize); + const std::string expected("\x16\0\0\0\0\0\0\0serialization::archive\x13\0\x04\x08\x04\x08\x01\0\0\0", cs::ArchiveInputBuf::kHeaderSize); + EXPECT_EQ(header, expected); +}