diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index e10e4c464adb..08ee5153f9bd 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -153,6 +153,7 @@ export abstract class Decorate extends Events { this._rpcCore = new RpcCore(this.#instanceId, this.#registry, { isPedantic: this._options.isPedantic, provider, + rpcCacheCapacity: this._options.rpcCacheCapacity, userRpc: this._options.rpc }) as (RpcCore & RpcInterface); this._isConnected = new BehaviorSubject(this._rpcCore.provider.isConnected); diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts index 3ea7ddc78fb7..40bde0cbd18f 100644 --- a/packages/api/src/types/index.ts +++ b/packages/api/src/types/index.ts @@ -80,6 +80,10 @@ export interface ApiOptions extends RegisteredTypes { * @description User-defined RPC methods */ rpc?: Record>; + /** + * @description Defines the size of the cache for the rpc-core. Defaults to 1024 * 10 * 10. + */ + rpcCacheCapacity?: number; /** * @description Overrides for state_call usage (this will be removed in some future version) */ diff --git a/packages/rpc-core/src/bundle.ts b/packages/rpc-core/src/bundle.ts index 72e99f0aff2e..ff81658a70d3 100644 --- a/packages/rpc-core/src/bundle.ts +++ b/packages/rpc-core/src/bundle.ts @@ -11,7 +11,7 @@ import type { RpcCoreStats, RpcInterfaceMethod } from './types/index.js'; import { Observable, publishReplay, refCount } from 'rxjs'; -import { DEFAULT_CAPACITY, LRUCache } from '@polkadot/rpc-provider'; +import { LRUCache } from '@polkadot/rpc-provider'; import { rpcDefinitions } from '@polkadot/types'; import { hexToU8a, isFunction, isNull, isUndefined, lazyMethod, logger, memoize, objectSpread, u8aConcat, u8aToU8a } from '@polkadot/util'; @@ -33,6 +33,7 @@ type MemoizedRpcInterfaceMethod = Memoized & { interface Options { isPedantic?: boolean; provider: ProviderInterface; + rpcCacheCapacity?: number; userRpc?: Record>; } @@ -47,6 +48,8 @@ const EMPTY_META = { } }; +const RPC_CORE_DEFAULT_CAPACITY = 1024 * 10 * 10; + // utility method to create a nicely-formatted error /** @internal */ function logErrorMessage (method: string, { noErrorLog, params, type }: DefinitionRpc, error: Error): void { @@ -109,7 +112,7 @@ export class RpcCore { * Default constructor for the core RPC handler * @param {ProviderInterface} provider An API provider using any of the supported providers (HTTP, SC or WebSocket) */ - constructor (instanceId: string, registry: Registry, { isPedantic = true, provider, userRpc = {} }: Options) { + constructor (instanceId: string, registry: Registry, { isPedantic = true, provider, rpcCacheCapacity, userRpc = {} }: Options) { if (!provider || !isFunction(provider.send)) { throw new Error('Expected Provider to API create'); } @@ -123,7 +126,7 @@ export class RpcCore { // these are the base keys (i.e. part of jsonrpc) this.sections.push(...sectionNames); - this.#storageCache = new LRUCache(DEFAULT_CAPACITY * 10 * 10); + this.#storageCache = new LRUCache(rpcCacheCapacity || RPC_CORE_DEFAULT_CAPACITY); // decorate all interfaces, defined and user on this instance this.addUserInterfaces(userRpc); }