|
| 1 | +# block-proxy |
| 2 | + |
| 3 | +Caching reverse proxy for NEAR block data. Sits between indexers and upstream block sources (fastnear/neardata, S3/MinIO, NEAR Lake), providing: |
| 4 | + |
| 5 | +- **Local disk cache** with sharded storage and TTL-based eviction |
| 6 | +- **Singleflight dedup** — concurrent requests for the same block height are collapsed into one upstream fetch |
| 7 | +- **Fallback chain** — cache → S3/MinIO → fastnear → NEAR Lake, with per-source metrics |
| 8 | +- **Prometheus metrics** and JSON stats on a separate admin port |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +The proxy is URL-compatible with neardata.xyz — any client that fetches `/v0/block/{height}` or `/v0/last_block/final` can point at the proxy instead. |
| 13 | + |
| 14 | +```bash |
| 15 | +# Minimal config — fastnear-only proxy, no S3/NEAR Lake |
| 16 | +FASTNEAR_ENABLED=true S3_ENABLED=false NEAR_LAKE_ENABLED=false node dist/index.js |
| 17 | +``` |
| 18 | + |
| 19 | +All env vars have safe defaults. With zero config, the proxy starts on port 3000 serving mainnet blocks via fastnear. |
| 20 | + |
| 21 | +## API |
| 22 | + |
| 23 | +### Data Plane (default port 3000) |
| 24 | + |
| 25 | +| Method | Path | Description | |
| 26 | +| ------ | ---------------------- | -------------------------------------------------------------------- | |
| 27 | +| GET | `/v0/block/:height` | Fetch block by height. Returns JSON with `X-Upstream-Source` header. | |
| 28 | +| GET | `/v0/last_block/final` | Latest finalized block (proxied from fastnear, never cached). | |
| 29 | +| GET | `/healthz` | Health check — always returns 200. | |
| 30 | +| GET | `/readyz` | Readiness — returns 200 when ready, 503 during startup. | |
| 31 | + |
| 32 | +### Admin Plane (default port 3001) |
| 33 | + |
| 34 | +| Method | Path | Description | |
| 35 | +| ------ | ---------- | ------------------------------------------------------------- | |
| 36 | +| GET | `/metrics` | Prometheus metrics (text/plain). | |
| 37 | +| GET | `/stats` | JSON stats snapshot with hit rates, latencies, dedup savings. | |
| 38 | + |
| 39 | +## Environment Variables |
| 40 | + |
| 41 | +### Core |
| 42 | + |
| 43 | +| Variable | Default | Description | |
| 44 | +| ------------ | --------- | ------------------------------------------------------- | |
| 45 | +| `PORT` | `3000` | Data server listen port | |
| 46 | +| `ADMIN_PORT` | `3001` | Admin server listen port | |
| 47 | +| `NETWORK` | `mainnet` | `mainnet` or `testnet` — controls default upstream URLs | |
| 48 | +| `LOG_LEVEL` | `info` | Log level (debug, info, warn, error) | |
| 49 | + |
| 50 | +### Cache |
| 51 | + |
| 52 | +| Variable | Default | Description | |
| 53 | +| ------------------- | ------------ | -------------------------------------------- | |
| 54 | +| `CACHE_ENABLED` | `true` | Enable local disk cache | |
| 55 | +| `CACHE_DIR` | `/app/cache` | Cache directory path | |
| 56 | +| `CACHE_TTL_SECS` | `3600` | TTL for cached block eviction (seconds) | |
| 57 | +| `CACHE_COMPRESSION` | `false` | Reserved for future zstd compression support | |
| 58 | + |
| 59 | +### Upstream: fastnear (neardata.xyz) |
| 60 | + |
| 61 | +| Variable | Default | Description | |
| 62 | +| ------------------ | ------------------------ | -------------------------- | |
| 63 | +| `FASTNEAR_ENABLED` | `true` | Enable fastnear upstream | |
| 64 | +| `FASTNEAR_URL` | _(derived from NETWORK)_ | Override fastnear base URL | |
| 65 | + |
| 66 | +### Upstream: S3/MinIO |
| 67 | + |
| 68 | +| Variable | Default | Description | |
| 69 | +| --------------- | ----------------------- | ------------------------ | |
| 70 | +| `S3_ENABLED` | `false` | Enable S3/MinIO upstream | |
| 71 | +| `S3_ENDPOINT` | _(required if enabled)_ | S3/MinIO endpoint URL | |
| 72 | +| `S3_BUCKET` | _(required if enabled)_ | Bucket name | |
| 73 | +| `S3_REGION` | `us-east-1` | S3 region | |
| 74 | +| `S3_ACCESS_KEY` | _(required if enabled)_ | Access key | |
| 75 | +| `S3_SECRET_KEY` | _(required if enabled)_ | Secret key | |
| 76 | + |
| 77 | +### Upstream: NEAR Lake |
| 78 | + |
| 79 | +| Variable | Default | Description | |
| 80 | +| ----------------------- | ------------------------ | ------------------------------------ | |
| 81 | +| `NEAR_LAKE_ENABLED` | `false` | Enable NEAR Lake upstream | |
| 82 | +| `NEAR_LAKE_BUCKET` | _(derived from NETWORK)_ | Override NEAR Lake S3 bucket | |
| 83 | +| `NEAR_LAKE_REGION` | `eu-central-1` | NEAR Lake S3 region | |
| 84 | +| `UPSTREAM_TIMEOUT_SECS` | `7` | Per-source request timeout (seconds) | |
| 85 | + |
| 86 | +## Architecture |
| 87 | + |
| 88 | +``` |
| 89 | + ┌──────────────┐ |
| 90 | + indexers ───────► │ block-proxy │ |
| 91 | + │ :3000 │ |
| 92 | + └──────┬───────┘ |
| 93 | + │ |
| 94 | + ┌────────────┼────────────┐ |
| 95 | + ▼ ▼ ▼ |
| 96 | + ┌────────┐ ┌──────────┐ ┌──────────┐ |
| 97 | + │ cache │ │ S3/MinIO │ │ fastnear │ |
| 98 | + │ (disk) │ │ │ │ neardata │ |
| 99 | + └────────┘ └──────────┘ └──────────┘ |
| 100 | + │ |
| 101 | + ┌────┘ |
| 102 | + ▼ |
| 103 | + ┌──────────┐ |
| 104 | + │NEAR Lake │ |
| 105 | + │ (S3) │ |
| 106 | + └──────────┘ |
| 107 | +``` |
| 108 | + |
| 109 | +Fallback order: cache → S3 → fastnear → NEAR Lake. On any upstream hit, the block is written to cache in the background. |
| 110 | + |
| 111 | +### Singleflight Dedup |
| 112 | + |
| 113 | +When multiple indexers request the same block simultaneously, only one upstream fetch is made. All other requests wait for the leader's result. This prevents upstream stampede. |
| 114 | + |
| 115 | +### Cache Eviction |
| 116 | + |
| 117 | +Every 60 seconds, the eviction loop scans the cache directory and removes any cached block file older than `CACHE_TTL_SECS`. |
| 118 | + |
| 119 | +## Docker |
| 120 | + |
| 121 | +```bash |
| 122 | +docker build -f apps/block-proxy/Dockerfile -t block-proxy . |
| 123 | +docker run -p 3000:3000 -p 3001:3001 block-proxy |
| 124 | +``` |
| 125 | + |
| 126 | +## Connecting Indexers |
| 127 | + |
| 128 | +Since block-proxy is URL-compatible with neardata.xyz, any indexer using `nb-neardata` can point to it: |
| 129 | + |
| 130 | +```bash |
| 131 | +# In your indexer's env |
| 132 | +NEARDATA_URL=http://localhost:3000 |
| 133 | +``` |
| 134 | + |
| 135 | +The `nb-neardata` package accepts an optional `url` field that overrides the default neardata.xyz endpoint. |
0 commit comments