-
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.
lang: Add
#[instruction]
attribute proc-macro (#3137)
- Loading branch information
1 parent
5a20cd9
commit 3f945f6
Showing
16 changed files
with
292 additions
and
13 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[programs.localnet] | ||
custom_discriminator = "CustomDiscriminator111111111111111111111111" | ||
|
||
[provider] | ||
cluster = "localnet" | ||
wallet = "~/.config/solana/id.json" | ||
|
||
[scripts] | ||
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.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,14 @@ | ||
[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 |
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,16 @@ | ||
{ | ||
"name": "custom-discriminator", | ||
"version": "0.30.1", | ||
"license": "(MIT OR Apache-2.0)", | ||
"homepage": "https://github.com/coral-xyz/anchor#readme", | ||
"bugs": { | ||
"url": "https://github.com/coral-xyz/anchor/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/coral-xyz/anchor.git" | ||
}, | ||
"engines": { | ||
"node": ">=17" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/custom-discriminator/programs/custom-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,19 @@ | ||
[package] | ||
name = "custom-discriminator" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "custom_discriminator" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
idl-build = ["anchor-lang/idl-build"] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
2 changes: 2 additions & 0 deletions
2
tests/custom-discriminator/programs/custom-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 = [] |
47 changes: 47 additions & 0 deletions
47
tests/custom-discriminator/programs/custom-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,47 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("CustomDiscriminator111111111111111111111111"); | ||
|
||
const CONST_DISC: &'static [u8] = &[55, 66, 77, 88]; | ||
|
||
const fn get_disc(input: &str) -> &'static [u8] { | ||
match input.as_bytes() { | ||
b"wow" => &[4 + 5, 55 / 5], | ||
_ => unimplemented!(), | ||
} | ||
} | ||
|
||
#[program] | ||
pub mod custom_discriminator { | ||
use super::*; | ||
|
||
#[instruction(discriminator = 0)] | ||
pub fn int(_ctx: Context<DefaultIx>) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
#[instruction(discriminator = [1, 2, 3, 4])] | ||
pub fn array(_ctx: Context<DefaultIx>) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
#[instruction(discriminator = b"hi")] | ||
pub fn byte_str(_ctx: Context<DefaultIx>) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
#[instruction(discriminator = CONST_DISC)] | ||
pub fn constant(_ctx: Context<DefaultIx>) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
#[instruction(discriminator = get_disc("wow"))] | ||
pub fn const_fn(_ctx: Context<DefaultIx>) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct DefaultIx<'info> { | ||
pub signer: Signer<'info>, | ||
} |
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 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import assert from "assert"; | ||
|
||
import type { CustomDiscriminator } from "../target/types/custom_discriminator"; | ||
|
||
describe("custom-discriminator", () => { | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
const program: anchor.Program<CustomDiscriminator> = | ||
anchor.workspace.customDiscriminator; | ||
|
||
describe("Can use custom instruction discriminators", () => { | ||
const testCommon = async (ixName: keyof typeof program["methods"]) => { | ||
const tx = await program.methods[ixName]().transaction(); | ||
|
||
// Verify discriminator | ||
const ix = program.idl.instructions.find((ix) => ix.name === ixName)!; | ||
assert(ix.discriminator.length < 8); | ||
const data = tx.instructions[0].data; | ||
assert(data.equals(Buffer.from(ix.discriminator))); | ||
|
||
// Verify tx runs | ||
await program.provider.sendAndConfirm!(tx); | ||
}; | ||
|
||
it("Integer", () => testCommon("int")); | ||
it("Array", () => testCommon("array")); | ||
it("Byte string", () => testCommon("byteStr")); | ||
it("Constant", () => testCommon("constant")); | ||
it("Const Fn", () => testCommon("constFn")); | ||
}); | ||
}); |
Oops, something went wrong.