diff --git a/src/processor/mine.rs b/src/processor/mine.rs index bbbb13b..dad7a61 100644 --- a/src/processor/mine.rs +++ b/src/processor/mine.rs @@ -70,12 +70,15 @@ pub fn process_mine<'a, 'info>( proof.claimable_rewards = proof.claimable_rewards.saturating_add(treasury.reward_rate); // Hash most recent slot hash into the next challenge to prevent pre-mining attacks - let slot_hash_bytes = &slot_hashes_info.data.borrow()[0..size_of::()]; - proof.hash = hashv(&[KeccakHash::from(args.hash).as_ref(), slot_hash_bytes]).into(); + proof.hash = hashv(&[ + KeccakHash::from(args.hash).as_ref(), + &slot_hashes_info.data.borrow()[0..size_of::()], + ]) + .into(); // Update lifetime stats proof.total_hashes = proof.total_hashes.saturating_add(1); - proof.total_rewards = proof.total_rewards.saturating_add(1); + proof.total_rewards = proof.total_rewards.saturating_add(treasury.reward_rate); // Log the mined rewards set_return_data(treasury.reward_rate.to_le_bytes().as_slice()); diff --git a/src/processor/register.rs b/src/processor/register.rs index 34c5545..ccac598 100644 --- a/src/processor/register.rs +++ b/src/processor/register.rs @@ -42,10 +42,9 @@ pub fn process_register<'a, 'info>( let mut proof_data = proof_info.data.borrow_mut(); proof_data[0] = Proof::discriminator() as u8; let mut proof = Proof::try_from_bytes_mut(&mut proof_data)?; - // proof.bump = args.bump as u64; proof.authority = *signer.key; proof.claimable_rewards = 0; - proof.hash = hashv(&[&signer.key.to_bytes()]).into(); + proof.hash = hashv(&[signer.key.as_ref()]).into(); proof.total_hashes = 0; proof.total_rewards = 0; diff --git a/tests/test_initialize.rs b/tests/test_initialize.rs index 9fc6762..cc9be55 100644 --- a/tests/test_initialize.rs +++ b/tests/test_initialize.rs @@ -93,12 +93,6 @@ async fn test_initialize() { let bus = Bus::try_from_bytes(&bus_account.data).unwrap(); assert_eq!(bus.id as u8, i as u8); assert_eq!(bus.rewards, 0); - println!( - "Bus {:?} {:?} {:?}", - bus_pdas[i].0, - bus_account, - bs64::encode(&bus_account.data) - ); } // Test treasury state @@ -111,12 +105,6 @@ async fn test_initialize() { assert_eq!(treasury.epoch_start_at as u8, 0); assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE); assert_eq!(treasury.total_claimed_rewards as u8, 0); - println!( - "Treasury {:?} {:?} {:?}", - treasury_pda.0, - treasury_account, - bs64::encode(&treasury_account.data) - ); // Test mint state let mint_account = banks.get_account(mint_pda.0).await.unwrap().unwrap(); @@ -127,12 +115,6 @@ async fn test_initialize() { assert_eq!(mint.decimals, ore::TOKEN_DECIMALS); assert_eq!(mint.is_initialized, true); assert_eq!(mint.freeze_authority, COption::None); - println!( - "Mint {:?} {:?} {:?}", - mint_pda.0, - mint_account, - bs64::encode(&mint_account.data) - ); // Test treasury token state let treasury_tokens_account = banks @@ -150,14 +132,6 @@ async fn test_initialize() { assert_eq!(treasury_tokens.is_native, COption::None); assert_eq!(treasury_tokens.delegated_amount, 0); assert_eq!(treasury_tokens.close_authority, COption::None); - println!( - "Treasury tokens {:?} {:?} {:?}", - treasury_tokens_address, - treasury_tokens_account, - bs64::encode(&treasury_tokens_account.data) - ); - - // assert!(false); } async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) { diff --git a/tests/test_mine.rs b/tests/test_mine.rs index fb45236..ef126fd 100644 --- a/tests/test_mine.rs +++ b/tests/test_mine.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{mem::size_of, str::FromStr}; use ore::{ state::{Bus, Proof, Treasury}, @@ -13,6 +13,7 @@ use solana_program::{ program_option::COption, program_pack::Pack, pubkey::Pubkey, + slot_hashes::SlotHash, sysvar, }; use solana_program_test::{processor, BanksClient, ProgramTest}; @@ -27,12 +28,10 @@ async fn test_mine() { // Setup let (mut banks, payer, hash) = setup_program_test_env().await; - // Build register ix + // Submit register tx let proof_pda = Pubkey::find_program_address(&[PROOF, payer.pubkey().as_ref()], &ore::id()); - let ix_0 = ore::instruction::register(payer.pubkey()); - - // Submit tx - let tx = Transaction::new_signed_with_payer(&[ix_0], Some(&payer.pubkey()), &[&payer], hash); + let ix = ore::instruction::register(payer.pubkey()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash); let res = banks.process_transaction(tx).await; assert!(res.is_ok()); @@ -40,6 +39,11 @@ async fn test_mine() { let proof_account = banks.get_account(proof_pda.0).await.unwrap().unwrap(); assert_eq!(proof_account.owner, ore::id()); let proof = Proof::try_from_bytes(&proof_account.data).unwrap(); + assert_eq!(proof.authority, payer.pubkey()); + assert_eq!(proof.claimable_rewards, 0); + assert_eq!(proof.hash, hashv(&[payer.pubkey().as_ref()]).into()); + assert_eq!(proof.total_hashes, 0); + assert_eq!(proof.total_rewards, 0); // Assert proof state let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id()); @@ -54,13 +58,29 @@ async fn test_mine() { ); // Submit mine tx - let ix_1 = ore::instruction::mine(payer.pubkey(), BUS_ADDRESSES[0], next_hash.into(), nonce); - let tx = Transaction::new_signed_with_payer(&[ix_1], Some(&payer.pubkey()), &[&payer], hash); + 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 res = banks.process_transaction(tx).await; assert!(res.is_ok()); - // TODO Assert proof state - // TODO Assert bus state + // Assert proof state + let slot_hashes_account = banks + .get_account(sysvar::slot_hashes::id()) + .await + .unwrap() + .unwrap(); + let slot_hash_bytes = &slot_hashes_account.data[0..size_of::()]; + let proof_account = banks.get_account(proof_pda.0).await.unwrap().unwrap(); + assert_eq!(proof_account.owner, ore::id()); + let proof = Proof::try_from_bytes(&proof_account.data).unwrap(); + assert_eq!(proof.authority, payer.pubkey()); + assert_eq!(proof.claimable_rewards, INITIAL_REWARD_RATE); + assert_eq!( + proof.hash, + hashv(&[&next_hash.as_ref(), slot_hash_bytes,]).into() + ); + assert_eq!(proof.total_hashes, 1); + assert_eq!(proof.total_rewards, INITIAL_REWARD_RATE); } fn find_next_hash(hash: KeccakHash, difficulty: KeccakHash, signer: Pubkey) -> (KeccakHash, u64) { diff --git a/tests/test_reset.rs b/tests/test_reset.rs index 6b938a2..61ce761 100644 --- a/tests/test_reset.rs +++ b/tests/test_reset.rs @@ -49,12 +49,6 @@ async fn test_reset() { let bus_account = banks.get_account(bus_pdas[i].0).await.unwrap().unwrap(); assert_eq!(bus_account.owner, ore::id()); let bus = Bus::try_from_bytes(&bus_account.data).unwrap(); - println!( - "Bus {:?} {:?} {:?}", - bus_pdas[i].0, - bus_account, - bs64::encode(&bus_account.data) - ); assert_eq!(bus.id as u8, i as u8); assert_eq!(bus.rewards, BUS_EPOCH_REWARDS); } @@ -72,12 +66,6 @@ async fn test_reset() { assert_eq!(treasury.epoch_start_at as u8, 100); assert_eq!(treasury.reward_rate, INITIAL_REWARD_RATE.saturating_div(2)); assert_eq!(treasury.total_claimed_rewards as u8, 0); - println!( - "Treasury {:?} {:?} {:?}", - treasury_pda.0, - treasury_account, - bs64::encode(&treasury_account.data) - ); // Test mint state let mint_account = banks.get_account(mint_pda.0).await.unwrap().unwrap(); @@ -88,12 +76,6 @@ async fn test_reset() { assert_eq!(mint.decimals, ore::TOKEN_DECIMALS); assert_eq!(mint.is_initialized, true); assert_eq!(mint.freeze_authority, COption::None); - println!( - "Mint {:?} {:?} {:?}", - mint_pda.0, - mint_account, - bs64::encode(&mint_account.data) - ); // Test treasury token state let treasury_tokens_account = banks @@ -111,14 +93,6 @@ async fn test_reset() { assert_eq!(treasury_tokens.is_native, COption::None); assert_eq!(treasury_tokens.delegated_amount, 0); assert_eq!(treasury_tokens.close_authority, COption::None); - println!( - "Treasury tokens {:?} {:?} {:?}", - treasury_tokens_address, - treasury_tokens_account, - bs64::encode(&treasury_tokens_account.data) - ); - - // assert!(false); } async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) {