Skip to content

Commit 3f3323b

Browse files
committed
feat(indexer): scaffold chain-indexer app with sandbox sync tracer bullet
1 parent de09757 commit 3f3323b

52 files changed

Lines changed: 2085 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/chain-indexer/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Chain Indexer
2+
3+
Rewrite of the Akash blockchain indexer as an encapsulated app with its own database and, eventually, its own public REST API. Design doc and discussion live in the Linear project (see CON-803).
4+
5+
One codebase, several processes. The role is picked at runtime:
6+
7+
```
8+
INDEXER_ROLE = sync | backfill | api | jobs
9+
NETWORK = mainnet | sandbox | testnet
10+
```
11+
12+
Currently implemented: `sync` (live tail with per-block atomic commits, advisory-lock leader election, parent-hash continuity check) and a minimal `api` (healthz + status). `backfill` and `jobs` exit with `ROLE_NOT_IMPLEMENTED`.
13+
14+
## Running locally
15+
16+
Create `env/.env` with at least:
17+
18+
```
19+
NETWORK=sandbox
20+
POSTGRES_DB_URI=postgres://user:password@localhost:5432/chain-indexer
21+
```
22+
23+
RPC endpoints default to the network's public nodes from `@akashnetwork/net`; override with a comma-separated `RPC_NODE_ENDPOINTS`. With no checkpoint in the database, sync starts at the current chain tip (set `SYNC_START_HEIGHT` to start elsewhere). Migrations run automatically on boot.
24+
25+
```bash
26+
npm run dev
27+
```
28+
29+
Then check progress:
30+
31+
```bash
32+
curl localhost:3092/v1/status
33+
```
34+
35+
The checkpoint height should advance as blocks land in `cosmos.blocks`, `cosmos.transactions`, and `cosmos.messages`.
36+
37+
## Tests
38+
39+
```bash
40+
npm test
41+
npm run lint -- --quiet
42+
```
43+
44+
## Schema changes
45+
46+
Edit `src/db/schema.ts`, then:
47+
48+
```bash
49+
npm run migration:gen
50+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import "@akashnetwork/env-loader";
2+
3+
import { defineConfig } from "drizzle-kit";
4+
5+
export default defineConfig({
6+
schema: "./src/db/schema.ts",
7+
out: "./drizzle",
8+
dialect: "postgresql",
9+
dbCredentials: {
10+
url: process.env.POSTGRES_DB_URI ?? "postgres://offline:offline@localhost:5432/offline"
11+
}
12+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
CREATE SCHEMA "cosmos";
2+
--> statement-breakpoint
3+
CREATE TABLE "cosmos"."blocks" (
4+
"height" bigint PRIMARY KEY NOT NULL,
5+
"datetime" timestamp with time zone NOT NULL,
6+
"hash" "bytea" NOT NULL,
7+
"parent_hash" "bytea",
8+
"proposer_address" text NOT NULL,
9+
"tx_count" integer NOT NULL
10+
);
11+
--> statement-breakpoint
12+
CREATE TABLE "indexer_state" (
13+
"stream" text PRIMARY KEY NOT NULL,
14+
"last_height" bigint NOT NULL,
15+
"updated_at" timestamp with time zone NOT NULL
16+
);
17+
--> statement-breakpoint
18+
CREATE TABLE "cosmos"."message_types" (
19+
"id" "smallserial" PRIMARY KEY NOT NULL,
20+
"type" text NOT NULL
21+
);
22+
--> statement-breakpoint
23+
CREATE TABLE "cosmos"."messages" (
24+
"height" bigint NOT NULL,
25+
"tx_index" integer NOT NULL,
26+
"index" integer NOT NULL,
27+
"type_id" smallint NOT NULL,
28+
"body" jsonb,
29+
CONSTRAINT "messages_height_tx_index_index_pk" PRIMARY KEY("height","tx_index","index")
30+
);
31+
--> statement-breakpoint
32+
CREATE TABLE "cosmos"."transactions" (
33+
"height" bigint NOT NULL,
34+
"index" integer NOT NULL,
35+
"hash" "bytea" NOT NULL,
36+
"code" integer NOT NULL,
37+
"gas_used" bigint NOT NULL,
38+
"gas_wanted" bigint NOT NULL,
39+
"fee" jsonb NOT NULL,
40+
CONSTRAINT "transactions_height_index_pk" PRIMARY KEY("height","index")
41+
);
42+
--> statement-breakpoint
43+
ALTER TABLE "cosmos"."messages" ADD CONSTRAINT "messages_type_id_message_types_id_fk" FOREIGN KEY ("type_id") REFERENCES "cosmos"."message_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
44+
CREATE UNIQUE INDEX "message_types_type_idx" ON "cosmos"."message_types" USING btree ("type");--> statement-breakpoint
45+
CREATE INDEX "messages_type_id_idx" ON "cosmos"."messages" USING btree ("type_id");--> statement-breakpoint
46+
CREATE INDEX "transactions_hash_idx" ON "cosmos"."transactions" USING btree ("hash");

0 commit comments

Comments
 (0)