Skip to content

Commit

Permalink
add cpi context unit tests, other cleanup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Jun 30, 2024
1 parent d84207c commit d0e2fe7
Show file tree
Hide file tree
Showing 33 changed files with 1,197 additions and 350 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub struct GroupAuthority {
pub authority: Pubkey,
pub seed: Pubkey,
}
// TODO: move to state
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub fn process_initialize_address_merkle_tree_and_queue<'info>(
if merkle_tree_config.close_threshold.is_some() {
unimplemented!("Close threshold not supported.");
}
if merkle_tree_config.height != 26 {
unimplemented!("Height other than 26 not supported.");
}
if merkle_tree_config.canopy_depth != 10 {
unimplemented!("Canopy depth other than 10 not supported.");
}

let merkle_tree_rent = ctx.accounts.merkle_tree.get_lamports();
process_initialize_address_queue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub fn process_initialize_state_merkle_tree_and_nullifier_queue(
if state_merkle_tree_config.close_threshold.is_some() {
unimplemented!("Close threshold not supported.");
}
if state_merkle_tree_config.height != 26 {
unimplemented!("Height other than 26 not supported.");
}
if state_merkle_tree_config.canopy_depth != 10 {
unimplemented!("Canopy depth other than 10 not supported.");
}

process_initialize_state_merkle_tree(
&ctx.accounts.merkle_tree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ pub struct InsertIntoQueues<'info> {
/// Inserts every element into the indexed array.
/// Throws an error if the element already exists.
/// Expects an indexed queue account as for every index as remaining account.
pub fn process_insert_into_queues<
'a,
'b,
'c: 'info,
'info,
MerkleTreeAccount: Owner + ZeroCopy, // SequenceNumber,
>(
pub fn process_insert_into_queues<'a, 'b, 'c: 'info, 'info, MerkleTreeAccount: Owner + ZeroCopy>(
ctx: Context<'a, 'b, 'c, 'info, InsertIntoQueues<'info>>,
elements: &'a [[u8; 32]],
queue_type: QueueType,
Expand Down
4 changes: 2 additions & 2 deletions programs/account-compression/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub use update_group_authority::*;
pub mod register_program;
pub use register_program::*;

pub mod rollover_state_merkle_tree;
pub use rollover_state_merkle_tree::*;
pub mod rollover_state_merkle_tree_and_queue;
pub use rollover_state_merkle_tree_and_queue::*;

pub mod rollover_address_merkle_tree_and_queue;
pub use rollover_address_merkle_tree_and_queue::*;
3 changes: 2 additions & 1 deletion programs/account-compression/src/state/access.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anchor_lang::prelude::*;

#[account(zero_copy)]
#[derive(AnchorDeserialize, Debug, PartialEq)]
#[derive(AnchorDeserialize, Debug, PartialEq, Default)]
pub struct AccessMetadata {
/// Owner of the Merkle tree.
pub owner: Pubkey,
// TODO: clean up all mention of delegate
/// Delegate of the Merkle tree. This will be used for program owned Merkle trees.
pub program_owner: Pubkey,
}
Expand Down
2 changes: 1 addition & 1 deletion programs/account-compression/src/state/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl GroupAccess for AddressMerkleTreeAccount {
fn get_owner(&self) -> &Pubkey {
&self.metadata.access_metadata.owner
}

// TODO: rename to get_program_owner
fn get_delegate(&self) -> &Pubkey {
&self.metadata.access_metadata.program_owner
}
Expand Down
3 changes: 2 additions & 1 deletion programs/account-compression/src/state/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anchor_lang::prelude::*;
use crate::{errors::AccountCompressionErrorCode, AccessMetadata, RolloverMetadata};

#[account(zero_copy)]
#[derive(AnchorDeserialize, Debug, PartialEq)]
#[derive(AnchorDeserialize, Debug, PartialEq, Default)]
pub struct MerkleTreeMetadata {
pub access_metadata: AccessMetadata,
pub rollover_metadata: RolloverMetadata,
Expand All @@ -13,6 +13,7 @@ pub struct MerkleTreeMetadata {
pub next_merkle_tree: Pubkey,
}

// TODO: unit test
impl MerkleTreeMetadata {
pub fn init(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{AccessMetadata, MerkleTreeMetadata, RolloverMetadata};
/// Concurrent state Merkle tree used for public compressed transactions.
#[account(zero_copy)]
#[aligned_sized(anchor)]
#[derive(AnchorDeserialize, Debug)]
#[derive(AnchorDeserialize, Debug, Default)]
pub struct StateMerkleTreeAccount {
pub metadata: MerkleTreeMetadata,
}
Expand Down
4 changes: 2 additions & 2 deletions programs/account-compression/src/state/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct QueueMetadata {
#[derive(AnchorDeserialize, AnchorSerialize, Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum QueueType {
NullifierQueue = 1, // Explicitly assign values to the enum variants
NullifierQueue = 1,
AddressQueue = 2,
}

Expand All @@ -36,7 +36,7 @@ pub fn check_queue_type(queue_type: &u64, expected_queue_type: &QueueType) -> Re
Ok(())
}
}

// TODO: unit test
impl QueueMetadata {
pub fn init(
&mut self,
Expand Down
42 changes: 40 additions & 2 deletions programs/account-compression/src/state/rollover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,46 @@ impl RolloverMetadata {
return err!(crate::errors::AccountCompressionErrorCode::MerkleTreeAlreadyRolledOver);
}

self.rolledover_slot = Clock::get()?.slot;

#[cfg(target_os = "solana")]
{
self.rolledover_slot = Clock::get()?.slot;
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_rollover_metadata() {
let mut metadata = RolloverMetadata::new(0, 0, Some(95), 0, Some(100));
assert_eq!(metadata.rollover_threshold, 95);
assert_eq!(metadata.close_threshold, 100);
assert_eq!(metadata.rolledover_slot, u64::MAX);

metadata.rollover().unwrap();

let mut metadata = RolloverMetadata::new(0, 0, None, 0, None);
assert_eq!(metadata.rollover_threshold, u64::MAX);
assert_eq!(metadata.close_threshold, u64::MAX);

assert_eq!(
metadata.rollover(),
Err(crate::errors::AccountCompressionErrorCode::RolloverNotConfigured.into())
);
let mut metadata = RolloverMetadata::new(0, 0, Some(95), 0, None);
assert_eq!(metadata.close_threshold, u64::MAX);

metadata.rollover().unwrap();
let mut metadata = RolloverMetadata::new(0, 0, Some(95), 0, None);
metadata.rolledover_slot = 0;
assert_eq!(metadata.close_threshold, u64::MAX);

assert_eq!(
metadata.rollover(),
Err(crate::errors::AccountCompressionErrorCode::MerkleTreeAlreadyRolledOver.into())
);
}
}
10 changes: 3 additions & 7 deletions programs/compressed-token/src/instructions/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub struct FreezeInstruction<'info> {
#[account(seeds = [CPI_AUTHORITY_PDA_SEED], bump,)]
pub cpi_authority_pda: UncheckedAccount<'info>,
pub light_system_program: Program<'info, light_system_program::program::LightSystemProgram>,
/// CHECK: this account
pub registered_program_pda:
Account<'info, account_compression::instructions::register_program::RegisteredProgram>,
/// CHECK: this account is checked in account compression program
pub registered_program_pda: AccountInfo<'info>,
/// CHECK: this account
pub noop_program: UncheckedAccount<'info>,
/// CHECK: this account in psp account compression program
Expand All @@ -30,10 +29,7 @@ pub struct FreezeInstruction<'info> {
}

impl<'info> InvokeAccounts<'info> for FreezeInstruction<'info> {
fn get_registered_program_pda(
&self,
) -> &Account<'info, account_compression::instructions::register_program::RegisteredProgram>
{
fn get_registered_program_pda(&self) -> &AccountInfo<'info> {
&self.registered_program_pda
}

Expand Down
10 changes: 3 additions & 7 deletions programs/compressed-token/src/instructions/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ pub struct GenericInstruction<'info> {
#[account(seeds = [CPI_AUTHORITY_PDA_SEED], bump,)]
pub cpi_authority_pda: UncheckedAccount<'info>,
pub light_system_program: Program<'info, light_system_program::program::LightSystemProgram>,
/// CHECK: this account
pub registered_program_pda:
Account<'info, account_compression::instructions::register_program::RegisteredProgram>,
/// CHECK: this account is checked in account compression program
pub registered_program_pda: AccountInfo<'info>,
/// CHECK: this account
pub noop_program: UncheckedAccount<'info>,
/// CHECK: this account in psp account compression program
Expand All @@ -27,10 +26,7 @@ pub struct GenericInstruction<'info> {
}

impl<'info> InvokeAccounts<'info> for GenericInstruction<'info> {
fn get_registered_program_pda(
&self,
) -> &Account<'info, account_compression::instructions::register_program::RegisteredProgram>
{
fn get_registered_program_pda(&self) -> &AccountInfo<'info> {
&self.registered_program_pda
}

Expand Down
8 changes: 2 additions & 6 deletions programs/compressed-token/src/instructions/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pub struct TransferInstruction<'info> {
pub cpi_authority_pda: UncheckedAccount<'info>,
pub light_system_program: Program<'info, light_system_program::program::LightSystemProgram>,
/// CHECK: this account
pub registered_program_pda:
Account<'info, account_compression::instructions::register_program::RegisteredProgram>,
pub registered_program_pda: AccountInfo<'info>,
/// CHECK: this account
pub noop_program: UncheckedAccount<'info>,
/// CHECK: this account in psp account compression program
Expand All @@ -36,10 +35,7 @@ pub struct TransferInstruction<'info> {

// TODO: transform all to account info
impl<'info> InvokeAccounts<'info> for TransferInstruction<'info> {
fn get_registered_program_pda(
&self,
) -> &Account<'info, account_compression::instructions::register_program::RegisteredProgram>
{
fn get_registered_program_pda(&self) -> &AccountInfo<'info> {
&self.registered_program_pda
}

Expand Down
4 changes: 4 additions & 0 deletions programs/system/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ pub enum SystemProgramError {
InstructionNotCallable,
#[msg("CpiContextFeePayerMismatch")]
CpiContextFeePayerMismatch,
#[msg("CpiContextAssociatedMerkleTreeMismatch")]
CpiContextAssociatedMerkleTreeMismatch,
#[msg("NoInputs")]
NoInputs,
}
Loading

0 comments on commit d0e2fe7

Please sign in to comment.