Caching JSON-RPC Provider

Overview

Providers include internal caching mechanisms to improve performance and reduce redundant network requests. Each provider maintains its own independent cache. This section explains how caching works and how to utilize it effectively.

Provider Caching Workflow Incoming Request Method Call Cache Layer Cache Hit? LRU Cache P2OP Cache Gas Parameters Challenge Cache Network RPC Call Store in Cache Return Cached Yes No

LRU Cache Behavior

An LRU (Least Recently Used) cache is implemented for frequently accessed data. When the cache reaches its maximum capacity, the least recently accessed items are automatically evicted.

Cached Data Types

Data Type Cache Duration Invalidation
Gas Parameters ~1 block On new block or timeout
Challenge Until expiry Automatic
P2OP Addresses Long-term Manual
CSV1 Addresses Long-term Manual
Chain ID Permanent Never

Gas Parameters Cache

Gas parameters are cached to reduce RPC calls.

typescript
Gas Parameters Cache Usage
// First call fetches from network
const gasParams1 = await provider.gasParameters();

// Second call returns cached value (if recent)
const gasParams2 = await provider.gasParameters();

// Cache refreshes when data becomes stale

Gas Parameters Cache Behavior

Scenario Behavior
First request Fetch from network, cache result
Subsequent request Return cached value
On new block or after a few seconds since last fetch Cache is invalidated

Challenge Cache

Transaction challenges are cached with expiration tracking.

typescript
Challenge Cache Usage
// Get current challenge
const challenge = await provider.getChallenge();

// If called again before expiry, returns cached
const challenge2 = await provider.getChallenge();  // Same challenge
        

Challenge Cache Behavior

Scenario Behavior
First request Fetch from network, cache result
Within expiry Return cached challenge
After expiry Fetch new challenge
On error Clear cache, retry

P2OP Address Cache

P2OP (Pay-to-OPNet) addresses are cached to avoid repeated conversions.

typescript
P2OP Address Cache Usage
// First lookup converts and caches
const p2op1 = contract.p2op;

// Subsequent lookups use cached value
const p2op2 = contract.p2op;  // From cache

P2OP Address Cache Behavior

Scenario Behavior
First request Convert to P2OP, cache result
Subsequent request Return cached address

CSV1 Address Cache

CSV1 (CheckSequenceVerify v1) addresses are cached per address.

typescript
CSV1 Address Cache Usage
// Get CSV1 address
const csv1 = provider.getCSV1ForAddress(address);

// Subsequent calls return cached
const csv1Again = provider.getCSV1ForAddress(address);  // From cache

CSV1 Address Cache Behavior

Scenario Behavior
First request Fetch from network, cache result
Subsequent request Return cached value

Performance Considerations

Benefits of Caching

  1. Reduced Network Calls: Fewer RPC requests.
  2. Lower Latency: Instant responses for cached data.
  3. Bandwidth Savings: Less data transferred.
  4. Node Relief: Less load on OP_NET nodes.

Cache Considerations

  1. Memory Usage: Caches consume memory.
  2. Stale Data: Cached data may become outdated.
  3. Cache Misses: First calls always hit network.

Optimizing Cache Usage

typescript
Optimizing Code to Use Cached Data
// Good: Batch operations that use same cached data
const [balance1, balance2, balance3] = await Promise.all([
    contract.balanceOf(addr1),
    contract.balanceOf(addr2),
    contract.balanceOf(addr3),
]);

// These share cached gas parameters

// Less optimal: Sequential calls with delays
const balance1 = await contract.balanceOf(addr1);
await sleep(60000);  // Cache may expire
const balance2 = await contract.balanceOf(addr2);

WebSocket vs JSON-RPC Caching

Both WebSocket and JSON-RPC providers implement their own independent caching mechanisms. WebSocket providers offer near real-time cache updates through persistent connections, while JSON-RPC providers rely on time-based polling to refresh cached data.

WebSocket Provider

  1. Receives real-time block notifications.
  2. Can invalidate gas cache immediately on new blocks.
  3. More up-to-date cached values.

JSON-RPC Provider

  1. Uses time-based cache invalidation.
  2. Polls for updates when needed.
  3. May have slightly stale data between polls.
typescript
WebSocket vs JSON-RPC Provider
// WebSocket: Cache updates with real-time blocks
wsProvider.subscribeToBlocks((block) => {
    // Gas cache automatically refreshes
});

// JSON-RPC: Cache updates on access after timeout
const gasParams = await httpProvider.gasParameters();