Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(debugger): supported debug message #358

Merged
merged 3 commits into from
Jun 22, 2022
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
7 changes: 5 additions & 2 deletions packages/debugger/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import path from "path";
import * as fs from "fs";
import { CKB_DEBUGGER_VERSION } from "./constants";

export const DEFAULT_CKB_DEBUGGER_DIRECTORY_PATH = envPaths("ckb-debugger", {
suffix: CKB_DEBUGGER_VERSION,
}).cache;

export interface DownloadDebuggerOptions {
version?: string;
dir?: string;
Expand All @@ -16,8 +20,7 @@ export class CKBDebuggerDownloader {
constructor(options?: DownloadDebuggerOptions) {
const version = options?.version || CKB_DEBUGGER_VERSION;

const saveToPath =
options?.dir || envPaths("ckb-debugger", { suffix: version }).cache;
const saveToPath = options?.dir || DEFAULT_CKB_DEBUGGER_DIRECTORY_PATH;

this.config = { dir: saveToPath, version };
}
Expand Down
25 changes: 20 additions & 5 deletions packages/debugger/src/executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataLoader, Executor, ExecuteResult } from "./types";
import { DataLoader, ExecuteResult, Executor } from "./types";
import { TransactionSkeletonType } from "@ckb-lumos/helpers";
import { execSync } from "child_process";
import { spawnSync } from "child_process";
import { Hash } from "@ckb-lumos/base";
import * as fs from "fs";
import * as os from "os";
Expand Down Expand Up @@ -54,9 +54,24 @@ export class CKBDebugger implements Executor {
): Promise<ExecuteResult> {
const tmpTxPath = this.saveTmpTxFile(txSkeleton);

const buf = execSync(
`${this.debuggerPath} --mode full --tx-file ${tmpTxPath} --script-hash ${options.scriptHash} --script-group-type ${options.scriptGroupType}`
const buf = spawnSync(
this.debuggerPath,
[
"--tx-file",
tmpTxPath,
"--script-hash",
options.scriptHash,
"--script-group-type",
options.scriptGroupType,
],
{
env: { RUST_LOG: "debug" },
}
);

return parseDebuggerMessage(
buf.stdout.toString("utf-8"),
buf.stderr.toString("utf-8")
);
return parseDebuggerMessage(buf.toString("utf-8"));
}
}
11 changes: 8 additions & 3 deletions packages/debugger/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import { CellDep, HexString } from "@ckb-lumos/base";
import { bytify } from "@ckb-lumos/codec/lib/bytes";
import { OutPointVec } from "./codecs";

export function parseDebuggerMessage(message: string): ExecuteResult {
export function parseDebuggerMessage(
message: string,
debugMessage = ""
): ExecuteResult {
const codeMatch = message.match(/Run result: (-?\d+)/);
const cycleMatch = message.match(/Total cycles consumed: (\d+)/);

if (!codeMatch || !cycleMatch) {
throw new Error("Invalid debugger result: " + message);
throw new Error(
"Invalid debugger result: " + message + (debugMessage ? debugMessage : "")
);
}

const code = Number(codeMatch[1]);
const cycles = Number(cycleMatch[1]);

return { code, cycles, message };
return { code, cycles, message, debugMessage };
}

type ResolvedCellDep = { cell_dep: CellDep; data: HexString };
Expand Down
1 change: 1 addition & 0 deletions packages/debugger/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ExecuteResult {
code: number;
cycles: number;
message: string;
debugMessage: string;
}

export interface DataLoader {
Expand Down
70 changes: 69 additions & 1 deletion packages/debugger/tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const context = createTestContext({
path: path.join(__dirname, "deps/secp256k1_blake160"),
includes: [path.join(__dirname, "deps/secp256k1_data_info")],
},
// https://github.com/nervosnetwork/ckb/blob/develop/script/testdata/debugger.c
DEBUGGER: {
// the dep_type is defaults to "code"
// dep_type: "code",
path: path.join(__dirname, "deps/debugger"),
},
},
});

Expand Down Expand Up @@ -150,5 +156,67 @@ test("context#CKBDebugger with secp256k1 with correct signature", async (t) => {
t.true(result.cycles > 0);
});

test.todo("context#CKBDebugger with secp256k1 with wrong signature");
test("context#CKBDebugger with secp256k1 with wrong signature", async (t) => {
let txSkeleton = TransactionSkeleton({});
const pk = hexify(randomBytes(32));
const blake160 = privateKeyToBlake160(pk);

const secp256k1Lock = registry.newScript("SECP256K1_BLAKE160", blake160);

txSkeleton = txSkeleton.update("inputs", (inputs) =>
inputs.push({
out_point: mockOutPoint(),
...createCellWithMinimalCapacity({ lock: secp256k1Lock }),
})
);
txSkeleton.update("outputs", (outputs) =>
outputs.push(createCellWithMinimalCapacity({ lock: secp256k1Lock }))
);
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) =>
cellDeps.push(registry.newCellDep("SECP256K1_BLAKE160"))
);

txSkeleton = txSkeleton.update("witnesses", (witnesses) =>
witnesses.push(hexify(WitnessArgs.pack({ lock: "0x" + "00".repeat(65) })))
);
const signingGroup = createP2PKHMessageGroup(txSkeleton, [secp256k1Lock]);
const wrongPK = hexify(randomBytes(32));
const signedMessage = signRecoverable(signingGroup[0].message, wrongPK);

txSkeleton = txSkeleton.update("witnesses", (witnesses) =>
witnesses.set(0, hexify(WitnessArgs.pack({ lock: signedMessage })))
);

const result = await context.executor.execute(txSkeleton, {
scriptGroupType: "lock",
scriptHash: computeScriptHash(secp256k1Lock),
});

t.is(result.code, -31);
t.true(result.cycles > 0);
});

test("context#CKBDebugger with printf debug message", async (t) => {
let txSkeleton = TransactionSkeleton({});
const debugScript = registry.newScript("DEBUGGER", "0x");

txSkeleton = txSkeleton.update("inputs", (inputs) =>
inputs.push({
out_point: mockOutPoint(),
...createCellWithMinimalCapacity({ lock: debugScript }),
})
);
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) =>
cellDeps.push(registry.newCellDep("DEBUGGER"))
);

const result = await context.executor.execute(txSkeleton, {
scriptGroupType: "lock",
scriptHash: computeScriptHash(debugScript),
});

t.regex(result.debugMessage, /debugger print utf-8 string/);
t.is(result.code, 0);
});

test.todo("context#CKBDebugger with transfer sUDT");
Binary file added packages/debugger/tests/deps/debugger
Binary file not shown.