Skip to content

Commit

Permalink
fix overwriting of fee flag on 'update' (#1114)
Browse files Browse the repository at this point in the history
* fix overwriting of fee flag on 'update'

* fix index in clean write
  • Loading branch information
samuelvanderwaal authored Jun 9, 2023
1 parent 17b1222 commit e196d7b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
5 changes: 3 additions & 2 deletions token-metadata/program/src/utils/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
state::{
Collection, CollectionDetails, Data, DataV2, Key, Metadata, ProgrammableConfig,
TokenStandard, Uses, EDITION, MAX_METADATA_LEN, PREFIX,
TokenStandard, Uses, EDITION, MAX_METADATA_LEN, METADATA_FEE_FLAG_INDEX, PREFIX,
},
};

Expand Down Expand Up @@ -262,7 +262,8 @@ pub fn clean_write_metadata(
) -> ProgramResult {
// Clear all data to ensure it is serialized cleanly with no trailing data due to creators array resizing.
let mut metadata_account_info_data = metadata_account_info.try_borrow_mut_data()?;
metadata_account_info_data[0..].fill(0);
// Don't overwrite fee flag.
metadata_account_info_data[0..METADATA_FEE_FLAG_INDEX].fill(0);

metadata.save(&mut metadata_account_info_data)?;

Expand Down
41 changes: 38 additions & 3 deletions token-metadata/program/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use utils::*;

mod fees {
use mpl_token_metadata::{
instruction::{collect_fees, BurnArgs},
instruction::{collect_fees, BurnArgs, UpdateArgs},
state::{CREATE_FEE, FEE_FLAG_CLEARED, METADATA_FEE_FLAG_INDEX},
};
use solana_program::{native_token::LAMPORTS_PER_SOL, pubkey::Pubkey};
Expand All @@ -25,6 +25,7 @@ mod fees {
let md = Metadata::new();
md.create_v3_default(&mut context).await.unwrap();

md.assert_fee_flag_set(&mut context).await.unwrap();
md.assert_create_fees_charged(&mut context).await.unwrap();
}

Expand All @@ -41,6 +42,38 @@ mod fees {
.await
.unwrap();

nft.assert_fee_flag_set(&mut context).await.unwrap();
nft.assert_create_fees_charged(&mut context).await.unwrap();
}

#[tokio::test]
async fn update_does_not_overwrite_flag() {
let mut context = program_test().start_with_context().await;

let update_authority = context.payer.dirty_clone();

let mut nft = DigitalAsset::new();
nft.create(
&mut context,
mpl_token_metadata::state::TokenStandard::NonFungible,
None,
)
.await
.unwrap();

let mut args = UpdateArgs::default_as_update_authority();
match &mut args {
UpdateArgs::AsUpdateAuthorityV2 { is_mutable, .. } => {
*is_mutable = Some(false);
}
_ => panic!("Unexpected enum variant"),
}

nft.update(&mut context, update_authority, args)
.await
.unwrap();

nft.assert_fee_flag_set(&mut context).await.unwrap();
nft.assert_create_fees_charged(&mut context).await.unwrap();
}

Expand All @@ -54,7 +87,8 @@ mod fees {
let authority_funding = 10 * LAMPORTS_PER_SOL;

let authority =
read_keypair_file("Levytx9LLPzAtDJJD7q813Zsm8zg9e1pb53mGxTKpD7.json").unwrap();
read_keypair_file("/media/veracrypt1/Levytx9LLPzAtDJJD7q813Zsm8zg9e1pb53mGxTKpD7.json")
.unwrap();
authority
.airdrop(&mut context, authority_funding)
.await
Expand Down Expand Up @@ -117,7 +151,8 @@ mod fees {
let fee_authority_funding = LAMPORTS_PER_SOL;

let fee_authority =
read_keypair_file("Levytx9LLPzAtDJJD7q813Zsm8zg9e1pb53mGxTKpD7.json").unwrap();
read_keypair_file("/media/veracrypt1/Levytx9LLPzAtDJJD7q813Zsm8zg9e1pb53mGxTKpD7.json")
.unwrap();
fee_authority
.airdrop(&mut context, fee_authority_funding)
.await
Expand Down
4 changes: 2 additions & 2 deletions token-metadata/program/tests/update_metadata_account_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,9 @@ mod update_metadata_account_v2 {
let data = get_account(&mut context, &test_metadata.pubkey).await.data;

let padding_index = 1 + 32 + 32 + 36 + 14 + 204 + 7 + 34 + 1 + 1 + 2 + 2 + 1 + 1;
let zeros_len = data.len() - padding_index;
let zeros_len = data.len() - padding_index - 1; // Fee flag at end
let zeros = vec![0u8; zeros_len];
assert_eq!(data[padding_index..], zeros[..]);
assert_eq!(data[padding_index..data.len() - 1], zeros[..]);
}

#[tokio::test]
Expand Down
11 changes: 11 additions & 0 deletions token-metadata/program/tests/utils/digital_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,17 @@ impl DigitalAsset {

Ok(())
}

pub async fn assert_fee_flag_set(
&self,
context: &mut ProgramTestContext,
) -> Result<(), BanksClientError> {
let account = get_account(context, &self.metadata).await;

assert_eq!(account.data[METADATA_FEE_FLAG_INDEX], FEE_FLAG_SET);

Ok(())
}
}

pub struct TransferParams<'a> {
Expand Down
11 changes: 11 additions & 0 deletions token-metadata/program/tests/utils/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,17 @@ impl Metadata {

Ok(())
}

pub async fn assert_fee_flag_set(
&self,
context: &mut ProgramTestContext,
) -> Result<(), BanksClientError> {
let account = get_account(context, &self.pubkey).await;

assert_eq!(account.data[METADATA_FEE_FLAG_INDEX], FEE_FLAG_SET);

Ok(())
}
}

impl Default for Metadata {
Expand Down

0 comments on commit e196d7b

Please sign in to comment.