From d158742958f30874233f3104e18e68bea633084d Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 16 Feb 2024 19:51:54 +0000 Subject: [PATCH] admin tests --- src/instruction.rs | 30 +++++++++++++ tests/test_initialize.rs | 77 ++++----------------------------- tests/test_mine.rs | 14 +++--- tests/test_reset.rs | 4 +- tests/test_update_admin.rs | 49 +++++++++++++++++++++ tests/test_update_difficulty.rs | 49 +++++++++++++++++++++ 6 files changed, 148 insertions(+), 75 deletions(-) create mode 100644 tests/test_update_admin.rs create mode 100644 tests/test_update_difficulty.rs diff --git a/src/instruction.rs b/src/instruction.rs index 6e8c747..0be9a65 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -311,3 +311,33 @@ pub fn reset(signer: Pubkey) -> Instruction { data: OreInstruction::Reset.to_vec(), } } + +pub fn update_admin(signer: Pubkey, new_admin: Pubkey) -> Instruction { + Instruction { + program_id: crate::id(), + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new(TREASURY_ADDRESS, false), + ], + data: [ + OreInstruction::UpdateAdmin.to_vec(), + UpdateAdminArgs { new_admin }.to_bytes().to_vec(), + ] + .concat(), + } +} + +pub fn update_difficulty(signer: Pubkey, new_difficulty: Hash) -> Instruction { + Instruction { + program_id: crate::id(), + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new(TREASURY_ADDRESS, false), + ], + data: [ + OreInstruction::UpdateDifficulty.to_vec(), + UpdateDifficultyArgs { new_difficulty }.to_bytes().to_vec(), + ] + .concat(), + } +} diff --git a/tests/test_initialize.rs b/tests/test_initialize.rs index cc9be55..b78b822 100644 --- a/tests/test_initialize.rs +++ b/tests/test_initialize.rs @@ -1,17 +1,9 @@ use ore::{ - instruction::{InitializeArgs, OreInstruction}, state::{Bus, Treasury}, utils::AccountDeserialize, - BUS, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, MINT, TREASURY, -}; -use solana_program::{ - hash::Hash, - instruction::{AccountMeta, Instruction}, - program_option::COption, - program_pack::Pack, - pubkey::Pubkey, - system_program, sysvar, + BUS_ADDRESSES, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, MINT_ADDRESS, TREASURY, }; +use solana_program::{hash::Hash, program_option::COption, program_pack::Pack, pubkey::Pubkey}; use solana_program_test::{processor, BanksClient, ProgramTest}; use solana_sdk::{ signature::{Keypair, Signer}, @@ -22,73 +14,22 @@ use spl_token::state::{AccountState, Mint}; #[tokio::test] async fn test_initialize() { // Setup - let (mut banks, payer, hash) = setup_program_test_env().await; + let (mut banks, payer, blockhash) = setup_program_test_env().await; // Pdas - let bus_pdas = vec![ - Pubkey::find_program_address(&[BUS, &[0]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[1]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[2]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[3]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[4]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[5]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[6]], &ore::id()), - Pubkey::find_program_address(&[BUS, &[7]], &ore::id()), - ]; - let mint_pda = Pubkey::find_program_address(&[MINT], &ore::id()); let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id()); let treasury_tokens_address = - spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &mint_pda.0); - - // Build ix - let ix = Instruction { - program_id: ore::ID, - accounts: vec![ - AccountMeta::new(payer.pubkey(), true), - AccountMeta::new(bus_pdas[0].0, false), - AccountMeta::new(bus_pdas[1].0, false), - AccountMeta::new(bus_pdas[2].0, false), - AccountMeta::new(bus_pdas[3].0, false), - AccountMeta::new(bus_pdas[4].0, false), - AccountMeta::new(bus_pdas[5].0, false), - AccountMeta::new(bus_pdas[6].0, false), - AccountMeta::new(bus_pdas[7].0, false), - AccountMeta::new(mint_pda.0, false), - AccountMeta::new(treasury_pda.0, false), - AccountMeta::new(treasury_tokens_address, false), - AccountMeta::new_readonly(system_program::id(), false), - AccountMeta::new_readonly(spl_token::id(), false), - AccountMeta::new_readonly(spl_associated_token_account::id(), false), - AccountMeta::new_readonly(sysvar::rent::id(), false), - ], - data: [ - OreInstruction::Initialize.to_vec(), - InitializeArgs { - bus_0_bump: bus_pdas[0].1, - bus_1_bump: bus_pdas[1].1, - bus_2_bump: bus_pdas[2].1, - bus_3_bump: bus_pdas[3].1, - bus_4_bump: bus_pdas[4].1, - bus_5_bump: bus_pdas[5].1, - bus_6_bump: bus_pdas[6].1, - bus_7_bump: bus_pdas[7].1, - mint_bump: mint_pda.1, - treasury_bump: treasury_pda.1, - } - .to_bytes() - .to_vec(), - ] - .concat(), - }; + spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &MINT_ADDRESS); // Submit tx - let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash); + let ix = ore::instruction::initialize(payer.pubkey()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); // Test bus state for i in 0..BUS_COUNT { - let bus_account = banks.get_account(bus_pdas[i].0).await.unwrap().unwrap(); + let bus_account = banks.get_account(BUS_ADDRESSES[i]).await.unwrap().unwrap(); assert_eq!(bus_account.owner, ore::id()); let bus = Bus::try_from_bytes(&bus_account.data).unwrap(); assert_eq!(bus.id as u8, i as u8); @@ -107,7 +48,7 @@ async fn test_initialize() { assert_eq!(treasury.total_claimed_rewards as u8, 0); // Test mint state - let mint_account = banks.get_account(mint_pda.0).await.unwrap().unwrap(); + let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap(); assert_eq!(mint_account.owner, spl_token::id()); let mint = Mint::unpack(&mint_account.data).unwrap(); assert_eq!(mint.mint_authority, COption::Some(treasury_pda.0)); @@ -124,7 +65,7 @@ async fn test_initialize() { .unwrap(); assert_eq!(treasury_tokens_account.owner, spl_token::id()); let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap(); - assert_eq!(treasury_tokens.mint, mint_pda.0); + assert_eq!(treasury_tokens.mint, MINT_ADDRESS); assert_eq!(treasury_tokens.owner, treasury_pda.0); assert_eq!(treasury_tokens.amount, 0); assert_eq!(treasury_tokens.delegate, COption::None); diff --git a/tests/test_mine.rs b/tests/test_mine.rs index fbab19a..d9f5bc0 100644 --- a/tests/test_mine.rs +++ b/tests/test_mine.rs @@ -29,12 +29,12 @@ use spl_token::state::{AccountState, Mint}; #[tokio::test] async fn test_mine() { // Setup - let (mut banks, payer, hash) = setup_program_test_env().await; + let (mut banks, payer, blockhash) = setup_program_test_env().await; // Submit register tx let proof_pda = Pubkey::find_program_address(&[PROOF, payer.pubkey().as_ref()], &ore::id()); let ix = ore::instruction::register(payer.pubkey()); - let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); @@ -57,7 +57,7 @@ async fn test_mine() { // Submit mine tx let ix = ore::instruction::mine(payer.pubkey(), BUS_ADDRESSES[0], next_hash.into(), nonce); - let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); @@ -90,8 +90,12 @@ async fn test_mine() { &spl_token::id(), ); let ix = ore::instruction::claim(payer.pubkey(), beneficiary_address, amount); - let tx = - Transaction::new_signed_with_payer(&[token_ix, ix], Some(&payer.pubkey()), &[&payer], hash); + let tx = Transaction::new_signed_with_payer( + &[token_ix, ix], + Some(&payer.pubkey()), + &[&payer], + blockhash, + ); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); diff --git a/tests/test_reset.rs b/tests/test_reset.rs index 61ce761..dd97372 100644 --- a/tests/test_reset.rs +++ b/tests/test_reset.rs @@ -20,7 +20,7 @@ use spl_token::state::{AccountState, Mint}; #[tokio::test] async fn test_reset() { // Setup - let (mut banks, payer, hash) = setup_program_test_env().await; + let (mut banks, payer, blockhash) = setup_program_test_env().await; // Pdas let bus_pdas = vec![ @@ -40,7 +40,7 @@ async fn test_reset() { // Submit tx let ix = ore::instruction::reset(payer.pubkey()); - let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); diff --git a/tests/test_update_admin.rs b/tests/test_update_admin.rs new file mode 100644 index 0000000..f057bf1 --- /dev/null +++ b/tests/test_update_admin.rs @@ -0,0 +1,49 @@ +use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS}; +use solana_program::{hash::Hash, pubkey::Pubkey}; +use solana_program_test::{processor, BanksClient, ProgramTest}; +use solana_sdk::{ + signature::{Keypair, Signer}, + transaction::Transaction, +}; + +#[tokio::test] +async fn test_update_admin() { + // Setup + let (mut banks, payer, blockhash) = setup_program_test_env().await; + + // Submit tx + let ix = ore::instruction::initialize(payer.pubkey()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Get treasury account + let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap(); + let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap(); + + // Submit update admin ix + let new_admin = Pubkey::new_unique(); + let ix = ore::instruction::update_admin(payer.pubkey(), new_admin); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Assert treasury state + let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap(); + let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap(); + assert_eq!(treasury_.bump, treasury.bump); + assert_eq!(treasury_.admin, new_admin); + assert_eq!(treasury_.difficulty, treasury.difficulty); + assert_eq!(treasury_.epoch_start_at, treasury.epoch_start_at); + assert_eq!(treasury_.reward_rate, treasury.reward_rate); + assert_eq!( + treasury_.total_claimed_rewards, + treasury.total_claimed_rewards + ); +} + +async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) { + let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction)); + program_test.prefer_bpf(true); + program_test.start().await +} diff --git a/tests/test_update_difficulty.rs b/tests/test_update_difficulty.rs new file mode 100644 index 0000000..2e8be0c --- /dev/null +++ b/tests/test_update_difficulty.rs @@ -0,0 +1,49 @@ +use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS}; +use solana_program::{hash::Hash, keccak::Hash as KeccakHash}; +use solana_program_test::{processor, BanksClient, ProgramTest}; +use solana_sdk::{ + signature::{Keypair, Signer}, + transaction::Transaction, +}; + +#[tokio::test] +async fn test_update_difficulty() { + // Setup + let (mut banks, payer, blockhash) = setup_program_test_env().await; + + // Submit tx + let ix = ore::instruction::initialize(payer.pubkey()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Get treasury account + let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap(); + let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap(); + + // Submit update admin ix + let new_difficulty = KeccakHash::new_unique(); + let ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Assert treasury state + let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap(); + let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap(); + assert_eq!(treasury_.bump, treasury.bump); + assert_eq!(treasury_.admin, treasury.admin); + assert_eq!(treasury_.difficulty, new_difficulty.into()); + assert_eq!(treasury_.epoch_start_at, treasury.epoch_start_at); + assert_eq!(treasury_.reward_rate, treasury.reward_rate); + assert_eq!( + treasury_.total_claimed_rewards, + treasury.total_claimed_rewards + ); +} + +async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) { + let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction)); + program_test.prefer_bpf(true); + program_test.start().await +}