Skip to content

Commit

Permalink
Revert "Cache eth_calls with in-memory cache (#410)" (#412)
Browse files Browse the repository at this point in the history
This reverts commit 1209099.
  • Loading branch information
mikeki authored Nov 6, 2023
1 parent 1209099 commit 701f0d9
Showing 1 changed file with 0 additions and 39 deletions.
39 changes: 0 additions & 39 deletions lib/handlers/evm/provider/InstrumentedEVMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,11 @@ export type InstrumentedEVMProviderProps = {
export class InstrumentedEVMProvider extends ethers.providers.StaticJsonRpcProvider {
private readonly name: ProviderName
private readonly metricPrefix: string
private readonly blockCache: Map<string, Promise<any>>

constructor({ url, network, name }: InstrumentedEVMProviderProps) {
super(url, network)
this.name = name
this.metricPrefix = `RPC_${this.name}_${this.network.chainId}`
this.blockCache = new Map()
// Set an event listener to clear the cache on every new block.
this.on('block', () => {
metric.putMetric('RPCProviderCacheClear', 1, MetricLoggerUnit.Count)
this.blockCache.clear()
})
}

// Adds caching functionality to the RPC provider
override send(method: string, params: Array<any>): Promise<any> {
// Only cache eth_call's.
if (method !== 'eth_call') return super.send(method, params)
let key: string | undefined = undefined

try {
key = `call:${JSON.stringify(params)}`
} catch (e) {
metric.putMetric('RPCProviderCacheKeyError', 1, MetricLoggerUnit.Count)
}

if (key) {
const cached = this.blockCache.get(key)
if (cached) {
metric.putMetric('RPCProviderCacheHit', 1, MetricLoggerUnit.Count)
return cached
} else {
metric.putMetric('RPCProviderCacheMiss', 1, MetricLoggerUnit.Count)
}
}

const result = super.send(method, params)

if (key) {
metric.putMetric('RPCProviderCacheInsert', 1, MetricLoggerUnit.Count)
this.blockCache.set(key, result)
}

return result
}

override call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
Expand Down

0 comments on commit 701f0d9

Please sign in to comment.