Skip to content

Commit

Permalink
First pass for reducing compute using create_program_address when pos…
Browse files Browse the repository at this point in the history
…sible.
  • Loading branch information
blockiosaurus committed Apr 6, 2024
1 parent 87af917 commit c965f19
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 62 deletions.
15 changes: 12 additions & 3 deletions programs/token-metadata/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions programs/token-metadata/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@ serde-feature = ["serde", "serde_with"]
[dependencies]
arrayref = "0.3.6"
borsh = "0.9.3"
mpl-token-auth-rules = { version = "=1.4.3-beta.1", features = ["no-entrypoint"] }
mpl-token-auth-rules = { version = "=1.4.3-beta.1", features = [
"no-entrypoint",
] }
mpl-token-metadata-context-derive = { version = "0.3.0", path = "../macro" }
mpl-utils = { version = "0.3.2", features = ["spl-token"] }
mpl-utils = { path = "/home/kelliott/work/mpl/metaplex-program-library/core/rust/utils", features = [
"spl-token",
] }
num-derive = "0.3"
num-traits = "0.2"
serde = { version = "1.0.149", optional = true }
serde_with = { version = "1.14.0", optional = true }
shank = { version = "0.3.0" }
solana-program = ">= 1.14.13, < 1.17"
spl-token-2022 = "0.8.0"
spl-associated-token-account = { version = ">= 1.1.3, < 3.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = ">= 1.1.3, < 3.0", features = [
"no-entrypoint",
] }
thiserror = "1.0"

[dev-dependencies]
async-trait = "0.1.64"
rmp-serde = "1.1.1"
rooster = { git = "https://github.com/metaplex-foundation/rooster", features = ["no-entrypoint"] }
rooster = { git = "https://github.com/metaplex-foundation/rooster", features = [
"no-entrypoint",
] }
serde = { version = "1.0.147", features = ["derive"] }
solana-program-test = ">= 1.14.13, < 1.17"
solana-sdk = ">= 1.14.13, < 1.17"
Expand Down
38 changes: 28 additions & 10 deletions programs/token-metadata/program/src/assertions/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{
},
};

use super::assert_derivation_with_bump;

/// Checks whether the collection update is allowed or not based on the `verified` status.
pub fn assert_collection_update_is_valid(
allow_direct_collection_verified_writes: bool,
Expand Down Expand Up @@ -114,16 +116,32 @@ pub fn assert_collection_verify_is_valid(
}
}

assert_derivation(
&crate::ID,
edition_account_info,
&[
PREFIX.as_bytes(),
crate::ID.as_ref(),
collection_metadata.mint.as_ref(),
EDITION.as_bytes(),
],
)
match collection_metadata.edition_nonce {
Some(nonce) => assert_derivation_with_bump(
&crate::ID,
edition_account_info,
&[
PREFIX.as_bytes(),
crate::ID.as_ref(),
collection_metadata.mint.as_ref(),
EDITION.as_bytes(),
&[nonce],
],
),
None => {
let _ = assert_derivation(
&crate::ID,
edition_account_info,
&[
PREFIX.as_bytes(),
crate::ID.as_ref(),
collection_metadata.mint.as_ref(),
EDITION.as_bytes(),
],
)?;
Ok(())
}
}
.map_err(|_| MetadataError::CollectionMasterEditionAccountInvalid)?;

assert_master_edition(collection_metadata, edition_account_info)?;
Expand Down
13 changes: 13 additions & 0 deletions programs/token-metadata/program/src/assertions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ pub fn assert_derivation(
mpl_utils::assert_derivation(program_id, account, path, MetadataError::DerivedKeyInvalid)
}

pub fn assert_derivation_with_bump(
program_id: &Pubkey,
account: &AccountInfo,
path: &[&[u8]],
) -> Result<(), ProgramError> {
mpl_utils::assert_derivation_with_bump(
program_id,
account,
path,
MetadataError::DerivedKeyInvalid,
)
}

