From febbf1ba34ee67f1284f0a5cb97958000796a301 Mon Sep 17 00:00:00 2001 From: Shunji Zhan Date: Tue, 12 Nov 2024 14:13:41 +0800 Subject: [PATCH] add option for cache capacity --- packages/eth-providers/src/rpc-provider.ts | 16 +++++++++++----- packages/eth-rpc-adapter/src/index.ts | 4 +++- packages/eth-rpc-adapter/src/utils/utils.ts | 7 +++++++ packages/eth-rpc-adapter/src/wrapped-provider.ts | 15 +++++++++------ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/eth-providers/src/rpc-provider.ts b/packages/eth-providers/src/rpc-provider.ts index 1d0b1c363..9d88e57c8 100644 --- a/packages/eth-providers/src/rpc-provider.ts +++ b/packages/eth-providers/src/rpc-provider.ts @@ -1,20 +1,26 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; -import { withAcalaTypes } from '@acala-network/api'; +import { acalaTypesBundle } from '@acala-network/types'; import { BaseProvider, BaseProviderOptions } from './base-provider'; +export type EvmRpcProviderOptions = BaseProviderOptions & { + rpcCacheCapacity?: number; +}; + export class EvmRpcProvider extends BaseProvider { - constructor(endpoint: string | string[], opts?: BaseProviderOptions) { + constructor(endpoint: string | string[], opts?: EvmRpcProviderOptions) { super(opts); - const api = new ApiPromise(withAcalaTypes({ + const api = new ApiPromise({ provider: new WsProvider(endpoint), - })); + typesBundle: acalaTypesBundle, + rpcCacheCapacity: opts?.rpcCacheCapacity, + }); this.setApi(api); } - static from(endpoint: string | string[], opt?: BaseProviderOptions): EvmRpcProvider { + static from(endpoint: string | string[], opt?: EvmRpcProviderOptions): EvmRpcProvider { return new EvmRpcProvider(endpoint, opt); } } diff --git a/packages/eth-rpc-adapter/src/index.ts b/packages/eth-rpc-adapter/src/index.ts index b7c283aeb..7e960dd60 100644 --- a/packages/eth-rpc-adapter/src/index.ts +++ b/packages/eth-rpc-adapter/src/index.ts @@ -18,6 +18,7 @@ export async function start(): Promise { subqlUrl: opts.subqlUrl, maxBlockCacheSize: opts.maxBlockCacheSize, storageCacheSize: opts.storageCacheSize, + rpcCacheCapacity: opts.rpcCacheCapacity, }); const bridge = new Eip1193Bridge(provider); @@ -58,8 +59,9 @@ export async function start(): Promise { server host : ${opts.host} server port : ${opts.port} max blockCache : ${opts.maxBlockCacheSize} - max batchSize : ${opts.maxBatchSize} + max batchSize : ${opts.maxBatchSize} max storageSize : ${opts.storageCacheSize} + cache capacity : ${opts.rpcCacheCapacity} safe mode : ${opts.safeMode} local mode : ${opts.localMode} http only : ${opts.httpOnly} diff --git a/packages/eth-rpc-adapter/src/utils/utils.ts b/packages/eth-rpc-adapter/src/utils/utils.ts index 62d4c29da..364d9f6bb 100644 --- a/packages/eth-rpc-adapter/src/utils/utils.ts +++ b/packages/eth-rpc-adapter/src/utils/utils.ts @@ -11,6 +11,7 @@ const { MAX_CACHE_SIZE, MAX_BATCH_SIZE, STORAGE_CACHE_SIZE, + RPC_CACHE_CAPACITY, SAFE_MODE, LOCAL_MODE, HTTP_ONLY, @@ -76,6 +77,12 @@ export const yargsOptions = yargs(hideBin(process.argv)) describe: 'max storage cache size', type: 'number', }, + rpcCacheCapacity: { + demandOption: false, + default: Number(RPC_CACHE_CAPACITY ?? 1000), + describe: 'polkadot api rpc cache capacity', + type: 'number', + }, safeMode: { alias: 's', demandOption: false, diff --git a/packages/eth-rpc-adapter/src/wrapped-provider.ts b/packages/eth-rpc-adapter/src/wrapped-provider.ts index a128f73e4..76ab8c104 100644 --- a/packages/eth-rpc-adapter/src/wrapped-provider.ts +++ b/packages/eth-rpc-adapter/src/wrapped-provider.ts @@ -1,6 +1,7 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; -import { BaseProvider, BaseProviderOptions } from '@acala-network/eth-providers/base-provider'; -import { withAcalaTypes } from '@acala-network/api'; +import { BaseProvider } from '@acala-network/eth-providers/base-provider'; +import { EvmRpcProviderOptions } from '@acala-network/eth-providers'; +import { acalaTypesBundle } from '@acala-network/types'; import tracer from 'dd-trace'; const TRACE_METHODS = [ @@ -78,17 +79,19 @@ export class BaseProviderWithTrace extends BaseProvider { } export class EvmRpcProviderWithTrace extends BaseProviderWithTrace { - constructor(endpoint: string | string[], opts?: BaseProviderOptions) { + constructor(endpoint: string | string[], opts?: EvmRpcProviderOptions) { super(opts); - const api = new ApiPromise(withAcalaTypes({ + const api = new ApiPromise({ provider: new WsProvider(endpoint), - })); + typesBundle: acalaTypesBundle, + rpcCacheCapacity: opts?.rpcCacheCapacity, + }); this.setApi(api); } - static from(endpoint: string | string[], opt?: BaseProviderOptions): EvmRpcProviderWithTrace { + static from(endpoint: string | string[], opt?: EvmRpcProviderOptions): EvmRpcProviderWithTrace { return new EvmRpcProviderWithTrace(endpoint, opt); } }