diff --git a/.github/workflows/testnet.yml b/.github/workflows/testnet.yml index 9601dcd69..fa8bbbda4 100644 --- a/.github/workflows/testnet.yml +++ b/.github/workflows/testnet.yml @@ -16,6 +16,21 @@ on: disk_full_node: description: 'Node whose block DB disk fills up for a while (0 = off)' default: '0' + restart_at: + description: 'Sequence at which the last node is restarted (0 = off)' + default: '300' + dpos_at: + description: 'Move StartingDPOS to this sequence via special order 9 (0 = off)' + default: '0' + reward_round: + description: 'Turn block rewards on at this round via special order 37 (0 = off)' + default: '0' + min_stake: + description: 'Set the minimum stake via special order 22, in CS (0 = off)' + default: '0' + delegate: + description: 'Nodes delegate to each other, with and without a time limit (0 = off)' + default: '0' schedule: - cron: '17 2 * * *' pull_request: @@ -111,6 +126,11 @@ jobs: TARGET_SEQ: ${{ inputs.target_seq || '1100' }} TIMEOUT_MIN: ${{ inputs.timeout_min || '45' }} DISK_FULL_NODE: ${{ inputs.disk_full_node || '0' }} + RESTART_AT: ${{ inputs.restart_at || '300' }} + DPOS_AT: ${{ inputs.dpos_at || '0' }} + REWARD_ROUND: ${{ inputs.reward_round || '0' }} + DELEGATE: ${{ inputs.delegate || '0' }} + MIN_STAKE: ${{ inputs.min_stake || '0' }} FUND: '1' LOAD_SECONDS: '300' WORK: ${{ github.workspace }}/testnet-run diff --git a/ci/testnet/.gitignore b/ci/testnet/.gitignore new file mode 100644 index 000000000..c18dd8d83 --- /dev/null +++ b/ci/testnet/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/ci/testnet/__pycache__/fund.cpython-312.pyc b/ci/testnet/__pycache__/fund.cpython-312.pyc deleted file mode 100644 index e082fecbe..000000000 Binary files a/ci/testnet/__pycache__/fund.cpython-312.pyc and /dev/null differ diff --git a/ci/testnet/fund.py b/ci/testnet/fund.py index 03ef83e88..d303dab42 100644 --- a/ci/testnet/fund.py +++ b/ci/testnet/fund.py @@ -2,11 +2,23 @@ """Fund the nodes of a private test network and keep some transfers flowing. Usage: fund.py [--port 9090] [--amount 60000] [--load-seconds 0] + [--dpos-at SEQ --reward-round SEQ] [--min-stake CS] [--delegations] 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. +With --dpos-at and/or --reward-round the master key, which is also the starter key of the test network +(CS_TESTNET_STARTER_KEY), sends two special "managing" transactions: order 9 moves StartingDPOS to +--dpos-at, and order 37 turns mining and staking on with a 1 CS block reward from --reward-round. +Trusted nodes then earn rewards split by stake, as on mainnet. --min-stake sends order 22, which +sets the minimum stake of a trusted node (keep it below the funded amount). + +With --delegations every node delegates to the next one twice: 1'000 CS without a time limit and +500 CS until --delegation-seconds from now (a timed delegation, which the nodes release on their +own once a block is past that time). Halfway through the load phase, the even nodes withdraw the +delegation without a time limit. + Transaction building and signing are adapted from BK's tools/tps_gen/tps_gen.py (akaitrade/node). """ import argparse @@ -31,6 +43,13 @@ CURRENCY_CS = 1 TX_TYPE_TRANSFER = 0 +UF_DELEGATED = 5 # cs::trx_uf::sp::delegated +UF_MANAGING = 7 # cs::trx_uf::sp::managing +UF_TYPE_INTEGER = 1 # csdb::UserField::Integer +UF_TYPE_STRING = 2 # csdb::UserField::String +DELEGATE = 1 # cs::trx_uf::sp::de::legate +DELEGATE_WITHDRAW = 2 # cs::trx_uf::sp::de::legated_withdraw (a value >= 4 is a UTC expiry time) +SPECIAL_TARGET = bytes(31) + b"\x07" # the target mainnet special transactions use (1111...118) def encode_max_fee(value): @@ -63,7 +82,15 @@ 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): +def transfer(src, dst_pk, amount_int, amount_frac=0, managing=None, delegated=None): + # signed bytes: Transaction::to_byte_stream_for_sig(); a string user field signs as u32 size + bytes, + # an integer one as u64 + if managing is not None: + fields_for_sig = struct.pack("= start + args.load_seconds / 2: + withdrawn = True + for i, src, dst in pairs[1::2]: + failures += delegate(f"n{i} withdraws 1000 from n{i % len(nodes) + 1}", + src, dst, 1000, DELEGATE_WITHDRAW) src, dst = random.sample(nodes, 2) ok, message = send(client, src, dst.pk, random.randint(1, 50)) sent += 1 if ok else 0 diff --git a/ci/testnet/run.sh b/ci/testnet/run.sh index 262cf5628..62f71612f 100755 --- a/ci/testnet/run.sh +++ b/ci/testnet/run.sh @@ -9,6 +9,12 @@ # 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) +# DPOS_AT / REWARD_ROUND (default 0 = off, needs FUND=1): the master key, which is also the +# starter key, moves StartingDPOS and turns block rewards on (special orders 9 and 37) +# MIN_STAKE (default 0 = off, needs FUND=1): special order 22 sets the minimum stake; with the +# restart test, the restarted node must apply it again after its quick start +# DELEGATE (default 0, needs FUND=1): nodes delegate to each other, with and without a time +# limit (DELEGATION_SECONDS, default 240), and some delegations are withdrawn later # DISK_FULL_NODE (default 0 = off): put that node's block DB on a DISK_TMPFS_MB (default 64) tmpfs, # fill it when the chain reaches DISK_FULL_AT (default 400) and free it DISK_FULL_SECONDS # (default 60) later; the node must recover and end with the same state digest (needs sudo) @@ -27,6 +33,11 @@ FUND=${FUND:-0} FUND_AT=${FUND_AT:-30} LOAD_SECONDS=${LOAD_SECONDS:-300} DISK_FULL_NODE=${DISK_FULL_NODE:-0} +DPOS_AT=${DPOS_AT:-0} +REWARD_ROUND=${REWARD_ROUND:-0} +DELEGATE=${DELEGATE:-0} +MIN_STAKE=${MIN_STAKE:-0} +DELEGATION_SECONDS=${DELEGATION_SECONDS:-240} DISK_FULL_AT=${DISK_FULL_AT:-400} DISK_FULL_SECONDS=${DISK_FULL_SECONDS:-60} DISK_TMPFS_MB=${DISK_TMPFS_MB:-64} @@ -160,7 +171,9 @@ while :; do 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 & + python3 "$HERE/fund.py" "$WORK" "$NODES" "$MASTER_SEED" --load-seconds "$LOAD_SECONDS" \ + --dpos-at "$DPOS_AT" --reward-round "$REWARD_ROUND" --delegation-seconds "$DELEGATION_SECONDS" --min-stake "$MIN_STAKE" \ + $([ "$DELEGATE" = "1" ] && echo --delegations) > "$WORK/fund.log" 2>&1 & FUND_PID=$! fi @@ -185,6 +198,16 @@ if [ "$DISK_FULL_NODE" -gt 0 ]; then echo "disk-full test: n$DISK_FULL_NODE logged $(grep -ah "Couldn't save block" "$WORK/n$DISK_FULL_NODE"/*.log | wc -l) failed block saves" fi +if [ "$MIN_STAKE" -gt 0 ] && [ "$RESTART_AT" -gt 0 ]; then + # order 22 lands well before the restart: once applied live, once more after the quick start + applied=$(grep -ah "MinStakeValue changed to" "$WORK/n$NODES"/*.log | wc -l) + echo "special orders: n$NODES applied order 22 $applied time(s)" + if [ "$applied" -lt 2 ]; then + echo "FAIL: n$NODES lost special order 22 across its quick start" + 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'