pub fn assert_owned_by(account: &AccountInfo, owner: &Pubkey) -> ProgramResult {
mpl_utils::assert_owned_by(account, owner, MetadataError::IncorrectOwner)
}
Expand Down
2 changes: 2 additions & 0 deletions programs/token-metadata/program/src/processor/burn/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ fn burn_v1(program_id: &Pubkey, ctx: Context<Burn>, args: BurnArgs) -> ProgramRe
ctx.accounts.token_info.clone(),
edition_info.clone(),
ctx.accounts.spl_token_program_info.clone(),
metadata.edition_nonce,
)?;

let mut args = BurnNonFungibleArgs {
Expand Down Expand Up @@ -269,6 +270,7 @@ fn burn_v1(program_id: &Pubkey, ctx: Context<Burn>, args: BurnArgs) -> ProgramRe
ctx.accounts.token_info.clone(),
edition_info.clone(),
ctx.accounts.spl_token_program_info.clone(),
metadata.edition_nonce,
)?;

let mut is_close_auth = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ pub(crate) fn burn_nonfungible(ctx: &Context<Burn>, args: BurnNonFungibleArgs) -
ctx.accounts.mint_info.key.as_ref(),
EDITION.as_bytes(),
]);
let bump = assert_derivation(&crate::ID, edition_info, &edition_info_path)?;

let bump = match args.metadata.edition_nonce {
Some(bump) => Ok(bump),
None => assert_derivation(&crate::ID, edition_info, &edition_info_path),
}?;

let edition_seeds = &[
PREFIX.as_bytes(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ fn create_persistent_delegate_v1(
token_info.clone(),
master_edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)?;
} else {
return Err(MetadataError::MissingEditionAccount.into());
Expand Down Expand Up @@ -510,6 +511,7 @@ fn create_persistent_delegate_v1(
token_info.clone(),
master_edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)?;
} else {
// sanity check: this should not happen at this point since the master
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,19 @@ fn revoke_persistent_delegate_v1(
token_info.clone(),
master_edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)?;

// Clear the close authority if it's a Utility Delegate.
if matches!(role, TokenDelegateRole::Utility) {
clear_close_authority(ClearCloseAuthorityParams {
token_info,
mint_info: ctx.accounts.mint_info,
token,
token: &token,
master_edition_info,
authority_info: master_edition_info,
spl_token_program_info,
edition_bump: metadata.edition_nonce,
})?;
}
} else {
Expand Down Expand Up @@ -380,6 +382,7 @@ fn revoke_persistent_delegate_v1(
token_info.clone(),
master_edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)?;
} else {
// sanity check: this should not happen at this point since the master
Expand Down
22 changes: 15 additions & 7 deletions programs/token-metadata/program/src/processor/metadata/mint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mpl_utils::{assert_signer, cmp_pubkeys};
use mpl_utils::{assert_derivation_with_bump, assert_signer, cmp_pubkeys};
use solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
Expand Down Expand Up @@ -212,17 +212,23 @@ pub fn mint_v1(program_id: &Pubkey, ctx: Context<Mint>, args: MintArgs) -> Progr
.edition_nonce
.ok_or(MetadataError::NotAMasterEdition)?],
];
let master_edition_key =
Pubkey::create_program_address(master_edition_seeds, program_id)?;
// let master_edition_key =
// Pubkey::create_program_address(master_edition_seeds, program_id)?;

let master_edition_info = ctx
.accounts
.master_edition_info
.ok_or(MetadataError::MissingMasterEditionAccount)?;

if !cmp_pubkeys(master_edition_info.key, &master_edition_key) {
return Err(MetadataError::InvalidMasterEdition.into());
}
// if !cmp_pubkeys(master_edition_info.key, &master_edition_key) {
// return Err(MetadataError::InvalidMasterEdition.into());
// }
assert_derivation_with_bump(
&crate::ID,
master_edition_info,
master_edition_seeds,
MetadataError::MissingMasterEditionAccount,
)?;

