Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abcalphabet committed Dec 9, 2024
1 parent 0c003dc commit fd29474
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 46 deletions.
128 changes: 83 additions & 45 deletions token/program-2022-test/tests/confidential_mint_burn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "test-sbf")]
//#![cfg(feature = "test-sbf")]

mod program_test;
use {
Expand All @@ -19,7 +19,7 @@ use {
},
spl_token_client::{
client::ProgramBanksClientProcessTransaction,
token::{ExtensionInitializationParams, ProofAccount, Token},
token::{ExtensionInitializationParams, ProofAccount, ProofAccountWithCiphertext, Token},
},
spl_token_confidential_transfer_proof_generation::{
burn::burn_split_proof_data, mint::mint_split_proof_data,
Expand All @@ -36,21 +36,27 @@ async fn test_confidential_mint() {
let auditor_elgamal_keypair = ElGamalKeypair::new_rand();
let auditor_elgamal_pubkey = (*auditor_elgamal_keypair.pubkey()).into();
let supply_aes_key = AeKey::new_rand();
let mint_account = Keypair::new();

let mut context = TestContext::new().await;
context
.init_token_with_mint(vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
authority: authority.pubkey(),
confidential_supply_pubkey: Some(auditor_elgamal_pubkey),
decryptable_supply: Some(supply_aes_key.encrypt(0).into()),
},
])
.init_token_with_mint_keypair_and_freeze_authority_and_mint_authority(
mint_account,
vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
confidential_supply_pubkey: auditor_elgamal_pubkey,
decryptable_supply: supply_aes_key.encrypt(0).into(),
},
],
None,
// hacky but we have to clone somehow
Keypair::from_bytes(&authority.to_bytes()).unwrap(),
)
.await
.unwrap();

Expand Down Expand Up @@ -121,21 +127,26 @@ async fn test_confidential_burn() {
let auditor_elgamal_keypair = ElGamalKeypair::new_rand();
let auditor_elgamal_pubkey = (*auditor_elgamal_keypair.pubkey()).into();
let supply_aes_key = AeKey::new_rand();
let mint_account = Keypair::new();

let mut context = TestContext::new().await;
context
.init_token_with_mint(vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
authority: authority.pubkey(),
confidential_supply_pubkey: Some(auditor_elgamal_pubkey),
decryptable_supply: Some(supply_aes_key.encrypt(0).into()),
},
])
.init_token_with_mint_keypair_and_freeze_authority_and_mint_authority(
mint_account,
vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
confidential_supply_pubkey: auditor_elgamal_pubkey,
decryptable_supply: supply_aes_key.encrypt(0).into(),
},
],
None,
Keypair::from_bytes(&authority.to_bytes()).unwrap(),
)
.await
.unwrap();

