-
Notifications
You must be signed in to change notification settings - Fork 196
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
feat(store-indexer): command to run decoded indexer #2001
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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,123 @@ | ||
#!/usr/bin/env node | ||
import "dotenv/config"; | ||
import { z } from "zod"; | ||
import { eq } from "drizzle-orm"; | ||
import { createPublicClient, fallback, webSocket, http, Transport } from "viem"; | ||
import { isDefined } from "@latticexyz/common/utils"; | ||
import { combineLatest, filter, first } from "rxjs"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import postgres from "postgres"; | ||
import { createStorageAdapter } from "@latticexyz/store-sync/postgres-decoded"; | ||
import { createStoreSync } from "@latticexyz/store-sync"; | ||
import { indexerEnvSchema, parseEnv } from "./parseEnv"; | ||
|
||
const env = parseEnv( | ||
z.intersection( | ||
indexerEnvSchema, | ||
z.object({ | ||
DATABASE_URL: z.string(), | ||
HEALTHCHECK_HOST: z.string().optional(), | ||
HEALTHCHECK_PORT: z.coerce.number().optional(), | ||
}) | ||
) | ||
); | ||
|
||
const transports: Transport[] = [ | ||
// prefer WS when specified | ||
env.RPC_WS_URL ? webSocket(env.RPC_WS_URL) : undefined, | ||
// otherwise use or fallback to HTTP | ||
env.RPC_HTTP_URL ? http(env.RPC_HTTP_URL) : undefined, | ||
].filter(isDefined); | ||
|
||
const publicClient = createPublicClient({ | ||
transport: fallback(transports), | ||
pollingInterval: env.POLLING_INTERVAL, | ||
}); | ||
|
||
const chainId = await publicClient.getChainId(); | ||
const database = drizzle(postgres(env.DATABASE_URL, { prepare: false })); | ||
|
||
const { storageAdapter, tables } = await createStorageAdapter({ database, publicClient }); | ||
|
||
let startBlock = env.START_BLOCK; | ||
|
||
// Resume from latest block stored in DB. This will throw if the DB doesn't exist yet, so we wrap in a try/catch and ignore the error. | ||
// TODO: query if the DB exists instead of try/catch | ||
try { | ||
const chainState = await database | ||
.select() | ||
.from(tables.configTable) | ||
.where(eq(tables.configTable.chainId, chainId)) | ||
.limit(1) | ||
.execute() | ||
// Get the first record in a way that returns a possible `undefined` | ||
// TODO: move this to `.findFirst` after upgrading drizzle or `rows[0]` after enabling `noUncheckedIndexedAccess: true` | ||
.then((rows) => rows.find(() => true)); | ||
|
||
if (chainState?.blockNumber != null) { | ||
startBlock = chainState.blockNumber + 1n; | ||
console.log("resuming from block number", startBlock); | ||
} | ||
} catch (error) { | ||
// ignore errors for now | ||
} | ||
|
||
const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ | ||
storageAdapter, | ||
publicClient, | ||
startBlock, | ||
maxBlockRange: env.MAX_BLOCK_RANGE, | ||
address: env.STORE_ADDRESS, | ||
}); | ||
|
||
storedBlockLogs$.subscribe(); | ||
|
||
let isCaughtUp = false; | ||
combineLatest([latestBlockNumber$, storedBlockLogs$]) | ||
.pipe( | ||
filter( | ||
([latestBlockNumber, { blockNumber: lastBlockNumberProcessed }]) => latestBlockNumber === lastBlockNumberProcessed | ||
), | ||
first() | ||
) | ||
.subscribe(() => { | ||
isCaughtUp = true; | ||
console.log("all caught up"); | ||
}); | ||
|
||
if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) { | ||
const { default: Koa } = await import("koa"); | ||
const { default: cors } = await import("@koa/cors"); | ||
const { default: Router } = await import("@koa/router"); | ||
|
||
const server = new Koa(); | ||
server.use(cors()); | ||
|
||
const router = new Router(); | ||
|
||
router.get("/", (ctx) => { | ||
ctx.body = "emit HelloWorld();"; | ||
}); | ||
|
||
// k8s healthchecks | ||
router.get("/healthz", (ctx) => { | ||
ctx.status = 200; | ||
}); | ||
router.get("/readyz", (ctx) => { | ||
if (isCaughtUp) { | ||
ctx.status = 200; | ||
ctx.body = "ready"; | ||
} else { | ||
ctx.status = 424; | ||
ctx.body = "backfilling"; | ||
} | ||
}); | ||
|
||
server.use(router.routes()); | ||
server.use(router.allowedMethods()); | ||
|
||
server.listen({ host: env.HEALTHCHECK_HOST, port: env.HEALTHCHECK_PORT }); | ||
console.log( | ||
`postgres indexer healthcheck server listening on http://${env.HEALTHCHECK_HOST}:${env.HEALTHCHECK_PORT}` | ||
); | ||
} |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file mostly mirrors
postgres-indexer.ts