Commit f334368
committed
fix: add pub bump: u8 to PDA structs per skill rule
The Solana Anchor coding-style skill requires every struct that lives in a PDA to store its canonical bump so later instructions can validate and re-sign without a second `find_program_address` derivation (which costs ~1.5k CU per call).
Structs updated (13 of 15 from the audit list):
basics/
- favorites::Favorites (new bump field + `set_inner` pass-through)
tokens/token-swap/
- state::Amm (new bump field + saved in `handle_create_amm`)
- state::Pool (new bump field + saved in `handle_create_pool`)
tokens/token-extensions/transfer-hook/
- counter::CounterAccount (`b"counter"` PDA)
- transfer-cost::CounterAccount (`b"counter"` PDA)
- account-data-as-seed::CounterAccount (`b"counter", payer` PDA)
- whitelist::WhiteList (`b"white_list"` PDA)
- allow-block-list-token::ABWallet (`[AB_WALLET_SEED, wallet]` PDA — bump threaded through the `init_wallet` impl signature)
- transfer-switch::TransferSwitch (`[wallet]` PDA — bump threaded through `handle_switch`)
- transfer-switch::AdminConfig (`[b"admin-config"]` PDA — bump threaded through `handle_configure_admin`; note the struct already had `pub bump: u8` in Config but wasn't saved)
tokens/token-extensions/nft-meta-data-pointer/
- extension_nft::GameData (new bump field; saved on first create in `init_player` *and* `chop_tree` which both use init_if_needed — guarded by `if bump == 0` so repeat calls don't overwrite)
- extension_nft::PlayerData (new bump field; saved in `handle_init_player`)
tokens/token-fundraiser/
- state::Contributor (new bump field; `handle_contribute` now takes `&ContributeBumps`; init_if_needed guarded with `if bump == 0`)
Deliberately not touched (audit mis-classified these as PDAs):
- basics/realloc/anchor::Message — declared with `#[account]` but its `#[account(init, ...)]` constraint has NO `seeds =` attribute, so it's allocated as a plain keypair account, not a PDA. No bump to store.
- tokens/external-delegate-token-master::UserAccount — same: initialized without `seeds =`. It's *referenced* as a seed in downstream instructions (`seeds = [user_account.key().as_ref()]`), but the account itself isn't a PDA.
For accounts using `init_if_needed`, the bump is only persisted on the first-init path, guarded by `if account.bump == 0`. Bump is deterministic (canonical bump is unique per (seeds, program_id) pair) so zero is a safe sentinel — a genuine `bump == 0` never occurs for canonical PDAs on mainnet (probability ~1/256 per PDA, and Anchor always picks the highest bump). Subsequent calls are no-ops for that field.
Verified with `cargo check --workspace` per affected anchor project: all pass (pre-existing warnings about `mut context` unused-mut in some handlers are unrelated).
4th of 5 commits in the skill-audit cleanup.1 parent a448bc6 commit f334368
26 files changed
Lines changed: 83 additions & 8 deletions
File tree
- basics/favorites/anchor/programs/favorites/src
- tokens
- token-extensions
- nft-meta-data-pointer/anchor-example/anchor/programs/extension_nft/src
- instructions
- state
- transfer-hook
- account-data-as-seed/anchor/programs/transfer-hook/src
- instructions
- allow-block-list-token/anchor/programs/abl-token/src
- instructions
- counter/anchor/programs/transfer-hook/src
- instructions
- transfer-cost/anchor/programs/transfer-hook/src
- instructions
- transfer-switch/anchor/programs/transfer-switch/src
- instructions
- whitelist/anchor/programs/transfer-hook/src
- instructions
- token-fundraiser/anchor/programs/fundraiser/src
- instructions
- state
- token-swap/anchor/programs/token-swap/src
- instructions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
10 | 18 | | |
11 | 19 | | |
12 | 20 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
93 | 95 | | |
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
0 commit comments