From 3649cb868a14c9091cdc9e02b070c82679c81594 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 08:22:54 +0200 Subject: [PATCH 1/5] csnode: add an ECMH state-digest primitive and canonical wallet serialisation Phase 1 of the state-root plan agreed with BK: an order-independent digest of the wallet state that every node can compute and compare, out of block, so nothing changes in the protocol. - cs::MultisetHash: elliptic-curve multiset hash over ristretto255 (libsodium, already linked through cscrypto). An item is mapped to a group element via BLAKE2b-512 and crypto_core_ristretto255_from_hash. The digest is the group sum, starting from the identity (32 zero bytes). Adding or removing an item costs O(1), about 50 us, independent of state size, and the result does not depend on insertion order. ristretto255 is used instead of raw ed25519 to avoid cofactor issues. - cs::serializeWalletState(): canonical, versioned serialisation of the balance-relevant state of a wallet: key, balance, delegated amount, and delegations in both directions. Counterparts are sorted by key (std::map); entries are sorted because their order depends on apply/revert history. Empty for a wallet without such state, so empty records never move the digest. Little-endian, fixed-width fields. Tests (tests/csnode/statedigest_tests.cpp): order independence, add/remove inverse, distinct sets, empty wallet, balance and key sensitivity, delegation order independence. Co-Authored-By: Claude Opus 5.5 --- csnode/CMakeLists.txt | 2 + csnode/include/csnode/statedigest.hpp | 46 +++++++++ csnode/src/statedigest.cpp | 128 ++++++++++++++++++++++++++ tests/csnode/statedigest_tests.cpp | 79 ++++++++++++++++ 4 files changed, 255 insertions(+) create mode 100644 csnode/include/csnode/statedigest.hpp create mode 100644 csnode/src/statedigest.cpp create mode 100644 tests/csnode/statedigest_tests.cpp diff --git a/csnode/CMakeLists.txt b/csnode/CMakeLists.txt index c100d398b..c016cf100 100644 --- a/csnode/CMakeLists.txt +++ b/csnode/CMakeLists.txt @@ -68,6 +68,7 @@ add_library(csnode include/csnode/compressor.hpp include/csnode/configholder.hpp include/csnode/multiwallets.hpp + include/csnode/statedigest.hpp include/csnode/sendcachedata.hpp include/csnode/eventreport.hpp include/csnode/idatastream.hpp @@ -107,6 +108,7 @@ add_library(csnode src/roundpackage.cpp src/configholder.cpp src/multiwallets.cpp + src/statedigest.cpp src/sendcachedata.cpp src/eventreport.cpp src/poolcache.cpp diff --git a/csnode/include/csnode/statedigest.hpp b/csnode/include/csnode/statedigest.hpp new file mode 100644 index 000000000..9e69608f1 --- /dev/null +++ b/csnode/include/csnode/statedigest.hpp @@ -0,0 +1,46 @@ +#ifndef STATEDIGEST_HPP +#define STATEDIGEST_HPP + +#include +#include + +#include +#include + +namespace cs { + +// Elliptic-curve multiset hash (ECMH) over ristretto255: an order-independent digest of a set of +// items, updated in O(1) per added or removed item. Two nodes holding the same set get the same +// digest regardless of the order in which the items were added. +class MultisetHash { +public: + using Digest = std::array; + using Element = std::array; + + MultisetHash(); + + // the group element an item contributes; callers may keep it to remove the item later + static Element toElement(const cs::Bytes& item); + + void add(const cs::Bytes& item); + void remove(const cs::Bytes& item); + void add(const Element& element); + void remove(const Element& element); + void reset(); + + const Digest& digest() const { + return sum_; + } + +private: + Digest sum_; +}; + +// Canonical serialisation of the balance-relevant state of a wallet: key, balance, delegated amount +// and delegations in both directions, sorted. Empty for a wallet without such state, so that empty +// records do not change the digest. +cs::Bytes serializeWalletState(const WalletsCache::WalletData& wallet); + +} // namespace cs + +#endif // STATEDIGEST_HPP diff --git a/csnode/src/statedigest.cpp b/csnode/src/statedigest.cpp new file mode 100644 index 000000000..a2a75db8b --- /dev/null +++ b/csnode/src/statedigest.cpp @@ -0,0 +1,128 @@ +#include + +#include +#include +#include + +#include + +#include + +namespace { +static_assert(crypto_core_ristretto255_BYTES == 32, "ristretto255 element size"); + +void putU8(cs::Bytes& out, uint8_t value) { + out.push_back(value); +} + +void putU32(cs::Bytes& out, uint32_t value) { + for (int i = 0; i < 4; ++i) { + out.push_back(static_cast(value >> (8 * i))); + } +} + +void putU64(cs::Bytes& out, uint64_t value) { + for (int i = 0; i < 8; ++i) { + out.push_back(static_cast(value >> (8 * i))); + } +} + +void putAmount(cs::Bytes& out, const csdb::Amount& amount) { + putU32(out, static_cast(amount.integral())); + putU64(out, amount.fraction()); +} + +void putKey(cs::Bytes& out, const cs::PublicKey& key) { + out.insert(out.end(), key.begin(), key.end()); +} + +using Delegations = std::shared_ptr>>; + +// std::map keeps the counterparts sorted by key; the entries of each counterpart are sorted here, +// since their order depends on how blocks were applied and reverted +void putDelegations(cs::Bytes& out, const Delegations& delegations) { + if (!delegations) { + putU32(out, 0); + return; + } + + putU32(out, static_cast(delegations->size())); + for (const auto& [counterpart, entries] : *delegations) { + auto sorted = entries; + std::sort(sorted.begin(), sorted.end(), [](const cs::TimeMoney& lhs, const cs::TimeMoney& rhs) { + return std::make_tuple(lhs.time, lhs.initialTime, lhs.amount.integral(), lhs.amount.fraction(), static_cast(lhs.coeff)) < + std::make_tuple(rhs.time, rhs.initialTime, rhs.amount.integral(), rhs.amount.fraction(), static_cast(rhs.coeff)); + }); + + putKey(out, counterpart); + putU32(out, static_cast(sorted.size())); + for (const auto& entry : sorted) { + putU64(out, entry.initialTime); + putU64(out, entry.time); + putAmount(out, entry.amount); + putU8(out, static_cast(entry.coeff)); + } + } +} + +bool hasEntries(const Delegations& delegations) { + if (!delegations) { + return false; + } + return std::any_of(delegations->begin(), delegations->end(), [](const auto& item) { return !item.second.empty(); }); +} +} // namespace + +namespace cs { + +MultisetHash::MultisetHash() { + reset(); +} + +MultisetHash::Element MultisetHash::toElement(const cs::Bytes& item) { + std::array hash; + crypto_generichash(hash.data(), hash.size(), item.data(), item.size(), nullptr, 0); + + Element element; + crypto_core_ristretto255_from_hash(element.data(), hash.data()); + return element; +} + +void MultisetHash::add(const cs::Bytes& item) { + add(toElement(item)); +} + +void MultisetHash::remove(const cs::Bytes& item) { + remove(toElement(item)); +} + +void MultisetHash::add(const Element& element) { + crypto_core_ristretto255_add(sum_.data(), sum_.data(), element.data()); +} + +void MultisetHash::remove(const Element& element) { + crypto_core_ristretto255_sub(sum_.data(), sum_.data(), element.data()); +} + +void MultisetHash::reset() { + sum_.fill(0); // the identity element of ristretto255 +} + +cs::Bytes serializeWalletState(const WalletsCache::WalletData& wallet) { + const bool hasState = wallet.balance_ != csdb::Amount{0} || wallet.delegated_ != csdb::Amount{0} || + hasEntries(wallet.delegateSources_) || hasEntries(wallet.delegateTargets_); + if (!hasState) { + return {}; + } + + cs::Bytes out; + putU8(out, 1); // format version + putKey(out, wallet.key_); + putAmount(out, wallet.balance_); + putAmount(out, wallet.delegated_); + putDelegations(out, wallet.delegateSources_); + putDelegations(out, wallet.delegateTargets_); + return out; +} + +} // namespace cs diff --git a/tests/csnode/statedigest_tests.cpp b/tests/csnode/statedigest_tests.cpp new file mode 100644 index 000000000..45d4410a0 --- /dev/null +++ b/tests/csnode/statedigest_tests.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include + +namespace { +cs::Bytes item(uint8_t b) { + return cs::Bytes(16, b); +} + +cs::WalletsCache::WalletData wallet(uint8_t keyByte, int32_t balance) { + cs::WalletsCache::WalletData w; + w.key_.fill(keyByte); + w.balance_ = csdb::Amount{ balance }; + return w; +} +} // namespace + +TEST(MultisetHash, OrderIndependent) { + cs::MultisetHash ab; + ab.add(item(1)); + ab.add(item(2)); + + cs::MultisetHash ba; + ba.add(item(2)); + ba.add(item(1)); + + ASSERT_EQ(ab.digest(), ba.digest()); +} + +TEST(MultisetHash, RemoveUndoesAdd) { + cs::MultisetHash empty; + cs::MultisetHash h; + h.add(item(1)); + ASSERT_NE(h.digest(), empty.digest()); + + h.add(item(2)); + h.remove(item(2)); + cs::MultisetHash one; + one.add(item(1)); + ASSERT_EQ(h.digest(), one.digest()); + + h.remove(item(1)); + ASSERT_EQ(h.digest(), empty.digest()); +} + +TEST(MultisetHash, DifferentSetsDiffer) { + cs::MultisetHash a; + a.add(item(1)); + cs::MultisetHash b; + b.add(item(2)); + ASSERT_NE(a.digest(), b.digest()); +} + +TEST(WalletState, EmptyWalletHasNoState) { + ASSERT_TRUE(cs::serializeWalletState(wallet(1, 0)).empty()); + ASSERT_FALSE(cs::serializeWalletState(wallet(1, 5)).empty()); +} + +TEST(WalletState, BalanceChangesSerialisation) { + ASSERT_NE(cs::serializeWalletState(wallet(1, 5)), cs::serializeWalletState(wallet(1, 6))); + ASSERT_NE(cs::serializeWalletState(wallet(1, 5)), cs::serializeWalletState(wallet(2, 5))); +} + +// the order of delegation entries depends on apply/revert history, the digest must not +TEST(WalletState, DelegationOrderDoesNotMatter) { + cs::PublicKey other{}; + other.fill(9); + + auto w1 = wallet(1, 5); + w1.delegateTargets_ = std::make_shared>>(); + (*w1.delegateTargets_)[other] = { cs::TimeMoney(1'000'000, 2'000, csdb::Amount{ 1 }), cs::TimeMoney(1'000'000, 3'000, csdb::Amount{ 2 }) }; + + auto w2 = wallet(1, 5); + w2.delegateTargets_ = std::make_shared>>(); + (*w2.delegateTargets_)[other] = { cs::TimeMoney(1'000'000, 3'000, csdb::Amount{ 2 }), cs::TimeMoney(1'000'000, 2'000, csdb::Amount{ 1 }) }; + + ASSERT_EQ(cs::serializeWalletState(w1), cs::serializeWalletState(w2)); +} From 4d4fed11d78a8a2787a5ed9edcdcfd4da9c2a489 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 08:22:54 +0200 Subject: [PATCH 2/5] csnode: keep the wallet state digest up to date and log it every 1000 blocks Wire the ECMH digest into MultiWallets, the single place where wallet records are inserted or replaced (onWalletCacheUpdated). - The digest is computed in full on the first call to stateDigest(). Node calls it right after the blockchain is loaded (slow or quick start), before taking part in the network. From then on, each wallet update removes the wallet's previous element and adds the new one. Slow-start replay does not pay a per-update cost. - The element of each wallet is stored in its record (stateElement_). The delegation maps are shared_ptrs that Staking updates in place before the record is replaced, so re-serialising the old record would remove the wrong element. The element is not persisted; it is rebuilt at start. - WalletsCache_Serializer aliases the MultiWallets container through a mirror WalletData type, so the mirror gets the same two fields (not serialised: the checkpoint format is unchanged). A static_assert on the two sizes now catches any future mismatch at compile time. - Node logs "STATE DIGEST # wallets " at start and on every block whose sequence is a multiple of 1000. Healthy nodes print the same digest for the same sequence; a different one pinpoints a divergent node and, between two log points, the block range where it diverged. No protocol, consensus or storage format change. nodetests: 132/132 pass; the node binary builds. Co-Authored-By: Claude Opus 5.5 --- csnode/include/csnode/multiwallets.hpp | 8 ++++ csnode/include/csnode/node.hpp | 4 ++ csnode/include/csnode/walletscache.hpp | 4 ++ .../csnode/walletscache_serializer.hpp | 5 +++ csnode/src/multiwallets.cpp | 45 ++++++++++++++++++- csnode/src/node.cpp | 16 +++++++ csnode/src/walletscache_serializer.cpp | 1 + 7 files changed, 81 insertions(+), 2 deletions(-) diff --git a/csnode/include/csnode/multiwallets.hpp b/csnode/include/csnode/multiwallets.hpp index 0eb3dc7c3..0719948de 100644 --- a/csnode/include/csnode/multiwallets.hpp +++ b/csnode/include/csnode/multiwallets.hpp @@ -9,6 +9,7 @@ #include #include +#include #include using namespace boost::multi_index; @@ -57,6 +58,10 @@ class MultiWallets { } csdb::Amount checkWallets(); + + // order-independent digest of every wallet's balance-relevant state (ECMH, see statedigest.hpp); + // computed in full on the first call, then kept up to date on every wallet update + MultisetHash::Digest stateDigest() const; void iterate(std::function func); protected: @@ -111,6 +116,9 @@ class MultiWallets { mutable std::mutex mutex_; Container indexes_; + + mutable MultisetHash digest_; + mutable bool digestReady_ = false; }; } diff --git a/csnode/include/csnode/node.hpp b/csnode/include/csnode/node.hpp index be20c8107..d26053670 100644 --- a/csnode/include/csnode/node.hpp +++ b/csnode/include/csnode/node.hpp @@ -385,6 +385,10 @@ public slots: // request current trusted nodes for block with specific sequence void sendBlockRequestToConfidants(cs::Sequence sequence); void processSpecialInfo(const csdb::Pool& pool); + // logs the wallet state digest every kStateDigestInterval blocks, comparable between nodes + void logStateDigest(const csdb::Pool& pool); + void logStateDigest(cs::Sequence sequence); + static constexpr cs::Sequence kStateDigestInterval = 1000; void checkConsensusSettings(cs::Sequence seq, std::string& msg); void validateBlock(const csdb::Pool& block, bool* shouldStop); diff --git a/csnode/include/csnode/walletscache.hpp b/csnode/include/csnode/walletscache.hpp index 16354ca0a..381ba1e78 100644 --- a/csnode/include/csnode/walletscache.hpp +++ b/csnode/include/csnode/walletscache.hpp @@ -1,6 +1,7 @@ #ifndef WALLETS_CACHE_HPP #define WALLETS_CACHE_HPP +#include #include #include #include @@ -56,6 +57,9 @@ class WalletsCache { #ifdef MONITOR_NODE uint64_t createTime_ = 0; #endif + // ECMH element of this wallet's state, kept by MultiWallets for the state digest (not persisted) + std::array stateElement_{}; + bool hasStateElement_ = false; }; struct TrustedData { diff --git a/csnode/include/csnode/walletscache_serializer.hpp b/csnode/include/csnode/walletscache_serializer.hpp index b06c3e8da..07d967e1d 100644 --- a/csnode/include/csnode/walletscache_serializer.hpp +++ b/csnode/include/csnode/walletscache_serializer.hpp @@ -1,5 +1,6 @@ #ifndef WALLETS_CACHE_SERIALIZER_HPP #define WALLETS_CACHE_SERIALIZER_HPP +#include #include #include @@ -161,6 +162,10 @@ class WalletsCache_Serializer { #ifdef MONITOR_NODE uint64_t createTime_; #endif + // keep the layout identical to WalletsCache::WalletData (wallets_ aliases its container); + // the state-digest element is rebuilt at start and never serialised + std::array stateElement_{}; + bool hasStateElement_ = false; }; #ifdef MONITOR_NODE class TrustedData { diff --git a/csnode/src/multiwallets.cpp b/csnode/src/multiwallets.cpp index 4ddcca564..a2d6f122e 100644 --- a/csnode/src/multiwallets.cpp +++ b/csnode/src/multiwallets.cpp @@ -69,12 +69,53 @@ void cs::MultiWallets::onWalletCacheUpdated(const cs::WalletsCache::WalletData& << ", delegated: " << data.delegated_.to_string(); } + InternalData stored = data; + stored.hasStateElement_ = false; + if (digestReady_) { + const auto state = cs::serializeWalletState(stored); + if (!state.empty()) { + stored.stateElement_ = cs::MultisetHash::toElement(state); + stored.hasStateElement_ = true; + } + } + if (auto iter = byKey.find(data.key_); iter != byKey.end()) { - byKey.replace(iter, data); + // the stored element, not the stored record: delegation maps are shared and already updated in place + if (digestReady_ && iter->hasStateElement_) { + digest_.remove(iter->stateElement_); + } + byKey.replace(iter, stored); } else { - indexes_.insert(data); + indexes_.insert(stored); } + + if (stored.hasStateElement_) { + digest_.add(stored.stateElement_); + } +} + +cs::MultisetHash::Digest cs::MultiWallets::stateDigest() const { + cs::Lock lock(mutex_); + + if (!digestReady_) { + digest_.reset(); + auto& byKey = indexes_.get(); + for (auto it = byKey.begin(); it != byKey.end(); ++it) { + const auto state = cs::serializeWalletState(*it); + const bool hasState = !state.empty(); + const auto element = hasState ? cs::MultisetHash::toElement(state) : cs::MultisetHash::Element{}; + if (hasState) { + digest_.add(element); + } + // the element is not a key of any index, so updating it in place keeps the container valid + const_cast(*it).stateElement_ = element; + const_cast(*it).hasStateElement_ = hasState; + } + digestReady_ = true; + } + + return digest_.digest(); } void cs::MultiWallets::iterate(std::function func) { diff --git a/csnode/src/node.cpp b/csnode/src/node.cpp index 00b01aa0e..6f1f5d7da 100644 --- a/csnode/src/node.cpp +++ b/csnode/src/node.cpp @@ -107,6 +107,7 @@ Node::Node(cs::config::Observer& observer) cs::Connector::connect(&blockChain_.tryToStoreBlockEvent, this, &Node::deepBlockValidation); cs::Connector::connect(&blockChain_.stopNode, this, &Node::stop); cs::Connector::connect(&blockChain_.storeBlockEvent, this, &Node::processSpecialInfo); + cs::Connector::connect(&blockChain_.storeBlockEvent, this, static_cast(&Node::logStateDigest)); cs::Connector::connect(&blockChain_.uncertainBlock, this, &Node::sendBlockRequestToConfidants); cs::Connector::connect(&blockChain_.orderNecessaryBlock, this, &Node::sendNecessaryBlockRequest); cs::Connector::connect(&stat_.accountInitiationRequest, this, &Node::accountInitiationRequest); @@ -248,6 +249,8 @@ bool Node::init() { } cslog() << "Blockchain is ready, contains " << WithDelimiters(stat_.totalTransactions()) << " transactions"; + // full digest once here, before taking part in the network; incremental from now on + logStateDigest(blockChain_.getLastSeq()); #ifdef NODE_API api_->run(); @@ -4300,6 +4303,19 @@ void Node::checkConsensusSettings(cs::Sequence seq, std::string& msg){ saveConsensusSettingsToChain(); } +void Node::logStateDigest(const csdb::Pool& pool) { + if (pool.sequence() % kStateDigestInterval == 0) { + logStateDigest(pool.sequence()); + } +} + +void Node::logStateDigest(cs::Sequence sequence) { + const auto& wallets = blockChain_.multiWallets(); + const auto digest = wallets.stateDigest(); + cslog() << "STATE DIGEST #" << sequence << " " << cs::Utils::byteStreamToHex(digest.data(), digest.size()) + << " wallets " << wallets.size(); +} + void Node::validateBlock(const csdb::Pool& block, bool* shouldStop) { // user-cancel is honoured via BlockChain::init's OpenCallback; *shouldStop here means DataIntegrityError if (!blockValidator_->validateBlock(block, diff --git a/csnode/src/walletscache_serializer.cpp b/csnode/src/walletscache_serializer.cpp index b4e0ff2fb..609d67659 100644 --- a/csnode/src/walletscache_serializer.cpp +++ b/csnode/src/walletscache_serializer.cpp @@ -24,6 +24,7 @@ void WalletsCache_Serializer::bind(WalletsCache& wCache) { smartPayableTransactions_ = reinterpret_cast(&wCache.smartPayableTransactions_); canceledSmarts_ = reinterpret_cast(&wCache.canceledSmarts_); + static_assert(sizeof(WalletData) == sizeof(WalletsCache::WalletData), "WalletData mirror must match WalletsCache::WalletData"); wallets_ = reinterpret_cast(&wCache.multiWallets_->indexes_); #ifdef MONITOR_NODE trusted_info_ = reinterpret_cast(&wCache.trusted_info_); From a4004d1e67d1d06bfd8e55ad7d52cb666b31711d Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 12:13:15 +0200 Subject: [PATCH 3/5] ci: run a private 7-node network and compare state digests Adds a nightly / on-demand / "testnet"-labelled workflow that builds the node and runs ci/testnet/run.sh on one runner: - ci/testnet/gen_keys.py writes NodePublic.txt / NodePrivate.txt in the format the node reads: Base58 of the ed25519 public key and of the 64-byte libsodium secret. Without them the node asks for keys interactively. The format was checked with `node --dumpkeys`. - run.sh creates one working dir per node: config.ini (127.0.0.1, its own port, APIs off, no NAT traversal, round_elapse_time 10 s), hosts.txt (the other nodes with their ids) and trusted.txt (all nodes as initial confidants). It then starts the nodes and follows "Last written sequence". - Around block 300 it stops one node and starts it again, exercising quick start from caches and the checkpoint chain check. - It passes when every node reaches block 1100 and all nodes print the same "STATE DIGEST" at every sequence they share (#1000 from all nodes is required). 7 nodes, because the bootstrap drops one node from an even set and needs 4 confidants: with 7 the network keeps going while one node restarts. The run stays well below round 10'000 (StartingDPOS), after which a private network with no stake cannot elect trusted nodes. The genesis funds go to an address nobody holds, so this first version runs without transactions. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/testnet.yml | 117 +++++++++++++++++++++++++++ ci/testnet/gen_keys.py | 32 ++++++++ ci/testnet/run.sh | 145 ++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 .github/workflows/testnet.yml create mode 100755 ci/testnet/gen_keys.py create mode 100755 ci/testnet/run.sh diff --git a/.github/workflows/testnet.yml b/.github/workflows/testnet.yml new file mode 100644 index 000000000..da1b563ef --- /dev/null +++ b/.github/workflows/testnet.yml @@ -0,0 +1,117 @@ +name: Testnet + +# A private network of 7 nodes on one runner, about 30 minutes: blocks are produced, a restarted +# node catches up, and every node reports the same STATE DIGEST. Too long for every push, so it runs +# nightly, on demand, and on pull requests labelled "testnet". +on: + workflow_dispatch: + schedule: + - cron: '17 2 * * *' + pull_request: + branches: [master] + types: [labeled, synchronize, opened, reopened] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + BOOST_VERSION: 1.83.0 + BOOST_SHA256: 6478edfe2f3305127cffe8caf73ea0176c53769f4bf1585be237eb30798c3b8e + BOOST_PREFIX: /home/runner/boost + CCACHE_DIR: /home/runner/.ccache + +jobs: + testnet: + if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'testnet') + name: Private network (7 nodes) + runs-on: ubuntu-24.04 + timeout-minutes: 120 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Checkout submodules + run: | + git submodule update --init -- cscrypto p2p \ + third-party/berkeleydb third-party/thrift third-party/thrift-interface-definitions \ + third-party/googletest third-party/lmdb third-party/lmdbxx third-party/nameof + git -C cscrypto submodule update --init + git -C p2p submodule update --init + + - name: Install build tools + run: | + sudo apt-get update -qq + sudo apt-get install -y -qq cmake ninja-build flex bison autoconf automake libtool pkg-config ccache python3-nacl python3-base58 + + - name: Restore Boost + id: cache-boost + uses: actions/cache/restore@v4 + with: + path: ${{ env.BOOST_PREFIX }} + key: boost-${{ env.BOOST_VERSION }}-static-rt-ubuntu-24.04-v1 + + - name: Build Boost + if: steps.cache-boost.outputs.cache-hit != 'true' + run: | + v=${BOOST_VERSION//./_} + curl -sSfL -o boost.tar.bz2 "https://archives.boost.io/release/${BOOST_VERSION}/source/boost_${v}.tar.bz2" + echo "${BOOST_SHA256} boost.tar.bz2" | sha256sum -c + 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 + ./b2 -j"$(nproc)" -d0 variant=release link=static runtime-link=static threading=multi \ + cxxflags=-fPIC --layout=tagged -sNO_BZIP2=1 -sNO_ZSTD=1 -sNO_LZMA=1 \ + install --prefix="${BOOST_PREFIX}" + + - name: Restore ccache + uses: actions/cache/restore@v4 + with: + path: ${{ env.CCACHE_DIR }} + key: ccache-ubuntu-24.04-${{ github.sha }} + restore-keys: ccache-ubuntu-24.04- + + # same configuration as build.yml, so the ccache entries are shared + - name: Build node + run: | + cmake -S . -B build -G Ninja \ + -DCMAKE_BUILD_TYPE=Release \ + -DENABLE_TESTS=ON \ + -DBoost_ROOT="${BOOST_PREFIX}" \ + -DBoost_NO_SYSTEM_PATHS=ON \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_FLAGS="-include cstdint -include cstddef" \ + -DWITH_HASKELL=OFF + ccache --max-size=2G + cmake --build build -j"$(nproc)" --target node + + - name: Run private network + timeout-minutes: 60 + env: + NODES: '7' + TARGET_SEQ: '1100' + TIMEOUT_MIN: '45' + WORK: ${{ github.workspace }}/testnet-run + run: ci/testnet/run.sh build/client/node + + - name: Resource usage + if: always() + run: | + free -m + df -h "${GITHUB_WORKSPACE}" + du -sh testnet-run 2>/dev/null || true + + - name: Upload node logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: testnet-logs-${{ github.run_id }} + path: | + testnet-run/n*/*.log + testnet-run/n*/config.ini + retention-days: 14 diff --git a/ci/testnet/gen_keys.py b/ci/testnet/gen_keys.py new file mode 100755 index 000000000..8cbc4986f --- /dev/null +++ b/ci/testnet/gen_keys.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Generate node key files for a private test network. + +Usage: gen_keys.py +Writes /n/NodePublic.txt and NodePrivate.txt for i = 1..count, in the format the node +reads: Base58 of the 32-byte ed25519 public key, and Base58 of the 64-byte libsodium secret key +(seed || public key). The node would otherwise ask for keys interactively. +""" +import os +import sys + +import base58 +import nacl.signing + + +def main(): + count, work = int(sys.argv[1]), sys.argv[2] + for i in range(1, count + 1): + node_dir = os.path.join(work, f"n{i}") + os.makedirs(node_dir, exist_ok=True) + key = nacl.signing.SigningKey.generate() + public = bytes(key.verify_key) + secret = bytes(key) + public + with open(os.path.join(node_dir, "NodePublic.txt"), "w") as f: + f.write(base58.b58encode(public).decode()) + with open(os.path.join(node_dir, "NodePrivate.txt"), "w") as f: + f.write(base58.b58encode(secret).decode()) + print(base58.b58encode(public).decode()) + + +if __name__ == "__main__": + main() diff --git a/ci/testnet/run.sh b/ci/testnet/run.sh new file mode 100755 index 000000000..3dc7ea444 --- /dev/null +++ b/ci/testnet/run.sh @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +# Run a private Credits network of N nodes on this machine and check that it works. +# +# Usage: run.sh +# Env: NODES (default 7, keep it odd: an even set of ready nodes drops one at bootstrap) +# TARGET_SEQ (default 1100: past the first STATE DIGEST at block 1000) +# TIMEOUT_MIN (default 45) RESTART_AT (default 300, 0 disables the restart test) +# WORK (default ./testnet-run) +# +# Checks: every node writes blocks up to TARGET_SEQ; a node stopped and restarted (quick start +# from its caches) catches up; all nodes report the same STATE DIGEST at every common sequence. +set -euo pipefail + +BIN=$(realpath "$1") +HERE=$(cd "$(dirname "$0")" && pwd) +NODES=${NODES:-7} +TARGET_SEQ=${TARGET_SEQ:-1100} +TIMEOUT_MIN=${TIMEOUT_MIN:-45} +RESTART_AT=${RESTART_AT:-300} +WORK=$(mkdir -p "${WORK:-testnet-run}" && cd "${WORK:-testnet-run}" && pwd) +BASE_PORT=6000 + +declare -a PIDS KEYS + +mapfile -t KEYS < <(python3 "$HERE/gen_keys.py" "$NODES" "$WORK") + +for i in $(seq 1 "$NODES"); do + dir="$WORK/n$i" + : > "$dir/hosts.txt" + : > "$dir/trusted.txt" + for j in $(seq 1 "$NODES"); do + echo "${KEYS[$((j - 1))]}" >> "$dir/trusted.txt" + [ "$j" -ne "$i" ] && echo "127.0.0.1:$((BASE_PORT + j)) ${KEYS[$((j - 1))]}" >> "$dir/hosts.txt" + done + cat > "$dir/config.ini" <> stdout.log 2>&1) & + PIDS[$i]=$! +} + +stop_all() { + for i in $(seq 1 "$NODES"); do + [ -n "${PIDS[$i]:-}" ] && kill -TERM "${PIDS[$i]}" 2>/dev/null || true + done + sleep 15 + for i in $(seq 1 "$NODES"); do + [ -n "${PIDS[$i]:-}" ] && kill -KILL "${PIDS[$i]}" 2>/dev/null || true + done +} +trap stop_all EXIT + +# last written sequence a node reported (WithDelimiters prints 1'234) +seq_of() { + grep -ah 'Last written sequence = ' "$WORK/n$1"/node.log "$WORK/n$1"/stdout.log 2>/dev/null \ + | tail -1 | sed -E "s/.*Last written sequence = ([0-9']+).*/\1/" | tr -d "'" || true +} + +for i in $(seq 1 "$NODES"); do start_node "$i"; done +echo "started $NODES nodes in $WORK" + +deadline=$(( $(date +%s) + TIMEOUT_MIN * 60 )) +restarted=0 +while :; do + sleep 20 + line="t=$(( TIMEOUT_MIN * 60 - (deadline - $(date +%s)) ))s" + min=-1 + for i in $(seq 1 "$NODES"); do + s=$(seq_of "$i"); s=${s:-0} + line+=" n$i=$s" + if ! kill -0 "${PIDS[$i]}" 2>/dev/null; then line+="(down)"; fi + if [ "$min" -lt 0 ] || [ "$s" -lt "$min" ]; then min=$s; fi + done + echo "$line" + + if [ "$RESTART_AT" -gt 0 ] && [ "$restarted" -eq 0 ] && [ "$min" -ge "$RESTART_AT" ]; then + echo "restart test: stopping n$NODES at sequence $min" + kill -TERM "${PIDS[$NODES]}" + wait "${PIDS[$NODES]}" 2>/dev/null || true + sleep 30 + start_node "$NODES" + restarted=1 + echo "restart test: n$NODES started again" + fi + + if [ "$min" -ge "$TARGET_SEQ" ]; then break; fi + if [ "$(date +%s)" -ge "$deadline" ]; then + echo "FAIL: not every node reached sequence $TARGET_SEQ within $TIMEOUT_MIN min" + exit 1 + fi +done + +# every node logs "STATE DIGEST # wallets "; at a sequence logged by several nodes the +# digests must be identical +python3 - "$WORK" "$NODES" <<'PY' +import collections, glob, re, sys +work, nodes = sys.argv[1], int(sys.argv[2]) +seen = collections.defaultdict(dict) +for i in range(1, nodes + 1): + for path in glob.glob(f"{work}/n{i}/*.log"): + for m in re.finditer(r"STATE DIGEST #(\d+) ([0-9a-fA-F]+) wallets (\d+)", open(path, errors="replace").read()): + seen[int(m.group(1))][i] = (m.group(2).lower(), int(m.group(3))) +ok = True +for seq in sorted(seen): + values = set(seen[seq].values()) + status = "OK" if len(values) == 1 else "MISMATCH" + if len(values) != 1: + ok = False + print(f"STATE DIGEST #{seq}: {status} ({len(seen[seq])} nodes) {sorted(values)[:3]}") +if 1000 not in seen or len(seen[1000]) < nodes: + print(f"FAIL: expected a STATE DIGEST #1000 from all {nodes} nodes, got {len(seen.get(1000, {}))}") + ok = False +sys.exit(0 if ok else 1) +PY +echo "PASS: $NODES nodes reached sequence $TARGET_SEQ with matching state digests" From 93d4916c31d07f96269e33ffc02c3865e5f96651 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 12:34:03 +0200 Subject: [PATCH 4/5] build: add a CREDITS_TESTNET option for private test networks A private network cannot move any funds today: the genesis block sends the whole supply to a hardcoded address (BlockChain::writeGenesisBlock), and the key allowed to sign special transactions and bootstrap tables is hardcoded too (PacketValidator's BlockChainKey). Nobody running a test network holds either key. With -DCREDITS_TESTNET=ON (OFF by default), and only in such builds: - CS_TESTNET_GENESIS_KEY, if set, replaces the genesis recipient (Base58 public key); the node logs a warning when it is used; - CS_TESTNET_STARTER_KEY, if set, replaces the starter key. The definition is applied only to the csnode and net targets. In a normal build the code is not compiled at all, so mainnet binaries are byte-for-byte unaffected and no configuration can change their genesis or starter key. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 3 +++ csnode/CMakeLists.txt | 4 ++++ csnode/src/blockchain.cpp | 8 ++++++++ net/CMakeLists.txt | 4 ++++ net/src/packetvalidator.cpp | 8 ++++++++ 5 files changed, 27 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 232af968f..8c8b2ede6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,9 @@ if (MSVC) endif(MSVC) option(ENABLE_TESTS "Enable tests" OFF) +# Test-network builds only: genesis and starter keys come from CS_TESTNET_GENESIS_KEY / +# CS_TESTNET_STARTER_KEY. Never enable for binaries meant for mainnet. +option(CREDITS_TESTNET "Build for a private test network" OFF) option(SKIP_SAMPLES "Skip samples" ON) option(WITH_BOOST_STATIC "" ON) option(BUILD_TESTING "" OFF) diff --git a/csnode/CMakeLists.txt b/csnode/CMakeLists.txt index c016cf100..373b3df8e 100644 --- a/csnode/CMakeLists.txt +++ b/csnode/CMakeLists.txt @@ -147,3 +147,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../lmdbxx ${CMAKE_CURRENT_SOURCE_DIR}/../client/config ${CMAKE_CURRENT_SOURCE_DIR}/../client/include) + +if(CREDITS_TESTNET) + target_compile_definitions(${PROJECT_NAME} PRIVATE CREDITS_TESTNET) +endif() diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index 19790f216..8ac52558b 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -374,6 +375,13 @@ void BlockChain::writeGenesisBlock() { csdb::Transaction transaction; std::string strAddr = "5B3YXqDTcWQFGAqEJQJP3Bg1ZK8FFtHtgCiFLT5VAxpe"; +#ifdef CREDITS_TESTNET + // test-network builds only: genesis funds go to a key the test network holds + if (const char* key = std::getenv("CS_TESTNET_GENESIS_KEY"); key != nullptr && *key != '\0') { + strAddr = key; + cswarning() << kLogPrefix << "TESTNET build: genesis funds go to " << strAddr; + } +#endif std::vector pub_key; DecodeBase58(strAddr, pub_key); diff --git a/net/CMakeLists.txt b/net/CMakeLists.txt index f129a7ffb..b9cd5c77d 100644 --- a/net/CMakeLists.txt +++ b/net/CMakeLists.txt @@ -28,3 +28,7 @@ suppress_boost_cmake_warnings() configure_msvc_flags() target_link_libraries(${PROJECT_NAME} csnode lib cscrypto p2p_network csconnector) + +if(CREDITS_TESTNET) + target_compile_definitions(${PROJECT_NAME} PRIVATE CREDITS_TESTNET) +endif() diff --git a/net/src/packetvalidator.cpp b/net/src/packetvalidator.cpp index fbaa4f88b..ee091e0f7 100644 --- a/net/src/packetvalidator.cpp +++ b/net/src/packetvalidator.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -18,6 +20,12 @@ std::string bcStr("81p93jgrHqA9L4Vkdut9ESSCV1XNoge7LXBW96cuA7sm"); struct BlockChainKey { BlockChainKey() { +#ifdef CREDITS_TESTNET + // test-network builds only: special transactions and bootstrap tables are signed by this key + if (const char* key = std::getenv("CS_TESTNET_STARTER_KEY"); key != nullptr && *key != '\0') { + bcStr = key; + } +#endif cs::Bytes keyBytes; DecodeBase58(bcStr, keyBytes); std::copy(keyBytes.begin(), keyBytes.end(), key_.begin()); From c76fdced3c7a6a5d457a5af5626e784a5bc18ddb Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 12:35:30 +0200 Subject: [PATCH 5/5] ci: fund the test network and keep transfers flowing With the CREDITS_TESTNET build the genesis funds go to a key the test network holds, so the network can now carry real state changes: - run.sh (FUND=1) generates a master key and passes it to the nodes as CS_TESTNET_GENESIS_KEY and CS_TESTNET_STARTER_KEY. It opens the Thrift API on node 1 only and, once the chain reaches FUND_AT, runs fund.py. - fund.py sends 60'000 CS from the genesis key to every node (above the 50'000 minimum stake), then LOAD_SECONDS of random 1-50 CS transfers between nodes, and prints the final balances. Transaction building and signing are adapted from BK's tools/tps_gen/tps_gen.py. - The run fails if funding or a transfer fails, or if by block 1000 the state digest still covers only the two genesis wallets. - Node logs are appended to, not truncated, so a restarted node keeps its earlier lines (its first STATE DIGEST was lost in the first run). The workflow builds with -DCREDITS_TESTNET=ON and installs thriftpy2. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/testnet.yml | 15 +- ci/testnet/__pycache__/fund.cpython-312.pyc | Bin 0 -> 9555 bytes ci/testnet/fund.py | 153 ++++++++++++++++++++ ci/testnet/run.sh | 40 ++++- 4 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 ci/testnet/__pycache__/fund.cpython-312.pyc create mode 100644 ci/testnet/fund.py diff --git a/.github/workflows/testnet.yml b/.github/workflows/testnet.yml index da1b563ef..c62db7c64 100644 --- a/.github/workflows/testnet.yml +++ b/.github/workflows/testnet.yml @@ -1,7 +1,8 @@ name: Testnet -# A private network of 7 nodes on one runner, about 30 minutes: blocks are produced, a restarted -# node catches up, and every node reports the same STATE DIGEST. Too long for every push, so it runs +# A private network of 7 nodes on one runner, about 30 minutes: the nodes are funded from the genesis +# key and exchange transfers, a restarted node catches up, and every node reports the same STATE +# DIGEST. Too long for every push, so it runs # nightly, on demand, and on pull requests labelled "testnet". on: workflow_dispatch: @@ -47,6 +48,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y -qq cmake ninja-build flex bison autoconf automake libtool pkg-config ccache python3-nacl python3-base58 + python3 -m pip install --user --quiet --break-system-packages thriftpy2 - name: Restore Boost id: cache-boost @@ -75,7 +77,8 @@ jobs: key: ccache-ubuntu-24.04-${{ github.sha }} restore-keys: ccache-ubuntu-24.04- - # same configuration as build.yml, so the ccache entries are shared + # same configuration as build.yml plus CREDITS_TESTNET (applied only to csnode and net), + # so most ccache entries are shared - name: Build node run: | cmake -S . -B build -G Ninja \ @@ -86,7 +89,8 @@ jobs: -DCMAKE_C_COMPILER_LAUNCHER=ccache \ -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ -DCMAKE_CXX_FLAGS="-include cstdint -include cstddef" \ - -DWITH_HASKELL=OFF + -DWITH_HASKELL=OFF \ + -DCREDITS_TESTNET=ON ccache --max-size=2G cmake --build build -j"$(nproc)" --target node @@ -96,6 +100,8 @@ jobs: NODES: '7' TARGET_SEQ: '1100' TIMEOUT_MIN: '45' + FUND: '1' + LOAD_SECONDS: '300' WORK: ${{ github.workspace }}/testnet-run run: ci/testnet/run.sh build/client/node @@ -113,5 +119,6 @@ jobs: name: testnet-logs-${{ github.run_id }} path: | testnet-run/n*/*.log + testnet-run/fund.log testnet-run/n*/config.ini retention-days: 14 diff --git a/ci/testnet/__pycache__/fund.cpython-312.pyc b/ci/testnet/__pycache__/fund.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e082fecbec7de824d86d8096681dfc3dea2754aa GIT binary patch literal 9555 zcmb_BYitwOnltv;9*Xj zrrQ;cij+j!yD@EdaaWaYDv+8=D>eO9R=Ync?H{{_CY`2IrCVv2{Y9bDYVV)j@0{_B zO&V_RmG&e)ulde*&iT&wJm=p%9)^H)3q ze4{>Gw}mSrm7|p;AzDMN#*#{9XlJQz)Sd$AH@=rjvGz8pw~TVM?k^a<@rpW2_>G!a zC)nZ7A<(yN>*i~)5TYxz-+(NYPg%!C*c$}rZ8#g01V(HKl6PFUt$KTO)Bg*8w~61g z4*zDsJK7|Wqs@ZvU1GEaW@`~DOloei5T^*C@?AoxGBAv?LN!2c6*dVq?~|t0Jq4ipoYs^-4^hV0nO>5XCsF#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x03w2<t`O3&}oWEadJ-Oe?wl;UGy+?sVd7YvZ>R{kQ!OtpugNZ~Y&Wav`Ar literal 0 HcmV?d00001 diff --git a/ci/testnet/fund.py b/ci/testnet/fund.py new file mode 100644 index 000000000..03ef83e88 --- /dev/null +++ b/ci/testnet/fund.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +"""Fund the nodes of a private test network and keep some transfers flowing. + +Usage: fund.py [--port 9090] [--amount 60000] [--load-seconds 0] + +The genesis funds of a CREDITS_TESTNET build go to the key given in CS_TESTNET_GENESIS_KEY; this +script holds its seed. It sends CS from it to every node (above the 50'000 minimum stake), +then, for --load-seconds, small random transfers between nodes, through the Thrift API of node 1. + +Transaction building and signing are adapted from BK's tools/tps_gen/tps_gen.py (akaitrade/node). +""" +import argparse +import math +import os +import random +import struct +import sys +import time + +import base58 +import nacl.signing +import thriftpy2 +from thriftpy2.protocol import TBinaryProtocolFactory +from thriftpy2.rpc import make_client +from thriftpy2.transport import TBufferedTransportFactory + +HERE = os.path.dirname(os.path.abspath(__file__)) +IDL = os.path.abspath(os.path.join(HERE, "..", "..", "third-party", "thrift-interface-definitions")) +api = thriftpy2.load(os.path.join(IDL, "api.thrift"), module_name="api_thrift", include_dirs=[IDL]) +general = thriftpy2.load(os.path.join(IDL, "general.thrift"), module_name="general_thrift", include_dirs=[IDL]) + +CURRENCY_CS = 1 +TX_TYPE_TRANSFER = 0 + + +def encode_max_fee(value): + """Mirror of csdb::AmountCommission(double) -> uint16 bit pattern.""" + if value <= 0: + return 0 + v = abs(value) + expf = math.log10(v) + expi = int(expf + 0.5) if expf >= 0 else int(expf - 0.5) + v /= 10 ** expi + if v >= 1.0: + v *= 0.1 + expi += 1 + exp = (expi + 18) & 0x1F + frac = int(round(v * 1024)) & 0x3FF + return (exp << 10) | frac + + +MAX_FEE = encode_max_fee(1.0) + + +class Account: + def __init__(self, seed): + self.sk = nacl.signing.SigningKey(seed) + self.pk = bytes(self.sk.verify_key) + self.next_inner_id = None + + +def pack_inner_id(inner_id): + return inner_id.to_bytes(6, "little") # source and target given as public keys, not wallet ids + + +def transfer(src, dst_pk, amount_int, amount_frac=0): + payload = b"".join([ + pack_inner_id(src.next_inner_id), + src.pk, + dst_pk, + struct.pack(" "$dir/hosts.txt" @@ -47,7 +60,7 @@ ip=127.0.0.1 port=$((BASE_PORT + i)) [api] -port=0 +port=$([ "$FUND" = "1" ] && [ "$i" -eq 1 ] && echo 9090 || echo 0) apiexec_port=0 ajax_port=0 diag_port=0 @@ -59,6 +72,7 @@ Filter="%Severity% >= info" Destination=TextFile FileName=node.log AutoFlush=true +Append=true Format="[%TimeStamp%] %Severity% %Message%" CFG done @@ -91,6 +105,7 @@ echo "started $NODES nodes in $WORK" deadline=$(( $(date +%s) + TIMEOUT_MIN * 60 )) restarted=0 +FUND_PID= while :; do sleep 20 line="t=$(( TIMEOUT_MIN * 60 - (deadline - $(date +%s)) ))s" @@ -113,6 +128,12 @@ while :; do echo "restart test: n$NODES started again" fi + if [ "$FUND" = "1" ] && [ -z "$FUND_PID" ] && [ "$min" -ge "$FUND_AT" ]; then + echo "funding nodes from the genesis key, then $LOAD_SECONDS s of transfers" + python3 "$HERE/fund.py" "$WORK" "$NODES" "$MASTER_SEED" --load-seconds "$LOAD_SECONDS" > "$WORK/fund.log" 2>&1 & + FUND_PID=$! + fi + if [ "$min" -ge "$TARGET_SEQ" ]; then break; fi if [ "$(date +%s)" -ge "$deadline" ]; then echo "FAIL: not every node reached sequence $TARGET_SEQ within $TIMEOUT_MIN min" @@ -120,11 +141,21 @@ while :; do fi done +if [ -n "$FUND_PID" ]; then + fund_status=0 + wait "$FUND_PID" || fund_status=$? + cat "$WORK/fund.log" + if [ "$fund_status" -ne 0 ]; then + echo "FAIL: funding or transfers failed" + exit 1 + fi +fi + # every node logs "STATE DIGEST # wallets "; at a sequence logged by several nodes the # digests must be identical -python3 - "$WORK" "$NODES" <<'PY' +python3 - "$WORK" "$NODES" "$FUND" <<'PY' import collections, glob, re, sys -work, nodes = sys.argv[1], int(sys.argv[2]) +work, nodes, funded = sys.argv[1], int(sys.argv[2]), sys.argv[3] == "1" seen = collections.defaultdict(dict) for i in range(1, nodes + 1): for path in glob.glob(f"{work}/n{i}/*.log"): @@ -140,6 +171,9 @@ for seq in sorted(seen): if 1000 not in seen or len(seen[1000]) < nodes: print(f"FAIL: expected a STATE DIGEST #1000 from all {nodes} nodes, got {len(seen.get(1000, {}))}") ok = False +if funded and 1000 in seen and max(w for _, w in seen[1000].values()) <= 2: + print("FAIL: funding did not change the wallet state by block 1000") + ok = False sys.exit(0 if ok else 1) PY echo "PASS: $NODES nodes reached sequence $TARGET_SEQ with matching state digests"