|
| 1 | +import { |
| 2 | + type ChainIdString, |
| 3 | + ChainIndexingConfigTypeIds, |
| 4 | + ChainIndexingStatusIds, |
| 5 | + type ChainIndexingStatusSnapshot, |
| 6 | + createIndexingConfig, |
| 7 | + deserializeBlockRef, |
| 8 | + deserializeChainId, |
| 9 | + deserializeChainIndexingStatusSnapshot, |
| 10 | + deserializeNonNegativeInteger, |
| 11 | + type SerializedChainIndexingStatusSnapshot, |
| 12 | + type SerializedChainIndexingStatusSnapshotBackfill, |
| 13 | + type SerializedChainIndexingStatusSnapshotCompleted, |
| 14 | + type SerializedChainIndexingStatusSnapshotFollowing, |
| 15 | + type SerializedChainIndexingStatusSnapshotQueued, |
| 16 | +} from "@ensnode/ensnode-sdk"; |
| 17 | +import type { |
| 18 | + ChainBlockRefs, |
| 19 | + ChainMetadata, |
| 20 | + ChainName, |
| 21 | + PonderMetricsResponse, |
| 22 | + PonderStatusResponse, |
| 23 | + UnvalidatedChainMetadata, |
| 24 | +} from "@ensnode/ponder-sdk"; |
| 25 | + |
| 26 | +/** |
| 27 | + * Create {@link ChainIndexingStatusSnapshot} for the indexed chain metadata. |
| 28 | + */ |
| 29 | +export function createChainIndexingSnapshot( |
| 30 | + chainMetadata: ChainMetadata, |
| 31 | +): ChainIndexingStatusSnapshot { |
| 32 | + const { |
| 33 | + config: chainBlocksConfig, |
| 34 | + backfillEndBlock: chainBackfillEndBlock, |
| 35 | + isSyncComplete, |
| 36 | + isSyncRealtime, |
| 37 | + syncBlock: chainSyncBlock, |
| 38 | + statusBlock: chainStatusBlock, |
| 39 | + } = chainMetadata; |
| 40 | + |
| 41 | + const { startBlock, endBlock } = chainBlocksConfig; |
| 42 | + const config = createIndexingConfig(startBlock, endBlock); |
| 43 | + |
| 44 | + // In omnichain ordering, if the startBlock is the same as the |
| 45 | + // status block, the chain has not started yet. |
| 46 | + if (chainBlocksConfig.startBlock.number === chainStatusBlock.number) { |
| 47 | + return deserializeChainIndexingStatusSnapshot({ |
| 48 | + chainStatus: ChainIndexingStatusIds.Queued, |
| 49 | + config, |
| 50 | + } satisfies SerializedChainIndexingStatusSnapshotQueued); |
| 51 | + } |
| 52 | + |
| 53 | + if (isSyncComplete) { |
| 54 | + if (config.configType !== ChainIndexingConfigTypeIds.Definite) { |
| 55 | + throw new Error( |
| 56 | + `The '${ChainIndexingStatusIds.Completed}' indexing status can be only created with the '${ChainIndexingConfigTypeIds.Definite}' indexing config type.`, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return deserializeChainIndexingStatusSnapshot({ |
| 61 | + chainStatus: ChainIndexingStatusIds.Completed, |
| 62 | + latestIndexedBlock: chainStatusBlock, |
| 63 | + config, |
| 64 | + } satisfies SerializedChainIndexingStatusSnapshotCompleted); |
| 65 | + } |
| 66 | + |
| 67 | + if (isSyncRealtime) { |
| 68 | + if (config.configType !== ChainIndexingConfigTypeIds.Indefinite) { |
| 69 | + throw new Error( |
| 70 | + `The '${ChainIndexingStatusIds.Following}' indexing status can be only created with the '${ChainIndexingConfigTypeIds.Indefinite}' indexing config type.`, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + return deserializeChainIndexingStatusSnapshot({ |
| 75 | + chainStatus: ChainIndexingStatusIds.Following, |
| 76 | + latestIndexedBlock: chainStatusBlock, |
| 77 | + latestKnownBlock: chainSyncBlock, |
| 78 | + config: { |
| 79 | + configType: config.configType, |
| 80 | + startBlock: config.startBlock, |
| 81 | + }, |
| 82 | + } satisfies SerializedChainIndexingStatusSnapshotFollowing); |
| 83 | + } |
| 84 | + |
| 85 | + return deserializeChainIndexingStatusSnapshot({ |
| 86 | + chainStatus: ChainIndexingStatusIds.Backfill, |
| 87 | + latestIndexedBlock: chainStatusBlock, |
| 88 | + backfillEndBlock: chainBackfillEndBlock, |
| 89 | + config, |
| 90 | + } satisfies SerializedChainIndexingStatusSnapshotBackfill); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Create serialized chain indexing snapshots. |
| 95 | + * |
| 96 | + * The output of this function is required for |
| 97 | + * calling {@link createOmnichainIndexingSnapshot}. |
| 98 | + */ |
| 99 | +export function createSerializedChainSnapshots( |
| 100 | + chainIds: ChainIdString[], |
| 101 | + chainsBlockRefs: Map<ChainName, ChainBlockRefs>, |
| 102 | + metrics: PonderMetricsResponse, |
| 103 | + status: PonderStatusResponse, |
| 104 | +): Record<ChainIdString, SerializedChainIndexingStatusSnapshot> { |
| 105 | + const serializedChainIndexingStatusSnapshots = {} as Record< |
| 106 | + ChainIdString, |
| 107 | + ChainIndexingStatusSnapshot |
| 108 | + >; |
| 109 | + |
| 110 | + // collect unvalidated chain metadata for each indexed chain |
| 111 | + for (const chainId of chainIds) { |
| 112 | + const chainBlockRefs = chainsBlockRefs.get(chainId); |
| 113 | + |
| 114 | + const statusChainId = deserializeChainId(`${status[chainId]?.id}`); |
| 115 | + |
| 116 | + const backfillEndBlock = deserializeBlockRef(chainBlockRefs?.backfillEndBlock); |
| 117 | + |
| 118 | + const syncBlock = deserializeBlockRef({ |
| 119 | + number: metrics.getValue("ponder_sync_block", { chain: chainId }), |
| 120 | + timestamp: metrics.getValue("ponder_sync_block_timestamp", { chain: chainId }), |
| 121 | + }); |
| 122 | + |
| 123 | + const statusBlock = deserializeBlockRef({ |
| 124 | + number: status[chainId]?.block.number, |
| 125 | + timestamp: status[chainId]?.block.timestamp, |
| 126 | + }); |
| 127 | + |
| 128 | + const historicalTotalBlocks = deserializeNonNegativeInteger( |
| 129 | + metrics.getValue("ponder_historical_total_blocks", { |
| 130 | + chain: chainId, |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + const isSyncComplete = metrics.getValue("ponder_sync_is_complete", { chain: chainId }); |
| 135 | + |
| 136 | + const isSyncRealtime = metrics.getValue("ponder_sync_is_realtime", { chain: chainId }); |
| 137 | + |
| 138 | + if (typeof isSyncRealtime === "string" && !["0", "1"].includes(isSyncRealtime)) { |
| 139 | + throw new Error( |
| 140 | + `The 'ponder_sync_is_realtime' metric for chain '${chainId}' must be a string with value "0" or "1".`, |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + const config = { |
| 145 | + startBlock: deserializeBlockRef(chainBlockRefs?.config.startBlock), |
| 146 | + endBlock: |
| 147 | + chainBlockRefs?.config.endBlock === null |
| 148 | + ? null |
| 149 | + : deserializeBlockRef(chainBlockRefs?.config.endBlock), |
| 150 | + }; |
| 151 | + |
| 152 | + const chainMetadata = { |
| 153 | + chainId: statusChainId, |
| 154 | + isSyncComplete: String(isSyncComplete) === "1", |
| 155 | + isSyncRealtime: String(isSyncRealtime) === "1", |
| 156 | + config, |
| 157 | + backfillEndBlock, |
| 158 | + historicalTotalBlocks, |
| 159 | + syncBlock, |
| 160 | + statusBlock, |
| 161 | + } satisfies UnvalidatedChainMetadata; |
| 162 | + |
| 163 | + serializedChainIndexingStatusSnapshots[chainId] = createChainIndexingSnapshot(chainMetadata); |
| 164 | + } |
| 165 | + |
| 166 | + return serializedChainIndexingStatusSnapshots; |
| 167 | +} |
0 commit comments