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

Commit

Permalink
refactor: re-use parsePolyjuiceSystemLogs
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu committed Jan 3, 2023
1 parent f135e45 commit 1a6e463
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
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 @@ -263,7 +263,7 @@ export async function filterWeb3Transaction(
return undefined;
}

function parsePolyjuiceSystemLog(data: HexString): PolyjuiceSystemLog {
export function parsePolyjuiceSystemLog(data: HexString): PolyjuiceSystemLog {
// 2 + (8 + 8 + 20 + 4) * 2
if (data.length !== 82) {
throw new Error(`invalid system log raw data length: ${data.length}`);
Expand Down
34 changes: 6 additions & 28 deletions packages/api-server/src/methods/gw-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ import {
} from "./error";
import { HexNumber, HexString } from "@ckb-lumos/base";
import { logger } from "../base/logger";
import {
ErrorTxReceipt,
isErrorTxReceipt,
LogItem,
} from "@godwoken-web3/godwoken";
import { ErrorTxReceipt, isErrorTxReceipt } from "@godwoken-web3/godwoken";
import {
EVMC_EXIT_CODE_MAPPING,
exitCodeNumberToHex,
GODWOKEN_EXIT_CODE_MAPPING,
matchExitCode,
POLYJUICE_EXIT_CODE_MAPPING,
} from "./exit-code";
import { PolyjuiceSystemLog } from "./types";
import { INTERNAL_ERROR } from "./error-code";
import { parsePolyjuiceSystemLog } from "../filter-web3-tx";

const GODWOKEN_SERVER_ERROR_MESSAGE_PREFIX = "JSONRPCError: server error ";

Expand Down Expand Up @@ -105,8 +100,8 @@ export function handleErrorTxReceipt(errorTxReceipt: ErrorTxReceipt) {

// if logItem exits, try parse polyjuice/evmc exit code for error message
if (logItem) {
const polySystemLog = unpackPolyjuiceSystemLog(logItem);
const statusCode = polySystemLog.statusCode;
const polySystemLog = parsePolyjuiceSystemLog(logItem.data);
const statusCode: HexNumber = polySystemLog.statusCode;

// 1. parse evmc exit code
const evmcCode = matchExitCode(statusCode, EVMC_EXIT_CODE_MAPPING);
Expand All @@ -118,7 +113,7 @@ export function handleErrorTxReceipt(errorTxReceipt: ErrorTxReceipt) {
message = evmcCode.message;
}
extraMessage = evmcCode.message;
extraExitCode = exitCodeNumberToHex(statusCode);
extraExitCode = statusCode;
extraStack = ExtraStack.evmc;
}

Expand All @@ -127,7 +122,7 @@ export function handleErrorTxReceipt(errorTxReceipt: ErrorTxReceipt) {
if (polyCode != null) {
message = polyCode.message;
extraMessage = polyCode.message;
extraExitCode = exitCodeNumberToHex(statusCode);
extraExitCode = statusCode;
extraStack = ExtraStack.poly;
}

Expand Down Expand Up @@ -178,23 +173,6 @@ function parseGwExitCodeForErrorMessage(exitCode: HexNumber): {
};
}

function unpackPolyjuiceSystemLog(logItem: LogItem): PolyjuiceSystemLog {
let buf = Buffer.from(logItem.data.slice(2), "hex");
if (buf.length !== 8 + 8 + 16 + 4 + 4) {
throw new Error(`invalid system log raw data length: ${buf.length}`);
}
const gasUsed = buf.readBigUInt64LE(0);
const cumulativeGasUsed = buf.readBigUInt64LE(8);
const createdAddress = "0x" + buf.slice(16, 36).toString("hex");
const statusCode = buf.readUInt32LE(36);
return {
gasUsed: gasUsed,
cumulativeGasUsed: cumulativeGasUsed,
createdAddress: createdAddress,
statusCode: statusCode,
};
}

/**
* Resolves the abi-encoded panic reason or revert reason.
*
Expand Down

0 comments on commit 1a6e463

Please sign in to comment.