From 23ff941b38c50ad2af68f95e43d5a6456a3f6d7d Mon Sep 17 00:00:00 2001 From: Flouse Date: Fri, 3 Dec 2021 14:54:46 +0000 Subject: [PATCH 1/3] perf: cache gw_get_account_id_by_script_hash --- packages/api-server/src/methods/modules/gw.ts | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/api-server/src/methods/modules/gw.ts b/packages/api-server/src/methods/modules/gw.ts index 0b079f7c..583de9ea 100644 --- a/packages/api-server/src/methods/modules/gw.ts +++ b/packages/api-server/src/methods/modules/gw.ts @@ -6,12 +6,23 @@ import abiCoder, { AbiCoder } from "web3-eth-abi"; import { LogItem } from "../types"; import { evmcCodeTypeMapping, parsePolyjuiceSystemLog } from "../gw-error"; import { FailedReason } from "../../base/types/api"; -import { HexNumber } from "@ckb-lumos/base"; +import { Hash, HexNumber } from "@ckb-lumos/base"; +import { HexU32 } from "@godwoken-web3/godwoken"; + +// TODO: use Redis +// import { Store } from "../../cache/store"; +// import { envConfig } from "../../base/env-config"; +// import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant"; export class Gw { private rpc: RPC; + private scriptHashToAccountIdcache: Map; + constructor() { this.rpc = new RPC(process.env.GODWOKEN_JSON_RPC as string); + // this.cache = new Store(envConfig.redisUrl, true, CACHE_EXPIRED_TIME_MILSECS); + // this.cahce.init(); + this.scriptHashToAccountIdcache = new Map(); this.ping = middleware(this.ping.bind(this), 0); this.get_tip_block_hash = middleware(this.get_tip_block_hash.bind(this), 0); @@ -159,7 +170,19 @@ export class Gw { */ async get_account_id_by_script_hash(args: any[]) { try { - const result = await this.rpc.gw_get_account_id_by_script_hash(...args); + const scriptHash = args[0]; + let result = this.scriptHashToAccountIdcache.get(scriptHash); + if (result !== undefined) { + console.debug(`using cache: ${scriptHash} -> ${result}`); + return result; + } + + result = await this.rpc.gw_get_account_id_by_script_hash(...args); + if (result) { + console.debug(`update cache: ${scriptHash} -> ${result}`); + this.scriptHashToAccountIdcache.set(scriptHash, result); + } + return result; } catch (error) { parseError(error); From 80b8e3cd9a863d83d2d6dcbcc04c421cc024f55a Mon Sep 17 00:00:00 2001 From: Flouse Date: Fri, 3 Dec 2021 16:00:24 +0000 Subject: [PATCH 2/3] perf: use redis cache in gwRPC --- packages/api-server/src/cache/constant.ts | 3 ++ packages/api-server/src/methods/modules/gw.ts | 31 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/api-server/src/cache/constant.ts b/packages/api-server/src/cache/constant.ts index c33ace03..550a0682 100644 --- a/packages/api-server/src/cache/constant.ts +++ b/packages/api-server/src/cache/constant.ts @@ -2,3 +2,6 @@ export const CACHE_EXPIRED_TIME_MILSECS = 5 * 60 * 1000; // milsec, default 5 minutes // limit redis store filter size export const MAX_FILTER_TOPIC_ARRAY_LENGTH = 20; + +// The Cache Key Prfixs +export const GW_RPC_KEY = 'gwRPC'; \ No newline at end of file diff --git a/packages/api-server/src/methods/modules/gw.ts b/packages/api-server/src/methods/modules/gw.ts index 1986ae4b..493ab541 100644 --- a/packages/api-server/src/methods/modules/gw.ts +++ b/packages/api-server/src/methods/modules/gw.ts @@ -1,23 +1,23 @@ import { RPC } from "ckb-js-toolkit"; import { parseGwRpcError } from "../gw-error"; import { middleware } from "../validator"; -import { Hash, HexNumber } from "@ckb-lumos/base"; -import { HexU32 } from "@godwoken-web3/godwoken"; - -// TODO: use Redis -// import { Store } from "../../cache/store"; -// import { envConfig } from "../../base/env-config"; -// import { CACHE_EXPIRED_TIME_MILSECS } from "../../cache/constant"; +import { HexNumber } from "@ckb-lumos/base"; +import { Store } from "../../cache/store"; +import { envConfig } from "../../base/env-config"; +import { CACHE_EXPIRED_TIME_MILSECS, GW_RPC_KEY } from "../../cache/constant"; export class Gw { private rpc: RPC; - private scriptHashToAccountIdcache: Map; + private gwCache: Store; constructor() { this.rpc = new RPC(process.env.GODWOKEN_JSON_RPC as string); - // this.cache = new Store(envConfig.redisUrl, true, CACHE_EXPIRED_TIME_MILSECS); - // this.cahce.init(); - this.scriptHashToAccountIdcache = new Map(); + this.gwCache = new Store( + envConfig.redisUrl, + true, + CACHE_EXPIRED_TIME_MILSECS + ); + this.gwCache.init(); this.ping = middleware(this.ping.bind(this), 0); this.get_tip_block_hash = middleware(this.get_tip_block_hash.bind(this), 0); @@ -166,18 +166,17 @@ export class Gw { async get_account_id_by_script_hash(args: any[]) { try { const scriptHash = args[0]; - let result = this.scriptHashToAccountIdcache.get(scriptHash); - if (result !== undefined) { + let result = await this.gwCache.get(`${GW_RPC_KEY}_${scriptHash}`); + if (result != null) { console.debug(`using cache: ${scriptHash} -> ${result}`); return result; } result = await this.rpc.gw_get_account_id_by_script_hash(...args); - if (result) { + if (result != null) { console.debug(`update cache: ${scriptHash} -> ${result}`); - this.scriptHashToAccountIdcache.set(scriptHash, result); + this.gwCache.insert(`${GW_RPC_KEY}_${scriptHash}`, result); } - return result; } catch (error) { parseGwRpcError(error); From 3ab1b0304178dec8c9afc3205ffb04a2eed597b2 Mon Sep 17 00:00:00 2001 From: Flouse Date: Fri, 3 Dec 2021 16:16:11 +0000 Subject: [PATCH 3/3] yarn lint and fmt --- packages/api-server/src/cache/constant.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-server/src/cache/constant.ts b/packages/api-server/src/cache/constant.ts index 550a0682..a8145a53 100644 --- a/packages/api-server/src/cache/constant.ts +++ b/packages/api-server/src/cache/constant.ts @@ -4,4 +4,4 @@ export const CACHE_EXPIRED_TIME_MILSECS = 5 * 60 * 1000; // milsec, default 5 mi export const MAX_FILTER_TOPIC_ARRAY_LENGTH = 20; // The Cache Key Prfixs -export const GW_RPC_KEY = 'gwRPC'; \ No newline at end of file +export const GW_RPC_KEY = "gwRPC";