Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rpcCacheCapacity option to ApiOptions #6020

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/base/Decorate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export abstract class Decorate<ApiType extends ApiTypes> 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);
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export interface ApiOptions extends RegisteredTypes {
* @description User-defined RPC methods
*/
rpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
/**
* @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)
*/
Expand Down
9 changes: 6 additions & 3 deletions packages/rpc-core/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -33,6 +33,7 @@ type MemoizedRpcInterfaceMethod = Memoized<RpcInterfaceMethod> & {
interface Options {
isPedantic?: boolean;
provider: ProviderInterface;
rpcCacheCapacity?: number;
userRpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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');
}
Expand All @@ -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);
}
Expand Down
Loading