Skip to content

Reuse stored PDA bumps in Anchor v1 and v2 constraints - #216

Open
mikemaccana wants to merge 1 commit into
mainfrom
claude/pda-bump-storage-solana-noyy1l
Open

mikemaccana wants to merge 1 commit into
mainfrom
claude/pda-bump-storage-solana-noyy1l

Conversation

@mikemaccana

@mikemaccana mikemaccana commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Several Anchor examples stored a canonical bump on the account at creation and then ignored it, re-deriving the address with a bare bump constraint on every later instruction. A bare bump makes Anchor search from 255 downwards until it finds an off-curve address; bump = <account>.bump reuses the stored value and costs a single create_program_address instead.

This came out of checking whether the examples actually demonstrate the claim that programs store the bump so nobody has to search for it again. In these cases they stored it and then searched anyway — nft/metadata-pointer even labels the field /// Canonical bump for this PDA and never reads it.

What changed

20 constraints, 10 in anchor/ and 10 in anchor-v1/:

Example Account
finance/token-swap config
transfer-hook/counter counter_account
transfer-hook/transfer-cost counter_account
transfer-hook/account-data-as-seed counter_account
transfer-hook/transfer-switch admin_config, wallet_switch
nft/metadata-pointer player

token-swap's pool_config already reuses its stored bump, so only its config entries were left to change.

The stale comment in transfer-switch's switch.rs, which explained why AdminConfig.bump was not used for validation, is updated to match.

What is deliberately left alone

  • init constraints — the bump has to be found the first time.
  • PDAs that own no account data to store a bump in — token vaults, authority PDAs, and PDAs owned by other programs (the extra-account-metas list, Metaplex accounts). These are the majority of the remaining bare bumps and there is nothing to reuse.
  • The allow-list transfer hook. Its per-wallet AllowedWallet PDA is an UncheckedAccount that may legitimately not exist — a missing account is how "not allowed" is represented — so there is no stored bump to read.
  • Quasar — not the same fix. Quasar has no seeds/bump constraint; it uses address = SomePda::seeds(...), and the stored-bump equivalent is a handler-side verify_program_address(... .with_bump(x.bump) ...). betting-market already does this in three places with a comment noting that the search "exhausts the CU budget on-chain". Worth doing separately.

Safety

Only accounts whose bump is written from context.bumps.* at initialisation are changed, so the stored value is always the canonical bump and each constraint stays exactly as strict as before. Two cases looked risky and were checked:

  • transfer-switch's bump: u8 is an internal function parameter fed from context.bumps in lib.rs, not instruction data. Had it come from the client, reusing it would have let a caller validate a non-canonical PDA.
  • the "only save bump if this is the first init" comment in init_player.rs governs game_data, not player; player is plain init with an unconditional canonical write.

chop_tree.rs backfills game_data.bump with if bump == 0, implying accounts predating the field, so that one keeps its bare bump until the data is migrated.

Testing

All 12 affected projects were built to a real .so with cargo build-sbf and their LiteSVM suites run. 12/12 pass, including both token-swap suites at 28 tests each, and both transfer-switch suites, which are the ones that exercise the admin_config and wallet_switch constraints changed here.

cargo fmt --check wants no change to any line touched here, and cargo check warnings are unchanged from baseline — verified by re-running with the change stashed.

Toolchain note: run with Solana CLI 4.3.2 (cargo-build-sbf 4.4.0, platform-tools v1.57), chosen to match the Agave ~4.3 crate line this repo pins, rather than the 3.1.14 the Anchor workflows install. So a green run here does not by itself prove the 3.1.14 CI job is green; the CI run on this PR covers that.

Several Anchor examples stored a canonical bump on the account at
creation and then ignored it, re-deriving the address with a bare
`bump` constraint on every later instruction. A bare `bump` makes
Anchor search from 255 downwards until it finds an off-curve address;
`bump = <account>.bump` reuses the stored value and costs a single
`create_program_address` instead.

Switch the 20 affected constraints (10 in `anchor/`, 10 in
`anchor-v1/`) to reuse the stored bump:

- finance/token-swap: `config`
- token-extensions/transfer-hook: counter, transfer-cost,
  account-data-as-seed, transfer-switch
- nft/metadata-pointer: `player`

token-swap's `pool_config` already reuses its stored bump, so only its
`config` entries are left to change. The allow-list transfer hook is not
included: its per-wallet `AllowedWallet` PDA is an `UncheckedAccount`
that may legitimately not exist, so there is no stored bump to read.

Only accounts whose bump is written from `context.bumps.*` at
initialisation are changed, so the stored value is always the canonical
bump and the constraint stays as strict as before. Constraints on
`init` accounts, and on PDAs that own no account data to store a bump in
(token vaults, authority PDAs, and PDAs owned by other programs such as
the extra-account-metas list), keep the bare `bump` - there is nothing
to reuse in those cases.

The stale comment in transfer-switch's `switch.rs`, which explained why
`AdminConfig.bump` was not used for validation, is updated to match.

Claude-Session: https://claude.ai/code/session_01YTTjefTz2Kbj8t5qg77kXg
@mikemaccana
mikemaccana force-pushed the claude/pda-bump-storage-solana-noyy1l branch from 511b386 to 4ecad7e Compare November 20, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants