randomize hash

This commit is contained in:
Hardhat Chad
2024-04-14 06:12:35 +00:00
parent ca1820606d
commit 9122429140
5 changed files with 13 additions and 27 deletions

View File

@@ -13,12 +13,10 @@ pub enum OreError {
ResetTooEarly = 2,
#[error("The provided hash was invalid")]
HashInvalid = 3,
#[error("The provided hash does not satisfy the difficulty requirement")]
DifficultyNotSatisfied = 4,
#[error("The bus does not have enough rewards to issue at this time")]
BusRewardsInsufficient = 5,
BusRewardsInsufficient = 4,
#[error("The claim amount cannot be greater than the claimable rewards")]
ClaimTooLarge = 6,
ClaimTooLarge = 5,
}
impl From<OreError> for ProgramError {

View File

@@ -197,6 +197,7 @@ pub fn register(signer: Pubkey) -> Instruction {
AccountMeta::new(signer, true),
AccountMeta::new(proof_pda.0, false),
AccountMeta::new_readonly(solana_program::system_program::id(), false),
AccountMeta::new_readonly(sysvar::slot_hashes::id(), false),
],
data: [
OreInstruction::Register.to_vec(),

View File

@@ -116,7 +116,7 @@ pub(crate) fn validate_hash(
signer.as_ref(),
]);
if hash.gt(&difficulty) {
return Err(OreError::DifficultyNotSatisfied.into());
return Err(OreError::HashInvalid.into());
}
Ok(hash)
}

View File

@@ -2,7 +2,7 @@ use std::mem::size_of;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, keccak::hashv,
program_error::ProgramError, pubkey::Pubkey, system_program,
program_error::ProgramError, pubkey::Pubkey, slot_hashes::SlotHash, system_program, sysvar,
};
use crate::{
@@ -32,7 +32,7 @@ pub fn process_register<'a, 'info>(
let args = RegisterArgs::try_from_bytes(data)?;
// Load accounts
let [signer, proof_info, system_program] = accounts else {
let [signer, proof_info, system_program, slot_hashes_info] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
@@ -43,6 +43,7 @@ pub fn process_register<'a, 'info>(
&crate::id(),
)?;
load_program(system_program, system_program::id())?;
load_sysvar(slot_hashes_info, sysvar::slot_hashes::id())?;
// Initialize proof
create_pda(
@@ -58,7 +59,11 @@ pub fn process_register<'a, 'info>(
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
proof.authority = *signer.key;
proof.claimable_rewards = 0;
proof.hash = hashv(&[signer.key.as_ref()]).into();
proof.hash = hashv(&[
signer.key.as_ref(),
&slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()],
])
.into();
proof.total_hashes = 0;
proof.total_rewards = 0;