// thaw the token account for programmable assets; the account
// is not frozen if we just initialized it
Expand All @@ -236,6 +242,7 @@ pub fn mint_v1(program_id: &Pubkey, ctx: Context<Mint>, args: MintArgs) -> Progr
ctx.accounts.token_info.clone(),
master_edition_info.clone(),
ctx.accounts.spl_token_program_info.clone(),
metadata.edition_nonce,
)?;
}

Expand All @@ -244,7 +251,7 @@ pub fn mint_v1(program_id: &Pubkey, ctx: Context<Mint>, args: MintArgs) -> Progr
ctx.accounts.spl_token_program_info.key,
ctx.accounts.mint_info.key,
ctx.accounts.token_info.key,
&master_edition_key,
master_edition_info.key,
&[],
amount,
)?,
Expand All @@ -266,6 +273,7 @@ pub fn mint_v1(program_id: &Pubkey, ctx: Context<Mint>, args: MintArgs) -> Progr
ctx.accounts.token_info.clone(),
master_edition_info.clone(),
ctx.accounts.spl_token_program_info.clone(),
metadata.edition_nonce,
)?;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ fn print_logic<'a>(
edition_token_account_info.clone(),
edition_account_info.clone(),
token_program.clone(),
None,
)?;

// for pNFTs, we store the token standard value at the end of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ fn transfer_v1(program_id: &Pubkey, ctx: Context<Transfer>, args: TransferArgs)
authority_signer_seeds: None,
token_program: ctx.accounts.spl_token_program_info.clone(),
decimals: mint.decimals,
edition_bump: metadata.edition_nonce,
};

let token_standard = metadata.token_standard;
Expand Down Expand Up @@ -408,10 +409,11 @@ fn transfer_v1(program_id: &Pubkey, ctx: Context<Transfer>, args: TransferArgs)
clear_close_authority(ClearCloseAuthorityParams {
token_info: ctx.accounts.token_info,
mint_info: ctx.accounts.mint_info,
token,
token: &token,
master_edition_info,
authority_info: master_edition_info,
spl_token_program_info: ctx.accounts.spl_token_program_info,
edition_bump: metadata.edition_nonce,
})?;

// If the token record account for the destination owner doesn't exist,
Expand Down
2 changes: 2 additions & 0 deletions programs/token-metadata/program/src/processor/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub(crate) fn toggle_asset_state(
accounts.token_info.clone(),
edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)
}
TokenState::Unlocked => {
Expand All @@ -195,6 +196,7 @@ pub(crate) fn toggle_asset_state(
accounts.token_info.clone(),
edition_info.clone(),
spl_token_program_info.clone(),
metadata.edition_nonce,
)
}
TokenState::Listed => Err(MetadataError::IncorrectTokenState.into()),
Expand Down
20 changes: 0 additions & 20 deletions programs/token-metadata/program/src/state/master_edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ pub fn get_master_edition(account: &AccountInfo) -> Result<Box<dyn MasterEdition
master_edition_result
}

#[macro_export]
macro_rules! edition_seeds {
($mint:expr) => {{
let path = vec![
"metadata".as_bytes(),
$crate::ID.as_ref(),
$mint.as_ref(),
"edition".as_bytes(),
];
let (_, bump) = Pubkey::find_program_address(&path, &$crate::ID);
&[
"metadata".as_bytes(),
$crate::ID.as_ref(),
$mint.as_ref(),
"edition".as_bytes(),
&[bump],
]
}};
}

#[repr(C)]
#[cfg_attr(feature = "serde-feature", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize, ShankAccount)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub fn extract_edition_number_from_deprecated_reservation_list(
let mut offset: Option<u64> = None;
let mut reservations = reservation_list.reservations();
for i in 0..reservations.len() {
let mut reservation = &mut reservations[i];
let reservation = &mut reservations[i];

if reservation.address == *mint_authority_info.key {
offset = Some(
Expand Down
Loading

0 comments on commit c965f19

Please sign in to comment.