|
| 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