-
Notifications
You must be signed in to change notification settings - Fork 2
feat(rpc): add /genesisBlock and /identities GET endpoints #921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tcsenpai
merged 3 commits into
stabilisation
from
feat/genesis-block-and-identities-endpoints
Jun 5, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| --- | ||
| type: runbook | ||
| title: The /genesisBlock and /identities Endpoints | ||
| date: 2026-06-05 | ||
| status: active | ||
| --- | ||
|
|
||
| # The /genesisBlock and /identities Endpoints | ||
|
|
||
| Two read-only GET routes on the node's RPC HTTP server | ||
| (`src/libs/network/server_rpc.ts`), served behind the same | ||
| CORS + rate-limiter + JSON middleware stack as every other GET route. | ||
| Default port is `53550`. | ||
|
|
||
| There are now three genesis-related GET routes — know which one you want: | ||
|
|
||
| | Route | Returns | | ||
| |---|---| | ||
| | `/genesis` | Only the embedded `genesisData` (chain params, balances, validators) parsed out of the genesis block's `content.extra.genesisData`. Pre-existing. | | ||
| | `/genesisBlock` | The **entire** genesis block (block 0) as stored — full `Blocks` record including `hash`, `number`, `content`, signatures, etc. | | ||
| | `/identities` | Paginated listing of every account's linked identities. | | ||
|
|
||
| --- | ||
|
|
||
| ## /genesisBlock | ||
|
|
||
| Dumps the whole genesis block (block 0) exactly as persisted, via | ||
| `Chain.getGenesisBlock()` (which is `getBlockByNumber(0)`). | ||
|
|
||
| ### Quick check | ||
|
|
||
| ```bash | ||
| curl -fsS http://localhost:53550/genesisBlock | jq | ||
| ``` | ||
|
|
||
| ### Responses | ||
|
|
||
| - **200** — the full genesis `Blocks` object. | ||
| - **503** — `{ result: 503, response: "STATE_NOT_READY", extra: { message } }` | ||
| when the genesis block is not found yet (node still booting / chain not | ||
| initialized) or the read threw. | ||
|
|
||
| Use `/genesisBlock` when you need block-level fields (hash, signatures, | ||
| the raw `content` envelope). Use `/genesis` when you only need the chain | ||
| parameters and pre-funded balances. | ||
|
|
||
| Rate limit: `/genesisBlock` maps to the `genesisblock` method in the rate | ||
| limiter's `pathMethodMap`, so it gets the default per-IP GET allowance | ||
| (2000 req/60s) rather than falling through to the lax POST bucket. | ||
|
|
||
| --- | ||
|
|
||
| ## /identities | ||
|
|
||
| Lists the linked identities of every account, **paginated**. Each row is | ||
| just the account `pubkey` plus its `identities` jsonb blob — never | ||
| balance, nonce, or points. Backed by `GCR.listIdentities(limit, cursor)` | ||
| in `src/libs/blockchain/gcr/gcr.ts`. | ||
|
|
||
| ### Why only pubkey + identities | ||
|
|
||
| The `gcr_main` table can hold a large number of jsonb-heavy rows, and the | ||
| `balance` column is a `bigint` that `JSON.stringify` cannot serialize. By | ||
| projecting only `pubkey` + `identities` at the SQL level | ||
| (`.select(["gcr.pubkey", "gcr.identities"])`) the payload stays focused | ||
| and the bigint serialization trap is never hit. | ||
|
|
||
| ### Pagination — keyset, not offset | ||
|
|
||
| Pagination seeks on the `pubkey` primary key: | ||
| `WHERE pubkey > :cursor ORDER BY pubkey ASC LIMIT :n`. This stays | ||
| O(log n) on the PK index no matter how deep into the table you page — | ||
| unlike `OFFSET`, which scans and discards rows. An unbounded `find()` | ||
| over the whole table is deliberately avoided so a single request can | ||
| never load the entire account set into memory. | ||
|
|
||
| ### Query parameters | ||
|
|
||
| | Param | Type | Default | Notes | | ||
| |---|---|---|---| | ||
| | `limit` | int | `100` | Clamped to `[1, 1000]`. Non-numeric / non-positive falls back to 100; values above 1000 are capped. | | ||
| | `cursor` | string | — | The `pubkey` of the last row from the previous page. Omit for the first page. | | ||
|
|
||
| ### Quick check | ||
|
|
||
| ```bash | ||
| # First page (default 100 per page) | ||
| curl -fsS "http://localhost:53550/identities" | jq | ||
|
|
||
| # Custom page size | ||
| curl -fsS "http://localhost:53550/identities?limit=250" | jq | ||
|
|
||
| # Next page — pass back the previous response's nextCursor | ||
| curl -fsS "http://localhost:53550/identities?cursor=<lastPubkey>" | jq | ||
| ``` | ||
|
|
||
| ### Response shape | ||
|
|
||
| ```jsonc | ||
| { | ||
| "success": true, | ||
| "identities": [ | ||
| { "pubkey": "0xabc…", "identities": { "xm": { … }, "web2": { … }, "pqc": { … }, "ud": [ … ] } } | ||
| // … | ||
| ], | ||
| "count": 100, // rows in THIS page | ||
| "limit": 100, // effective (clamped) page size | ||
| "nextCursor": "0xfff…" // pass as ?cursor= for the next page; null = end of table | ||
| } | ||
| ``` | ||
|
|
||
| ### Paging to the end | ||
|
|
||
| Keep calling with `?cursor=<nextCursor>` until `nextCursor` is `null`. | ||
| A `null` cursor means the last page returned fewer rows than `limit`, | ||
| i.e. the table is exhausted. | ||
|
|
||
| ```bash | ||
| cursor="" | ||
| while :; do | ||
| url="http://localhost:53550/identities?limit=500" | ||
| [ -n "$cursor" ] && url="$url&cursor=$cursor" | ||
| resp=$(curl -fsS "$url") | ||
| echo "$resp" | jq -c '.identities[]' | ||
| cursor=$(echo "$resp" | jq -r '.nextCursor') | ||
| [ "$cursor" = "null" ] && break | ||
| done | ||
| ``` | ||
|
|
||
| ### Errors | ||
|
|
||
| - **500** — `{ success: false, error: "Failed to list identities", message }` | ||
| if the DB query throws. | ||
| - **503** + `Retry-After` header — `{ success: false, error: "Service busy", message }` | ||
| when the node is already serving its max concurrent `/identities` requests | ||
| and a slot did not free in time (see DDoS hardening below). | ||
|
|
||
| ### DDoS hardening | ||
|
|
||
| `/identities` is the most expensive GET route (a paginated full-table read | ||
| over jsonb-heavy rows), so it has three independent brakes layered on top | ||
| of each other. Tunable via `src/utilities/constants.ts`. | ||
|
|
||
| 1. **Per-IP rate limit** — `identities` method limit | ||
| (`RATE_LIMIT_IDENTITIES_MAX_REQUESTS` = 30 per | ||
| `RATE_LIMIT_IDENTITIES_WINDOW_MS` = 60s), far below the default GET | ||
| allowance of 2000/60s. Registered in the rate limiter's `pathMethodMap` | ||
| (`src/libs/network/middleware/rateLimiter.ts`) and `methodLimits` | ||
| (`src/utilities/sharedState.ts`). Stops a single source from flooding. | ||
| Over the limit → standard 429 from the rate-limiter middleware. | ||
|
|
||
| 2. **Global concurrency gate** — at most `IDENTITIES_MAX_CONCURRENT` (= 3) | ||
| `/identities` handlers execute at once across **all** callers, so a | ||
| distributed burst (many IPs × 1 request each, which the per-IP limit | ||
| can't catch) still can't pile unbounded DB load. Overflow callers queue | ||
| up to `IDENTITIES_MAX_QUEUE` (= 12) deep and wait at most | ||
| `IDENTITIES_ACQUIRE_TIMEOUT_MS` (= 2000ms) for a slot; if none frees (or | ||
| the queue is already full) they get the 503 + `Retry-After` above | ||
| instead of waiting forever or deepening the load. Implemented by | ||
| `ConcurrencyGate` (`src/libs/network/utils/concurrencyGate.ts`), a pure | ||
| in-process counting semaphore with a bounded FIFO wait queue. | ||
|
|
||
| 3. **Bounded work per request** — `limit` is hard-capped at 1000, the query | ||
| projects only `pubkey` + `identities` columns, and pagination is keyset | ||
| (PK seek) not offset, so no single request can pull the whole table into | ||
| memory. | ||
|
|
||
| **Note on Bun workers:** workers were considered and deliberately *not* | ||
| used here. Workers (as in `txValidatorPool`) offload **CPU-bound** crypto | ||
| off the event loop. `/identities` is **I/O-bound** (a Postgres query) — the | ||
| event loop is already free while awaiting the DB, so a worker would add IPC | ||
| copy overhead, would not reduce DB load, and would give an attacker more | ||
| threads/memory to exhaust. The concurrency gate caps DB load directly, | ||
| which is the actual bottleneck. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.