Skip to content

mgpai22/evm-json-rpc-cache

Repository files navigation

EVM JSON RPC Cache

This program caches requests obtained from interacting with EVM's json rpc.

It is very useful for indexers/programs which need to make repeated historical calls. It reduces strain on node and saves api credits.

Caching Mechanism

  1. Cache Storage: The server uses BigCache as the underlying cache storage mechanism, which is wrapped by the gocache library for easier management.

  2. Cache Key: For each request, a unique cache key is generated based on the HTTP method and the full URL of the request.

  3. Caching Decision: The shouldCacheEndpoint method determines whether a request should be cached and for how long, based on the Ethereum JSON-RPC method being called.

  4. Cached Response: When a response is cached, it stores the entire HTTP response, including status code, headers, and body.

  5. Cache Hit: If a cached response is found, it's immediately returned to the client without forwarding the request to the backend.

  6. Cache Miss: If no cached response is found, the request is forwarded to the backend, and the response is then cached (if the method is cacheable).

  7. Confirmation-based Caching: For transaction and block-related methods, the caching decision also considers the number of confirmations:

    • Transaction data is cached only if the transaction has reached the required number of confirmations.
    • Block data is cached for a long time if the block is considered finalized (has enough confirmations).
  8. Dynamic TTL: Different cache TTLs (Time To Live) are set for different types of data:

    • Confirmed transaction and finalized block data: 365 days (some long time)
    • Gas price estimates: based on config
  9. Latest Block Tracking: The server continuously tracks tip through wss to determine finality

Setup

make
./proxy -config ./config.yml

docker

docker compose up -d

config

here is an example config

server:
  listenAddress: "0.0.0.0"
  listenPort: 8080

backend:
  urls:
    - "https://mainnet.infura.io/v3/API_KEY"
    - "https://mainnet.infura.io/v3/API_KEY_2"

ws_backend:
  urls:
    - "wss://mainnet.infura.io/ws/v3/API_KEY"
    - "wss://mainnet.infura.io/ws/v3/API_KEY_2"

blockchain:
  confirmations: 15
  gasFeeTTL: 30s

logging:
  level: "info"
  logDirectory: "logs"
  logFileName: "app.log"
  maxLogFileSize: 100
  maxBackups: 3
  maxAge: 28

cache:
  shards: 1024
  lifeWindow: 10m
  cleanWindow: 5m
  maxEntriesInWindow: 600000
  maxEntrySize: 500
  verbose: false
  hardMaxCacheSize: 8192

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published