From 19da8c15791b15539986ce369eb421278751b819 Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Fri, 28 Feb 2020 16:57:57 +0900 Subject: [PATCH 1/3] Create keys for chainA and chainB --- ibc.ts/.env.default | 8 ++--- ibc.ts/chainA/chainA.schem.json | 61 +++++++++++++++++++++++++++++++++ ibc.ts/chainA/keystore.db | 28 +++++++++++++++ ibc.ts/chainB/chainB.schem.json | 61 +++++++++++++++++++++++++++++++++ ibc.ts/chainB/keystore.db | 28 +++++++++++++++ 5 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 ibc.ts/chainA/chainA.schem.json create mode 100644 ibc.ts/chainA/keystore.db create mode 100644 ibc.ts/chainB/chainB.schem.json create mode 100644 ibc.ts/chainB/keystore.db diff --git a/ibc.ts/.env.default b/ibc.ts/.env.default index 05b9ceaa48..139aac3992 100644 --- a/ibc.ts/.env.default +++ b/ibc.ts/.env.default @@ -1,12 +1,12 @@ CHAIN_A_RPC_URL="http://localhost:8080" -CHAIN_A_NETWORK_ID="tc" -CHAIN_A_FAUCET_ADDRESS="tccqym6znlgc48qeelrzccehkcaut7yz39wwq96q3y7" +CHAIN_A_NETWORK_ID="ac" +CHAIN_A_FAUCET_ADDRESS="accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9" CHAIN_A_COUNTERPARTY_CLIENT_ID="BClient" CHAIN_A_COUNTERPARTY_CONNECTION_ID="BConnection" CHAIN_A_COUNTERPARTY_CHANNEL_ID="BChannel" CHAIN_B_RPC_URL="http://localhost:8081" -CHAIN_B_NETWORK_ID="fc" -CHAIN_B_FAUCET_ADDRESS="fccqyd6clszl2aeq4agrk8sgq8whkty6ktljuemc9y3" +CHAIN_B_NETWORK_ID="bc" +CHAIN_B_FAUCET_ADDRESS="bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0" CHAIN_B_COUNTERPARTY_CLIENT_ID="AClient" CHAIN_B_COUNTERPARTY_CONNECTION_ID="AConnection" CHAIN_B_COUNTERPARTY_CHANNEL_ID="AChannel" diff --git a/ibc.ts/chainA/chainA.schem.json b/ibc.ts/chainA/chainA.schem.json new file mode 100644 index 0000000000..0462461bcb --- /dev/null +++ b/ibc.ts/chainA/chainA.schem.json @@ -0,0 +1,61 @@ +{ + "name": "Solo", + "engine": { + "solo": { + "params": { + "hit": {}, + "genesisStakes": { + "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9": 100000 + } + } + } + }, + "params": { + "maxExtraDataSize": "0x20", + "maxAssetSchemeMetadataSize": "0x0400", + "maxTransferMetadataSize": "0x0100", + "maxTextContentSize": "0x0200", + "networkID": "tc", + "minPayCost": 10, + "minSetRegularKeyCost": 10, + "minCreateShardCost": 10, + "minSetShardOwnersCost": 10, + "minSetShardUsersCost": 10, + "minWrapCccCost": 10, + "minCustomCost": 10, + "minMintAssetCost": 10, + "minTransferAssetCost": 10, + "minChangeAssetSchemeCost": 10, + "minIncreaseAssetSupplyCost": 10, + "minComposeAssetCost": 10, + "minDecomposeAssetCost": 10, + "minUnwrapCccCost": 10, + "maxBodySize": 4194304, + "snapshotPeriod": 16384 + }, + "genesis": { + "seal": { + "generic": "0x0" + }, + "score": "0x20000", + "author": "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x" + }, + "accounts": { + "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9": { + "balance": "10000000000000000000", + "seq": "0" + } + }, + "shards": { + "0": { + "seq": 0, + "owners": [ + "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9" + ], + "users": [] + } + } +} diff --git a/ibc.ts/chainA/keystore.db b/ibc.ts/chainA/keystore.db new file mode 100644 index 0000000000..6f4feb0a6f --- /dev/null +++ b/ibc.ts/chainA/keystore.db @@ -0,0 +1,28 @@ +{ + "meta": "", + "platform": [ + { + "crypto": { + "ciphertext": "767072f50b72e9404a66239ccb6872f37d8fc9d339dee1e09d31ed764e9d6ab2", + "cipherparams": { + "iv": "6abcf74e12778a38f858e29610046060" + }, + "cipher": "aes-128-ctr", + "kdf": "pbkdf2", + "kdfparams": { + "dklen": 32, + "salt": "8dbe337903dce4cab897c375535fea1f268da5f0a8a5525b6d4505942b91c637", + "c": 262144, + "prf": "hmac-sha256" + }, + "mac": "747f4d8df3728a3a53c1f174287e31248ff2c8f3bc3118f213fafa9a00015e64" + }, + "id": "3698c735-25a8-4639-9c1d-fb794efea6b0", + "version": 3, + "address": "37e06e7424945c37f57d0dfd9b5d736298904ff2", + "meta": "{}" + } + ], + "asset": [], + "hdwseed": [] +} \ No newline at end of file diff --git a/ibc.ts/chainB/chainB.schem.json b/ibc.ts/chainB/chainB.schem.json new file mode 100644 index 0000000000..6ce77a3eb9 --- /dev/null +++ b/ibc.ts/chainB/chainB.schem.json @@ -0,0 +1,61 @@ +{ + "name": "Solo", + "engine": { + "solo": { + "params": { + "hit": {}, + "genesisStakes": { + "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0": 100000 + } + } + } + }, + "params": { + "maxExtraDataSize": "0x20", + "maxAssetSchemeMetadataSize": "0x0400", + "maxTransferMetadataSize": "0x0100", + "maxTextContentSize": "0x0200", + "networkID": "tc", + "minPayCost": 10, + "minSetRegularKeyCost": 10, + "minCreateShardCost": 10, + "minSetShardOwnersCost": 10, + "minSetShardUsersCost": 10, + "minWrapCccCost": 10, + "minCustomCost": 10, + "minMintAssetCost": 10, + "minTransferAssetCost": 10, + "minChangeAssetSchemeCost": 10, + "minIncreaseAssetSupplyCost": 10, + "minComposeAssetCost": 10, + "minDecomposeAssetCost": 10, + "minUnwrapCccCost": 10, + "maxBodySize": 4194304, + "snapshotPeriod": 16384 + }, + "genesis": { + "seal": { + "generic": "0x0" + }, + "score": "0x20000", + "author": "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0", + "timestamp": "0x00", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "extraData": "0x" + }, + "accounts": { + "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0": { + "balance": "10000000000000000000", + "seq": "0" + } + }, + "shards": { + "0": { + "seq": 0, + "owners": [ + "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0" + ], + "users": [] + } + } +} diff --git a/ibc.ts/chainB/keystore.db b/ibc.ts/chainB/keystore.db new file mode 100644 index 0000000000..87ff792fbc --- /dev/null +++ b/ibc.ts/chainB/keystore.db @@ -0,0 +1,28 @@ +{ + "meta": "", + "platform": [ + { + "crypto": { + "ciphertext": "4abf3dbf2e52387a4c575d125b3a8490f20497033791e2932b38186b27f69a24", + "cipherparams": { + "iv": "73ad8c88fbda0ddd7851e6070e2439e6" + }, + "cipher": "aes-128-ctr", + "kdf": "pbkdf2", + "kdfparams": { + "dklen": 32, + "salt": "07b24154ec7a80d025d72c38c796fc6170526092dbedfffc5509645c2e287196", + "c": 262144, + "prf": "hmac-sha256" + }, + "mac": "ce3f3d77e676f464958275d4a848613fbd7da83eade1f0a5061481a2c57d9a9f" + }, + "id": "685ddee6-166e-4be4-8ad7-6deed4ce7e2c", + "version": 3, + "address": "11270a47770382efcde1718ca6b2d595c8e472e3", + "meta": "{}" + } + ], + "asset": [], + "hdwseed": [] +} \ No newline at end of file From 0a661a0d1ffdfad5cf851ee7dc12495dc44fbf88 Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Fri, 28 Feb 2020 18:37:39 +0900 Subject: [PATCH 2/3] Implement runChains script --- ibc.ts/.env.default | 4 +- ibc.ts/.gitignore | 2 + ibc.ts/chainA/chainA.config.toml | 35 +++++++ ibc.ts/chainA/chainA.schem.json | 4 +- ibc.ts/chainB/chainB.config.toml | 35 +++++++ ibc.ts/chainB/chainB.schem.json | 4 +- ibc.ts/package.json | 3 +- ibc.ts/src/scenario/runChains.ts | 175 +++++++++++++++++++++++++++++++ 8 files changed, 255 insertions(+), 7 deletions(-) create mode 100644 ibc.ts/chainA/chainA.config.toml create mode 100644 ibc.ts/chainB/chainB.config.toml create mode 100644 ibc.ts/src/scenario/runChains.ts diff --git a/ibc.ts/.env.default b/ibc.ts/.env.default index 139aac3992..7b90570c51 100644 --- a/ibc.ts/.env.default +++ b/ibc.ts/.env.default @@ -1,10 +1,10 @@ -CHAIN_A_RPC_URL="http://localhost:8080" +CHAIN_A_RPC_URL="http://localhost:18080" CHAIN_A_NETWORK_ID="ac" CHAIN_A_FAUCET_ADDRESS="accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9" CHAIN_A_COUNTERPARTY_CLIENT_ID="BClient" CHAIN_A_COUNTERPARTY_CONNECTION_ID="BConnection" CHAIN_A_COUNTERPARTY_CHANNEL_ID="BChannel" -CHAIN_B_RPC_URL="http://localhost:8081" +CHAIN_B_RPC_URL="http://localhost:18081" CHAIN_B_NETWORK_ID="bc" CHAIN_B_FAUCET_ADDRESS="bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0" CHAIN_B_COUNTERPARTY_CLIENT_ID="AClient" diff --git a/ibc.ts/.gitignore b/ibc.ts/.gitignore index 5b04d32945..3d3ac1a77f 100644 --- a/ibc.ts/.gitignore +++ b/ibc.ts/.gitignore @@ -3,3 +3,5 @@ yarn-error.log yarn.lock build/ .env +/chainA/db/ +/chainB/db/ diff --git a/ibc.ts/chainA/chainA.config.toml b/ibc.ts/chainA/chainA.config.toml new file mode 100644 index 0000000000..c3bdd2e18f --- /dev/null +++ b/ibc.ts/chainA/chainA.config.toml @@ -0,0 +1,35 @@ +[codechain] +quiet = false +base_path = "." + +[mining] +mem_pool_mem_limit = 512 # MB +mem_pool_size = 524288 +self_nomination_enable =false +mem_pool_fee_bump_shift = 3 # 12.5% +allow_create_shard = false +reseal_on_txs = "all" +reseal_min_period = 4000 +no_reseal_timer = false +allowed_past_gap = 30000 +allowed_future_gap = 5000 + +[network] +disable = true + +[rpc] +disable = false +interface = "127.0.0.1" +port = 18080 + +[ipc] +disable = true + +[ws] +disable = true + +[snapshot] +disable = true + +[email_alarm] +disable = true diff --git a/ibc.ts/chainA/chainA.schem.json b/ibc.ts/chainA/chainA.schem.json index 0462461bcb..3d193e5dce 100644 --- a/ibc.ts/chainA/chainA.schem.json +++ b/ibc.ts/chainA/chainA.schem.json @@ -1,5 +1,5 @@ { - "name": "Solo", + "name": "Chain A", "engine": { "solo": { "params": { @@ -15,7 +15,7 @@ "maxAssetSchemeMetadataSize": "0x0400", "maxTransferMetadataSize": "0x0100", "maxTextContentSize": "0x0200", - "networkID": "tc", + "networkID": "ac", "minPayCost": 10, "minSetRegularKeyCost": 10, "minCreateShardCost": 10, diff --git a/ibc.ts/chainB/chainB.config.toml b/ibc.ts/chainB/chainB.config.toml new file mode 100644 index 0000000000..cf52a011af --- /dev/null +++ b/ibc.ts/chainB/chainB.config.toml @@ -0,0 +1,35 @@ +[codechain] +quiet = false +base_path = "." + +[mining] +mem_pool_mem_limit = 512 # MB +mem_pool_size = 524288 +self_nomination_enable =false +mem_pool_fee_bump_shift = 3 # 12.5% +allow_create_shard = false +reseal_on_txs = "all" +reseal_min_period = 4000 +no_reseal_timer = false +allowed_past_gap = 30000 +allowed_future_gap = 5000 + +[network] +disable = true + +[rpc] +disable = false +interface = "127.0.0.1" +port = 18081 + +[ipc] +disable = true + +[ws] +disable = true + +[snapshot] +disable = true + +[email_alarm] +disable = true diff --git a/ibc.ts/chainB/chainB.schem.json b/ibc.ts/chainB/chainB.schem.json index 6ce77a3eb9..ad0e865ecd 100644 --- a/ibc.ts/chainB/chainB.schem.json +++ b/ibc.ts/chainB/chainB.schem.json @@ -1,5 +1,5 @@ { - "name": "Solo", + "name": "Chain B", "engine": { "solo": { "params": { @@ -15,7 +15,7 @@ "maxAssetSchemeMetadataSize": "0x0400", "maxTransferMetadataSize": "0x0100", "maxTextContentSize": "0x0200", - "networkID": "tc", + "networkID": "bc", "minPayCost": 10, "minSetRegularKeyCost": 10, "minCreateShardCost": 10, diff --git a/ibc.ts/package.json b/ibc.ts/package.json index e7aa525942..ed1157d160 100644 --- a/ibc.ts/package.json +++ b/ibc.ts/package.json @@ -6,7 +6,8 @@ "fmt": "tslint -p . --fix && prettier 'src/**/*.{ts, json}' --write", "lint": "tsc -p . --noEmit && tslint -p . && prettier 'src/**/*.{ts, json}' -l", "scenario": "yarn build && node build/scenario/index.js", - "relayer": "yarn build && node build/relayer/index.js" + "relayer": "yarn build && node build/relayer/index.js", + "runChains": "yarn build && node build/scenario/runChains.js" }, "devDependencies": { "@types/debug": "^4.1.5", diff --git a/ibc.ts/src/scenario/runChains.ts b/ibc.ts/src/scenario/runChains.ts new file mode 100644 index 0000000000..a25578164f --- /dev/null +++ b/ibc.ts/src/scenario/runChains.ts @@ -0,0 +1,175 @@ +import { promises as fs } from "fs"; +import { + spawn, + ChildProcess, + ChildProcessWithoutNullStreams +} from "child_process"; +import * as readline from "readline"; +import Debug from "debug"; +import { SDK } from "codechain-sdk"; +import { delay } from "../common/util"; +import { H256, PlatformAddressValue } from "codechain-primitives/lib"; + +const debug = Debug("runChains"); + +async function main() { + console.log("Build foundry"); + await buildFoundry(); + console.log("Reset DB"); + await resetDB(); + console.log("Run Chain A"); + await runChainA(); + console.log("Run Chain B"); + await runChainB(); + console.log("Check the chains"); + await checkChainAAndBAreRunning(); + console.log("Chains are running!"); +} + +main().catch(console.error); + +async function buildFoundry() { + const cargoProcess = spawn("cargo", ["build"], { + cwd: "../" + }); + streamOutToDebug(cargoProcess); + await waitForChildProcess(cargoProcess); +} + +async function resetDB() { + debug("Remove chain A DB"); + await fs.rmdir("./chainA/db", { + recursive: true + }); + + debug("Remove chain B DB"); + await fs.rmdir("./chainB/db", { + recursive: true + }); +} + +async function runChainA() { + const foundryProcess = spawn( + "../../target/debug/foundry", + ["-c", "./chainA.schem.json", "--config", "./chainA.config.toml"], + { + cwd: "./chainA" + } + ); + streamOutToDebug(foundryProcess); +} + +async function runChainB() { + const foundryProcess = spawn( + "../../target/debug/foundry", + ["-c", "./chainB.schem.json", "--config", "./chainB.config.toml"], + { + cwd: "./chainB" + } + ); + streamOutToDebug(foundryProcess); +} + +async function checkChainAAndBAreRunning() { + // Wait for Foundry to listen on the port, three seconds is an arbitrary value. + await delay(3000); + + // FIXME: read values from config + const sdkA = new SDK({ + server: "http://localhost:18080", + networkId: "ac", + keyStoreType: { type: "local", path: "./chainA/keystore.db" } + }); + const sdkB = new SDK({ + server: "http://localhost:18081", + networkId: "bc", + keyStoreType: { type: "local", path: "./chainB/keystore.db" } + }); + + debug("Send ping to A"); + await sdkA.rpc.node.ping(); + debug("Send ping to B"); + await sdkB.rpc.node.ping(); + + await sendPayTx({ + sdk: sdkA, + from: "accqym7qmn5yj29cdl405xlmx6awd3f3yz07g7vq2c9", + chainName: "Chain A" + }); + + await sendPayTx({ + sdk: sdkB, + from: "bccqygjwzj8wupc9m7du9ccef4j6k2u3erjuv2w8pt0", + chainName: "Chain B" + }); +} + +async function sendPayTx({ + sdk, + from, + chainName +}: { + sdk: SDK; + from: PlatformAddressValue; + chainName: string; +}) { + const pay = sdk.core.createPayTransaction({ + recipient: from, + quantity: 1000 + }); + const signedPay = await sdk.key.signTransaction(pay, { + account: from, + passphrase: "", + fee: 1000, + seq: 0 + }); + debug(`Send payTx to ${chainName}`); + const txhash = await sdk.rpc.chain.sendSignedTransaction(signedPay); + await waitForTx(sdk, txhash); +} + +async function waitForTx(sdk: SDK, txHash: H256) { + const timeout = delay(10 * 1000).then(() => { + throw new Error("Timeout"); + }); + const wait = (async () => { + while (true) { + debug(`wait tx: ${txHash.toString()}`); + if (sdk.rpc.chain.containsTransaction(txHash)) { + return; + } + await delay(500); + } + })(); + return Promise.race([timeout, wait]); +} + +function streamOutToDebug(childProcess: ChildProcessWithoutNullStreams) { + const rlStdout = readline.createInterface({ + input: childProcess.stdout, + terminal: false + }); + rlStdout.on("line", line => debug(line)); + + const rlStdErr = readline.createInterface({ + input: childProcess.stderr, + terminal: false + }); + rlStdErr.on("line", line => debug(line)); +} + +async function waitForChildProcess(childProcess: ChildProcess) { + let killed = false; + return new Promise((resolve, reject) => { + childProcess.on("exit", () => { + if (!killed) { + resolve(); + killed = true; + } + }); + childProcess.on("error", error => { + reject(error); + killed = true; + }); + }); +} From 5d655d4bbea97ec98639928eae7edbf467bac62d Mon Sep 17 00:00:00 2001 From: Park Juhyung Date: Fri, 28 Feb 2020 19:45:40 +0900 Subject: [PATCH 3/3] Write a simple guide on how to run chains in the README.md file --- ibc.ts/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ibc.ts/README.md b/ibc.ts/README.md index a7e0834812..8d19ac5082 100644 --- a/ibc.ts/README.md +++ b/ibc.ts/README.md @@ -2,3 +2,20 @@ IBC relayer and scenario ========================= This directory contains IBC relayer implementation and IBC demo scenario script. + +## Before start + +1. Please run `yarn install`. It will install dependencies. +2. Please run `cp .env.default .env` + +## How to run chains + +Run `yarn run runChains` + +## Print debug log + +Please use `DEBUG` environment variable. +If you want to print all debug log, +please use "\*" for the `DEBUG` environment variable, like: `DEBUG="*"` + +For example you can run like this: `DEBUG="*" yarn run runChains`