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

Commit

Permalink
Merge branch 'hot-fix' into cache-get_account_id_by_script_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Flouse committed Dec 3, 2021
2 parents 23ff941 + e9980bd commit e3ecb57
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 134 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SENTRY_ENVIRONMENT=<sentry environment, optional, default to `development`>,
NEW_RELIC_LICENSE_KEY=<new relic license key, optional>
CLUSTER_COUNT=<cluster count, optional, default to num of cpus>
REDIS_URL=redis://user:password@localhost:6379 <redis url, optional, default to localhost on port 6379>
PG_POOL_MAX=<pg pool max count, optional, default to 20>
EOF

$ yarn
Expand Down
1 change: 1 addition & 0 deletions packages/api-server/src/base/env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const envConfig = {
tronAccountLockHash: getOptional("TRON_ACCOUNT_LOCK_HASH"),
newRelicLicenseKey: getOptional("NEW_RELIC_LICENSE_KEY"),
redisUrl: getOptional("REDIS_URL"),
pgPoolMax: getOptional("PG_POOL_MAX"),
};

function getRequired(name: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-server/src/block-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class BlockEmitter {
private emitter: EventEmitter;

constructor() {
this.query = new Query(envConfig.databaseUrl);
this.query = new Query();
this.isRunning = false;
this.currentTip = -1n;
this.emitter = new EventEmitter();
Expand Down
15 changes: 10 additions & 5 deletions packages/api-server/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import Knex, { Knex as KnexType } from "knex";
import { LogQueryOption } from "./types";
import { FilterTopic } from "../cache/types";
import { AccountsQuery } from "./accounts";
import { envConfig } from "../base/env-config";

const poolMax = envConfig.pgPoolMax || 20;
const GLOBAL_KNEX = Knex({
client: "postgresql",
connection: envConfig.databaseUrl,
pool: { min: 2, max: +poolMax },
});

export class Query {
private knex: KnexType;
private innerAccounts: AccountsQuery;

constructor(databaseUrl: string) {
this.knex = Knex({
client: "postgresql",
connection: databaseUrl,
});
constructor() {
this.knex = GLOBAL_KNEX;
this.innerAccounts = new AccountsQuery(this.knex);
}

Expand Down
53 changes: 53 additions & 0 deletions packages/api-server/src/methods/gw-error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// From https://github.com/ethereum/evmc/blob/v9.0.0/include/evmc/evmc.h#L212

import abiCoder, { AbiCoder } from "web3-eth-abi";
import { FailedReason } from "../base/types/api";
import { RpcError } from "./error";
import { GW_RPC_REQUEST_ERROR } from "./error-code";
import { LogItem, PolyjuiceSystemLog } from "./types";

export const evmcCodeTypeMapping: {
Expand Down Expand Up @@ -72,6 +75,56 @@ export function parseGwError(error: any): GwErrorDetail {
throw error;
}

export function parseGwRpcError(error: any): void {
const prefix = "JSONRPCError: server error ";
let message: string = error.message;
if (message.startsWith(prefix)) {
const jsonErr = message.slice(prefix.length);
const err = JSON.parse(jsonErr);

const last_log: LogItem | undefined = err.data?.last_log;
if (last_log != null) {
const polyjuiceSystemLog = parsePolyjuiceSystemLog(err.data.last_log);
const return_data = err.data.return_data;

let statusReason = "";
if (return_data !== "0x") {
const abi = abiCoder as unknown as AbiCoder;
statusReason = abi.decodeParameter(
"string",
return_data.substring(10)
) as unknown as string;
}

const failedReason: FailedReason = {
status_code: "0x" + polyjuiceSystemLog.statusCode.toString(16),
status_type:
evmcCodeTypeMapping[polyjuiceSystemLog.statusCode.toString()],
message: statusReason,
};
const data = { failed_reason: failedReason };
const newMessage = `${failedReason.status_type.toLowerCase()}: ${
failedReason.message
}`;
throw new RpcError(err.code, newMessage, data);
}

// can't find backend by script hash error
if (err.message?.startsWith("can't find backend for script_hash")) {
throw new RpcError(err.code, "to address is not a valid contract.");
}

throw new RpcError(err.code, err.message);
}

// connection error
if (message.startsWith("request to")) {
throw new Error(message);
}

throw new RpcError(GW_RPC_REQUEST_ERROR, error.message);
}

export function parsePolyjuiceSystemLog(logItem: LogItem): PolyjuiceSystemLog {
let buf = Buffer.from(logItem.data.slice(2), "hex");
if (buf.length !== 8 + 8 + 16 + 4 + 4) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-server/src/methods/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Eth {

constructor(ethWallet: boolean = false) {
this.ethWallet = ethWallet;
this.query = new Query(envConfig.databaseUrl);
this.query = new Query();
this.rpc = new GodwokenClient(envConfig.godwokenJsonRpc);
this.filterManager = new FilterManager(true);
this.filterManager.connect();
Expand Down
92 changes: 21 additions & 71 deletions packages/api-server/src/methods/modules/gw.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { RPC } from "ckb-js-toolkit";
import { RpcError } from "../error";
import { GW_RPC_REQUEST_ERROR } from "../error-code";
import { parseGwRpcError } from "../gw-error";
import { middleware } from "../validator";
import abiCoder, { AbiCoder } from "web3-eth-abi";
import { LogItem } from "../types";
import { evmcCodeTypeMapping, parsePolyjuiceSystemLog } from "../gw-error";
import { FailedReason } from "../../base/types/api";
import { Hash, HexNumber } from "@ckb-lumos/base";
import { HexU32 } from "@godwoken-web3/godwoken";

Expand Down Expand Up @@ -70,7 +65,7 @@ export class Gw {
const result = await this.rpc.gw_ping(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -79,7 +74,7 @@ export class Gw {
const result = await this.rpc.gw_get_tip_block_hash(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -95,7 +90,7 @@ export class Gw {
const result = await this.rpc.gw_get_block_hash(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -109,7 +104,7 @@ export class Gw {
const result = await this.rpc.gw_get_block(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -125,7 +120,7 @@ export class Gw {
const result = await this.rpc.gw_get_block_by_number(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -142,7 +137,7 @@ export class Gw {
const result = await this.rpc.gw_get_balance(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -159,7 +154,7 @@ export class Gw {
const result = await this.rpc.gw_get_storage_at(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -182,10 +177,10 @@ export class Gw {
console.debug(`update cache: ${scriptHash} -> ${result}`);
this.scriptHashToAccountIdcache.set(scriptHash, result);
}

return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -202,7 +197,7 @@ export class Gw {
const result = await this.rpc.gw_get_nonce(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -216,7 +211,7 @@ export class Gw {
const result = await this.rpc.gw_get_script(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -232,7 +227,7 @@ export class Gw {
const result = await this.rpc.gw_get_script_hash(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -248,7 +243,7 @@ export class Gw {
const result = await this.rpc.gw_get_data(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -262,7 +257,7 @@ export class Gw {
const result = await this.rpc.gw_get_transaction_receipt(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -276,7 +271,7 @@ export class Gw {
const result = await this.rpc.gw_get_transaction(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -290,7 +285,7 @@ export class Gw {
const result = await this.rpc.gw_execute_l2transaction(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -306,7 +301,7 @@ export class Gw {
const result = await this.rpc.gw_execute_raw_l2transaction(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -320,7 +315,7 @@ export class Gw {
const result = await this.rpc.gw_submit_l2transaction(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -334,7 +329,7 @@ export class Gw {
const result = await this.rpc.gw_submit_withdrawal_request(...args);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}

Expand All @@ -350,56 +345,11 @@ export class Gw {
);
return result;
} catch (error) {
parseError(error);
parseGwRpcError(error);
}
}
}

function parseError(error: any): void {
const prefix = "JSONRPCError: server error ";
let message: string = error.message;
if (message.startsWith(prefix)) {
const jsonErr = message.slice(prefix.length);
const err = JSON.parse(jsonErr);

const last_log: LogItem | undefined = err.data?.last_log;
if (last_log != null) {
const polyjuiceSystemLog = parsePolyjuiceSystemLog(err.data.last_log);
const return_data = err.data.return_data;

let statusReason = "";
if (return_data !== "0x") {
const abi = abiCoder as unknown as AbiCoder;
statusReason = abi.decodeParameter(
"string",
return_data.substring(10)
) as unknown as string;
}

const failedReason: FailedReason = {
status_code: "0x" + polyjuiceSystemLog.statusCode.toString(16),
status_type:
evmcCodeTypeMapping[polyjuiceSystemLog.statusCode.toString()],
message: statusReason,
};
const data = { failed_reason: failedReason };
const newMessage = `${failedReason.status_type.toLowerCase()}: ${
failedReason.message
}`;
throw new RpcError(err.code, newMessage, data);
}

throw new RpcError(err.code, err.message);
}

// connection error
if (message.startsWith("request to")) {
throw new Error(message);
}

throw new RpcError(GW_RPC_REQUEST_ERROR, error.message);
}

function formatHexNumber(
num: HexNumber | undefined | null
): HexNumber | undefined | null {
Expand Down
Loading

0 comments on commit e3ecb57

Please sign in to comment.