Skip to content

Commit

Permalink
Move pool miner to V3 (#4781)
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh authored Feb 28, 2024
1 parent 651bf16 commit 68d91e7
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 52 deletions.
6 changes: 6 additions & 0 deletions ironfish-cli/src/commands/miners/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class Miner extends IronfishCommand {
default: false,
allowNo: true,
}),
blake3: Flags.boolean({
description: 'Mine with the blake3 algorithm instead of fish hash',
default: false,
}),
}

async start(): Promise<void> {
Expand Down Expand Up @@ -124,6 +128,8 @@ export class Miner extends IronfishCommand {
batchSize,
stratum,
name: flags.name,
blake3: flags.blake3,
fishHashFullContext: flags.fishHashFull,
})

miner.start()
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class FoundBlockResult {
}
export class ThreadPoolHandler {
constructor(threadCount: number, batchSize: number, pauseOnSuccess: boolean, useFishHash: boolean, fishHashFullContext: boolean)
newWork(headerBytes: Buffer, target: Buffer, miningRequestId: number, fishHash: boolean): void
newWork(headerBytes: Buffer, target: Buffer, miningRequestId: number, fishHash: boolean, xnLength: number): void
stop(): void
pause(): void
getFoundBlock(): FoundBlockResult | null
Expand Down
10 changes: 8 additions & 2 deletions ironfish-rust-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ impl ThreadPoolHandler {
target: Buffer,
mining_request_id: u32,
fish_hash: bool,
xn_length: u8,
) {
self.threadpool
.new_work(&header_bytes, &target, mining_request_id, fish_hash)
self.threadpool.new_work(
&header_bytes,
&target,
mining_request_id,
fish_hash,
xn_length,
)
}

#[napi]
Expand Down
54 changes: 32 additions & 22 deletions ironfish-rust/src/mining/mine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use byteorder::{BigEndian, WriteBytesExt};
use fish_hash::Context;

/// returns true if a <= b when treating both as 32 byte big endian numbers.
Expand All @@ -18,24 +17,24 @@ pub(crate) fn bytes_lte(a: &[u8], b: &[u8]) -> bool {
true
}

fn randomize_header(i: u64, mut header_bytes: &mut [u8]) {
header_bytes.write_u64::<BigEndian>(i).unwrap();
}

pub(crate) fn mine_batch_blake3(
header_bytes: &mut [u8],
xn_length: u8,
target: &[u8],
start: u64,
step_size: usize,
batch_size: u64,
) -> Option<u64> {
let end = start + batch_size;
for i in (start..=end).step_by(step_size) {
randomize_header(i, header_bytes);
header_bytes[xn_length as usize..8].copy_from_slice(&i.to_be_bytes()[xn_length as usize..]);

let hash = blake3::hash(header_bytes);

if bytes_lte(hash.as_bytes(), target) {
return Some(i);
let mut bytes = [0u8; 8];
bytes.copy_from_slice(&header_bytes[0..8]);
return Some(u64::from_be_bytes(bytes));
}
}
None
Expand All @@ -44,22 +43,26 @@ pub(crate) fn mine_batch_blake3(
pub(crate) fn mine_batch_fish_hash(
context: &mut Context,
header_bytes: &mut [u8],
xn_length: u8,
target: &[u8],
start: u64,
step_size: usize,
batch_size: u64,
) -> Option<u64> {
let end = start + batch_size;
for i in (start..=end).step_by(step_size) {
header_bytes[172..].copy_from_slice(&i.to_be_bytes());
header_bytes[172 + xn_length as usize..]
.copy_from_slice(&i.to_be_bytes()[xn_length as usize..]);

let mut hash = [0u8; 32];
{
fish_hash::hash(&mut hash, context, header_bytes);
}

if bytes_lte(&hash, target) {
return Some(i);
let mut bytes = [0u8; 8];
bytes.copy_from_slice(&header_bytes[172..180]);
return Some(u64::from_be_bytes(bytes));
}
}

Expand All @@ -84,7 +87,7 @@ mod test {
let start = 42;
let step_size = 1;

let result = mine_batch_blake3(header_bytes, target, start, step_size, batch_size);
let result = mine_batch_blake3(header_bytes, 0, target, start, step_size, batch_size);

assert!(result.is_none())
}
Expand All @@ -103,7 +106,7 @@ mod test {
67, 145, 116, 198, 241, 183, 88, 140, 172, 79, 139, 210, 162,
];

let result = mine_batch_blake3(header_bytes, target, start, step_size, batch_size);
let result = mine_batch_blake3(header_bytes, 0, target, start, step_size, batch_size);

assert!(result.is_some());
assert_eq!(result.unwrap(), 43);
Expand All @@ -125,8 +128,15 @@ mod test {
151, 122, 236, 65, 253, 171, 148, 82, 130, 54, 122, 195,
];

let result =
mine_batch_fish_hash(context, header_bytes, target, start, step_size, batch_size);
let result = mine_batch_fish_hash(
context,
header_bytes,
0,
target,
start,
step_size,
batch_size,
);

assert!(result.is_some());
assert_eq!(result.unwrap(), 45);
Expand All @@ -147,23 +157,23 @@ mod test {

// Uses i values: 0, 3, 6, 9
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 9);

// Uses i values: 1, 4, 7, 10
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 1, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 1, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 10);

// Uses i values: 2, 5, 8, 11
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 2, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 2, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
Expand All @@ -176,23 +186,23 @@ mod test {

// Uses i values: 12, 15, 18, 21
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 21);

// Uses i values: 13, 16, 19, 22
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 1, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 1, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 22);

// Uses i values: 14, 17, 20, 23
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 2, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 2, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
Expand All @@ -205,23 +215,23 @@ mod test {

// Uses i values: 24, 27, 30, 33
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 33);

// Uses i values: 25, 28, 31, 34
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 1, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 1, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
assert_eq!(end, 34);

// Uses i values: 26, 29, 32, 35
let header_bytes = &mut header_bytes_base.clone();
let _ = mine_batch_blake3(header_bytes, target, start + 2, step_size, batch_size);
let _ = mine_batch_blake3(header_bytes, 0, target, start + 2, step_size, batch_size);

let mut cursor = Cursor::new(header_bytes);
let end = cursor.read_u64::<BigEndian>().unwrap();
Expand Down
16 changes: 14 additions & 2 deletions ironfish-rust/src/mining/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) enum Command {
Vec<u8>, // target
u32, // mining request id
bool, // use fish hash
u8, // xn length
),
Stop,
Pause,
Expand Down Expand Up @@ -81,12 +82,14 @@ impl Thread {
target: Vec<u8>,
mining_request_id: u32,
use_fish_hash: bool,
xn_length: u8,
) -> Result<(), SendError<Command>> {
self.command_channel.send(Command::NewWork(
header_bytes,
target,
mining_request_id,
use_fish_hash,
xn_length,
))
}

Expand Down Expand Up @@ -126,10 +129,17 @@ fn process_commands(

let command = commands.pop_front().unwrap();
match command {
Command::NewWork(mut header_bytes, target, mining_request_id, use_fish_hash) => {
Command::NewWork(
mut header_bytes,
target,
mining_request_id,
use_fish_hash,
xn_length,
) => {
let search_space = 2_u64.pow(64 - (xn_length as u32 * 8)) - 1;
let mut batch_start = start;
loop {
let remaining_search_space = u64::MAX - batch_start;
let remaining_search_space = search_space - batch_start;
let batch_size = if remaining_search_space > default_batch_size {
default_batch_size
} else {
Expand All @@ -139,6 +149,7 @@ fn process_commands(
let match_found = match use_fish_hash {
false => mine::mine_batch_blake3(
&mut header_bytes,
xn_length,
&target,
batch_start,
step_size,
Expand All @@ -147,6 +158,7 @@ fn process_commands(
true => mine::mine_batch_fish_hash(
fish_hash_context.as_mut().unwrap(),
&mut header_bytes,
xn_length,
&target,
batch_start,
step_size,
Expand Down
2 changes: 2 additions & 0 deletions ironfish-rust/src/mining/threadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl ThreadPool {
target: &[u8],
mining_request_id: u32,
fish_hash: bool,
xn_length: u8,
) {
self.mining_request_id = mining_request_id;

Expand All @@ -66,6 +67,7 @@ impl ThreadPool {
target.to_vec(),
mining_request_id,
fish_hash,
xn_length,
)
.unwrap();
}
Expand Down
6 changes: 6 additions & 0 deletions ironfish/src/mining/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RawBlockTemplateSerde, SerializedBlockTemplate } from '../serde/BlockTe
import { BigIntUtils } from '../utils/bigint'
import { ErrorUtils } from '../utils/error'
import { FileUtils } from '../utils/file'
import { GraffitiUtils } from '../utils/graffiti'
import { SetIntervalToken, SetTimeoutToken } from '../utils/types'
import { TransactionStatus } from '../wallet'
import { MiningPoolShares } from './poolShares'
Expand Down Expand Up @@ -457,6 +458,10 @@ export class MiningPool {
this.logger.debug('target recalculated', { prevHash: latestBlock.header.previousBlockHash })
}

graffiti(): Buffer {
return GraffitiUtils.fromString(this.name)
}

private distributeNewBlock(newBlock: SerializedBlockTemplate) {
Assert.isNotNull(this.currentHeadTimestamp)
Assert.isNotNull(this.currentHeadDifficulty)
Expand All @@ -467,6 +472,7 @@ export class MiningPool {
this.recentSubmissions.clear()

const rawBlock = RawBlockTemplateSerde.deserialize(newBlock)
rawBlock.header.graffiti = this.graffiti()
const newWork = this.blockHasher.serializeHeader(rawBlock.header)

this.stratum.newWork(miningRequestId, newWork)
Expand Down
Loading

0 comments on commit 68d91e7

Please sign in to comment.