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

lang: Require IdlCreateAccounts instruction authority to be the program authority #3423

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/BINARY_SIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Solana version: 2.1.0

| Program | Binary Size | - |
| ------- | ----------- | ------------------------ |
| bench | 1,041,928 | 🔴 **+250,920 (31.72%)** |
| bench | 1,106,136 | 🔴 **+315,128 (39.84%)** |
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's crazy that this simple change adds ~60 KB to the program binary. I guess we could optimize it by not using the existing types such as ProgramData (which imports bincode) and interpreting the account data ourselves, but given we're planning to get rid of these instructions, it's probably fine to keep it as is and not complicate things further.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool to see that you're tracking this. 60kb does sound way too much, but I agree with you here


### Notable changes

Expand Down
4 changes: 3 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use solana_program::instruction::{AccountMeta, Instruction};
use solana_sdk::account_utils::StateMut;
use solana_sdk::bpf_loader;
use solana_sdk::bpf_loader_deprecated;
use solana_sdk::bpf_loader_upgradeable::{self, UpgradeableLoaderState};
use solana_sdk::bpf_loader_upgradeable::{self, get_program_data_address, UpgradeableLoaderState};
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::compute_budget::ComputeBudgetInstruction;
use solana_sdk::pubkey::Pubkey;
Expand Down Expand Up @@ -3989,12 +3989,14 @@ fn create_idl_account(
let mut instructions = Vec::new();
let data = serialize_idl_ix(anchor_lang::idl::IdlInstruction::Create { data_len })?;
let program_signer = Pubkey::find_program_address(&[], program_id).0;
let program_data_address = get_program_data_address(program_id);
let accounts = vec![
AccountMeta::new_readonly(keypair.pubkey(), true),
AccountMeta::new(idl_address, false),
AccountMeta::new_readonly(program_signer, false),
AccountMeta::new_readonly(solana_program::system_program::ID, false),
AccountMeta::new_readonly(*program_id, false),
AccountMeta::new_readonly(program_data_address, false),
];
instructions.push(Instruction {
program_id: *program_id,
Expand Down
3 changes: 1 addition & 2 deletions lang/syn/src/codegen/program/entry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::Program;
use heck::CamelCase;
use quote::quote;

pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let name: proc_macro2::TokenStream = program.name.to_string().to_camel_case().parse().unwrap();
let name = program.struct_name();
quote! {
#[cfg(not(feature = "no-entrypoint"))]
anchor_lang::solana_program::entrypoint!(entry);
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let program_name = &program.name;
// A constant token stream that stores the accounts and functions, required to live
// inside the target program in order to get the program ID.
let idl_accounts_and_functions = idl_accounts_and_functions();
let idl_accounts_and_functions = idl_accounts_and_functions(&program.struct_name());
let non_inlined_idl: proc_macro2::TokenStream = {
quote! {
// Entry for all IDL related instructions. Use the "no-idl" feature
Expand Down
11 changes: 8 additions & 3 deletions lang/syn/src/codegen/program/idl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use quote::quote;

pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
pub fn idl_accounts_and_functions(
program_struct_ident: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
quote! {
use anchor_lang::idl::ERASED_AUTHORITY;

Expand Down Expand Up @@ -50,8 +52,11 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
// The system program.
pub system_program: Program<'info, System>,
// The program whose state is being constructed.
#[account(executable)]
pub program: AccountInfo<'info>,
#[account(constraint = program.programdata_address()? == Some(program_data.key()))]
pub program: Program<'info, program::#program_struct_ident>,
// Program's data account necessary to check the upgrade authority.
#[account(constraint = program_data.upgrade_authority_address == Some(from.key()))]
pub program_data: Account<'info, ProgramData>,
}

// Accounts for Idl instructions.
Expand Down
8 changes: 8 additions & 0 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) mod hash;

use codegen::accounts as accounts_codegen;
use codegen::program as program_codegen;
use heck::CamelCase;
use parser::accounts as accounts_parser;
use parser::program as program_parser;
use proc_macro2::{Span, TokenStream};
Expand Down Expand Up @@ -41,6 +42,13 @@ pub struct Program {
pub fallback_fn: Option<FallbackFn>,
}

impl Program {
/// Get program struct name (in PascalCase).
pub fn struct_name(&self) -> proc_macro2::TokenStream {
self.name.to_string().to_camel_case().parse().unwrap()
}
}

impl Parse for Program {
fn parse(input: ParseStream) -> ParseResult<Self> {
let program_mod = <ItemMod as Parse>::parse(input)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/bench/bench.json
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@
"solanaVersion": "2.1.0",
"result": {
"binarySize": {
"bench": 1041928
"bench": 1106136
},
"computeUnits": {
"accountInfo1": 571,
Expand Down
Loading