integrate equix based drillx

This commit is contained in:
Hardhat Chad
2024-05-15 14:39:28 +00:00
parent 3cfffdffc0
commit 96548235dc
4 changed files with 109 additions and 12 deletions

View File

@@ -9,18 +9,20 @@ pub enum OreError {
IsPaused = 0,
#[error("The epoch has ended and needs reset")]
NeedsReset = 1,
#[error("The provided hash is invalid")]
HashInvalid = 2,
#[error("The provided hash did not satisfy the minimum required difficulty")]
HashTooEasy = 2,
HashTooEasy = 3,
#[error("The claim amount cannot be greater than the claimable rewards")]
ClaimTooLarge = 3,
ClaimTooLarge = 4,
#[error("The clock time is invalid")]
ClockInvalid = 4,
ClockInvalid = 5,
#[error("Only one hash may be validated per transaction")]
TransactionInvalid = 5,
TransactionInvalid = 6,
#[error("The tolerance cannot exceed i64 max value")]
ToleranceOverflow = 6,
ToleranceOverflow = 7,
#[error("The maximum supply has been reached")]
MaxSupply = 7,
MaxSupply = 8,
}
impl From<OreError> for ProgramError {

View File

@@ -151,6 +151,7 @@ pub struct RegisterArgs {
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct MineArgs {
pub digest: [u8; 16],
pub nonce: [u8; 8],
}
@@ -273,7 +274,7 @@ pub fn deregister(signer: Pubkey) -> Instruction {
}
/// Builds a mine instruction.
pub fn mine(signer: Pubkey, bus: Pubkey, nonce: u64) -> Instruction {
pub fn mine(signer: Pubkey, bus: Pubkey, digest: [u8; 16], nonce: u64) -> Instruction {
let proof = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()).0;
Instruction {
program_id: crate::id(),
@@ -288,6 +289,7 @@ pub fn mine(signer: Pubkey, bus: Pubkey, nonce: u64) -> Instruction {
data: [
OreInstruction::Mine.to_vec(),
MineArgs {
digest,
nonce: nonce.to_le_bytes(),
}
.to_bytes()

View File

@@ -1,5 +1,6 @@
use std::mem::size_of;
use drillx::Solution;
#[allow(deprecated)]
use solana_program::{
account_info::AccountInfo,
@@ -88,11 +89,15 @@ pub fn process_mine<'a, 'info>(
return Err(OreError::NeedsReset.into());
}
// Calculate the hash from the provided nonce
let hx = drillx::hash(&proof.challenge, &args.nonce);
// Validate the digest
let solution = Solution::new(args.digest, args.nonce);
if !solution.is_valid(&proof.challenge) {
return Err(OreError::HashInvalid.into());
}
// Validate hash satisfies the minimnum difficulty
let difficulty = drillx::difficulty(hx);
let hash = solution.to_hash();
let difficulty = hash.difficulty();
sol_log(&format!("Diff {}", difficulty));
if difficulty.lt(&MIN_DIFFICULTY) {
return Err(OreError::HashTooEasy.into());
@@ -165,7 +170,7 @@ pub fn process_mine<'a, 'info>(
// Hash recent slot hash into the next challenge to prevent pre-mining attacks
proof.challenge = hashv(&[
hx.as_slice(),
hash.h.as_slice(),
&slot_hashes_sysvar.data.borrow()[0..size_of::<SlotHash>()],
])
.0;