|
23 | 23 | * in this module, which is executed in step 5 b) and is guaranteed to be executed on every ENSIndexer instance startup, |
24 | 24 | * regardless of the state of Ponder Checkpoints or whether any setup handlers were registered. |
25 | 25 | */ |
26 | | -import { getUnixTime } from "date-fns"; |
27 | | - |
28 | | -import { |
29 | | - buildCrossChainIndexingStatusSnapshotOmnichain, |
30 | | - buildEnsIndexerStackInfo, |
31 | | - buildIndexingMetadataContextInitialized, |
32 | | - IndexingMetadataContextStatusCodes, |
33 | | - OmnichainIndexingStatusIds, |
34 | | - validateEnsIndexerPublicConfigCompatibility, |
35 | | -} from "@ensnode/ensnode-sdk"; |
36 | 26 |
|
37 | 27 | import { migrateEnsNodeSchema } from "@/lib/ensdb/migrate-ensnode-schema"; |
38 | 28 | import { ensDbClient } from "@/lib/ensdb/singleton"; |
39 | 29 | import { startEnsDbWriterWorker } from "@/lib/ensdb-writer-worker/singleton"; |
40 | 30 | import { |
41 | | - ensRainbowClient, |
42 | 31 | waitForEnsRainbowToBeHealthy, |
43 | 32 | waitForEnsRainbowToBeReady, |
44 | 33 | } from "@/lib/ensrainbow/singleton"; |
45 | | -import { indexingStatusBuilder } from "@/lib/indexing-status-builder/singleton"; |
| 34 | +import { indexingMetadataContextBuilder } from "@/lib/indexing-metadata-context-builder/singleton"; |
46 | 35 | import { logger } from "@/lib/logger"; |
47 | | -import { publicConfigBuilder } from "@/lib/public-config-builder/singleton"; |
48 | 36 |
|
49 | 37 | /** |
50 | 38 | * Prepare for executing the "onchain" event handlers. |
@@ -74,104 +62,36 @@ import { publicConfigBuilder } from "@/lib/public-config-builder/singleton"; |
74 | 62 | * 1. Make ENSDb instance "ready" for ENSDb clients to use. |
75 | 63 | */ |
76 | 64 | export async function initIndexingOnchainEvents(): Promise<void> { |
77 | | - // TODO: wait for ENSDb instance to be healthy |
78 | | - // Ensure the ENSNode Schema in ENSDb is up to date by running any pending migrations. |
79 | | - await migrateEnsNodeSchema(); |
80 | | - |
81 | | - // Before calling `ensRainbowClient.config()`, we want to make sure that |
82 | | - // the ENSRainbow instance is healthy and ready to serve requests. |
83 | | - // This is a quick check, as we expect the ENSRainbow instance to be healthy |
84 | | - // by the time ENSIndexer instance executes `initIndexingOnchainEvents`. |
85 | | - await waitForEnsRainbowToBeHealthy(); |
86 | | - |
87 | 65 | try { |
88 | | - const [ |
89 | | - inMemoryIndexingStatusSnapshot, |
90 | | - inMemoryEnsDbPublicConfig, |
91 | | - inMemoryEnsIndexerPublicConfig, |
92 | | - inMemoryEnsRainbowPublicConfig, |
93 | | - storedIndexingMetadataContext, |
94 | | - ] = await Promise.all([ |
95 | | - indexingStatusBuilder.getOmnichainIndexingStatusSnapshot(), |
96 | | - ensDbClient.buildEnsDbPublicConfig(), |
97 | | - publicConfigBuilder.getPublicConfig(), |
98 | | - ensRainbowClient.config(), |
99 | | - ensDbClient.getIndexingMetadataContext(), |
100 | | - ]); |
101 | | - |
102 | | - if ( |
103 | | - storedIndexingMetadataContext.statusCode === IndexingMetadataContextStatusCodes.Uninitialized |
104 | | - ) { |
105 | | - logger.info({ |
106 | | - msg: `Indexing Metadata Context is "uninitialized"`, |
107 | | - }); |
108 | | - |
109 | | - // Invariant: indexing status must be "unstarted" when the indexing metadata context is uninitialized, |
110 | | - // since we haven't started processing any onchain events yet |
111 | | - if (inMemoryIndexingStatusSnapshot.omnichainStatus !== OmnichainIndexingStatusIds.Unstarted) { |
112 | | - throw new Error( |
113 | | - `Omnichain indexing status must be "unstarted" for "uninitialized" Indexing Metadata Context. Provided omnichain indexing status "${inMemoryIndexingStatusSnapshot.omnichainStatus}".`, |
114 | | - ); |
115 | | - } |
116 | | - } else { |
117 | | - logger.info({ |
118 | | - msg: `Indexing Metadata Context is "initialized"`, |
119 | | - }); |
120 | | - logger.debug({ |
121 | | - msg: `Indexing Metadata Context`, |
122 | | - indexingStatus: storedIndexingMetadataContext.indexingStatus, |
123 | | - stackInfo: storedIndexingMetadataContext.stackInfo, |
124 | | - }); |
125 | | - // if (ensIndexerPublicConfig.ensIndexerBuildId !== storedIndexingMetadataContext.stackInfo.ensIndexer.ensIndexerBuildId) { |
126 | | - // TODO: store the `ensIndexerPublicConfig` object in ENSDb so `storedIndexingMetadataContext.stackInfo.ensIndexer` is updated |
127 | | - // } |
128 | | - const { ensIndexer: storedEnsIndexerPublicConfig } = storedIndexingMetadataContext.stackInfo; |
129 | | - validateEnsIndexerPublicConfigCompatibility( |
130 | | - inMemoryEnsIndexerPublicConfig, |
131 | | - storedEnsIndexerPublicConfig, |
132 | | - ); |
133 | | - } |
| 66 | + // TODO: wait for ENSDb instance to be healthy before running any queries against it. |
134 | 67 |
|
135 | | - // Build the {@link CrossChainIndexingStatusSnapshot} with the current snapshot time. |
136 | | - // This is important to make sure the `snapshotTime` is always up to date in |
137 | | - // the indexing status snapshot stored in ENSDb. |
138 | | - const now = getUnixTime(new Date()); |
139 | | - const crossChainIndexingStatusSnapshot = buildCrossChainIndexingStatusSnapshotOmnichain( |
140 | | - inMemoryIndexingStatusSnapshot, |
141 | | - now, |
142 | | - ); |
| 68 | + // Ensure the ENSNode Schema in ENSDb is up to date by running any pending migrations. |
| 69 | + await migrateEnsNodeSchema(); |
143 | 70 |
|
144 | | - // Build EnsIndexerStackInfo based on the current state of in-memory public |
145 | | - // config objects. It's unlikely, but possible, that after the ENSIndexer |
146 | | - // instance restarts, some values in the public config objects have changed |
147 | | - // compared to the previous instance before the restart. For example, |
148 | | - // if the ENSIndexer instance is redeployed with a new version of the code that has different default values for some config parameters, or if there are changes in the environment variables used to build the public config objects. |
149 | | - const updatedStackInfo = buildEnsIndexerStackInfo( |
150 | | - inMemoryEnsDbPublicConfig, |
151 | | - inMemoryEnsIndexerPublicConfig, |
152 | | - inMemoryEnsRainbowPublicConfig, |
153 | | - ); |
| 71 | + // Before calling `ensRainbowClient.config()`, we want to make sure that |
| 72 | + // the ENSRainbow instance is healthy and ready to serve requests. |
| 73 | + // This is a quick check, as we expect the ENSRainbow instance to be healthy |
| 74 | + // by the time ENSIndexer instance executes `initIndexingOnchainEvents`. |
| 75 | + await waitForEnsRainbowToBeHealthy(); |
154 | 76 |
|
155 | | - const updatedIndexingMetadataContext = buildIndexingMetadataContextInitialized( |
156 | | - crossChainIndexingStatusSnapshot, |
157 | | - updatedStackInfo, |
158 | | - ); |
| 77 | + const indexingMetadataContext = |
| 78 | + await indexingMetadataContextBuilder.getIndexingMetadataContext(); |
159 | 79 |
|
160 | 80 | logger.info({ |
161 | 81 | msg: `Upserting Indexing Metadata Context Initialized`, |
162 | 82 | }); |
163 | 83 | logger.debug({ |
164 | 84 | msg: `Indexing Metadata Context`, |
165 | | - indexingStatus: updatedIndexingMetadataContext.indexingStatus, |
166 | | - stackInfo: updatedIndexingMetadataContext.stackInfo, |
| 85 | + indexingStatus: indexingMetadataContext.indexingStatus, |
| 86 | + stackInfo: indexingMetadataContext.stackInfo, |
167 | 87 | }); |
168 | | - await ensDbClient.upsertIndexingMetadataContext(updatedIndexingMetadataContext); |
| 88 | + await ensDbClient.upsertIndexingMetadataContext(indexingMetadataContext); |
169 | 89 | logger.info({ |
170 | 90 | msg: `Successfully upserted Indexing Metadata Context Initialized`, |
171 | 91 | }); |
172 | 92 |
|
173 | 93 | // Before starting to process onchain events, we want to make sure that |
174 | | - // ENSRainbow is ready and ready to serve the "heal" requests. |
| 94 | + // ENSRainbow is ready to serve the "heal" requests. |
175 | 95 | await waitForEnsRainbowToBeReady(); |
176 | 96 |
|
177 | 97 | // TODO: start Indexing Status Sync worker |
|
0 commit comments