Skip to content

Commit 24371ef

Browse files
authored
lang: remove the state and interface attributes (otter-sec#2285)
1 parent 262c592 commit 24371ef

69 files changed

Lines changed: 148 additions & 3421 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/no-cashing-tests.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,8 @@ jobs:
251251
path: spl/token-proxy
252252
- cmd: cd tests/multisig && anchor test --skip-lint
253253
path: tests/multisig
254-
- cmd: cd tests/interface && anchor test --skip-lint
255-
path: tests/interface
256-
- cmd: cd tests/lockup && anchor test --skip-lint
257-
path: tests/lockup
254+
# - cmd: cd tests/lockup && anchor test --skip-lint
255+
# path: tests/lockup
258256
- cmd: cd tests/swap/deps/openbook-dex/dex && cargo build-bpf -- --locked && cd ../../../ && anchor test --skip-lint
259257
path: tests/swap
260258
- cmd: cd tests/escrow && anchor test --skip-lint && npx tsc --noEmit

.github/workflows/tests.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,8 @@ jobs:
361361
path: spl/token-proxy
362362
- cmd: cd tests/multisig && anchor test --skip-lint
363363
path: tests/multisig
364-
- cmd: cd tests/interface && anchor test --skip-lint
365-
path: tests/interface
366-
- cmd: cd tests/lockup && anchor test --skip-lint
367-
path: tests/lockup
364+
# - cmd: cd tests/lockup && anchor test --skip-lint
365+
# path: tests/lockup
368366
- cmd: cd tests/swap/deps/openbook-dex/dex && cargo build-bpf -- --locked && cd ../../../ && anchor test --skip-lint
369367
path: tests/swap
370368
- cmd: cd tests/escrow && anchor test --skip-lint && npx tsc --noEmit

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ The minor version will be incremented upon a breaking change and the patch versi
1111
## [Unreleased]
1212

1313
### Features
14+
1415
- cli: Add `env` option to verifiable builds ([#2325](https://github.com/coral-xyz/anchor/pull/2325)).
1516

1617
### Fixes
1718

1819
### Breaking
1920

21+
- lang: Remove `state` and `interface` attributes ([#2285](https://github.com/coral-xyz/anchor/pull/2285)).
22+
2023
## [0.26.0] - 2022-12-15
2124

2225
### Features

Cargo.lock

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ publish:
2323
sleep 25
2424
cd lang/attribute/error/ && cargo publish && cd ../../../
2525
sleep 25
26-
cd lang/attribute/interface/ && cargo publish && cd ../../../
27-
sleep 25
2826
cd lang/attribute/program/ && cargo publish && cd ../../..
2927
sleep 25
30-
cd lang/attribute/state/ && cargo publish && cd ../../../
31-
sleep 25
3228
cd lang/attribute/event/ && cargo publish && cd ../../../
3329
sleep 25
3430
cd lang/ && cargo publish && cd ../

client/example/src/main.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use optional::accounts::Initialize as OptionalInitialize;
1616
use optional::instruction as optional_instruction;
1717
// The `accounts` and `instructions` modules are generated by the framework.
1818
use basic_4::accounts as basic_4_accounts;
19-
use basic_4::basic_4::Counter as CounterState;
2019
use basic_4::instruction as basic_4_instruction;
20+
use basic_4::Counter as CounterAccount;
2121
use clap::Parser;
2222
// The `accounts` and `instructions` modules are generated by the framework.
2323
use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
@@ -207,24 +207,28 @@ fn events(client: &Client, pid: Pubkey) -> Result<()> {
207207
pub fn basic_4(client: &Client, pid: Pubkey) -> Result<()> {
208208
let program = client.program(pid);
209209
let authority = program.payer();
210+
let (counter, _) = Pubkey::find_program_address(&[b"counter"], &pid);
210211

211-
// Invoke the state's `new` constructor.
212212
program
213-
.state_request()
214-
.accounts(basic_4_accounts::Auth { authority })
215-
.new(basic_4_instruction::state::New)
213+
.request()
214+
.accounts(basic_4_accounts::Initialize {
215+
counter,
216+
authority,
217+
system_program: system_program::ID,
218+
})
219+
.args(basic_4_instruction::Initialize {})
216220
.send()?;
217-
let counter_account: CounterState = program.state()?;
221+
let counter_account: CounterAccount = program.account(counter)?;
218222
assert_eq!(counter_account.authority, authority);
219223
assert_eq!(counter_account.count, 0);
220224

221-
// Call a state method.
222225
program
223-
.state_request()
224-
.accounts(basic_4_accounts::Auth { authority })
225-
.args(basic_4_instruction::state::Increment)
226+
.request()
227+
.accounts(basic_4_accounts::Increment { counter, authority })
228+
.args(basic_4_instruction::Increment {})
226229
.send()?;
227-
let counter_account: CounterState = program.state()?;
230+
231+
let counter_account: CounterAccount = program.account(counter)?;
228232
assert_eq!(counter_account.authority, authority);
229233
assert_eq!(counter_account.count, 1);
230234

client/src/lib.rs

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use anchor_lang::solana_program::hash::Hash;
55
use anchor_lang::solana_program::instruction::{AccountMeta, Instruction};
66
use anchor_lang::solana_program::program_error::ProgramError;
77
use anchor_lang::solana_program::pubkey::Pubkey;
8-
use anchor_lang::solana_program::system_program;
98
use anchor_lang::{AccountDeserialize, Discriminator, InstructionData, ToAccountMetas};
109
use regex::Regex;
1110
use solana_account_decoder::UiAccountEncoding;
@@ -112,18 +111,6 @@ impl Program {
112111
self.cfg.cluster.url(),
113112
self.cfg.payer.clone(),
114113
self.cfg.options,
115-
RequestNamespace::Global,
116-
)
117-
}
118-
119-
/// Returns a request builder for program state.
120-
pub fn state_request(&self) -> RequestBuilder {
121-
RequestBuilder::from(
122-
self.program_id,
123-
self.cfg.cluster.url(),
124-
self.cfg.payer.clone(),
125-
self.cfg.options,
126-
RequestNamespace::State { new: false },
127114
)
128115
}
129116

@@ -176,10 +163,6 @@ impl Program {
176163
})
177164
}
178165

179-
pub fn state<T: AccountDeserialize>(&self) -> Result<T, ClientError> {
180-
self.account(anchor_lang::__private::state::address(&self.program_id))
181-
}
182-
183166
pub fn rpc(&self) -> RpcClient {
184167
RpcClient::new_with_commitment(
185168
self.cfg.cluster.url().to_string(),
@@ -400,18 +383,6 @@ pub struct RequestBuilder<'a> {
400383
// Serialized instruction data for the target RPC.
401384
instruction_data: Option<Vec<u8>>,
402385
signers: Vec<&'a dyn Signer>,
403-
// True if the user is sending a state instruction.
404-
namespace: RequestNamespace,
405-
}
406-
407-
#[derive(PartialEq, Eq)]
408-
pub enum RequestNamespace {
409-
Global,
410-
State {
411-
// True if the request is to the state's new ctor.
412-
new: bool,
413-
},
414-
Interface,
415386
}
416387

417388
impl<'a> RequestBuilder<'a> {
@@ -420,7 +391,6 @@ impl<'a> RequestBuilder<'a> {
420391
cluster: &str,
421392
payer: Rc<dyn Signer>,
422393
options: Option<CommitmentConfig>,
423-
namespace: RequestNamespace,
424394
) -> Self {
425395
Self {
426396
program_id,
@@ -431,7 +401,6 @@ impl<'a> RequestBuilder<'a> {
431401
instructions: Vec::new(),
432402
instruction_data: None,
433403
signers: Vec::new(),
434-
namespace,
435404
}
436405
}
437406

@@ -478,53 +447,19 @@ impl<'a> RequestBuilder<'a> {
478447
self
479448
}
480449

481-
/// Invokes the `#[state]`'s `new` constructor.
482-
#[allow(clippy::wrong_self_convention)]
483-
#[must_use]
484-
pub fn new(mut self, args: impl InstructionData) -> Self {
485-
assert!(self.namespace == RequestNamespace::State { new: false });
486-
self.namespace = RequestNamespace::State { new: true };
487-
self.instruction_data = Some(args.data());
488-
self
489-
}
490-
491450
#[must_use]
492451
pub fn signer(mut self, signer: &'a dyn Signer) -> Self {
493452
self.signers.push(signer);
494453
self
495454
}
496455

497456
pub fn instructions(&self) -> Result<Vec<Instruction>, ClientError> {
498-
let mut accounts = match self.namespace {
499-
RequestNamespace::State { new } => match new {
500-
false => vec![AccountMeta::new(
501-
anchor_lang::__private::state::address(&self.program_id),
502-
false,
503-
)],
504-
true => vec![
505-
AccountMeta::new_readonly(self.payer.pubkey(), true),
506-
AccountMeta::new(
507-
anchor_lang::__private::state::address(&self.program_id),
508-
false,
509-
),
510-
AccountMeta::new_readonly(
511-
Pubkey::find_program_address(&[], &self.program_id).0,
512-
false,
513-
),
514-
AccountMeta::new_readonly(system_program::ID, false),
515-
AccountMeta::new_readonly(self.program_id, false),
516-
],
517-
},
518-
_ => Vec::new(),
519-
};
520-
accounts.extend_from_slice(&self.accounts);
521-
522457
let mut instructions = self.instructions.clone();
523458
if let Some(ix_data) = &self.instruction_data {
524459
instructions.push(Instruction {
525460
program_id: self.program_id,
526461
data: ix_data.clone(),
527-
accounts,
462+
accounts: self.accounts.clone(),
528463
});
529464
}
530465

docs/programs/tic-tac-toe/programs/tic-tac-toe/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ cpi = ["no-entrypoint"]
1616
default = []
1717

1818
[dependencies]
19-
anchor-lang = "=0.24.1"
19+
anchor-lang = "=0.26.0"
2020
num-traits = "0.2"
2121
num-derive = "0.3"
Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,78 @@
1-
// #region code
21
use anchor_lang::prelude::*;
2+
use std::ops::DerefMut;
33

44
declare_id!("CwrqeMj2U8tFr1Rhkgwc84tpAsqbt9pTt2a4taoTADPr");
55

66
#[program]
77
pub mod basic_4 {
88
use super::*;
99

10-
#[state]
11-
pub struct Counter {
12-
pub authority: Pubkey,
13-
pub count: u64,
10+
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
11+
let counter = ctx.accounts.counter.deref_mut();
12+
let bump = *ctx.bumps.get("counter").ok_or(ErrorCode::CannotGetBump)?;
13+
14+
*counter = Counter {
15+
authority: *ctx.accounts.authority.key,
16+
count: 0,
17+
bump,
18+
};
19+
20+
Ok(())
1421
}
1522

16-
impl Counter {
17-
pub fn new(ctx: Context<Auth>) -> anchor_lang::Result<Self> {
18-
Ok(Self {
19-
authority: *ctx.accounts.authority.key,
20-
count: 0,
21-
})
22-
}
23-
24-
pub fn increment(&mut self, ctx: Context<Auth>) -> anchor_lang::Result<()> {
25-
if &self.authority != ctx.accounts.authority.key {
26-
return Err(error!(ErrorCode::Unauthorized));
27-
}
28-
self.count += 1;
29-
Ok(())
30-
}
23+
pub fn increment(ctx: Context<Increment>) -> Result<()> {
24+
require_keys_eq!(
25+
ctx.accounts.authority.key(),
26+
ctx.accounts.counter.authority,
27+
ErrorCode::Unauthorized
28+
);
29+
30+
ctx.accounts.counter.count += 1;
31+
Ok(())
3132
}
3233
}
3334

3435
#[derive(Accounts)]
35-
pub struct Auth<'info> {
36+
pub struct Initialize<'info> {
37+
#[account(
38+
init,
39+
payer = authority,
40+
space = Counter::SIZE,
41+
seeds = [b"counter"],
42+
bump
43+
)]
44+
counter: Account<'info, Counter>,
45+
#[account(mut)]
3646
authority: Signer<'info>,
47+
system_program: Program<'info, System>,
48+
}
49+
50+
#[derive(Accounts)]
51+
pub struct Increment<'info> {
52+
#[account(
53+
mut,
54+
seeds = [b"counter"],
55+
bump = counter.bump
56+
)]
57+
counter: Account<'info, Counter>,
58+
authority: Signer<'info>,
59+
}
60+
61+
#[account]
62+
pub struct Counter {
63+
pub authority: Pubkey,
64+
pub count: u64,
65+
pub bump: u8,
66+
}
67+
68+
impl Counter {
69+
pub const SIZE: usize = 8 + 32 + 8 + 1;
3770
}
38-
// #endregion code
3971

4072
#[error_code]
4173
pub enum ErrorCode {
4274
#[msg("You are not authorized to perform this action.")]
4375
Unauthorized,
76+
#[msg("Cannot get the bump.")]
77+
CannotGetBump,
4478
}

0 commit comments

Comments
 (0)