use crate::util::RwLock;
use chrono::prelude::Utc;
use std::sync::Arc;
use crate::chain;
use crate::common::types::StratumServerConfig;
use crate::core::core::hash::{Hash, Hashed};
use crate::core::core::verifier_cache::VerifierCache;
use crate::core::core::{Block, BlockHeader};
use crate::core::global;
use crate::mining::mine_block;
use crate::pool;
use crate::util::StopState;
pub struct Miner {
config: StratumServerConfig,
chain: Arc<chain::Chain>,
tx_pool: Arc<RwLock<pool::TransactionPool>>,
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
stop_state: Arc<StopState>,
debug_output_id: String,
}
impl Miner {
pub fn new(
config: StratumServerConfig,
chain: Arc<chain::Chain>,
tx_pool: Arc<RwLock<pool::TransactionPool>>,
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
stop_state: Arc<StopState>,
) -> Miner {
Miner {
config,
chain,
tx_pool,
verifier_cache,
debug_output_id: String::from("none"),
stop_state,
}
}
pub fn set_debug_output_id(&mut self, debug_output_id: String) {
self.debug_output_id = debug_output_id;
}
fn inner_mining_loop(
&self,
b: &mut Block,
head: &BlockHeader,
attempt_time_per_block: u32,
latest_hash: &mut Hash,
) -> bool {
let deadline = Utc::now().timestamp() + attempt_time_per_block as i64;
debug!(
"(Server ID: {}) Mining Cuckoo{} for max {}s on {} @ {} [{}].",
self.debug_output_id,
global::min_edge_bits(),
attempt_time_per_block,
b.header.total_difficulty(),
b.header.height,
latest_hash
);
let mut iter_count = 0;
while head.hash() == *latest_hash && Utc::now().timestamp() < deadline {
let mut ctx = global::create_pow_context::<u32>(
head.height,
global::min_edge_bits(),
global::proofsize(),
10,
)
.unwrap();
ctx.set_header_nonce(b.header.pre_pow(), None, true)
.unwrap();
if let Ok(proofs) = ctx.find_cycles() {
b.header.pow.proof = proofs[0].clone();
let proof_diff = b.header.pow.to_difficulty(b.header.height);
if proof_diff >= (b.header.total_difficulty() - head.total_difficulty()) {
return true;
}
}
b.header.pow.nonce += 1;
*latest_hash = self.chain.head().unwrap().last_block_h;
iter_count += 1;
}
debug!(
"(Server ID: {}) No solution found after {} iterations, continuing...",
self.debug_output_id, iter_count
);
false
}
pub fn run_loop(&self, wallet_listener_url: Option<String>) {
info!(
"(Server ID: {}) Starting test miner loop.",
self.debug_output_id
);
let mut key_id = None;
loop {
if self.stop_state.is_stopped() {
break;
}
trace!("in miner loop. key_id: {:?}", key_id);
let head = self.chain.head_header().unwrap();
let mut latest_hash = self.chain.head().unwrap().last_block_h;
let (mut b, block_fees) = mine_block::get_block(
&self.chain,
&self.tx_pool,
self.verifier_cache.clone(),
key_id.clone(),
wallet_listener_url.clone(),
);
let sol = self.inner_mining_loop(
&mut b,
&head,
self.config.attempt_time_per_block,
&mut latest_hash,
);
if sol {
info!(
"(Server ID: {}) Found valid proof of work, adding block {} (prev_root {}).",
self.debug_output_id,
b.hash(),
b.header.prev_root,
);
let res = self.chain.process_block(b, chain::Options::MINE);
if let Err(e) = res {
error!(
"(Server ID: {}) Error validating mined block: {:?}",
self.debug_output_id, e
);
}
trace!("resetting key_id in miner to None");
key_id = None;
} else {
debug!(
"setting pubkey in miner to pubkey from block_fees - {:?}",
block_fees
);
key_id = block_fees.key_id();
}
}
info!("(Server ID: {}) test miner exit.", self.debug_output_id);
}
}