diff --git a/.github/workflows/testnet.yml b/.github/workflows/testnet.yml new file mode 100644 index 000000000..c62db7c64 --- /dev/null +++ b/.github/workflows/testnet.yml @@ -0,0 +1,124 @@ +name: Testnet + +# 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: + 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 + python3 -m pip install --user --quiet --break-system-packages thriftpy2 + + - 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 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 \ + -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 \ + -DCREDITS_TESTNET=ON + 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' + FUND: '1' + LOAD_SECONDS: '300' + 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/fund.log + testnet-run/n*/config.ini + retention-days: 14 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/ci/testnet/__pycache__/fund.cpython-312.pyc b/ci/testnet/__pycache__/fund.cpython-312.pyc new file mode 100644 index 000000000..e082fecbe Binary files /dev/null and b/ci/testnet/__pycache__/fund.cpython-312.pyc differ 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(" +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..c71740345 --- /dev/null +++ b/ci/testnet/run.sh @@ -0,0 +1,179 @@ +#!/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) +# FUND (default 0; needs a -DCREDITS_TESTNET=ON binary): genesis funds go to a generated master +# key, which funds every node through node 1's API (port 9090) once the chain reaches FUND_AT +# (default 30) and then keeps random transfers flowing for LOAD_SECONDS (default 300) +# +# 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} +FUND=${FUND:-0} +FUND_AT=${FUND_AT:-30} +LOAD_SECONDS=${LOAD_SECONDS:-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") + +if [ "$FUND" = "1" ]; then + read -r MASTER_SEED MASTER_PUB < <(python3 -c "import base58, nacl.signing; k = nacl.signing.SigningKey.generate(); print(base58.b58encode(bytes(k)).decode(), base58.b58encode(bytes(k.verify_key)).decode())") + # read by CREDITS_TESTNET builds only: genesis recipient and starter key of this network + export CS_TESTNET_GENESIS_KEY=$MASTER_PUB CS_TESTNET_STARTER_KEY=$MASTER_PUB + echo "testnet master key $MASTER_PUB" +fi + +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 +FUND_PID= +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 [ "$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" + exit 1 + 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" "$FUND" <<'PY' +import collections, glob, re, sys +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"): + 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 +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" diff --git a/csnode/CMakeLists.txt b/csnode/CMakeLists.txt index c100d398b..373b3df8e 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 @@ -145,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/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/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/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/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/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/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/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_); 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()); 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)); +}