2017-07-11 20:11:03 +03:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
//! Plugin wrapper for cuckoo miner, implementing common traits
|
|
|
|
//! with the existing embedded miner. This is all included conditionally
|
|
|
|
//! for compatibility reasons with those who aren't interested in playing
|
|
|
|
//! with cuckoo-miner at present
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
|
2017-08-22 21:23:54 +03:00
|
|
|
use cuckoo;
|
|
|
|
use cuckoo::Error;
|
|
|
|
use MiningWorker;
|
2017-08-10 03:54:10 +03:00
|
|
|
use core::global;
|
2017-07-11 20:11:03 +03:00
|
|
|
|
|
|
|
use core::core::Proof;
|
2017-08-22 21:23:54 +03:00
|
|
|
use types::MinerConfig;
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-07-19 19:40:57 +03:00
|
|
|
use std::sync::{Mutex};
|
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
use cuckoo_miner::{
|
|
|
|
CuckooMiner,
|
|
|
|
CuckooPluginManager,
|
|
|
|
CuckooMinerConfig,
|
2017-08-10 03:54:10 +03:00
|
|
|
CuckooMinerSolution};
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-07-19 19:40:57 +03:00
|
|
|
//For now, we're just going to keep a static reference around to the loaded config
|
|
|
|
//And not allow querying the plugin directory twice once a plugin has been selected
|
|
|
|
//This is to keep compatibility with multi-threaded testing, so that spawned
|
|
|
|
//testing threads don't try to load/unload the library while another thread is
|
|
|
|
//using it.
|
|
|
|
lazy_static!{
|
|
|
|
static ref LOADED_CONFIG: Mutex<Option<CuckooMinerConfig>> = Mutex::new(None);
|
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// plugin miner
|
2017-07-11 20:11:03 +03:00
|
|
|
pub struct PluginMiner {
|
2017-08-10 03:54:10 +03:00
|
|
|
/// the miner
|
2017-08-03 19:57:55 +03:00
|
|
|
pub miner:Option<CuckooMiner>,
|
2017-07-11 20:11:03 +03:00
|
|
|
last_solution: CuckooMinerSolution,
|
2017-08-03 19:57:55 +03:00
|
|
|
config: CuckooMinerConfig,
|
2017-07-11 20:11:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-18 23:57:09 +03:00
|
|
|
impl Default for PluginMiner {
|
|
|
|
fn default() -> PluginMiner {
|
|
|
|
PluginMiner {
|
|
|
|
miner: None,
|
2017-08-03 19:57:55 +03:00
|
|
|
config: CuckooMinerConfig::new(),
|
2017-07-18 23:57:09 +03:00
|
|
|
last_solution: CuckooMinerSolution::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-07-18 23:57:09 +03:00
|
|
|
impl PluginMiner {
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Init the plugin miner
|
2017-08-22 21:23:54 +03:00
|
|
|
pub fn init(&mut self, miner_config: MinerConfig){
|
2017-07-18 23:57:09 +03:00
|
|
|
//Get directory of executable
|
2017-07-11 20:11:03 +03:00
|
|
|
let mut exe_path=env::current_exe().unwrap();
|
|
|
|
exe_path.pop();
|
|
|
|
let exe_path=exe_path.to_str().unwrap();
|
|
|
|
|
2017-08-22 21:23:54 +03:00
|
|
|
//println!("Plugin dir: {}", miner_config.clone().cuckoo_miner_plugin_dir.unwrap());
|
|
|
|
|
2017-07-18 23:57:09 +03:00
|
|
|
let plugin_install_path = match miner_config.cuckoo_miner_plugin_dir {
|
|
|
|
Some(s) => s,
|
2017-08-25 00:43:11 +03:00
|
|
|
None => String::from(format!("{}/plugins", exe_path))
|
2017-07-18 23:57:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let plugin_impl_filter = match miner_config.cuckoo_miner_plugin_type {
|
|
|
|
Some(s) => s,
|
|
|
|
None => String::from("simple")
|
|
|
|
};
|
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
//First, load and query the plugins in the given directory
|
2017-08-25 00:43:11 +03:00
|
|
|
//These should all be stored in 'plugins' at the moment relative
|
2017-08-10 03:54:10 +03:00
|
|
|
//to the executable path, though they should appear somewhere else
|
|
|
|
//when packaging is more//thought out
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-07-19 19:40:57 +03:00
|
|
|
let mut loaded_config_ref = LOADED_CONFIG.lock().unwrap();
|
2017-08-22 21:23:54 +03:00
|
|
|
|
2017-07-19 19:40:57 +03:00
|
|
|
//Load from here instead
|
|
|
|
if let Some(ref c) = *loaded_config_ref {
|
|
|
|
debug!("Not re-loading plugin or directory.");
|
|
|
|
//this will load the associated plugin
|
|
|
|
let result=CuckooMiner::new(c.clone());
|
|
|
|
self.miner=Some(result.unwrap());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
let mut plugin_manager = CuckooPluginManager::new().unwrap();
|
2017-07-18 23:57:09 +03:00
|
|
|
let result=plugin_manager.load_plugin_dir(plugin_install_path);
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
if let Err(_) = result {
|
2017-08-25 00:43:11 +03:00
|
|
|
error!("Unable to load cuckoo-miner plugin directory, either from configuration or [exe_path]/plugins.");
|
2017-07-18 23:57:09 +03:00
|
|
|
panic!("Unable to load plugin directory... Please check configuration values");
|
|
|
|
}
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-08-09 19:40:23 +03:00
|
|
|
let sz = global::sizeshift();
|
2017-07-18 23:57:09 +03:00
|
|
|
|
|
|
|
//So this is built dynamically based on the plugin implementation
|
|
|
|
//type and the consensus sizeshift
|
|
|
|
let filter = format!("{}_{}", plugin_impl_filter, sz);
|
2017-07-11 20:11:03 +03:00
|
|
|
|
|
|
|
let caps = plugin_manager.get_available_plugins(&filter).unwrap();
|
|
|
|
//insert it into the miner configuration being created below
|
2017-07-18 23:57:09 +03:00
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
let mut config = CuckooMinerConfig::new();
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
info!("Mining using plugin: {}", caps[0].full_path.clone());
|
|
|
|
config.plugin_full_path = caps[0].full_path.clone();
|
2017-07-18 23:57:09 +03:00
|
|
|
if let Some(l) = miner_config.cuckoo_miner_parameter_list {
|
|
|
|
config.parameter_list = l.clone();
|
|
|
|
}
|
2017-07-11 20:11:03 +03:00
|
|
|
|
2017-07-19 19:40:57 +03:00
|
|
|
//Store this config now, because we just want one instance
|
|
|
|
//of the plugin lib per invocation now
|
|
|
|
*loaded_config_ref=Some(config.clone());
|
|
|
|
|
2017-07-11 20:11:03 +03:00
|
|
|
//this will load the associated plugin
|
2017-08-03 19:57:55 +03:00
|
|
|
let result=CuckooMiner::new(config.clone());
|
2017-07-18 23:57:09 +03:00
|
|
|
if let Err(e) = result {
|
|
|
|
error!("Error initializing mining plugin: {:?}", e);
|
|
|
|
error!("Accepted values are: {:?}", caps[0].parameters);
|
|
|
|
panic!("Unable to init mining plugin.");
|
2017-07-11 20:11:03 +03:00
|
|
|
}
|
2017-07-19 19:40:57 +03:00
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
self.config=config.clone();
|
2017-07-18 23:57:09 +03:00
|
|
|
self.miner=Some(result.unwrap());
|
2017-08-03 19:57:55 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
/// Get the miner
|
2017-08-03 19:57:55 +03:00
|
|
|
pub fn get_consumable(&mut self)->CuckooMiner{
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-08-03 19:57:55 +03:00
|
|
|
//this will load the associated plugin
|
|
|
|
let result=CuckooMiner::new(self.config.clone());
|
|
|
|
if let Err(e) = result {
|
|
|
|
error!("Error initializing mining plugin: {:?}", e);
|
|
|
|
panic!("Unable to init mining plugin.");
|
|
|
|
}
|
|
|
|
result.unwrap()
|
|
|
|
}
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-07-18 23:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MiningWorker for PluginMiner {
|
|
|
|
|
|
|
|
/// This will initialise a plugin according to what's currently
|
|
|
|
/// included in CONSENSUS::TEST_SIZESHIFT, just using the edgetrim
|
|
|
|
/// version of the miner for now, though this should become
|
|
|
|
/// configurable somehow
|
|
|
|
|
2017-08-10 03:54:10 +03:00
|
|
|
fn new(_ease: u32,
|
|
|
|
_sizeshift: u32,
|
|
|
|
_proof_size: usize) -> Self {
|
2017-07-18 23:57:09 +03:00
|
|
|
PluginMiner::default()
|
2017-07-11 20:11:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// And simply calls the mine function of the loaded plugin
|
|
|
|
/// returning whether a solution was found and the solution itself
|
|
|
|
|
|
|
|
fn mine(&mut self, header: &[u8]) -> Result<Proof, cuckoo::Error> {
|
2017-07-18 23:57:09 +03:00
|
|
|
let result = self.miner.as_mut().unwrap().mine(&header, &mut self.last_solution).unwrap();
|
2017-07-11 20:11:03 +03:00
|
|
|
if result == true {
|
2017-08-09 19:40:23 +03:00
|
|
|
return Ok(Proof::new(self.last_solution.solution_nonces.to_vec()));
|
2017-07-11 20:11:03 +03:00
|
|
|
}
|
|
|
|
Err(Error::NoSolution)
|
|
|
|
}
|
|
|
|
}
|