Skip to content
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

Update IDL program #2365

Merged
merged 13 commits into from
Jan 26, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The minor version will be incremented upon a breaking change and the patch versi

- cli: Don't regenerate idl in read_all_programs(). ([#2332](https://github.com/coral-xyz/anchor/pull/2332)).
- ts: `provider.simulate` will send the transaction with `sigVerify: false` if no `signers` are present ([#2331](https://github.com/coral-xyz/anchor/pull/2331)).
- idl: Update the IDL program to use non-deprecated account types ([#2365](https://github.com/coral-xyz/anchor/pull/2365)).

### Breaking

Expand Down
27 changes: 0 additions & 27 deletions lang/src/ctor.rs

This file was deleted.

88 changes: 6 additions & 82 deletions lang/src/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//! Note that IDL account instructions are automatically inserted into all
//! Anchor programs. To remove them, one can use the `no-idl` feature.

#[allow(deprecated)]
use crate::accounts::program_account::ProgramAccount;
use crate::prelude::*;
use solana_program::pubkey::Pubkey;

Expand All @@ -28,6 +26,7 @@ use solana_program::pubkey::Pubkey;
//
// Sha256(anchor:idl)[..8];
pub const IDL_IX_TAG: u64 = 0x0a69e9a778bcf440;
pub const IDL_IX_TAG_LE: [u8; 8] = IDL_IX_TAG.to_le_bytes();

// The Pubkey that is stored as the 'authority' on the IdlAccount when the authority
// is "erased".
Expand All @@ -50,72 +49,16 @@ pub enum IdlInstruction {
Resize { data_len: u64 },
}

// Accounts for the Create instruction.
pub type IdlCreateAccounts<'info> = crate::ctor::Ctor<'info>;

// Accounts for Idl instructions.
#[derive(Accounts)]
pub struct IdlAccounts<'info> {
#[account(mut, has_one = authority)]
#[allow(deprecated)]
pub idl: ProgramAccount<'info, IdlAccount>,
#[account(constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
}

// Accounts for resize account instruction
#[derive(Accounts)]
pub struct IdlResizeAccount<'info> {
#[account(mut, has_one = authority)]
#[allow(deprecated)]
pub idl: ProgramAccount<'info, IdlAccount>,
#[account(mut, constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}

// Accounts for creating an idl buffer.
#[derive(Accounts)]
pub struct IdlCreateBuffer<'info> {
#[account(zero)]
#[allow(deprecated)]
pub buffer: ProgramAccount<'info, IdlAccount>,
#[account(constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
}

// Accounts for upgrading the canonical IdlAccount with the buffer.
#[derive(Accounts)]
pub struct IdlSetBuffer<'info> {
// The buffer with the new idl data.
#[account(mut, constraint = buffer.authority == idl.authority)]
#[allow(deprecated)]
pub buffer: ProgramAccount<'info, IdlAccount>,
// The idl account to be updated with the buffer's data.
#[account(mut, has_one = authority)]
#[allow(deprecated)]
pub idl: ProgramAccount<'info, IdlAccount>,
#[account(constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
}

// Accounts for closing the canonical Idl buffer.
#[derive(Accounts)]
pub struct IdlCloseAccount<'info> {
#[account(mut, has_one = authority, close = sol_destination)]
#[allow(deprecated)]
pub account: ProgramAccount<'info, IdlAccount>,
#[account(constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
#[account(mut)]
pub sol_destination: AccountInfo<'info>,
}

// The account holding a program's IDL. This is stored on chain so that clients
// can fetch it and generate a client with nothing but a program's ID.
//
// Note: we use the same account for the "write buffer", similar to the
// bpf upgradeable loader's mechanism.
//
// TODO: IdlAccount exists here only because it's needed by the CLI, the IDL
// itself uses an IdlAccount defined inside the program itself, see program/idl.rs.
// Ideally it would be deleted and a better solution for sharing the type with CLI
// could be found.
#[account("internal")]
#[derive(Debug)]
pub struct IdlAccount {
Expand All @@ -136,22 +79,3 @@ impl IdlAccount {
"anchor:idl"
}
}

use std::cell::{Ref, RefMut};

pub trait IdlTrailingData<'info> {
fn trailing_data(self) -> Ref<'info, [u8]>;
fn trailing_data_mut(self) -> RefMut<'info, [u8]>;
}

#[allow(deprecated)]
impl<'a, 'info: 'a> IdlTrailingData<'a> for &'a ProgramAccount<'info, IdlAccount> {
fn trailing_data(self) -> Ref<'a, [u8]> {
let info = self.as_ref();
Ref::map(info.try_borrow_data().unwrap(), |d| &d[44..])
}
fn trailing_data_mut(self) -> RefMut<'a, [u8]> {
let info = self.as_ref();
RefMut::map(info.try_borrow_mut_data().unwrap(), |d| &mut d[44..])
}
}
3 changes: 0 additions & 3 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ mod bpf_upgradeable_state;
mod bpf_writer;
mod common;
pub mod context;
mod ctor;
pub mod error;
#[doc(hidden)]
pub mod idl;
Expand Down Expand Up @@ -276,8 +275,6 @@ pub mod __private {
/// The discriminator anchor uses to mark an account as closed.
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];

pub use crate::ctor::Ctor;

pub use anchor_attribute_account::ZeroCopyAccessor;

pub use anchor_attribute_event::EventIndex;
Expand Down
24 changes: 13 additions & 11 deletions lang/syn/src/codegen/program/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,23 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
sighash
};

// If the method identifier is the IDL tag, then execute an IDL
// instruction, injected into all Anchor programs.
if cfg!(not(feature = "no-idl")) {
if sighash == anchor_lang::idl::IDL_IX_TAG.to_le_bytes() {
return __private::__idl::__idl_dispatch(
program_id,
accounts,
&ix_data,
);
}
}

use anchor_lang::Discriminator;
match sighash {
#(#global_dispatch_arms)*
anchor_lang::idl::IDL_IX_TAG_LE => {
// If the method identifier is the IDL tag, then execute an IDL
// instruction, injected into all Anchor programs.
if cfg!(not(feature = "no-idl")) {
__private::__idl::__idl_dispatch(
program_id,
accounts,
&ix_data,
)
} else {
Err(anchor_lang::error::ErrorCode::IdlInstructionStub.into())
}
}
_ => {
#fallback_fn
}
Expand Down
Loading