Skip to content

Commit

Permalink
chore: add unit tests and cleanups (#913)
Browse files Browse the repository at this point in the history
Changes:
- add signer checks unit tests
- add cpi context unit tests
- add cpi context associated Merkle tree check
- moved hash output compressed accounts and prepare append cpi instruction data into function
- removed dead code in hash input compressed accounts
- removed registered program pda type from anchor Instruction structs other than in account compression program
  • Loading branch information
ananas-block authored Jul 1, 2024
1 parent aa03d83 commit 0e0fec6
Show file tree
Hide file tree
Showing 43 changed files with 1,184 additions and 344 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub fn process_escrow_compressed_tokens_with_compressed_pda<'info>(
proof.clone(),
cpi_context,
)?;
msg!("escrow compressed tokens with compressed pda");
cpi_compressed_pda_transfer(
ctx,
proof,
Expand Down
2 changes: 0 additions & 2 deletions programs/account-compression/src/config_accounts/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl GroupAccess for StateMerkleTreeAccount {
&self.metadata.access_metadata.owner
}

fn get_delegate(&self) -> &Pubkey {
fn get_program_owner(&self) -> &Pubkey {
&self.metadata.access_metadata.program_owner
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ pub fn process_initialize_address_merkle_tree_and_queue<'info>(
merkle_tree_config: AddressMerkleTreeConfig,
queue_config: AddressQueueConfig,
) -> Result<()> {
if merkle_tree_config.close_threshold.is_some() {
unimplemented!("Close threshold not supported.");
}

let merkle_tree_rent = ctx.accounts.merkle_tree.get_lamports();
process_initialize_address_queue(
&ctx.accounts.queue.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::{prelude::*, solana_program::pubkey::Pubkey};

use crate::{config_accounts::GroupAuthority, utils::constants::GROUP_AUTHORITY_SEED};
use crate::{state::GroupAuthority, utils::constants::GROUP_AUTHORITY_SEED};

#[derive(Accounts)]
pub struct InitializeGroupAuthority<'info> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ pub fn process_initialize_state_merkle_tree_and_nullifier_queue(
nullifier_queue_config: NullifierQueueConfig,
additional_rent: u64,
) -> Result<()> {
if state_merkle_tree_config.close_threshold.is_some() {
unimplemented!("Close threshold not supported.");
}

process_initialize_state_merkle_tree(
&ctx.accounts.merkle_tree,
index,
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::*;
4 changes: 1 addition & 3 deletions programs/account-compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ pub mod instructions;
pub use instructions::*;
pub mod state;
pub use state::*;
pub mod config_accounts;
pub mod utils;
pub use config_accounts::*;
pub mod processor;
pub mod utils;
pub use processor::*;
pub mod sdk;
use anchor_lang::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions programs/account-compression/src/state/access.rs
Original file line number Diff line number Diff line change
@@ -1,11 +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,
/// Delegate of the Merkle tree. This will be used for program owned Merkle trees.
/// Program owner of the Merkle tree. This will be used for program owned Merkle trees.
pub program_owner: Pubkey,
}

Expand Down
3 changes: 1 addition & 2 deletions programs/account-compression/src/state/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ impl GroupAccess for AddressMerkleTreeAccount {
fn get_owner(&self) -> &Pubkey {
&self.metadata.access_metadata.owner
}

fn get_delegate(&self) -> &Pubkey {
fn get_program_owner(&self) -> &Pubkey {
&self.metadata.access_metadata.program_owner
}
}
Expand Down
2 changes: 1 addition & 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 Down
3 changes: 3 additions & 0 deletions programs/account-compression/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub use queue::*;

pub mod rollover;
pub use rollover::*;

pub mod group_authority;
pub use group_authority::*;
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
5 changes: 2 additions & 3 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,6 @@ pub fn check_queue_type(queue_type: &u64, expected_queue_type: &QueueType) -> Re
Ok(())
}
}

impl QueueMetadata {
pub fn init(
&mut self,
Expand Down Expand Up @@ -96,7 +95,7 @@ impl GroupAccess for QueueAccount {
&self.metadata.access_metadata.owner
}

fn get_delegate(&self) -> &Pubkey {
fn get_program_owner(&self) -> &Pubkey {
&self.metadata.access_metadata.program_owner
}
}
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())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::constants::CPI_AUTHORITY_PDA_SEED;

pub trait GroupAccess {
fn get_owner(&self) -> &Pubkey;
fn get_delegate(&self) -> &Pubkey;
fn get_program_owner(&self) -> &Pubkey;
}

pub trait GroupAccounts<'info> {
Expand Down
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
2 changes: 1 addition & 1 deletion programs/registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::too_many_arguments)]
use account_compression::utils::constants::CPI_AUTHORITY_PDA_SEED;
use account_compression::{config_accounts::GroupAuthority, program::AccountCompression};
use account_compression::{program::AccountCompression, state::GroupAuthority};
use anchor_lang::prelude::*;

pub mod forester;
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 0e0fec6

Please sign in to comment.