Repository navigation
[2.x] Resolve data field program ids and validate PDA and instruction links - #1289
Conversation
🦋 Changeset detectedLatest commit: 9760fe8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
trevor-cortex
left a comment
There was a problem hiding this comment.
Summary
Ports three codama-rs fixes to the JS side:
getResolvedInstructionInputsVisitor—getDependenciesnow pushespdaValueNode.programIdunconditionally (its type isAccountValueNode | DataValueNode, so the oldaccountValueNode-only guard silently dropped data-field program ids and the field could resolve after the PDA that reads it).getResolvedInstructionInputsVisitor— theCANNOT_USE_OPTIONAL_ACCOUNT_AS_PDA_SEED_VALUEpath now keys offisNode(account.defaultValue, 'injectedValueNode')rather than reference equality, so a PDA whose seed was injected (and therefore rebuilt byProvidedScope.resolveWithin) keeps[instruction, account, …]as its prefix.getValidationItemsVisitor— newvisitPdaLink/visitInstructionLink/visitInstructionAccountLinkchecks, with the existing account / defined-type link checks folded into a sharedgetMissingLinkItemsthat skips links naming a program absent fromlinkables.
I traced the new checks through LinkableDictionary: getPath([programLinkNode]) works for the external-program probe (program name comes from the link itself, no ancestors needed), instructionAccountLinkNode without an instruction falls back to the enclosing instruction via findInstructionNodeFromPath, and [...path, node.instruction] is a legitimate path for the nested instruction link. The exhaustiveness of the injectedValueNode dichotomy also holds: resolveDefaultValue only ever yields a pdaValueNode from a pdaValueNode or an injection, never from a conditionalValueNode.
Things to be aware of
- Behaviour change for consumers:
accountLinkNode/definedTypeLinkNodepointing at a program outside the tree used to be reported and are now silent. That's the intended trade-off (documented ingetMissingLinkItems's docblock — a typo inprogramis indistinguishable from an external program), and the changeset calls it out. Worth remembering that this also applies when validating a loneprogramNode: any cross-program link is now "external". - Validators goes
minor, visitors-corepatch. Since the core packages are a fixed group the whole group will bump minor anyway, so the split is cosmetic — just flagging in case you wanted both aligned. - One non-blocking note inline on the rebuilt-PDA error path: its tail nodes aren't tree nodes by identity. The new test deliberately only asserts kinds, so this is a conscious choice; see the comment for an alternative if path fidelity matters.
- Pre-existing and untouched: an
injectedValueNodewith afallback: pdaValueNode(...)that resolves via its fallback still reports[resolvedDefaultValue]even though that node is in the tree at[…, account, injected, fallback]. Not in scope here, just noting it for whoever next touches this branch.
Tests cover the typo case, same-name-in-other-program, external program, instruction-account-without-instruction, and the "only report the missing instruction once" case. LGTM.
| const pdaPath = isNode(account.defaultValue, 'injectedValueNode') | ||
| ? [resolvedDefaultValue] | ||
| : [...stack.getPath('instructionNode'), account, resolvedDefaultValue]; |
There was a problem hiding this comment.
Non-blocking. When a seed was injected, resolveWithin rebuilds the whole pdaValueNode through identityVisitor, so resolvedDefaultValue and the seed appended below are fresh objects — not the ones in the tree. The prefix is now right, but a consumer walking from account by identity (e.g. getNodePathUntilLastNode + child lookup) still won't find the tail.
If path fidelity matters, the original nodes are cheap to recover here since the offending seed is an accountValueNode and therefore untouched by resolution:
const original = account.defaultValue;
const originalSeed = (original.seeds ?? []).find(s => s.name === seed.name) ?? seed;
// path: [...stack.getPath('instructionNode'), account, original, originalSeed]If codama-rs reports the rebuilt value and you'd rather keep parity, a one-liner in the comment saying the tail is the resolved value would save the next reader the same detour.
There was a problem hiding this comment.
Keeping parity with codama-rs, which reports the rebuilt value too, and added the one-liner to the comment.
This PR ports three fixes from codama-rs to the JS visitors and validators.
getResolvedInstructionInputsVisitornow depends on a PDA value'sprogramIdwhen it is a data field, and keeps the instruction account in the path of the optional-seed error unless the whole default was injected (codama-idl/codama-rs#199).getValidationItemsVisitorreports PDA, instruction and instruction account links that point to no node, and treats links naming a program outside the tree as external (codama-idl/codama-rs#196).