-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: enfore type & count matching between ix handler and #instruction[(..)] args #4000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b92b2b1
fix: silent compiles of ix when #instruction[(..)] expects more args …
6f302a8
cargo fmt
c327b91
fix fmt
d7d22a0
fix: enfore type matching between ix and #instruction[(..)] args
79e9b73
fix: for non #instruction[(..)] accounts
a1d7347
fix: check for min(expect, actual) params and make remaining stub
0ae1bd3
fix: check for min(expect, actual) params and make remaining stub
b694443
chore(add): add unreachable derivate to silence unwanted warnings
5bb16b2
add diagnostic message
04e966d
Merge branch 'master' into anchor#3997
jacobcreech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
15 changes: 15 additions & 0 deletions
15
tests/test-instruction-validation/fail-args-count/Anchor.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [toolchain] | ||
|
|
||
| [features] | ||
| resolution = true | ||
| skip-lint = false | ||
|
|
||
| [programs.localnet] | ||
| test_instruction_validation = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" | ||
|
|
||
| [registry] | ||
| url = "https://api.apr.dev" | ||
|
|
||
| [provider] | ||
| cluster = "Localnet" | ||
| wallet = "~/.config/solana/id.json" |
16 changes: 16 additions & 0 deletions
16
tests/test-instruction-validation/fail-args-count/Cargo.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [workspace] | ||
| members = [ | ||
| "programs/*" | ||
| ] | ||
| resolver = "2" | ||
|
|
||
| [profile.release] | ||
| overflow-checks = true | ||
| lto = "fat" | ||
| codegen-units = 1 | ||
|
|
||
| [profile.release.build-override] | ||
| opt-level = 3 | ||
| incremental = false | ||
| codegen-units = 1 | ||
|
|
22 changes: 22 additions & 0 deletions
22
...st-instruction-validation/fail-args-count/programs/test-instruction-validation/Cargo.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [package] | ||
| name = "test-instruction-validation" | ||
| version = "0.1.0" | ||
| description = "Test for instruction parameter validation" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "lib"] | ||
| name = "test_instruction_validation" | ||
|
|
||
| [features] | ||
| default = [] | ||
| cpi = ["no-entrypoint"] | ||
| no-entrypoint = [] | ||
| no-idl = [] | ||
| no-log-ix-name = [] | ||
| idl-build = ["anchor-lang/idl-build"] | ||
| anchor-debug = ["anchor-lang/anchor-debug"] | ||
|
|
||
| [dependencies] | ||
| anchor-lang = { path = "../../../../../lang" } | ||
|
|
53 changes: 53 additions & 0 deletions
53
...st-instruction-validation/fail-args-count/programs/test-instruction-validation/src/lib.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #![allow(unexpected_cfgs)] | ||
|
|
||
| use anchor_lang::prelude::*; | ||
|
|
||
| declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); | ||
|
|
||
| type MyType = u64; | ||
| #[program] | ||
| pub mod test_instruction_validation { | ||
| use super::*; | ||
|
|
||
| // Test 1: Missing parameter - handler only has 1 arg but #[instruction] expects 2 | ||
| pub fn missing_instruction_attr( | ||
| _ctx: Context<MissingInstructionAttr>, | ||
| data: u64, // Handler has only 1 parameter | ||
| ) -> Result<()> { | ||
| msg!("Data: {}", data); | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn no_params(_ctx: Context<NoParams>) -> Result<()> { | ||
| msg!("No params needed"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Test 2: Type mismatch - handler has u64 but #[instruction(...)] has u8 | ||
| pub fn type_mismatch( | ||
| _ctx: Context<TypeMismatch>, | ||
| data: u64, // Handler parameter is u64 | ||
| ) -> Result<()> { | ||
| msg!("Data: {}", data); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Accounts)] | ||
| #[instruction(data: u64, ehe: u64)] // Expects 2 params but handler only has 1 | ||
| pub struct MissingInstructionAttr<'info> { | ||
| #[account(mut)] | ||
| pub user: Signer<'info>, | ||
| } | ||
|
|
||
| #[derive(Accounts)] | ||
| // No #[instruction(...)] - correct for no params | ||
| pub struct NoParams<'info> { | ||
| pub user: Signer<'info>, | ||
| } | ||
|
|
||
| #[derive(Accounts)] | ||
| #[instruction(data: MyType)] // Attribute specifies u8 but handler has u64 | ||
| pub struct TypeMismatch<'info> { | ||
| pub user: Signer<'info>, | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [toolchain] | ||
|
|
||
| [features] | ||
| resolution = true | ||
| skip-lint = false | ||
|
|
||
| [programs.localnet] | ||
| test_instruction_validation = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" | ||
|
|
||
| [registry] | ||
| url = "https://api.apr.dev" | ||
|
|
||
| [provider] | ||
| cluster = "Localnet" | ||
| wallet = "~/.config/solana/id.json" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [workspace] | ||
| members = [ | ||
| "programs/*" | ||
| ] | ||
| resolver = "2" | ||
|
|
||
| [profile.release] | ||
| overflow-checks = true | ||
| lto = "fat" | ||
| codegen-units = 1 | ||
|
|
||
| [profile.release.build-override] | ||
| opt-level = 3 | ||
| incremental = false | ||
| codegen-units = 1 | ||
|
|
22 changes: 22 additions & 0 deletions
22
tests/test-instruction-validation/fail-type/programs/test-instruction-validation/Cargo.toml
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [package] | ||
| name = "test-instruction-validation" | ||
| version = "0.1.0" | ||
| description = "Test for instruction parameter validation" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "lib"] | ||
| name = "test_instruction_validation" | ||
|
|
||
| [features] | ||
| default = [] | ||
| cpi = ["no-entrypoint"] | ||
| no-entrypoint = [] | ||
| no-idl = [] | ||
| no-log-ix-name = [] | ||
| idl-build = ["anchor-lang/idl-build"] | ||
| anchor-debug = ["anchor-lang/anchor-debug"] | ||
|
|
||
| [dependencies] | ||
| anchor-lang = { path = "../../../../../lang" } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.