Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions .github/workflows/testnet.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file added ci/testnet/__pycache__/fund.cpython-312.pyc
Binary file not shown.
153 changes: 153 additions & 0 deletions ci/testnet/fund.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env python3
"""Fund the nodes of a private test network and keep some transfers flowing.

Usage: fund.py <work_dir> <nodes> <master_seed_b58> [--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 <amount> 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("<iQ", amount_int, amount_frac),
struct.pack("<H", MAX_FEE),
struct.pack("<B", CURRENCY_CS),
struct.pack("<B", 0), # no user fields
])
t = api.Transaction()
t.id = src.next_inner_id
t.source = src.pk
t.target = dst_pk
t.amount = general.Amount(integral=amount_int, fraction=amount_frac)
t.balance = general.Amount(integral=0, fraction=0)
t.currency = CURRENCY_CS
t.signature = src.sk.sign(payload).signature
t.fee = api.AmountCommission(commission=MAX_FEE)
t.timeCreation = int(time.time() * 1000)
t.userFields = b""
t.type = TX_TYPE_TRANSFER
t.poolNumber = 0
return t


def send(client, src, dst_pk, amount_int, amount_frac=0):
if src.next_inner_id is None:
last = client.WalletTransactionsCountGet(src.pk).lastTransactionInnerId or 0
src.next_inner_id = int(last) + 1
result = client.TransactionFlow(transfer(src, dst_pk, amount_int, amount_frac))
ok = result.status.code == 0
if ok:
src.next_inner_id += 1
return ok, result.status.message


def node_account(work, i):
secret = base58.b58decode(open(os.path.join(work, f"n{i}", "NodePrivate.txt")).read().strip())
return Account(secret[:32])


def balance(client, pk):
b = client.WalletBalanceGet(pk).balance
return b.integral + b.fraction / 1e18


def main():
parser = argparse.ArgumentParser()
parser.add_argument("work")
parser.add_argument("nodes", type=int)
parser.add_argument("master_seed")
parser.add_argument("--port", type=int, default=9090)
parser.add_argument("--amount", type=int, default=60000)
parser.add_argument("--load-seconds", type=int, default=0)
args = parser.parse_args()

client = make_client(api.API, host="127.0.0.1", port=args.port, proto_factory=TBinaryProtocolFactory(),
trans_factory=TBufferedTransportFactory(), timeout=120000)
master = Account(base58.b58decode(args.master_seed))
nodes = [node_account(args.work, i) for i in range(1, args.nodes + 1)]
print(f"master balance before funding: {balance(client, master.pk):.4f}", flush=True)

failures = 0
for i, node in enumerate(nodes, 1):
ok, message = send(client, master, node.pk, args.amount)
print(f"fund n{i}: {'ok' if ok else 'FAILED ' + message}", flush=True)
failures += 0 if ok else 1

sent = 0
deadline = time.time() + args.load_seconds
while time.time() < deadline:
src, dst = random.sample(nodes, 2)
ok, message = send(client, src, dst.pk, random.randint(1, 50))
sent += 1 if ok else 0
if not ok:
print(f"load transfer failed: {message}", flush=True)
failures += 1
time.sleep(1)
print(f"load transfers sent: {sent}", flush=True)

for i, node in enumerate(nodes, 1):
print(f"balance n{i}: {balance(client, node.pk):.4f}", flush=True)
sys.exit(1 if failures else 0)


if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions ci/testnet/gen_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""Generate node key files for a private test network.

Usage: gen_keys.py <count> <work_dir>
Writes <work_dir>/n<i>/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()
Loading
Loading