Skip to content

Commit acf37a6

Browse files
committed
now chainspec is being read from zombienet folder, parametrized scripts and kill processes from old runs -- very useful when you run test locally
1 parent e49b719 commit acf37a6

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

examples/authorize_and_store_papi_smoldot.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
22
import * as smoldot from 'smoldot';
3-
import { ApiPromise, WsProvider } from "@polkadot/api";
3+
import { readFileSync } from 'fs';
44
import { createClient } from 'polkadot-api';
55
import { getSmProvider } from 'polkadot-api/sm-provider';
66
import { cryptoWaitReady } from '@polkadot/util-crypto';
@@ -10,22 +10,14 @@ import { cidFromBytes } from "./cid_dag_metadata.js";
1010
import { bulletin } from './.papi/descriptors/dist/index.mjs';
1111

1212
// Constants
13-
const BOB_NODE_WS = 'ws://localhost:12346';
1413
const SYNC_WAIT_SEC = 15;
1514
const SMOLDOT_LOG_LEVEL = 3; // 0=off, 1=error, 2=warn, 3=info, 4=debug, 5=trace
1615
const HTTP_IPFS_API = 'http://127.0.0.1:8080' // Local IPFS HTTP gateway
1716

18-
async function fetchChainSpec(nodeWs) {
19-
console.log('Fetching chainspec from node...');
20-
const provider = new WsProvider(nodeWs);
21-
const api = await ApiPromise.create({ provider });
22-
await api.isReady;
23-
24-
const chainSpec = (await api.rpc.syncstate.genSyncSpec(true)).toString();
25-
const chainSpecObj = JSON.parse(chainSpec);
26-
chainSpecObj.protocolId = null; // Allow smoldot to sync with local chain
27-
28-
await api.disconnect();
17+
function readChainSpec(chainspecPath) {
18+
const chainSpecContent = readFileSync(chainspecPath, 'utf8');
19+
const chainSpecObj = JSON.parse(chainSpecContent);
20+
chainSpecObj.protocolId = null;
2921
return JSON.stringify(chainSpecObj);
3022
}
3123

@@ -41,8 +33,8 @@ function initSmoldot() {
4133
return sd;
4234
}
4335

44-
async function createSmoldotClient() {
45-
const chainSpec = await fetchChainSpec(BOB_NODE_WS);
36+
async function createSmoldotClient(chainspecPath) {
37+
const chainSpec = readChainSpec(chainspecPath);
4638
const sd = initSmoldot();
4739
const chain = await sd.addChain({ chainSpec });
4840
const client = createClient(getSmProvider(chain));
@@ -53,10 +45,18 @@ async function createSmoldotClient() {
5345
async function main() {
5446
await cryptoWaitReady();
5547

48+
// Get chainspec path from command line argument
49+
const chainspecPath = process.argv[2];
50+
if (!chainspecPath) {
51+
console.error('❌ Error: Chainspec path is required as first argument');
52+
console.error('Usage: node authorize_and_store_papi_smoldot.js <chainspec-path>');
53+
process.exit(1);
54+
}
55+
5656
let sd, client, resultCode;
5757
try {
5858
// Init Smoldot PAPI client and typed api.
59-
({ client, sd } = await createSmoldotClient());
59+
({ client, sd } = await createSmoldotClient(chainspecPath));
6060
console.log(`⏭️ Waiting ${SYNC_WAIT_SEC} seconds for smoldot to sync...`);
6161
// TODO: check better way, when smoldot is synced, maybe some RPC/runtime api that checks best vs finalized block?
6262
await new Promise(resolve => setTimeout(resolve, SYNC_WAIT_SEC * 1000));

examples/justfile

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# PID file locations
66
PID_DIR := "/tmp/bulletin-pids"
7+
ZOMBIENET_DIR := "/tmp/bulletin-tests-run"
78

89
# Default recipe - run the complete PAPI workflow test
910
default: (run-authorize-and-store "ws" "bulletin-westend-runtime")
@@ -58,9 +59,7 @@ setup-westend-prerequisites:
5859
cp target/release/polkadot $BIN_DIR/
5960
cp target/release/polkadot-prepare-worker $BIN_DIR/
6061
cp target/release/polkadot-execute-worker $BIN_DIR/
61-
62-
# Verify polkadot version
63-
$BIN_DIR/polkadot --version
62+
echo " ✓ Polkadot binaries copied"
6463

6564
# Build polkadot-parachain binary
6665
echo " Building polkadot-parachain binary (this may take a while)..."
@@ -70,9 +69,7 @@ setup-westend-prerequisites:
7069
echo " Copying polkadot-parachain binary..."
7170
ls -la target/release/polkadot-parachain
7271
cp target/release/polkadot-parachain $BIN_DIR/
73-
74-
# Verify polkadot-parachain version
75-
$BIN_DIR/polkadot-parachain --version
72+
echo " ✓ Polkadot-parachain binary copied"
7673

7774
# Build and install chain-spec-builder
7875
echo " Building chain-spec-builder..."
@@ -82,9 +79,7 @@ setup-westend-prerequisites:
8279
echo " Copying chain-spec-builder binary..."
8380
ls -la target/release/chain-spec-builder
8481
cp target/release/chain-spec-builder $BIN_DIR/
85-
86-
# Verify chain-spec-builder
87-
$BIN_DIR/chain-spec-builder --version
82+
echo " ✓ Chain-spec-builder binary copied"
8883

8984
# Add BIN_DIR to PATH for subsequent scripts
9085
export PATH="$BIN_DIR:$PATH"
@@ -117,6 +112,23 @@ bulletin-solo-zombienet-start runtime="bulletin-polkadot-runtime":
117112

118113
mkdir -p {{ PID_DIR }}
119114

115+
# Kill any existing zombienet processes to prevent conflicts
116+
echo "🔍 Checking for existing zombienet processes..."
117+
ZOMBIE_PIDS=$(pgrep -f "zombienet.*spawn" || true)
118+
if [ -n "$ZOMBIE_PIDS" ]; then
119+
echo " Found existing zombienet processes: $ZOMBIE_PIDS"
120+
echo " Killing existing zombienet processes..."
121+
pkill -9 -f "zombienet.*spawn" || true
122+
# Also kill any polkadot/polkadot-parachain processes from previous runs
123+
pkill -9 -f "polkadot.*--chain.*bulletin" || true
124+
pkill -9 -f "polkadot-parachain.*--chain.*bulletin" || true
125+
pkill -9 -f "polkadot-bulletin-chain" || true
126+
echo " ✓ Cleaned up existing processes"
127+
fi
128+
129+
# Clean up stale PID file
130+
[ -f "{{ PID_DIR }}/zombienet.pid" ] && rm "{{ PID_DIR }}/zombienet.pid"
131+
120132
echo "⚡ Starting Bulletin chain with zombienet (runtime: {{ runtime }})..."
121133

122134
# Find zombienet binary (use env var if set, otherwise search)
@@ -168,7 +180,7 @@ bulletin-solo-zombienet-start runtime="bulletin-polkadot-runtime":
168180
cd ..
169181
POLKADOT_BINARY_PATH=$POLKADOT_BINARY_PATH \
170182
POLKADOT_PARACHAIN_BINARY_PATH=$POLKADOT_PARACHAIN_BINARY_PATH \
171-
$ZOMBIENET_BIN -p native spawn $ZOMBIENET_CONFIG > /tmp/zombienet.log 2>&1 &
183+
$ZOMBIENET_BIN -p native spawn --dir {{ ZOMBIENET_DIR }} $ZOMBIENET_CONFIG > /tmp/zombienet.log 2>&1 &
172184
cd examples
173185
else # bulletin-polkadot-runtime
174186
echo " Setting up Polkadot runtime..."
@@ -177,7 +189,7 @@ bulletin-solo-zombienet-start runtime="bulletin-polkadot-runtime":
177189
BULLETIN_BINARY=$(cd .. && pwd)/target/release/polkadot-bulletin-chain
178190
ZOMBIENET_CONFIG=$(cd .. && pwd)/zombienet/bulletin-polkadot-local.toml
179191
cd ..
180-
POLKADOT_BULLETIN_BINARY_PATH=$BULLETIN_BINARY $ZOMBIENET_BIN -p native spawn $ZOMBIENET_CONFIG > /tmp/zombienet.log 2>&1 &
192+
POLKADOT_BULLETIN_BINARY_PATH=$BULLETIN_BINARY $ZOMBIENET_BIN -p native spawn --dir {{ ZOMBIENET_DIR }} $ZOMBIENET_CONFIG > /tmp/zombienet.log 2>&1 &
181193
cd examples
182194
fi
183195

@@ -387,7 +399,19 @@ run-authorize-and-store mode="ws" runtime="bulletin-polkadot-runtime": npm-insta
387399
fi
388400

389401
just setup-services {{ runtime }}
390-
node $SCRIPT_NAME
402+
403+
# Run the script with chainspec_path parameter only for smoldot mode
404+
if [ "{{ mode }}" = "smoldot" ]; then
405+
# Set chainspec path based on runtime
406+
if [ "{{ runtime }}" = "bulletin-westend-runtime" ]; then
407+
CHAINSPEC_PATH="{{ ZOMBIENET_DIR }}/bob/cfg/westend-local.json"
408+
else # bulletin-polkadot-runtime
409+
CHAINSPEC_PATH="{{ ZOMBIENET_DIR }}/bob/cfg/bulletin-polkadot-local.json"
410+
fi
411+
node $SCRIPT_NAME "$CHAINSPEC_PATH"
412+
else
413+
node $SCRIPT_NAME
414+
fi
391415
EXAMPLE_EXIT=$?
392416

393417
echo ""

0 commit comments

Comments
 (0)