Skip to content

Commit 0a661a0

Browse files
author
Park Juhyung
committed
Implement runChains script
1 parent 19da8c1 commit 0a661a0

File tree

8 files changed

+255
-7
lines changed

8 files changed

+255
-7
lines changed

ibc.ts/.env.default

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
CHAIN_A_RPC_URL="http://localhost:8080"
1+
CHAIN_A_RPC_URL="http://localhost:18080"
22
CHAIN_A_NETWORK_ID="ac"
33
CHAIN_A_FAUCET_ADDRESS="accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9"
44
CHAIN_A_COUNTERPARTY_CLIENT_ID="BClient"
55
CHAIN_A_COUNTERPARTY_CONNECTION_ID="BConnection"
66
CHAIN_A_COUNTERPARTY_CHANNEL_ID="BChannel"
7-
CHAIN_B_RPC_URL="http://localhost:8081"
7+
CHAIN_B_RPC_URL="http://localhost:18081"
88
CHAIN_B_NETWORK_ID="bc"
99
CHAIN_B_FAUCET_ADDRESS="bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0"
1010
CHAIN_B_COUNTERPARTY_CLIENT_ID="AClient"

ibc.ts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ yarn-error.log
33
yarn.lock
44
build/
55
.env
6+
/chainA/db/
7+
/chainB/db/

ibc.ts/chainA/chainA.config.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[codechain]
2+
quiet = false
3+
base_path = "."
4+
5+
[mining]
6+
mem_pool_mem_limit = 512 # MB
7+
mem_pool_size = 524288
8+
self_nomination_enable =false
9+
mem_pool_fee_bump_shift = 3 # 12.5%
10+
allow_create_shard = false
11+
reseal_on_txs = "all"
12+
reseal_min_period = 4000
13+
no_reseal_timer = false
14+
allowed_past_gap = 30000
15+
allowed_future_gap = 5000
16+
17+
[network]
18+
disable = true
19+
20+
[rpc]
21+
disable = false
22+
interface = "127.0.0.1"
23+
port = 18080
24+
25+
[ipc]
26+
disable = true
27+
28+
[ws]
29+
disable = true
30+
31+
[snapshot]
32+
disable = true
33+
34+
[email_alarm]
35+
disable = true

