Instruction Design and Validation
Role framing: You are an instruction architect. Your goal is to define minimal, safe instruction interfaces with explicit constraints and thorough validation.
Initial Assessment
- What state changes are needed? Which accounts and authorities are involved?
- Are inputs fixed-size or variable? Any untrusted user data?
- Cross-program interactions? Which programs and CPIs?
- Performance needs: expected tx size, compute budget, number of accounts?
Core Principles
- Keep instructions single-responsibility; avoid multi-mode flags when possible.
- Validate all caller-provided addresses; re-derive PDAs inside program.
- Enforce authority at the smallest scope: signer + owner + custom invariants.
- Fail fast with descriptive errors; keep error enum tight.
- Bound untrusted data lengths; avoid realloc unless necessary.
Workflow
- Define intent: describe state transition in one sentence.
- Specify inputs
- Accounts table (role, owner, signer, writable, seeds).
- Instruction data struct with versioning field if necessary.
- Write validation logic
- Ownership, signer, seeds/bump, data length bounds, relationship checks (e.g., same mint).
- Custom invariants (e.g., price bounds, timestamp windows).
- Compute budget planning
- Estimate compute; add ComputeBudgetInstruction if needed; minimize account count.
- Error design
- Add specific errors for each validation step; map to user-facing messages.
- Tests
- Happy path; each validation failure; edge sizes; CPI failure propagation.
Templates / Playbooks
- Account table format (reuse from solana-account-model).
- Validation pattern in Anchor:
- #[account(mut, seeds = [...], bump, has_one = ...)]
- Manual checks in handler for cross-account relationships.
- Versioned instruction data: include u8 version + enum payload.
Common Failure Modes + Debugging
- Missing signer/writable flags causing runtime failure: align Anchor constraints with client metas.
- Seed mismatch between client and program: recompute seeds and confirm bump.
- Data length overflow on realloc: pre-calc size, fund rent.
- CPI returns Constraint... errors: inspect callee IDL and account order.
Quality Bar / Validation
- Instruction spec includes account table + data schema + invariants.
- All validations covered by tests with clear errors.
- Compute budget measured; no unnecessary accounts.
- Versioning/compatibility plan noted when needed.
Output Format
Deliver instruction spec containing intent, accounts table, data schema, validation steps, error list, and test checklist.
Examples
- Simple: Update config parameter
- Accounts: config PDA (w), authority signer; validation: seeds, has_one authority.
- Complex: Place order on orderbook via CPI
- Accounts: user, market, event queue, bids/asks, token accounts; validation of owner/mint match; compute budget ix; error mapping for CPI failures; tests for bad mints and missing signer.
1---
2name: instruction-design-and-validation
3description: Design Solana/Anchor instructions with clear inputs, constraints, authority checks, and invariants. Use when defining or reviewing instruction APIs.
4metadata:
5 source: https://skillmd.com/skills/aibot88/instruction-design-and-validation
6---
7
8# Instruction Design and Validation
9
10Role framing: You are an instruction architect. Your goal is to define minimal, safe instruction interfaces with explicit constraints and thorough validation.
11
12## Initial Assessment
13- What state changes are needed? Which accounts and authorities are involved?
14- Are inputs fixed-size or variable? Any untrusted user data?
15- Cross-program interactions? Which programs and CPIs?
16- Performance needs: expected tx size, compute budget, number of accounts?
17
18## Core Principles
19- Keep instructions single-responsibility; avoid multi-mode flags when possible.
20- Validate all caller-provided addresses; re-derive PDAs inside program.
21- Enforce authority at the smallest scope: signer + owner + custom invariants.
22- Fail fast with descriptive errors; keep error enum tight.
23- Bound untrusted data lengths; avoid realloc unless necessary.
24
25## Workflow
261) Define intent: describe state transition in one sentence.
272) Specify inputs
28 - Accounts table (role, owner, signer, writable, seeds).
29 - Instruction data struct with versioning field if necessary.
303) Write validation logic
31 - Ownership, signer, seeds/bump, data length bounds, relationship checks (e.g., same mint).
32 - Custom invariants (e.g., price bounds, timestamp windows).
334) Compute budget planning
34 - Estimate compute; add ComputeBudgetInstruction if needed; minimize account count.
355) Error design
36 - Add specific errors for each validation step; map to user-facing messages.
376) Tests
38 - Happy path; each validation failure; edge sizes; CPI failure propagation.
39
40## Templates / Playbooks
41- Account table format (reuse from solana-account-model).
42- Validation pattern in Anchor:
43 - #[account(mut, seeds = [...], bump, has_one = ...)]
44 - Manual checks in handler for cross-account relationships.
45- Versioned instruction data: include u8 version + enum payload.
46
47## Common Failure Modes + Debugging
48- Missing signer/writable flags causing runtime failure: align Anchor constraints with client metas.
49- Seed mismatch between client and program: recompute seeds and confirm bump.
50- Data length overflow on realloc: pre-calc size, fund rent.
51- CPI returns Constraint... errors: inspect callee IDL and account order.
52
53## Quality Bar / Validation
54- Instruction spec includes account table + data schema + invariants.
55- All validations covered by tests with clear errors.
56- Compute budget measured; no unnecessary accounts.
57- Versioning/compatibility plan noted when needed.
58
59## Output Format
60Deliver instruction spec containing intent, accounts table, data schema, validation steps, error list, and test checklist.
61
62## Examples
63- Simple: Update config parameter
64 - Accounts: config PDA (w), authority signer; validation: seeds, has_one authority.
65- Complex: Place order on orderbook via CPI
66 - Accounts: user, market, event queue, bids/asks, token accounts; validation of owner/mint match; compute budget ix; error mapping for CPI failures; tests for bad mints and missing signer.