EIPs Explained

ERC-7774 · Draft · ERC · 2024-09-20

ERC-7774: Cache invalidation in ERC-5219 mode Web3 URL

Lets on-chain websites served over web3:// use normal HTTP caching (ETag, 304 Not Modified) by announcing cache invalidations through a contract event, cutting RPC calls.

What problem does it solve?

web3:// URLs (ERC-6860) load web pages straight from smart contracts. In the ERC-6944 resolve mode, a contract returns HTTP-like responses through the ERC-5219 interface. Every page load is an RPC call, which costs CPU on a local node or money and rate limits on a hosted RPC.

Standard HTTP caching would help, but ERC-6944 does not forward request headers to the contract, so a contract cannot answer If-None-Match or If-Modified-Since with 304 Not Modified. The only option is max-age, and every revalidation still triggers a full RPC call.

ERC-7774 moves cache validation out of the contract: the contract emits an event when content changes, and the web3:// client keeps its own cache.

How it works

Opt-in directive. A response that wants event-based caching includes evm-events in its Cache-Control header, plus a normal ETag and/or max-age. Optional values list extra paths or other contract addresses to watch, e.g. Cache-Control: evm-events="/index.html 0xabc…/data".

Invalidation event.

event ClearPathCache(string[] paths);

Paths must not end in / (except the root /). Query parameter order does not matter. Simple * wildcards are allowed for a whole path segment or a whole parameter value, and * alone clears every path of that contract.

Client behavior. For each chain and contract the client is either listening for events or not. While listening it keeps a map (chainId, contract, path) → (lastModified, ETag):

  • On a conditional request that matches the stored ETag or date, return 304 immediately without RPC.
  • Otherwise query the contract and refresh the entry.
  • When a ClearPathCache event matches, delete the entry.

If the client stops listening, it must clear the whole map. Clients that do not implement this ERC simply ignore conditional headers.

Sample code

A contract that serves a page and invalidates it on update:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Blog {
    event ClearPathCache(string[] paths);
    mapping(string => string) private posts;
    address public owner = msg.sender;

    function updatePost(string calldata slug, string calldata html) external {
        require(msg.sender == owner, "owner");
        posts[slug] = html;
        string[] memory paths = new string[](2);
        paths[0] = string.concat("/posts/", slug);
        paths[1] = "/"; // index lists posts too
        emit ClearPathCache(paths);
    }
    // ERC-5219 request() would return headers such as:
    //   Cache-Control: evm-events
    //   ETag: "<hash of content>"
}

Things to know

  • Status: Draft, and it extends ERC-6944 (also Draft).
  • The evm-events directive is required so that a site sending ordinary cache headers without emitting events is not served stale content forever by clients that implement this ERC.
  • Expect a short window of stale content between the transaction emitting the event and the client processing it.
  • On a chain reorganization the client must roll back cache state, or reverted content may be served until the next invalidation.
  • When combined with ERC-7618 content encoding, Accept-Encoding becomes part of the cache key.

#web3-url #caching #http #events #dweb

Details

Authors
Nicolas Deschildre (@nand2), Sam Wilson (@SamWilsn)
Status
Draft
Type
Standards Track / ERC
Created
2024-09-20
Requires

Machine-readable: Markdown · JSON