ibc.ts/chainA/chainA.schem.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "Solo",
2+
"name": "Chain A",
33
"engine": {
44
"solo": {
55
"params": {
@@ -15,7 +15,7 @@
1515
"maxAssetSchemeMetadataSize": "0x0400",
1616
"maxTransferMetadataSize": "0x0100",
1717
"maxTextContentSize": "0x0200",
18-
"networkID": "tc",
18+
"networkID": "ac",
1919
"minPayCost": 10,
2020
"minSetRegularKeyCost": 10,
2121
"minCreateShardCost": 10,

ibc.ts/chainB/chainB.config.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[codechain]
2+
quiet = false
3+
base_path = "."
4+
5+
[mining]
6+
mem_pool_mem_limit = 512 # MB
7+
mem_pool_size = 524288
8+
self_nomination_enable =false
9+
mem_pool_fee_bump_shift = 3 # 12.5%
10+
allow_create_shard = false
11+
reseal_on_txs = "all"
12+
reseal_min_period = 4000
13+
no_reseal_timer = false
14+
allowed_past_gap = 30000
15+
allowed_future_gap = 5000
16+
17+
[network]
18+
disable = true
19+
20+
[rpc]
21+
disable = false
22+
interface = "127.0.0.1"
23+
port = 18081
24+
25+
[ipc]
26+
disable = true
27+
28+
[ws]
29+
disable = true
30+
31+
[snapshot]
32+
disable = true
33+
34+
[email_alarm]
35+
disable = true

ibc.ts/chainB/chainB.schem.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "Solo",
2+
"name": "Chain B",
33
"engine": {
44
"solo": {
55
"params": {
@@ -15,7 +15,7 @@
1515
"maxAssetSchemeMetadataSize": "0x0400",
1616
"maxTransferMetadataSize": "0x0100",
1717
"maxTextContentSize": "0x0200",
18-
"networkID": "tc",
18+
"networkID": "bc",
1919
"minPayCost": 10,
2020
"minSetRegularKeyCost": 10,
2121
"minCreateShardCost": 10,

ibc.ts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"fmt": "tslint -p . --fix && prettier 'src/**/*.{ts, json}' --write",
77
"lint": "tsc -p . --noEmit && tslint -p . && prettier 'src/**/*.{ts, json}' -l",
88
"scenario": "yarn build && node build/scenario/index.js",
9-
"relayer": "yarn build && node build/relayer/index.js"
9+
"relayer": "yarn build && node build/relayer/index.js",
10+
"runChains": "yarn build && node build/scenario/runChains.js"
1011
},
1112
"devDependencies": {
1213
"@types/debug": "^4.1.5",

ibc.ts/src/scenario/runChains.ts

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import { promises as fs } from "fs";
2+
import {
3+
spawn,
4+
ChildProcess,
5+
ChildProcessWithoutNullStreams
6+
} from "child_process";
7+
import * as readline from "readline";
8+
import Debug from "debug";
9+
import { SDK } from "codechain-sdk";
10+
import { delay } from "../common/util";
11+
import { H256, PlatformAddressValue } from "codechain-primitives/lib";
12+
13+
const debug = Debug("runChains");
14+
15+
async function main() {
16+
console.log("Build foundry");
17+
await buildFoundry();
18+
console.log("Reset DB");
19+
await resetDB();
20+
console.log("Run Chain A");
21+
await runChainA();
22+
console.log("Run Chain B");
23+
await runChainB();
24+
console.log("Check the chains");
25+
await checkChainAAndBAreRunning();
26+
console.log("Chains are running!");
27+
}
28+
29+
main().catch(console.error);
30+
31+
async function buildFoundry() {
32+
const cargoProcess = spawn("cargo", ["build"], {
33+
cwd: "../"
34+
});
35+
streamOutToDebug(cargoProcess);
36+
await waitForChildProcess(cargoProcess);
37+
}
38+
39+
async function resetDB() {
40+
debug("Remove chain A DB");
41+
await fs.rmdir("./chainA/db", {
42+
recursive: true
43+
});
44+
45+
debug("Remove chain B DB");
46+
await fs.rmdir("./chainB/db", {
47+
recursive: true
48+
});
49+
}
50+
51+
async function runChainA() {
52+
const foundryProcess = spawn(
53+
"../../target/debug/foundry",
54+
["-c", "./chainA.schem.json", "--config", "./chainA.config.toml"],
55+
{
56+
cwd: "./chainA"
57+
}
58+
);
59+
streamOutToDebug(foundryProcess);
60+
}
61+
62+
async function runChainB() {
63+
const foundryProcess = spawn(
64+
"../../target/debug/foundry",
65+
["-c", "./chainB.schem.json", "--config", "./chainB.config.toml"],
66+
{
67+
cwd: "./chainB"
68+
}
69+
);
70+
streamOutToDebug(foundryProcess);
71+
}
72+
73+
async function checkChainAAndBAreRunning() {
74+
// Wait for Foundry to listen on the port, three seconds is an arbitrary value.
75+
await delay(3000);
76+
77+
// FIXME: read values from config
78+
const sdkA = new SDK({
79+
server: "http://localhost:18080",
80+
networkId: "ac",
81+
keyStoreType: { type: "local", path: "./chainA/keystore.db" }
82+
});
83+
const sdkB = new SDK({
84+
server: "http://localhost:18081",
85+
networkId: "bc",
86+
keyStoreType: { type: "local", path: "./chainB/keystore.db" }
87+
});
88+
89+
debug("Send ping to A");
90+
await sdkA.rpc.node.ping();
91+
debug("Send ping to B");
92+
await sdkB.rpc.node.ping();
93+
94+
await sendPayTx({
95+
sdk: sdkA,
96+
from: "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9",
97+
chainName: "Chain A"
98+
});
99+
100+
await sendPayTx({
101+
sdk: sdkB,
102+
from: "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0",
103+
chainName: "Chain B"
104+
});
105+
}
106+
107+
async function sendPayTx({
108+
sdk,
109+
from,
110+
chainName
111+
}: {
112+
sdk: SDK;
113+
from: PlatformAddressValue;
114+
chainName: string;
115+
}) {
116+
const pay = sdk.core.createPayTransaction({
117+
recipient: from,
118+
quantity: 1000
119+
});
120+
const signedPay = await sdk.key.signTransaction(pay, {
121+
account: from,
122+
passphrase: "",
123+
fee: 1000,
124+
seq: 0
125+
});
126+
debug(`Send payTx to ${chainName}`);
127+
const txhash = await sdk.rpc.chain.sendSignedTransaction(signedPay);
128+
await waitForTx(sdk, txhash);
129+
}
130+
131+
async function waitForTx(sdk: SDK, txHash: H256) {
132+
const timeout = delay(10 * 1000).then(() => {
133+
throw new Error("Timeout");
134+
});
135+
const wait = (async () => {
136+
while (true) {
137+
debug(`wait tx: ${txHash.toString()}`);
138+
if (sdk.rpc.chain.containsTransaction(txHash)) {
139+
return;
140+
}
141+
await delay(500);
142+
}
143+
})();
144+
return Promise.race([timeout, wait]);
145+
}
146+
147+
function streamOutToDebug(childProcess: ChildProcessWithoutNullStreams) {
148+
const rlStdout = readline.createInterface({
149+
input: childProcess.stdout,
150+
terminal: false
151+
});
152+
rlStdout.on("line", line => debug(line));
153+
154+
const rlStdErr = readline.createInterface({
155+
input: childProcess.stderr,
156+
terminal: false
157+
});
158+
rlStdErr.on("line", line => debug(line));
159+
}
160+
161+
async function waitForChildProcess(childProcess: ChildProcess) {
162+
let killed = false;
163+
return new Promise((resolve, reject) => {
164+
childProcess.on("exit", () => {
165+
if (!killed) {
166+
resolve();
167+
killed = true;
168+
}
169+
});
170+
childProcess.on("error", error => {
171+
reject(error);
172+
killed = true;
173+
});
174+
});
175+
}

0 commit comments

Comments
 (0)