indexer

๐Ÿš€ HeaderProtocol: Event Indexer Service


๐Ÿ“š Table of Contents


Overview

The HeaderProtocol Event Indexer listens to blockchain events for requested block headers, responses, commits, and refunds. It efficiently organizes this data, enabling ๐Ÿ”Ž fast lookups and ๐Ÿ›  incremental updates, even when new events arrive long after the initial request.


Key Features

  1. ๐Ÿ—ƒ Granular Storage
    Each argsBlockNumber is stored in its own file, making lookups and updates lightning-fast ๐Ÿš€.

  2. ๐Ÿ“ Instant Lookup via map.json
    map.json provides an O(1) lookup from argsBlockNumber to its corresponding date directory, simplifying file access.

  3. ๐Ÿ“ฆ Commit Event Standardization

    • If commit is the only event for a block, it remains standalone.
    • If other events are added, the commit data is replicated in all event entries for the block, ensuring consistent structure.
  4. ๐Ÿ“œ Update History
    history.json maintains a chronological log of updates, so you can process only the latest changes.

  5. ๐Ÿ“Š Aggregated Snapshots
    Aggregates are rebuilt at ๐Ÿ“… daily, ๐Ÿ—“ monthly, and ๐Ÿ“† yearly levels for streamlined access to analytics.


Directory Structure

๐Ÿ“‚ data/<network>/
โ”œโ”€โ”€ ๐Ÿ—‚ map.json           # ๐Ÿ—บ Maps argsBlockNumber โ†’ "YYYY/MM/DD"
โ”œโ”€โ”€ ๐Ÿ—‚ history.json       # ๐Ÿ“œ Chronological record of updates
โ”œโ”€โ”€ ๐Ÿ“‚ YYYY/              # ๐Ÿ“† Yearly directory
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ MM/            # ๐Ÿ—“ Monthly directory
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ DD/        # ๐Ÿ“… Daily directory
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ—‚ [blockNumber].json  # ๐Ÿ—ƒ Events for blockNumber
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ—‚ index.json         # ๐Ÿ“Š Daily aggregated data
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ—‚ index.json             # ๐Ÿ“Š Monthly aggregated data
โ”‚   โ””โ”€โ”€ ๐Ÿ—‚ index.json                 # ๐Ÿ“Š Yearly aggregated data

Data Structure

Example [blockNumber].json:

[
  {
    "chainId": "31337",
    "blockNumber": "20",
    "headerIndex": "9",
    "createdAt": "2024-12-20T19:28:42.156Z",
    "updatedAt": "2024-12-20T19:28:42.157Z",
    "request": { "rewardAmount": "0", ... },
    "responses": [ { "responder": "0xf39Fd6...", ... } ],
    "commit": { "blockNumber": "27", ... }
  }
]

Usage in a Client Interface

Quick Lookup ๐Ÿš€

  1. Use map.json to find the directory path for argsBlockNumber:
    { "1000000": "2024/12/20" }
    
  2. Retrieve the file at data/<network>/2024/12/20/1000000.json.

โš™๏ธ Running the Indexer

  1. Install Dependencies:

    npm install
    
  2. Run Anvil:

    anvil
    
  3. Deploy and Indexer HeaderProtocol:

    sudo bash ./src/anvil.sh
    
  4. Verify Outputs:

    • โœ… map.json: Lookup table.
    • โœ… history.json: Update logs.
    • โœ… [blockNumber].json: Event data.
    • โœ… Aggregated index.json files for analytics.

Diagrams

Sequence Diagram: How the Indexer Works

sequenceDiagram
    participant Client
    participant Indexer
    participant Blockchain
    participant Storage

    Client->>Indexer: Request indexing for block headers
    Indexer->>Blockchain: Listen for events (request, response, commit)
    Blockchain-->>Indexer: Emit events
    Indexer->>Storage: Write `argsBlockNumber.json`
    Indexer->>Storage: Update `map.json` and `history.json`
    Storage-->>Client: Return updated JSON files