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
6 changes: 3 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- 8114:8114 # rpc
- 8115:8115 # p2p network
volumes:
- ./ckb:/var/lib/ckb
- ./devnet:/var/lib/ckb
command: [ "run", "-C", "/var/lib/ckb" ]

# TODO: update CKB version
Expand All @@ -19,15 +19,15 @@ services:
image: nervos/ckb:v0.113.1
user: root
volumes:
- ./ckb:/var/lib/ckb
- ./devnet:/var/lib/ckb
command: [ "miner", "-C", "/var/lib/ckb" ]

# "nervos/ckb" image have no http clients to do health check for CKB.
# This short-term service act as a workaround to do health check.
check-ckb-started-successfully:
image: curlimages/curl
volumes:
- ./ckb:/var/lib/ckb
- ./devnet:/var/lib/ckb
command: [ 'http://ckb:8114', '-H', 'content-type: application/json', '-d', '{ "id": 2, "jsonrpc": "2.0", "method": "get_tip_block_number", "params": [] }' ]
deploy:
restart_policy:
Expand Down
8 changes: 8 additions & 0 deletions src/cfg/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as path from "path";
const currentExecPath = process.cwd();
const packageRootPath = path.dirname(require.main!.filename);

// path
export const devnetSourcePath = path.resolve(packageRootPath, '../docker/devnet');
export const devnetPath = path.resolve(currentExecPath, `target/devnet`);

13 changes: 11 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Command } from "commander";
import { installDependency } from "./cmd/install";
import { genkey } from "./cmd/genkey";
import { listHashes } from "./cmd/list-hashes";
import { node } from "./cmd/node";
import { initChain } from "./cmd/init-chain";

const program = new Command();

program
.name("offckb")
.description(
"CLI to provide full ckb development environment for professionals",
"CLI to provide full ckb development environment for professionals"
)
.version("0.1.0");

Expand All @@ -19,13 +21,20 @@ program
.description("Install the ckb dependency binary")
.action(installDependency);

program.command("genkey").description("genereate 20 accounts").action(genkey);
program.command("genkey").description("generate 20 accounts").action(genkey);

program
.command("list-hashes")
.description("Use the CKB to list blockchain scripts hashes")
.action(listHashes);

program.command("node").description("Use the CKB to start devnet").action(node);

program
.command("init-chain")
.description("Use the CKB to init devnet")
.action(initChain);

// Parse command-line arguments
program.parse(process.argv);

Expand Down
38 changes: 38 additions & 0 deletions src/cmd/init-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as fs from "fs";
import { exec, execSync } from "child_process";
import { devnetPath, devnetSourcePath } from "../cfg/const";
import path from "path";

export function initChain() {
execSync(`cp -r ${devnetSourcePath} ${devnetPath}`);
console.log("copy devnet config folder");
copyAndEditMinerToml();

execSync(`rm -rf ${devnetPath}/data`);
}

function copyAndEditMinerToml() {
const minerToml = path.join(devnetSourcePath, "ckb-miner.toml");
const newMinerToml = path.join(devnetPath, "ckb-miner.toml");
// Read the content of the ckb-miner.toml file
fs.readFile(minerToml, "utf8", (err, data) => {
if (err) {
return console.error("Error reading file:", err);
}

// Replace the URL
const modifiedData = data.replace(
"http://ckb:8114/",
"http://localhost:8114"
);

// Write the modified content back to the file
fs.writeFile(newMinerToml, modifiedData, "utf8", (err) => {
if (err) {
return console.error("Error writing file:", err);
}
console.log("modified ", newMinerToml);
});
});
}

39 changes: 39 additions & 0 deletions src/cmd/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { exec } from "child_process";
import { devnetPath } from "../cfg/const";

export function node() {
const binPath = "target/ckb/ckb";
const ckbCmd = `${binPath} run -C ${devnetPath}`;
const minerCmd = `${binPath} miner -C ${devnetPath}`;
try {
// Run first command
const ckbProcess = exec(ckbCmd);
// Log first command's output
ckbProcess.stdout?.on("data", (data) => {
console.log("CKB output:", data.toString());
});

ckbProcess.stderr?.on("data", (data) => {
console.error("CKB error:", data.toString());
});

// Start the second command after 3 seconds
setTimeout(async () => {
try {
// Run second command
const minerProcess = exec(minerCmd);
minerProcess.stdout?.on("data", (data) => {
console.log("CKB-Miner:", data.toString());
});

minerProcess.stderr?.on("data", (data) => {
console.error("CKB-Miner error:", data.toString());
});
} catch (error) {
console.error("Error running CKB-Miner:", error);
}
}, 3000);
} catch (error) {
console.error("Error:", error);
}
}