Repository navigation
Reuse stored PDA bumps in Anchor v1 and v2 constraints - #216
Open
mikemaccana wants to merge 1 commit into
Open
mikemaccana wants to merge 1 commit into
mikemaccana wants to merge 1 commit into
Conversation
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
force-pushed
the
claude/pda-bump-storage-solana-noyy1l
branch
from
November 20, 2026 22:33
511b386 to
4ecad7e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several Anchor examples stored a canonical bump on the account at creation and then ignored it, re-deriving the address with a bare
bumpconstraint on every later instruction. A barebumpmakes Anchor search from 255 downwards until it finds an off-curve address;bump = <account>.bumpreuses the stored value and costs a singlecreate_program_addressinstead.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-pointereven labels the field/// Canonical bump for this PDAand never reads it.What changed
20 constraints, 10 in
anchor/and 10 inanchor-v1/:finance/token-swapconfigtransfer-hook/countercounter_accounttransfer-hook/transfer-costcounter_accounttransfer-hook/account-data-as-seedcounter_accounttransfer-hook/transfer-switchadmin_config,wallet_switchnft/metadata-pointerplayertoken-swap's
pool_configalready reuses its stored bump, so only itsconfigentries were left to change.The stale comment in transfer-switch's
switch.rs, which explained whyAdminConfig.bumpwas not used for validation, is updated to match.What is deliberately left alone
initconstraints — the bump has to be found the first time.AllowedWalletPDA is anUncheckedAccountthat may legitimately not exist — a missing account is how "not allowed" is represented — so there is no stored bump to read.seeds/bumpconstraint; it usesaddress = SomePda::seeds(...), and the stored-bump equivalent is a handler-sideverify_program_address(... .with_bump(x.bump) ...).betting-marketalready 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:bump: u8is an internal function parameter fed fromcontext.bumpsinlib.rs, not instruction data. Had it come from the client, reusing it would have let a caller validate a non-canonical PDA.init_player.rsgovernsgame_data, notplayer;playeris plaininitwith an unconditional canonical write.chop_tree.rsbackfillsgame_data.bumpwithif bump == 0, implying accounts predating the field, so that one keeps its barebumpuntil the data is migrated.Testing
All 12 affected projects were built to a real
.sowithcargo build-sbfand 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 theadmin_configandwallet_switchconstraints changed here.cargo fmt --checkwants no change to any line touched here, andcargo checkwarnings are unchanged from baseline — verified by re-running with the change stashed.Toolchain note: run with Solana CLI 4.3.2 (
cargo-build-sbf4.4.0, platform-tools v1.57), chosen to match the Agave~4.3crate 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.