-
Notifications
You must be signed in to change notification settings - Fork 30
feat(indexer): add idempotency and runnable entrypoints (issues #54 &… #73
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,10 @@ export interface IndexerConfig { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly startLedger?: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Logging verbosity. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly logLevel: "error" | "warn" | "info" | "debug"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Configured Stellar contract IDs for streams indexer. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly streamsContractIds: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Configured Stellar contract IDs for distributions indexer. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly distributionsContractIds: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** A positive integer parsed from an environment string. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,6 +44,30 @@ const configSchema = z.object({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| POLL_INTERVAL_MS: positiveIntFromString, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| START_LEDGER: positiveIntFromString.optional(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| INDEXER_LOG_LEVEL: z.enum(["error", "warn", "info", "debug"]).optional().default("info"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| STREAMS_CONTRACT_IDS: z | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .string() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .optional() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .default("") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .transform((val) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(",") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((s) => s.trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(Boolean) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DISTRIBUTIONS_CONTRACT_IDS: z | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .string() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .optional() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .default("") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .transform((val) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(",") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((s) => s.trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(Boolean) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Deduplicate contract IDs during parsing. This transform leaves duplicates intact. Both indexer entrypoints register one handler set per configured contract ID, so Suggested fix STREAMS_CONTRACT_IDS: z
.string()
.optional()
.default("")
.transform((val) =>
val
- ? val
- .split(",")
- .map((s) => s.trim())
- .filter(Boolean)
+ ? [...new Set(val.split(",").map((s) => s.trim()).filter(Boolean))]
: [],
),
DISTRIBUTIONS_CONTRACT_IDS: z
.string()
.optional()
.default("")
.transform((val) =>
val
- ? val
- .split(",")
- .map((s) => s.trim())
- .filter(Boolean)
+ ? [...new Set(val.split(",").map((s) => s.trim()).filter(Boolean))]
: [],
),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -69,6 +97,8 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): IndexerConfig | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "POLL_INTERVAL_MS", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "START_LEDGER", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "INDEXER_LOG_LEVEL", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "STREAMS_CONTRACT_IDS", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "DISTRIBUTIONS_CONTRACT_IDS", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const value = env[key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalized[key] = value === undefined || value.trim() === "" ? undefined : value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -89,6 +119,8 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): IndexerConfig | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| port: parsed.INDEXER_PORT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pollIntervalMs: parsed.POLL_INTERVAL_MS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logLevel: parsed.INDEXER_LOG_LEVEL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| streamsContractIds: parsed.STREAMS_CONTRACT_IDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| distributionsContractIds: parsed.DISTRIBUTIONS_CONTRACT_IDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...(parsed.START_LEDGER !== undefined ? { startLedger: parsed.START_LEDGER } : {}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,5 @@ | ||
| import type { | ||
| EventHandler, | ||
| HandlerFilter, | ||
| HandlerResult, | ||
| SorobanEventInput, | ||
| } from "./types.js"; | ||
| import { type EventRepository, getSorobanEventIdentity } from "../db/repository.js"; | ||
| import type { EventHandler, HandlerFilter, HandlerResult, SorobanEventInput } from "./types.js"; | ||
|
|
||
| interface RegisteredHandler { | ||
| filter: HandlerFilter; | ||
|
|
@@ -35,16 +31,63 @@ export class HandlerRegistry { | |
| .map(({ handler }) => handler); | ||
| } | ||
|
|
||
| async dispatch(event: SorobanEventInput): Promise<HandlerResult[]> { | ||
| async dispatch(event: SorobanEventInput, eventRepo?: EventRepository): Promise<HandlerResult[]> { | ||
| let identity: | ||
| | { | ||
| contractId: string; | ||
| ledgerNumber: number; | ||
| txHash: string; | ||
| eventIndex: number; | ||
| } | ||
| | undefined; | ||
|
|
||
| if (eventRepo) { | ||
| identity = getSorobanEventIdentity(event); | ||
| const isProcessed = await eventRepo.isEventProcessed( | ||
| identity.contractId, | ||
| identity.ledgerNumber, | ||
| identity.txHash, | ||
| identity.eventIndex, | ||
| ); | ||
| if (isProcessed) { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| const handlers = this.matches(event); | ||
| return Promise.all( | ||
|
|
||
| if (handlers.length === 0) { | ||
| if (eventRepo && identity) { | ||
| await eventRepo.recordEventProcessed( | ||
| identity.contractId, | ||
| identity.ledgerNumber, | ||
| identity.txHash, | ||
| identity.eventIndex, | ||
| ); | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| const results = await Promise.all( | ||
| handlers.map((h) => | ||
| h(event).catch((err) => ({ | ||
| ok: false as const, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| retriable: true, | ||
| })) | ||
| ) | ||
| })), | ||
| ), | ||
| ); | ||
|
|
||
| const allSucceeded = results.every((r) => r.ok); | ||
| if (allSucceeded && eventRepo && identity) { | ||
| await eventRepo.recordEventProcessed( | ||
| identity.contractId, | ||
| identity.ledgerNumber, | ||
| identity.txHash, | ||
| identity.eventIndex, | ||
| ); | ||
|
Comment on lines
+44
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift The new idempotency flow is still race-prone across concurrent runners.
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| return results; | ||
| } | ||
| } | ||
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Mark the contract ID env vars as optional here.
Lines 73-74 label
STREAMS_CONTRACT_IDSandDISTRIBUTIONS_CONTRACT_IDSas required, but the runner explicitly starts with an emptycontractIdsarray and only warns. Reword these as "required to index events" or "optional, but the indexer becomes a no-op without them" to match runtime behavior.🤖 Prompt for AI Agents