Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #573 from godwokenrises/add-debug-api
Browse files Browse the repository at this point in the history
Add debug RPCs
  • Loading branch information
RetricSu authored Nov 17, 2022
2 parents 9c5b3a1 + 275ebe8 commit f22daf4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
10 changes: 9 additions & 1 deletion docs/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ You can find most usage guidelines from Ethereum RPC docs like <https://eth.wiki
- gw_get_withdrawal
- gw_get_last_submitted_info
- gw_get_node_info
- gw_is_request_in_queue
- gw_get_pending_tx_hashes
- gw_debug_replay_transaction (should enable `Debug` RPC module in Godwoken)

#### Usage

Get details at [Godwoken Docs](https://github.com/nervosnetwork/godwoken/blob/develop/docs/RPC.md)
Get details at [Godwoken Docs](https://github.com/godwokenrises/godwoken/blob/develop/docs/RPC.md)

### poly (Polyjuice RPCs)

Expand All @@ -115,3 +118,8 @@ Get details at [Godwoken Docs](https://github.com/nervosnetwork/godwoken/blob/de
#### Usage

Get details at [Poly APIs doc](poly-apis.md)

### debug (Debug RPCs)

#### Methods
- debug_replayTransaction (should enable `Debug` RPC module in Godwoken)
2 changes: 1 addition & 1 deletion packages/api-server/src/filter-web3-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function parsePolyjuiceSystemLog(data: HexString): PolyjuiceSystemLog {
};
}

function parsePolyjuiceUserLog(data: HexString): PolyjuiceUserLog {
export function parsePolyjuiceUserLog(data: HexString): PolyjuiceUserLog {
const dataWithoutPrefix = data.slice(2);

let offset = 0;
Expand Down
69 changes: 69 additions & 0 deletions packages/api-server/src/methods/modules/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Hash } from "@ckb-lumos/base";
import { RPC } from "@ckb-lumos/toolkit";
import { LogItem } from "@godwoken-web3/godwoken";
import { envConfig } from "../../base/env-config";
import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant";
import { Store } from "../../cache/store";
import { ethTxHashToGwTxHash } from "../../cache/tx-hash";
import { Query } from "../../db";
import { parsePolyjuiceUserLog } from "../../filter-web3-tx";
import { POLYJUICE_USER_LOG_FLAG } from "../constant";
import { handleGwError } from "../gw-error";
import { middleware } from "../validator";

export class Debug {
private readonlyRpc: RPC;
private cacheStore: Store;
private query: Query;

constructor() {
this.readonlyRpc = new RPC(
envConfig.godwokenReadonlyJsonRpc || envConfig.godwokenJsonRpc
);
this.cacheStore = new Store(true, CACHE_EXPIRED_TIME_MILSECS);
this.query = new Query();

this.replayTransaction = middleware(this.replayTransaction.bind(this), 1);
}

async replayTransaction(args: any[]) {
const ethTxHash: Hash = args[0];
const gwTxHash: Hash | undefined = await ethTxHashToGwTxHash(
ethTxHash,
this.query,
this.cacheStore
);
if (gwTxHash == null) {
throw new Error(`gw tx hash not found by eth tx hash ${ethTxHash}`);
}
let result;
try {
result = await this.readonlyRpc.debug_replay_transaction(
gwTxHash,
...args.slice(1)
);
} catch (error) {
handleGwError(error);
}

if (result == null) {
return undefined;
}

const web3Logs = result.logs
.filter((log: LogItem) => log.service_flag === POLYJUICE_USER_LOG_FLAG)
.map((log: LogItem) => {
const info = parsePolyjuiceUserLog(log.data);
return {
address: info.address,
data: info.data,
topics: info.topics,
};
});

// Replace logs with web3 logs
result.logs = web3Logs;

return result;
}
}
13 changes: 13 additions & 0 deletions packages/api-server/src/methods/modules/gw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export class Gw {
this.get_pending_tx_hashes.bind(this),
0
);
this.debug_replay_transaction = middleware(
this.debug_replay_transaction.bind(this),
1
);
}

async ping(args: any[]) {
Expand Down Expand Up @@ -685,6 +689,15 @@ export class Gw {
handleGwError(error);
}
}

async debug_replay_transaction(args: []) {
try {
const result = await this.readonlyRpc.debug_replay_transaction(...args);
return result;
} catch (error) {
handleGwError(error);
}
}
}

function formatHexNumber(
Expand Down
3 changes: 2 additions & 1 deletion packages/api-server/src/methods/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { envConfig } from "../../base/env-config";

const enableList = ["Eth", "Web3", "Net", "Gw", "Poly"];
const enableList = ["Eth", "Web3", "Net", "Gw", "Poly", "Debug"];
if (envConfig.enableProfRpc === "true") {
enableList.push("Prof");
}
Expand All @@ -13,3 +13,4 @@ export * from "./net";
export * from "./gw";
export * from "./poly";
export * from "./prof";
export * from "./debug";

0 comments on commit f22daf4

Please sign in to comment.