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
20 changes: 20 additions & 0 deletions .github/workflows/testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ci/testnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
Binary file removed ci/testnet/__pycache__/fund.cpython-312.pyc
Binary file not shown.
91 changes: 85 additions & 6 deletions ci/testnet/fund.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
"""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]
[--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 <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.

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
Expand All @@ -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):
Expand Down Expand Up @@ -63,15 +82,23 @@ 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("<BI", 1, len(managing)) + managing
elif delegated is not None:
fields_for_sig = struct.pack("<BQ", 1, delegated)
else:
fields_for_sig = struct.pack("<B", 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
fields_for_sig,
])
t = api.Transaction()
t.id = src.next_inner_id
Expand All @@ -83,17 +110,23 @@ def transfer(src, dst_pk, amount_int, amount_frac=0):
t.signature = src.sk.sign(payload).signature
t.fee = api.AmountCommission(commission=MAX_FEE)
t.timeCreation = int(time.time() * 1000)
t.userFields = b""
# API encoding (APIHandler): flag 0, count, then id u32, type u8, u32 size, bytes
if managing is not None:
t.userFields = struct.pack("<BBIBI", 0, 1, UF_MANAGING, UF_TYPE_STRING, len(managing)) + managing
elif delegated is not None:
t.userFields = struct.pack("<BBIBQ", 0, 1, UF_DELEGATED, UF_TYPE_INTEGER, delegated)
else:
t.userFields = b""
t.type = TX_TYPE_TRANSFER
t.poolNumber = 0
return t


def send(client, src, dst_pk, amount_int, amount_frac=0):
def send(client, src, dst_pk, amount_int, amount_frac=0, managing=None, delegated=None):
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))
result = client.TransactionFlow(transfer(src, dst_pk, amount_int, amount_frac, managing, delegated))
ok = result.status.code == 0
if ok:
src.next_inner_id += 1
Expand All @@ -118,6 +151,11 @@ def main():
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)
parser.add_argument("--dpos-at", type=int, default=0)
parser.add_argument("--reward-round", type=int, default=0)
parser.add_argument("--min-stake", type=int, default=0)
parser.add_argument("--delegations", action="store_true")
parser.add_argument("--delegation-seconds", type=int, default=240)
args = parser.parse_args()

client = make_client(api.API, host="127.0.0.1", port=args.port, proto_factory=TBinaryProtocolFactory(),
Expand All @@ -132,9 +170,50 @@ def main():
print(f"fund n{i}: {'ok' if ok else 'FAILED ' + message}", flush=True)
failures += 0 if ok else 1

# Node::processSpecialInfo: u16 order, then the order's fields (cs::IDataStream, little endian);
# each order is optional, without order 9 StartingDPOS stays at its default (10'000)
orders = []
if args.dpos_at:
orders.append(("order 9 (StartingDPOS = %d)" % args.dpos_at, struct.pack("<HQ", 9, args.dpos_at)))
if args.reward_round:
# staking+mining, reward 1.0, coeff 0
orders.append(("order 37 (rewards from round %d)" % args.reward_round,
struct.pack("<HBQiQiQ", 37, 3, args.reward_round, 1, 0, 0, 0)))
if args.min_stake:
orders.append(("order 22 (MinStakeValue = %d)" % args.min_stake, struct.pack("<HiQ", 22, args.min_stake, 0)))
if orders:
for name, cmd in orders:
ok, message = send(client, master, SPECIAL_TARGET, 0, managing=cmd)
print(f"{name}: {'ok' if ok else 'FAILED ' + message}", flush=True)
failures += 0 if ok else 1

def delegate(name, src, dst, amount, value):
ok, message = send(client, src, dst.pk, amount, delegated=value)
print(f"{name}: {'ok' if ok else 'FAILED ' + message}", flush=True)
return 0 if ok else 1

pairs = [(i, nodes[i - 1], nodes[i % len(nodes)]) for i in range(1, len(nodes) + 1)]
if args.delegations:
# a delegation is checked against the confirmed balance, so wait for the funding to land
wait_until = time.time() + 180
while time.time() < wait_until and any(balance(client, n.pk) < args.amount for n in nodes):
time.sleep(5)
expiry = int(time.time()) + args.delegation_seconds
for i, src, dst in pairs:
failures += delegate(f"n{i} delegates 1000 to n{i % len(nodes) + 1}", src, dst, 1000, DELEGATE)
failures += delegate(f"n{i} delegates 500 to n{i % len(nodes) + 1} until {expiry}",
src, dst, 500, expiry)

sent = 0
deadline = time.time() + args.load_seconds
start = time.time()
deadline = start + args.load_seconds
withdrawn = not args.delegations
while time.time() < deadline:
if not withdrawn and time.time() >= 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
Expand Down
25 changes: 24 additions & 1 deletion ci/testnet/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down Expand Up @@ -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

Expand All @@ -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 #<seq> <hex> wallets <n>"; at a sequence logged by several nodes the
# digests must be identical
python3 - "$WORK" "$NODES" "$FUND" <<'PY'
Expand Down
Loading