Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 3b8b34d

Browse files
author
bhat
committed
Add mid-state caching.
1 parent ed29d53 commit 3b8b34d

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/hasher.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use duino_miner::error::MinerError;
2+
13
use std::collections::HashMap;
24
use std::sync::Arc;
35

@@ -8,8 +10,8 @@ use sha1::{Digest, Sha1};
810

911
type BlockHash = [u8; 20];
1012

11-
fn to_block_hash(s: &str) -> BlockHash {
12-
<BlockHash>::from_hex(s).unwrap_or_else(|_| panic!("{} is not hex string.", s))
13+
fn to_block_hash(s: &str) -> Result<BlockHash, MinerError> {
14+
<BlockHash>::from_hex(s).map_err(|_| MinerError::MalformedJob(format!("Non hex string: {}", s)))
1315
}
1416

1517
#[derive(Clone)]
@@ -28,29 +30,35 @@ impl Sha1Hasher {
2830
}
2931
}
3032

31-
pub async fn get_hash(&self, last_block_hash: &str, expected_hash: &str, diff: u32) -> u32 {
32-
let last_block_hash = to_block_hash(last_block_hash);
33-
let expected_hash = to_block_hash(expected_hash);
33+
pub async fn get_hash(
34+
&self,
35+
last_block_hash: &str,
36+
expected_hash: &str,
37+
diff: u32,
38+
) -> Result<u32, MinerError> {
39+
let last_block_hash = to_block_hash(last_block_hash)?;
40+
let expected_hash = to_block_hash(expected_hash)?;
3441

3542
let mut hashmap = self.hashmap.lock().await;
3643
if let Some(hashes) = hashmap.get_mut(&last_block_hash) {
3744
// Optimized for lower difficulty, uses AVX.
3845
for (duco_numeric_result, hash) in hashes.iter().enumerate() {
3946
if hash == &expected_hash {
40-
return duco_numeric_result as u32;
47+
return Ok(duco_numeric_result as u32);
4148
}
4249
}
4350

4451
let current_progress = hashes.len() as u32;
4552
if current_progress < diff {
4653
log::info!("Continuing calculation.");
4754

55+
let hasher = self.precompute_sha1(&last_block_hash);
4856
for duco_numeric_result in current_progress..diff {
49-
let hash = self.hash_next_block(&last_block_hash, duco_numeric_result);
57+
let hash = self.next_compute_numeric(hasher.clone(), duco_numeric_result);
5058
hashes.push(hash);
5159

5260
if hash == expected_hash {
53-
return duco_numeric_result as u32;
61+
return Ok(duco_numeric_result);
5462
}
5563
}
5664
}
@@ -68,31 +76,39 @@ impl Sha1Hasher {
6876
}
6977

7078
let mut hashes: Vec<BlockHash> = Vec::with_capacity(diff as usize);
79+
let hasher = self.precompute_sha1(&last_block_hash);
7180
for duco_numeric_result in 0..diff {
72-
let hash = self.hash_next_block(&last_block_hash, duco_numeric_result);
81+
let hash = self.next_compute_numeric(hasher.clone(), duco_numeric_result);
7382
hashes.push(hash);
7483

7584
if hash == expected_hash {
7685
hashmap.insert(last_block_hash, hashes);
7786
stack.push(last_block_hash);
7887

79-
return duco_numeric_result;
88+
return Ok(duco_numeric_result);
8089
}
8190
}
8291

8392
hashmap.insert(last_block_hash, hashes);
8493
stack.push(last_block_hash);
8594

86-
0
95+
Err(MinerError::MalformedJob(
96+
"Job impossible to solve.".to_string(),
97+
))
8798
}
8899

89-
fn hash_next_block(&self, last_block_hash: &BlockHash, duco_numeric_result: u32) -> BlockHash {
100+
fn precompute_sha1(&self, last_block_hash: &BlockHash) -> Sha1 {
90101
let mut hasher = Sha1::new();
91102

92103
let mut encode_slice: [u8; 40] = [0; 40];
93104
hex::encode_to_slice(&last_block_hash, &mut encode_slice).unwrap();
94105

95106
sha1::Digest::update(&mut hasher, &encode_slice);
107+
108+
hasher
109+
}
110+
111+
fn next_compute_numeric(&self, mut hasher: Sha1, duco_numeric_result: u32) -> BlockHash {
96112
sha1::Digest::update(&mut hasher, duco_numeric_result.to_string().as_bytes());
97113
let h = hasher.finalize();
98114

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ async fn start_miner(device: Device, pool: String, hasher: Sha1Hasher) -> Result
161161

162162
let start = SystemTime::now();
163163

164-
let duco_numeric_result = hasher.get_hash(last_block_hash, expected_hash, diff).await;
164+
let duco_numeric_result = hasher
165+
.get_hash(last_block_hash, expected_hash, diff)
166+
.await
167+
.unwrap_or(0);
165168

166169
let end = SystemTime::now();
167170
let duration = end.duration_since(start).unwrap().as_micros();

0 commit comments

Comments
 (0)