Skip to content

Commit

Permalink
add option for cache capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 12, 2024
1 parent ce8fd1d commit febbf1b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
16 changes: 11 additions & 5 deletions packages/eth-providers/src/rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 3 additions & 1 deletion packages/eth-rpc-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function start(): Promise<void> {
subqlUrl: opts.subqlUrl,
maxBlockCacheSize: opts.maxBlockCacheSize,
storageCacheSize: opts.storageCacheSize,
rpcCacheCapacity: opts.rpcCacheCapacity,
});

const bridge = new Eip1193Bridge(provider);
Expand Down Expand Up @@ -58,8 +59,9 @@ export async function start(): Promise<void> {
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}
Expand Down
7 changes: 7 additions & 0 deletions packages/eth-rpc-adapter/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
MAX_CACHE_SIZE,
MAX_BATCH_SIZE,
STORAGE_CACHE_SIZE,
RPC_CACHE_CAPACITY,
SAFE_MODE,
LOCAL_MODE,
HTTP_ONLY,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions packages/eth-rpc-adapter/src/wrapped-provider.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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);
}
}

0 comments on commit febbf1b

Please sign in to comment.