Skip to content

Commit

Permalink
more rust (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyFFM authored Apr 11, 2019
1 parent 8181075 commit a008de0
Show file tree
Hide file tree
Showing 8 changed files with 595 additions and 154 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
*.o
/log
/.vscode
**/*.exe
**/*.bat
82 changes: 31 additions & 51 deletions src/cpu_worker.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
use crate::miner::{Buffer, NonceData};
use crate::poc_hashing::find_best_deadline_rust;
use crate::reader::ReadReply;
use crossbeam_channel::{Receiver, Sender};
use futures::sync::mpsc;
use futures::{Future, Sink};
#[cfg(any(feature = "simd", feature = "neon"))]
use libc::{c_void, uint64_t};
use std::u64;

extern "C" {
pub fn find_best_deadline_sph(
scoops: *mut c_void,
nonce_count: uint64_t,
gensig: *const c_void,
best_deadline: *mut uint64_t,
best_offset: *mut uint64_t,
) -> ();
}

cfg_if! {
if #[cfg(feature = "simd")] {
extern "C" {
Expand Down Expand Up @@ -125,8 +117,10 @@ pub fn hash(
if read_reply.info.len == 1 && read_reply.info.gpu_signal > 0 {
return;
}


#[allow(unused_assignments)]
let mut deadline: u64 = u64::MAX;
#[allow(unused_assignments)]
let mut offset: u64 = 0;

let bs = buffer.get_buffer_for_writing();
Expand Down Expand Up @@ -167,13 +161,13 @@ pub fn hash(
&mut offset,
);
} else {
find_best_deadline_sph(
bs.as_ptr() as *mut c_void,
let result = find_best_deadline_rust(
&bs,
(read_reply.info.len as u64) / 64,
read_reply.info.gensig.as_ptr() as *const c_void,
&mut deadline,
&mut offset,
&*read_reply.info.gensig,
);
deadline = result.0;
offset = result.1;
}
}
#[cfg(feature = "neon")]
Expand All @@ -191,24 +185,25 @@ pub fn hash(
&mut offset,
);
} else {
find_best_deadline_sph(
bs.as_ptr() as *mut c_void,
let result = find_best_deadline_rust(
&bs,
(read_reply.info.len as u64) / 64,
read_reply.info.gensig.as_ptr() as *const c_void,
&mut deadline,
&mut offset,
&*read_reply.info.gensig,
);
deadline = result.0;
offset = result.1;
}
}

#[cfg(not(any(feature = "simd", feature = "neon")))]
unsafe {
find_best_deadline_sph(
bs.as_ptr() as *mut c_void,
{
let result = find_best_deadline_rust(
&bs,
(read_reply.info.len as u64) / 64,
read_reply.info.gensig.as_ptr() as *const c_void,
&mut deadline,
&mut offset,
&*read_reply.info.gensig,
);
deadline = result.0;
offset = result.1;
}

tx_nonce_data
Expand All @@ -231,20 +226,10 @@ pub fn hash(

#[cfg(test)]
mod tests {
use crate::poc_hashing::find_best_deadline_rust;
use hex;
use libc::{c_void, uint64_t};
use std::u64;

extern "C" {
pub fn find_best_deadline_sph(
scoops: *mut c_void,
nonce_count: uint64_t,
gensig: *const c_void,
best_deadline: *mut uint64_t,
best_offset: *mut uint64_t,
) -> ();
}

cfg_if! {
if #[cfg(feature = "simd")] {
extern "C" {
Expand Down Expand Up @@ -304,30 +289,25 @@ mod tests {

#[test]
fn test_deadline_hashing() {
let mut deadline: u64 = u64::MAX;
let mut offset: u64 = 0;
let mut deadline: u64;
let gensig =
hex::decode("4a6f686e6e7946464d206861742064656e206772f6df74656e2050656e697321")
.unwrap();

let mut gensig_array = [0u8; 32];
gensig_array.copy_from_slice(&gensig[..]);

let winner: [u8; 64] = [0; 64];
let loser: [u8; 64] = [5; 64];
let mut data: [u8; 64 * 32] = [5; 64 * 32];

for i in 0..32 {
data[i * 64..i * 64 + 64].clone_from_slice(&winner);

unsafe {
find_best_deadline_sph(
data.as_ptr() as *mut c_void,
(i + 1) as u64,
gensig.as_ptr() as *const c_void,
&mut deadline,
&mut offset,
);
}
let result = find_best_deadline_rust(&data, (i + 1) as u64, &gensig_array);
deadline = result.0;

assert_eq!(3084580316385335914u64, deadline);
deadline = u64::MAX;
offset = 0;
data[i * 64..i * 64 + 64].clone_from_slice(&loser);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ mod cpu_worker;
mod logger;
mod miner;
mod plot;
mod pocmath;
mod poc_hashing;
mod reader;
mod requests;
mod shabals;
mod shabal256;
mod utils;

#[cfg(feature = "opencl")]
Expand Down
11 changes: 5 additions & 6 deletions src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ocl::GpuBuffer;
#[cfg(feature = "opencl")]
use crate::ocl::GpuContext;
use crate::plot::{Plot, SCOOP_SIZE};
use crate::pocmath;
use crate::poc_hashing;
use crate::reader::Reader;
use crate::requests::RequestHandler;
use crate::utils::{get_device_id, new_thread_pool};
Expand Down Expand Up @@ -132,9 +132,7 @@ fn scan_plots(

if let Ok(p) = Plot::new(file, use_direct_io, dummy) {
let drive_id = get_device_id(&file.to_str().unwrap().to_string());
let plots = drive_id_to_plots
.entry(drive_id)
.or_insert(Vec::new());
let plots = drive_id_to_plots.entry(drive_id).or_insert(Vec::new());

local_capacity += p.meta.nonces as u64;
plots.push(Mutex::new(p));
Expand Down Expand Up @@ -454,10 +452,11 @@ impl Miner {
state.server_target_deadline = mining_info.target_deadline;

let gensig =
pocmath::decode_gensig(&mining_info.generation_signature);
poc_hashing::decode_gensig(&mining_info.generation_signature);
state.generation_signature = mining_info.generation_signature;

let scoop = pocmath::calculate_scoop(mining_info.height, &gensig);
let scoop =
poc_hashing::calculate_scoop(mining_info.height, &gensig);
info!(
"{: <80}",
format!(
Expand Down
43 changes: 43 additions & 0 deletions src/poc_hashing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::shabal256::{shabal256_deadline_fast, shabal256_hash_fast};
use hex;
use std::mem::transmute;
use std::u64;

const SCOOP_SIZE: usize = 64;

pub fn decode_gensig(gensig: &str) -> [u8; 32] {
let mut gensig_bytes = [0; 32];
gensig_bytes[..].clone_from_slice(&hex::decode(gensig).unwrap());
gensig_bytes
}

pub fn calculate_scoop(height: u64, gensig: &[u8; 32]) -> u32 {
let mut data: [u8; 64] = [0; 64];
let height_bytes: [u8; 8] = unsafe { transmute(height.to_be()) };

data[..32].clone_from_slice(gensig);
data[32..40].clone_from_slice(&height_bytes);
data[40] = 0x80;
let data = unsafe { std::mem::transmute::<&[u8; 64], &[u32; 16]>(&data) };

let new_gensig = &shabal256_hash_fast(&[], &data);
(u32::from(new_gensig[30] & 0x0F) << 8) | u32::from(new_gensig[31])
}

pub fn find_best_deadline_rust(
data: &[u8],
number_of_nonces: u64,
gensig: &[u8; 32],
) -> (u64, u64) {
let mut best_deadline = u64::MAX;
let mut best_offset = 0;
for i in 0..number_of_nonces as usize {
let result =
shabal256_deadline_fast(&data[i * SCOOP_SIZE..i * SCOOP_SIZE + SCOOP_SIZE], &gensig);
if result < best_deadline {
best_deadline = result;
best_offset = i;
}
}
(best_deadline, best_offset as u64)
}
20 changes: 0 additions & 20 deletions src/pocmath.rs

This file was deleted.

Loading

0 comments on commit a008de0

Please sign in to comment.