-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
idl: Check ambiguous discriminators (#3157)
- Loading branch information
1 parent
dfb2de5
commit 438c481
Showing
7 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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
20 changes: 20 additions & 0 deletions
20
tests/custom-discriminator/programs/ambiguous-discriminator/Cargo.toml
This file contains 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,20 @@ | ||
[package] | ||
name = "ambiguous-discriminator" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "ambiguous_discriminator" | ||
|
||
[features] | ||
default = [] | ||
cpi = ["no-entrypoint"] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
idl-build = ["anchor-lang/idl-build"] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
2 changes: 2 additions & 0 deletions
2
tests/custom-discriminator/programs/ambiguous-discriminator/Xargo.toml
This file contains 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,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
31 changes: 31 additions & 0 deletions
31
tests/custom-discriminator/programs/ambiguous-discriminator/src/lib.rs
This file contains 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,31 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("AmbiguousDiscriminator111111111111111111111"); | ||
|
||
#[program] | ||
pub mod ambiguous_discriminator { | ||
use super::*; | ||
|
||
/// Compilation should error due to ambiguous discriminators. | ||
pub fn check_accounts(_ctx: Context<CheckAccounts>) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct CheckAccounts<'info> { | ||
pub some_account: Account<'info, SomeAccount>, | ||
pub another_account: Account<'info, AnotherAccount>, | ||
} | ||
|
||
#[account(discriminator = 1)] | ||
pub struct SomeAccount { | ||
pub a: u8, | ||
pub b: u16, | ||
pub c: u32, | ||
} | ||
|
||
#[account(discriminator = [1, 2, 3, 4])] | ||
pub struct AnotherAccount { | ||
pub a: u32, | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/custom-discriminator/tests/ambiguous-discriminator.ts
This file contains 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 @@ | ||
import { spawnSync } from "child_process"; | ||
|
||
describe("ambiguous-discriminator", () => { | ||
it("Returns ambiguous discriminator error on builds", () => { | ||
const result = spawnSync("anchor", [ | ||
"idl", | ||
"build", | ||
"-p", | ||
"ambiguous-discriminator", | ||
]); | ||
if (result.status === 0) { | ||
throw new Error("Ambiguous errors did not make building the IDL fail"); | ||
} | ||
|
||
const output = result.output.toString(); | ||
if (!output.includes("Error: Program ambiguous-discriminator not found")) { | ||
throw new Error( | ||
`Ambiguous discriminators did not return the expected error: "${output}"` | ||
); | ||
} | ||
}); | ||
}); |