|
| 1 | +import config from "@/config"; |
| 2 | + |
| 3 | +import { type LabelHash, makeSubdomainNode, type Node } from "enssdk"; |
| 4 | + |
| 5 | +import { getENSRootChainId } from "@ensnode/datasources"; |
| 6 | + |
| 7 | +import { ensIndexerSchema, type IndexingEngineContext } from "@/lib/indexing-engines/ponder"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Why two tables for one logical "is this node migrated?" check. |
| 11 | + * |
| 12 | + * The check fires from many Registry handlers, but the event payload differs between them: |
| 13 | + * - ENSv1Registry(Old)#NewOwner emits `parentNode` and `labelHash` as separate args. |
| 14 | + * - ENSv1RegistryOld#Transfer / NewTTL / NewResolver emit only the post-namehash `node` |
| 15 | + * |
| 16 | + * Ponder's indexing-cache prefetch path predicts hot-table reads ahead of each event by deriving |
| 17 | + * the lookup key from the event's args — but its profile-pattern matcher can only do direct equality |
| 18 | + * and single-level string-delimiter splits. It can NOT invert keccak. So a table keyed by the |
| 19 | + * post-namehash `node` is unprofileable from a NewOwner event (where `node` is a computed namehash |
| 20 | + * of `(parentNode, labelHash)`), and a table keyed by `(parentNode, labelHash)` is unprofileable |
| 21 | + * from a Transfer/NewTTL/NewResolver event (which doesn't carry those fields). |
| 22 | + * |
| 23 | + * Either single-table choice surrenders prefetch on other handlers. Keying solely by |
| 24 | + * `(parentNode, labelHash)` would help the NewOwner hot path but disable prefetching on the other |
| 25 | + * three handlers, which can't reconstruct that pair from `node` without a reverse-index whose lookup |
| 26 | + * key is itself a un-prefetchable namehash. |
| 27 | + * |
| 28 | + * The two-table layout sidesteps both problems: write _both_ rows on every migration, then have each |
| 29 | + * read site address the table whose key matches its event payload. Both reads stay on the prefetch |
| 30 | + * hot-path. The cost is one extra "insert on conflict do nothing" per migration, and the storage of |
| 31 | + * that information, naturally, doubles. As of 2026-04-29, the size of the migrated_nodes_by_parent |
| 32 | + * table is ~1GB, meaning that this optimization will consume an additional ~1GB of storage but |
| 33 | + * will result in significantly faster indexing for the ENSv1Registry(Old) events. |
| 34 | + * |
| 35 | + * See {@link migratedNodeByParent} and {@link migratedNodeByNode} in the ensdb-sdk schema. |
| 36 | + */ |
| 37 | + |
| 38 | +const invariant_isENSRootChain = (context: IndexingEngineContext) => { |
| 39 | + if (context.chain.id === getENSRootChainId(config.namespace)) return; |
| 40 | + |
| 41 | + throw new Error( |
| 42 | + `Invariant: Node migration status is only relevant on the ENS Root Chain, and this function was called in the context of ${context.chain.id}.`, |
| 43 | + ); |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Returns whether `(parentNode, labelHash)` has migrated to the new Registry contract. Used by |
| 48 | + * ENSv1RegistryOld#NewOwner where both fields are emitted as event args directly — keyed access |
| 49 | + * keeps the read on Ponder's prefetch hot-path. |
| 50 | + */ |
| 51 | +export async function nodeIsMigratedByParentAndLabel( |
| 52 | + context: IndexingEngineContext, |
| 53 | + parentNode: Node, |
| 54 | + labelHash: LabelHash, |
| 55 | +) { |
| 56 | + invariant_isENSRootChain(context); |
| 57 | + |
| 58 | + const record = await context.ensDb.find(ensIndexerSchema.migratedNodeByParent, { |
| 59 | + parentNode, |
| 60 | + labelHash, |
| 61 | + }); |
| 62 | + return record !== null; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Returns whether `node` has migrated to the new Registry contract. Used by |
| 67 | + * ENSv1RegistryOld#Transfer/NewTTL/NewResolver where only `node` is emitted as an event arg — |
| 68 | + * keyed access on the sibling {@link migratedNodeByNode} table keeps the read on the prefetch |
| 69 | + * hot-path even though the composite-key {@link migratedNodeByParent} table can't be addressed |
| 70 | + * without a reverse lookup. |
| 71 | + */ |
| 72 | +export async function nodeIsMigrated(context: IndexingEngineContext, node: Node) { |
| 73 | + invariant_isENSRootChain(context); |
| 74 | + |
| 75 | + const record = await context.ensDb.find(ensIndexerSchema.migratedNodeByNode, { node }); |
| 76 | + return record !== null; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Record that `(parentNode, labelHash)` has migrated to the new Registry contract. Writes both |
| 81 | + * the composite-key {@link migratedNodeByParent} row and its sibling {@link migratedNodeByNode} |
| 82 | + * index so each downstream read site can address whichever key it can profile against event args. |
| 83 | + */ |
| 84 | +export async function migrateNode( |
| 85 | + context: IndexingEngineContext, |
| 86 | + parentNode: Node, |
| 87 | + labelHash: LabelHash, |
| 88 | +) { |
| 89 | + invariant_isENSRootChain(context); |
| 90 | + |
| 91 | + await context.ensDb |
| 92 | + .insert(ensIndexerSchema.migratedNodeByParent) |
| 93 | + .values({ parentNode, labelHash }) |
| 94 | + .onConflictDoNothing(); |
| 95 | + |
| 96 | + const node = makeSubdomainNode(labelHash, parentNode); |
| 97 | + await context.ensDb |
| 98 | + .insert(ensIndexerSchema.migratedNodeByNode) |
| 99 | + .values({ node }) |
| 100 | + .onConflictDoNothing(); |
| 101 | +} |
0 commit comments