mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 15:10:13 +00:00
remove hash arg and move nonce to front of digest
This commit is contained in:
@@ -4,10 +4,9 @@ use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
clock::Clock,
|
||||
entrypoint::ProgramResult,
|
||||
keccak::{hashv, Hash as KeccakHash, HASH_BYTES},
|
||||
keccak::{hashv, Hash as KeccakHash},
|
||||
program::set_return_data,
|
||||
program_error::ProgramError,
|
||||
program_memory::sol_memcmp,
|
||||
pubkey::Pubkey,
|
||||
slot_hashes::SlotHash,
|
||||
sysvar::{self, Sysvar},
|
||||
@@ -70,8 +69,7 @@ pub fn process_mine<'a, 'info>(
|
||||
// Validate provided hash
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
validate_hash(
|
||||
args.hash.into(),
|
||||
let hash = validate_hash(
|
||||
proof.hash.into(),
|
||||
*signer.key,
|
||||
u64::from_le_bytes(args.nonce),
|
||||
@@ -89,7 +87,7 @@ pub fn process_mine<'a, 'info>(
|
||||
|
||||
// Hash recent slot hash into the next challenge to prevent pre-mining attacks
|
||||
proof.hash = hashv(&[
|
||||
KeccakHash::from(args.hash).as_ref(),
|
||||
hash.as_ref(),
|
||||
&slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()],
|
||||
])
|
||||
.into();
|
||||
@@ -107,34 +105,26 @@ pub fn process_mine<'a, 'info>(
|
||||
/// Validates the provided hash, ensursing it is equal to SHA3(current_hash, singer, nonce).
|
||||
/// Fails if the provided hash is valid but does not satisfy the required difficulty.
|
||||
pub(crate) fn validate_hash(
|
||||
hash: KeccakHash,
|
||||
current_hash: KeccakHash,
|
||||
signer: Pubkey,
|
||||
nonce: u64,
|
||||
difficulty: KeccakHash,
|
||||
) -> Result<(), ProgramError> {
|
||||
// Validate hash correctness
|
||||
let hash_ = hashv(&[
|
||||
) -> Result<KeccakHash, ProgramError> {
|
||||
let hash = hashv(&[
|
||||
nonce.to_le_bytes().as_slice(),
|
||||
current_hash.as_ref(),
|
||||
signer.as_ref(),
|
||||
nonce.to_le_bytes().as_slice(),
|
||||
]);
|
||||
if sol_memcmp(hash.as_ref(), hash_.as_ref(), HASH_BYTES) != 0 {
|
||||
return Err(OreError::HashInvalid.into());
|
||||
}
|
||||
|
||||
// Validate hash difficulty
|
||||
if hash.gt(&difficulty) {
|
||||
return Err(OreError::DifficultyNotSatisfied.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(hash)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use solana_program::{
|
||||
keccak::{hashv, Hash, HASH_BYTES},
|
||||
keccak::{Hash, HASH_BYTES},
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
@@ -146,38 +136,17 @@ mod tests {
|
||||
let signer = Pubkey::new_unique();
|
||||
let nonce = 10u64;
|
||||
let difficulty = Hash::new_from_array([255; HASH_BYTES]);
|
||||
let h2 = hashv(&[
|
||||
h1.to_bytes().as_slice(),
|
||||
signer.to_bytes().as_slice(),
|
||||
nonce.to_le_bytes().as_slice(),
|
||||
]);
|
||||
let res = validate_hash(h2, h1, signer, nonce, difficulty);
|
||||
let res = validate_hash(h1, signer, nonce, difficulty);
|
||||
assert!(res.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_hash_fail() {
|
||||
let h1 = Hash::new_from_array([1; HASH_BYTES]);
|
||||
let signer = Pubkey::new_unique();
|
||||
let nonce = 10u64;
|
||||
let difficulty = Hash::new_from_array([255; HASH_BYTES]);
|
||||
let h2 = Hash::new_from_array([2; HASH_BYTES]);
|
||||
let res = validate_hash(h2, h1, signer, nonce, difficulty);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_hash_fail_difficulty() {
|
||||
let h1 = Hash::new_from_array([1; HASH_BYTES]);
|
||||
let signer = Pubkey::new_unique();
|
||||
let nonce = 10u64;
|
||||
let difficulty = Hash::new_from_array([0; HASH_BYTES]);
|
||||
let h2 = hashv(&[
|
||||
h1.to_bytes().as_slice(),
|
||||
signer.to_bytes().as_slice(),
|
||||
nonce.to_le_bytes().as_slice(),
|
||||
]);
|
||||
let res = validate_hash(h2, h1, signer, nonce, difficulty);
|
||||
let res = validate_hash(h1, signer, nonce, difficulty);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user