Expand Down Expand Up @@ -227,7 +238,9 @@ async fn test_confidential_burn() {
.confidential_transfer_create_context_state_account(
&ciphertext_validity_proof_context_pubkey,
&context_state_auth_pubkey,
&proof_data.ciphertext_validity_proof_data,
&proof_data
.ciphertext_validity_proof_data_with_ciphertext
.proof_data,
false,
ciphertext_validity_proof_signer,
)
Expand All @@ -247,6 +260,15 @@ async fn test_confidential_burn() {
let equality_proof_location = ProofAccount::ContextAccount(equality_proof_context_pubkey);
let ciphertext_validity_proof_location =
ProofAccount::ContextAccount(ciphertext_validity_proof_context_pubkey);
let ciphertext_validity_proof_location = ProofAccountWithCiphertext {
proof_account: ciphertext_validity_proof_location,
ciphertext_lo: proof_data
.ciphertext_validity_proof_data_with_ciphertext
.ciphertext_lo,
ciphertext_hi: proof_data
.ciphertext_validity_proof_data_with_ciphertext
.ciphertext_hi,
};
let range_proof_location = ProofAccount::ContextAccount(range_proof_context_pubkey);

// do the burn
Expand Down Expand Up @@ -324,21 +346,26 @@ async fn test_rotate_supply_elgamal() {
let auditor_elgamal_keypair = ElGamalKeypair::new_rand();
let auditor_elgamal_pubkey = (*auditor_elgamal_keypair.pubkey()).into();
let supply_aes_key = AeKey::new_rand();
let mint_account = Keypair::new();

let mut context = TestContext::new().await;
context
.init_token_with_mint(vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
authority: authority.pubkey(),
confidential_supply_pubkey: Some(auditor_elgamal_pubkey),
decryptable_supply: Some(supply_aes_key.encrypt(0).into()),
},
])
.init_token_with_mint_keypair_and_freeze_authority_and_mint_authority(
mint_account,
vec![
ExtensionInitializationParams::ConfidentialTransferMint {
authority: Some(authority.pubkey()),
auto_approve_new_accounts: true,
auditor_elgamal_pubkey: Some(auditor_elgamal_pubkey),
},
ExtensionInitializationParams::ConfidentialMintBurnMint {
confidential_supply_pubkey: auditor_elgamal_pubkey,
decryptable_supply: supply_aes_key.encrypt(0).into(),
},
],
None,
Keypair::from_bytes(&authority.to_bytes()).unwrap(),
)
.await
.unwrap();

Expand Down Expand Up @@ -400,11 +427,7 @@ async fn test_rotate_supply_elgamal() {

assert_eq!(
mint_burn_extension.supply_elgamal_pubkey,
Some(Into::<PodElGamalPubkey>::into(
*new_supply_elgamal_keypair.pubkey(),
))
.try_into()
.unwrap(),
Into::<PodElGamalPubkey>::into(*new_supply_elgamal_keypair.pubkey(),),
);
}

Expand Down Expand Up @@ -465,7 +488,9 @@ async fn mint_tokens(
.confidential_transfer_create_context_state_account(
&ciphertext_validity_proof_context_pubkey,
&context_state_auth.pubkey(),
&proof_data.ciphertext_validity_proof_data,
&proof_data
.ciphertext_validity_proof_data_with_ciphertext
.proof_data,
false,
ciphertext_validity_proof_signer,
)
Expand All @@ -485,8 +510,21 @@ async fn mint_tokens(
let equality_proof_location = ProofAccount::ContextAccount(equality_proof_context_pubkey);
let ciphertext_validity_proof_location =
ProofAccount::ContextAccount(ciphertext_validity_proof_context_pubkey);
let ciphertext_validity_proof_location = ProofAccountWithCiphertext {
proof_account: ciphertext_validity_proof_location,
ciphertext_lo: proof_data
.ciphertext_validity_proof_data_with_ciphertext
.ciphertext_lo,
ciphertext_hi: proof_data
.ciphertext_validity_proof_data_with_ciphertext
.ciphertext_hi,
};
let range_proof_location = ProofAccount::ContextAccount(range_proof_context_pubkey);

println!(
"TOKEN: {}, ata: {token_account}, auth: {authority}",
token.get_address()
);
token
.confidential_mint(
token_account,
Expand Down
18 changes: 17 additions & 1 deletion token/program-2022-test/tests/program_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ impl TestContext {
mint_account: Keypair,
extension_init_params: Vec<ExtensionInitializationParams>,
freeze_authority: Option<Keypair>,
) -> TokenResult<()> {
let mint_authority = Keypair::new();
self.init_token_with_mint_keypair_and_freeze_authority_and_mint_authority(
mint_account,
extension_init_params,
freeze_authority,
mint_authority,
)
.await
}

pub async fn init_token_with_mint_keypair_and_freeze_authority_and_mint_authority(
&mut self,
mint_account: Keypair,
extension_init_params: Vec<ExtensionInitializationParams>,
freeze_authority: Option<Keypair>,
mint_authority: Keypair,
) -> TokenResult<()> {
let payer = keypair_clone(&self.context.lock().await.payer);
let client: Arc<dyn ProgramClient<ProgramBanksClientProcessTransaction>> =
Expand All @@ -113,7 +130,6 @@ impl TestContext {

let decimals: u8 = 9;

let mint_authority = Keypair::new();
let mint_authority_pubkey = mint_authority.pubkey();
let freeze_authority_pubkey = freeze_authority
.as_ref()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,48 @@ fn process_confidential_mint(
accounts: &[AccountInfo],
data: &MintInstructionData,
) -> ProgramResult {
println!("MINT 1");
let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?;
let mint_info = next_account_info(account_info_iter)?;
println!("MINT 2");

check_program_account(mint_info.owner)?;
let mint_data = &mut mint_info.data.borrow_mut();
let mut mint = PodStateWithExtensionsMut::<PodMint>::unpack(mint_data)?;
let mint_authority = mint.base.mint_authority;
println!("MINT 3");

let auditor_elgamal_pubkey = mint
.get_extension::<ConfidentialTransferMint>()?
.auditor_elgamal_pubkey;
println!("MINT 4");
if let Ok(extension) = mint.get_extension::<PausableConfig>() {
if extension.paused.into() {
return Err(TokenError::MintPaused.into());
}
}
println!("MINT 5");
let mint_burn_extension = mint.get_extension_mut::<ConfidentialMintBurn>()?;
println!("MINT 6");

let proof_context = verify_mint_proof(
account_info_iter,
data.equality_proof_instruction_offset,
data.ciphertext_validity_proof_instruction_offset,
data.range_proof_instruction_offset,
)?;
println!("MINT 7");

check_program_account(token_account_info.owner)?;
let token_account_data = &mut token_account_info.data.borrow_mut();
let mut token_account = PodStateWithExtensionsMut::<PodAccount>::unpack(token_account_data)?;
println!("MINT 8");

let authority_info = next_account_info(account_info_iter)?;
let authority_info_data_len = authority_info.data_len();
let authority = mint_authority.ok_or(TokenError::NoAuthorityExists)?;
println!("MINT 9");

Processor::validate_owner(
program_id,
Expand All @@ -191,6 +200,7 @@ fn process_confidential_mint(
authority_info_data_len,
account_info_iter.as_slice(),
)?;
println!("MINT 10");

if token_account.base.is_frozen() {
return Err(TokenError::AccountFrozen.into());
Expand All @@ -206,6 +216,7 @@ fn process_confidential_mint(
token_account.get_extension_mut::<ConfidentialTransferAccount>()?;
confidential_transfer_account.valid_as_destination()?;

println!("MINT 12");
if proof_context.mint_pubkeys.destination != confidential_transfer_account.elgamal_pubkey {
return Err(ProgramError::InvalidInstructionData);
}
Expand All @@ -215,6 +226,7 @@ fn process_confidential_mint(
return Err(ProgramError::InvalidInstructionData);
}
}
println!("MINT 13");

let proof_context_auditor_ciphertext_lo = proof_context
.mint_amount_ciphertext_lo
Expand All @@ -224,13 +236,15 @@ fn process_confidential_mint(
.mint_amount_ciphertext_hi
.try_extract_ciphertext(2)
.map_err(TokenError::from)?;
println!("MINT 14");

check_auditor_ciphertext(
&data.mint_amount_auditor_ciphertext_lo,
&data.mint_amount_auditor_ciphertext_hi,
&proof_context_auditor_ciphertext_lo,
&proof_context_auditor_ciphertext_hi,
)?;
println!("MINT 15");

confidential_transfer_account.pending_balance_lo = ciphertext_arithmetic::add(
&confidential_transfer_account.pending_balance_lo,
Expand All @@ -240,6 +254,7 @@ fn process_confidential_mint(
.map_err(TokenError::from)?,
)
.ok_or(TokenError::CiphertextArithmeticFailed)?;
println!("MINT 16");
confidential_transfer_account.pending_balance_hi = ciphertext_arithmetic::add(
&confidential_transfer_account.pending_balance_hi,
&proof_context
Expand All @@ -248,6 +263,7 @@ fn process_confidential_mint(
.map_err(TokenError::from)?,
)
.ok_or(TokenError::CiphertextArithmeticFailed)?;
println!("MINT 17");

confidential_transfer_account.increment_pending_balance_credit_counter()?;

Expand All @@ -268,6 +284,7 @@ fn process_confidential_mint(
.map_err(|_| ProgramError::InvalidAccountData)?,
)
.ok_or(TokenError::CiphertextArithmeticFailed)?;
println!("MINT 18");
mint_burn_extension.decryptable_supply = data.new_decryptable_supply;

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions token/program-2022/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,10 @@ impl Processor {
owner_account_data_len: usize,
signers: &[AccountInfo],
) -> ProgramResult {
println!(
"expected owner: {expected_owner}; acc_info_owner: {}",
owner_account_info.key
);
if expected_owner != owner_account_info.key {
return Err(TokenError::OwnerMismatch.into());
}
Expand Down

0 comments on commit fd29474

Please sign in to comment.