Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/chain-indexer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ curl localhost:3092/v1/status

The checkpoint height should advance as blocks land in `cosmos.blocks`, `cosmos.transactions`, and `cosmos.messages`.

## Genesis import

Set `GENESIS_IMPORT=true` to seed genesis state before the first block: accounts, per-denom balances (as `genesis`-reason ledger entries), validators, and staking delegations, all in one transaction. Because balance history is only trustworthy from the network's genesis, a fresh `sync` with the flag on must begin at the genesis height, and a fresh start anywhere else is rejected with a clear error. On sandbox that height is 1 (`SYNC_START_HEIGHT=1`); the height is read from the genesis file itself, so a chain continued from an export uses its continuation height.

The import runs once. A `genesis` checkpoint in `indexer_state` makes a restart skip it, and the seed commits in a single transaction, so an interrupted run rolls back and retries cleanly. Genesis is fetched over RPC `/genesis_chunked` from the same nodes sync uses, and its `chain_id` must match the chain being indexed. Leave the flag unset (the default) and sync tails blocks, transactions, and messages from any height exactly as before.

## Backfill

The backfill role fills the database over an explicit, inclusive height range and exits when done, so it fits a one-off K8s Job:
Expand Down
55 changes: 55 additions & 0 deletions apps/chain-indexer/drizzle/0001_long_mach_iv.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
CREATE TYPE "cosmos"."balance_change_reason" AS ENUM('genesis', 'transfer', 'fee', 'reward', 'commission', 'slash', 'gov', 'ibc', 'escrow', 'bme', 'mint', 'burn');--> statement-breakpoint
CREATE TABLE "cosmos"."account_balances" (
"account_id" integer NOT NULL,
"denom" text NOT NULL,
"amount" numeric(38, 0) NOT NULL,
CONSTRAINT "account_balances_account_id_denom_pk" PRIMARY KEY("account_id","denom")
);
--> statement-breakpoint
CREATE TABLE "cosmos"."accounts" (
"id" serial PRIMARY KEY NOT NULL,
"address" text NOT NULL,
"account_number" bigint,
"account_type" text,
"is_module_account" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE "cosmos"."balance_changes" (
"id" bigserial PRIMARY KEY NOT NULL,
"account_id" integer NOT NULL,
"denom" text NOT NULL,
"delta" numeric(38, 0) NOT NULL,
"balance_after" numeric(38, 0) NOT NULL,
"reason" "cosmos"."balance_change_reason" NOT NULL,
"height" bigint NOT NULL,
"counterparty_account_id" integer
);
--> statement-breakpoint
CREATE TABLE "cosmos"."delegations" (
"delegator_account_id" integer NOT NULL,
"validator_operator_address" text NOT NULL,
"shares" numeric(38, 18) NOT NULL,
CONSTRAINT "delegations_delegator_account_id_validator_operator_address_pk" PRIMARY KEY("delegator_account_id","validator_operator_address")
);
--> statement-breakpoint
CREATE TABLE "cosmos"."validators" (
"operator_address" text PRIMARY KEY NOT NULL,
"account_address" text,
"hex_address" text,
"moniker" text,
"identity" text,
"website" text,
"details" text,
"security_contact" text,
"commission_rate" numeric(20, 18),
"commission_max_rate" numeric(20, 18),
"commission_max_change_rate" numeric(20, 18),
"min_self_delegation" numeric(38, 0)
);
--> statement-breakpoint
ALTER TABLE "cosmos"."account_balances" ADD CONSTRAINT "account_balances_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "cosmos"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cosmos"."balance_changes" ADD CONSTRAINT "balance_changes_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "cosmos"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cosmos"."balance_changes" ADD CONSTRAINT "balance_changes_counterparty_account_id_accounts_id_fk" FOREIGN KEY ("counterparty_account_id") REFERENCES "cosmos"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cosmos"."delegations" ADD CONSTRAINT "delegations_delegator_account_id_accounts_id_fk" FOREIGN KEY ("delegator_account_id") REFERENCES "cosmos"."accounts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "accounts_address_idx" ON "cosmos"."accounts" USING btree ("address");--> statement-breakpoint
CREATE INDEX "balance_changes_account_denom_height_idx" ON "cosmos"."balance_changes" USING btree ("account_id","denom","height");
Loading