2018-04-24 11:18:24 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Mining service, gets a block to mine, and based on mining configuration
|
|
|
|
//! chooses a version of the cuckoo miner to mine the block and produce a valid
|
|
|
|
//! header with its proof-of-work. Any valid mined blocks are submitted to the
|
|
|
|
//! network.
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use crate::util::RwLock;
|
2018-08-20 01:50:43 +03:00
|
|
|
use chrono::prelude::Utc;
|
2018-04-24 11:18:24 +03:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-10-20 03:13:07 +03:00
|
|
|
use std::sync::Arc;
|
2018-04-24 11:18:24 +03:00
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
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;
|
2018-04-24 11:18:24 +03:00
|
|
|
|
|
|
|
pub struct Miner {
|
|
|
|
config: StratumServerConfig,
|
|
|
|
chain: Arc<chain::Chain>,
|
2018-08-28 00:22:48 +03:00
|
|
|
tx_pool: Arc<RwLock<pool::TransactionPool>>,
|
2018-12-08 02:59:40 +03:00
|
|
|
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
|
2018-04-24 11:18:24 +03:00
|
|
|
stop: Arc<AtomicBool>,
|
|
|
|
|
|
|
|
// Just to hold the port we're on, so this miner can be identified
|
|
|
|
// while watching debug output
|
|
|
|
debug_output_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Miner {
|
|
|
|
/// Creates a new Miner. Needs references to the chain state and its
|
|
|
|
/// storage.
|
|
|
|
pub fn new(
|
|
|
|
config: StratumServerConfig,
|
2018-08-30 17:44:34 +03:00
|
|
|
chain: Arc<chain::Chain>,
|
2018-08-28 00:22:48 +03:00
|
|
|
tx_pool: Arc<RwLock<pool::TransactionPool>>,
|
2018-12-08 02:59:40 +03:00
|
|
|
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
|
2018-04-24 11:18:24 +03:00
|
|
|
stop: Arc<AtomicBool>,
|
|
|
|
) -> Miner {
|
|
|
|
Miner {
|
2018-08-30 17:44:34 +03:00
|
|
|
config,
|
|
|
|
chain,
|
|
|
|
tx_pool,
|
|
|
|
verifier_cache,
|
2018-04-24 11:18:24 +03:00
|
|
|
debug_output_id: String::from("none"),
|
2018-08-30 17:44:34 +03:00
|
|
|
stop,
|
2018-04-24 11:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keeping this optional so setting in a separate function
|
|
|
|
/// instead of in the new function
|
|
|
|
pub fn set_debug_output_id(&mut self, debug_output_id: String) {
|
|
|
|
self.debug_output_id = debug_output_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The inner part of mining loop for the internal miner
|
|
|
|
/// kept around mostly for automated testing purposes
|
2018-09-19 01:12:57 +03:00
|
|
|
fn inner_mining_loop(
|
2018-04-24 11:18:24 +03:00
|
|
|
&self,
|
|
|
|
b: &mut Block,
|
|
|
|
head: &BlockHeader,
|
|
|
|
attempt_time_per_block: u32,
|
|
|
|
latest_hash: &mut Hash,
|
2018-09-19 01:12:57 +03:00
|
|
|
) -> bool {
|
2018-04-24 11:18:24 +03:00
|
|
|
// look for a pow for at most 2 sec on the same block (to give a chance to new
|
|
|
|
// transactions) and as long as the head hasn't changed
|
2018-07-30 11:33:28 +03:00
|
|
|
let deadline = Utc::now().timestamp() + attempt_time_per_block as i64;
|
2018-04-24 11:18:24 +03:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
"(Server ID: {}) Mining Cuckoo{} for max {}s on {} @ {} [{}].",
|
|
|
|
self.debug_output_id,
|
2018-10-16 02:14:23 +03:00
|
|
|
global::min_edge_bits(),
|
2018-04-24 11:18:24 +03:00
|
|
|
attempt_time_per_block,
|
2018-09-11 01:36:57 +03:00
|
|
|
b.header.total_difficulty(),
|
2018-04-24 11:18:24 +03:00
|
|
|
b.header.height,
|
|
|
|
latest_hash
|
|
|
|
);
|
|
|
|
let mut iter_count = 0;
|
|
|
|
|
2018-07-30 11:33:28 +03:00
|
|
|
while head.hash() == *latest_hash && Utc::now().timestamp() < deadline {
|
2018-11-24 23:33:17 +03:00
|
|
|
let mut ctx = global::create_pow_context::<u32>(
|
|
|
|
head.height,
|
|
|
|
global::min_edge_bits(),
|
|
|
|
global::proofsize(),
|
|
|
|
10,
|
2018-12-08 02:59:40 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-09-28 13:53:14 +03:00
|
|
|
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();
|
2018-11-29 01:05:55 +03:00
|
|
|
let proof_diff = b.header.pow.to_difficulty(b.header.height);
|
2018-09-11 01:36:57 +03:00
|
|
|
if proof_diff >= (b.header.total_difficulty() - head.total_difficulty()) {
|
2018-09-19 01:12:57 +03:00
|
|
|
return true;
|
2018-04-24 11:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:36:57 +03:00
|
|
|
b.header.pow.nonce += 1;
|
2018-04-24 11:18:24 +03:00
|
|
|
*latest_hash = self.chain.head().unwrap().last_block_h;
|
|
|
|
iter_count += 1;
|
|
|
|
}
|
|
|
|
|
2018-09-19 01:12:57 +03:00
|
|
|
debug!(
|
|
|
|
"(Server ID: {}) No solution found after {} iterations, continuing...",
|
2018-10-21 23:30:56 +03:00
|
|
|
self.debug_output_id, iter_count
|
2018-09-19 01:12:57 +03:00
|
|
|
);
|
|
|
|
false
|
2018-04-24 11:18:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts the mining loop, building a new block on top of the existing
|
|
|
|
/// chain anytime required and looking for PoW solution.
|
|
|
|
pub fn run_loop(&self, wallet_listener_url: Option<String>) {
|
|
|
|
info!(
|
2018-10-21 23:30:56 +03:00
|
|
|
"(Server ID: {}) Starting test miner loop.",
|
|
|
|
self.debug_output_id
|
2018-04-24 11:18:24 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// iteration, we keep the returned derivation to provide it back when
|
|
|
|
// nothing has changed. We only want to create a new key_id for each new block.
|
|
|
|
let mut key_id = None;
|
|
|
|
|
2018-10-14 15:13:49 +03:00
|
|
|
while !self.stop.load(Ordering::Relaxed) {
|
2018-10-21 23:30:56 +03:00
|
|
|
trace!("in miner loop. key_id: {:?}", key_id);
|
2018-04-24 11:18:24 +03:00
|
|
|
|
|
|
|
// get the latest chain state and build a block on top of it
|
|
|
|
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,
|
2018-08-30 17:44:34 +03:00
|
|
|
self.verifier_cache.clone(),
|
2018-04-24 11:18:24 +03:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
// we found a solution, push our block through the chain processing pipeline
|
2018-09-19 01:12:57 +03:00
|
|
|
if sol {
|
2018-04-24 11:18:24 +03:00
|
|
|
info!(
|
2018-11-01 12:51:32 +03:00
|
|
|
"(Server ID: {}) Found valid proof of work, adding block {} (prev_root {}).",
|
2018-04-24 11:18:24 +03:00
|
|
|
self.debug_output_id,
|
2018-11-01 12:51:32 +03:00
|
|
|
b.hash(),
|
|
|
|
b.header.prev_root,
|
2018-04-24 11:18:24 +03:00
|
|
|
);
|
|
|
|
let res = self.chain.process_block(b, chain::Options::MINE);
|
|
|
|
if let Err(e) = res {
|
|
|
|
error!(
|
|
|
|
"(Server ID: {}) Error validating mined block: {:?}",
|
2018-10-21 23:30:56 +03:00
|
|
|
self.debug_output_id, e
|
2018-04-24 11:18:24 +03:00
|
|
|
);
|
|
|
|
}
|
2018-10-21 23:30:56 +03:00
|
|
|
trace!("resetting key_id in miner to None");
|
2018-04-24 11:18:24 +03:00
|
|
|
key_id = None;
|
|
|
|
} else {
|
|
|
|
debug!(
|
2018-10-21 23:30:56 +03:00
|
|
|
"setting pubkey in miner to pubkey from block_fees - {:?}",
|
|
|
|
block_fees
|
2018-04-24 11:18:24 +03:00
|
|
|
);
|
|
|
|
key_id = block_fees.key_id();
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 15:13:49 +03:00
|
|
|
|
2018-10-21 23:30:56 +03:00
|
|
|
info!("(Server ID: {}) test miner exit.", self.debug_output_id);
|
2018-04-24 11:18:24 +03:00
|
|
|
}
|
|
|
|
}
|