mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
Cuckoo-miner better merged into Grin (#80)
* Adding cuckoo-miner into grin as a proper dependency now * Defaulting to using cuckoo-miner * Updates to tests to use cuckoo_miner by default, (using cuckoo16)
This commit is contained in:
parent
40090fcdbc
commit
42376e68bc
17 changed files with 223 additions and 133 deletions
10
Cargo.toml
10
Cargo.toml
|
@ -9,6 +9,7 @@ members = ["api", "chain", "config", "core", "grin", "p2p", "store", "util", "po
|
|||
[dependencies]
|
||||
grin_api = { path = "./api" }
|
||||
grin_wallet = { path = "./wallet" }
|
||||
grin_grin = { path = "./grin" }
|
||||
grin_config = { path = "./config" }
|
||||
secp256k1zkp = { path = "./secp256k1zkp" }
|
||||
|
||||
|
@ -21,13 +22,4 @@ serde_derive = "~1.0.8"
|
|||
serde_json = "~1.0.2"
|
||||
tiny-keccak = "1.1"
|
||||
|
||||
[dependencies.grin_grin]
|
||||
path = "./grin"
|
||||
version = "*"
|
||||
default-features = false
|
||||
#Comment this in to use the cuckoo-miner package
|
||||
#ensure cuckoo-miner is cloned next to the
|
||||
#grin directory
|
||||
#features = ["cuckoo_miner", "use-cuckoo-miner"]
|
||||
|
||||
|
||||
|
|
|
@ -19,3 +19,8 @@ secp256k1zkp = { path = "../secp256k1zkp" }
|
|||
[dev-dependencies]
|
||||
env_logger="^0.3.5"
|
||||
rand = "^0.3"
|
||||
|
||||
#just to instantiate a mining worker during a test
|
||||
#while the miner implementation supports both
|
||||
#cuckoo_miner and the built-in version
|
||||
grin_grin = { path = "../grin" }
|
||||
|
|
|
@ -23,11 +23,14 @@ use core::core::{Block, BlockHeader, Output};
|
|||
use core::core::target::Difficulty;
|
||||
use core::core::hash::Hash;
|
||||
use core::{consensus, genesis, pow};
|
||||
use core::pow::MiningWorker;
|
||||
use grin_store;
|
||||
use pipe;
|
||||
use store;
|
||||
use types::*;
|
||||
|
||||
|
||||
|
||||
/// Helper macro to transform a Result into an Option with None in case
|
||||
/// of error
|
||||
macro_rules! none_err {
|
||||
|
@ -78,7 +81,8 @@ impl Chain {
|
|||
} else {
|
||||
consensus::DEFAULT_SIZESHIFT
|
||||
};
|
||||
pow::pow_size(&mut gen.header, diff, sz as u32).unwrap();
|
||||
let mut internal_miner = pow::cuckoo::Miner::new(consensus::EASINESS, sz as u32);
|
||||
pow::pow_size(&mut internal_miner, &mut gen.header, diff, sz as u32).unwrap();
|
||||
chain_store.save_block(&gen)?;
|
||||
|
||||
// saving a new tip based on genesis
|
||||
|
|
|
@ -19,6 +19,8 @@ extern crate time;
|
|||
extern crate rand;
|
||||
extern crate secp256k1zkp as secp;
|
||||
|
||||
extern crate grin_grin as grin;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use rand::os::OsRng;
|
||||
|
@ -31,6 +33,11 @@ use grin_core::pow;
|
|||
use grin_core::core;
|
||||
use grin_core::consensus;
|
||||
|
||||
use grin::{ServerConfig, MinerConfig};
|
||||
use grin::PluginMiner;
|
||||
|
||||
use grin_core::pow::MiningWorker;
|
||||
|
||||
#[test]
|
||||
fn mine_empty_chain() {
|
||||
env_logger::init();
|
||||
|
@ -41,6 +48,18 @@ fn mine_empty_chain() {
|
|||
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
|
||||
let reward_key = secp::key::SecretKey::new(&secp, &mut rng);
|
||||
|
||||
let server_config = ServerConfig::default();
|
||||
let mut miner_config = grin::MinerConfig{
|
||||
enable_mining: true,
|
||||
burn_reward: true,
|
||||
..Default::default()
|
||||
};
|
||||
miner_config.cuckoo_miner_plugin_dir = Some(String::from("../target/debug/deps"));
|
||||
|
||||
let mut cuckoo_miner = PluginMiner::new(consensus::EASINESS,
|
||||
consensus::TEST_SIZESHIFT as u32 );
|
||||
cuckoo_miner.init(miner_config ,server_config);
|
||||
|
||||
for n in 1..4 {
|
||||
let prev = chain.head_header().unwrap();
|
||||
let mut b = core::Block::new(&prev, vec![], reward_key).unwrap();
|
||||
|
@ -49,7 +68,7 @@ fn mine_empty_chain() {
|
|||
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
|
||||
b.header.difficulty = difficulty.clone();
|
||||
|
||||
pow::pow_size(&mut b.header, difficulty, consensus::TEST_SIZESHIFT as u32).unwrap();
|
||||
pow::pow_size(&mut cuckoo_miner, &mut b.header, difficulty, consensus::TEST_SIZESHIFT as u32).unwrap();
|
||||
chain.process_block(&b, grin_chain::EASY_POW).unwrap();
|
||||
|
||||
// checking our new head
|
||||
|
|
|
@ -198,9 +198,6 @@ fn test_read_config() {
|
|||
burn_reward = false
|
||||
#testing value, optional
|
||||
#slow_down_in_millis = 30
|
||||
#testing value, should really be removed and read from consensus instead, optional
|
||||
#cuckoo_size = 12
|
||||
|
||||
|
||||
"#;
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ pub const DEFAULT_SIZESHIFT: u8 = 30;
|
|||
/// Lower Cuckoo size shift for tests and testnet
|
||||
/// This should be changed to correspond with the
|
||||
/// loaded plugin if using cuckoo-miner
|
||||
pub const TEST_SIZESHIFT: u8 = 12;
|
||||
pub const TEST_SIZESHIFT: u8 = 16;
|
||||
|
||||
/// Default Cuckoo Cycle easiness, high enough to have good likeliness to find
|
||||
/// a solution.
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
//! miner will be much faster in almost every environment.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::HashMap;
|
||||
use std::cmp;
|
||||
|
||||
use crypto::digest::Digest;
|
||||
|
@ -162,7 +163,8 @@ pub struct Miner {
|
|||
impl MiningWorker for Miner {
|
||||
|
||||
/// Creates a new miner
|
||||
fn new(ease: u32, sizeshift: u32) -> Miner {
|
||||
fn new(ease: u32,
|
||||
sizeshift: u32) -> Miner {
|
||||
let size = 1 << sizeshift;
|
||||
let graph = vec![0; size + 1];
|
||||
let easiness = (ease as u64) * (size as u64) / 100;
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
mod siphash;
|
||||
pub mod cuckoo;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use time;
|
||||
|
||||
|
@ -42,7 +43,8 @@ use pow::cuckoo::{Cuckoo, Miner, Error};
|
|||
pub trait MiningWorker {
|
||||
|
||||
//This only sets parameters and does initialisation work now
|
||||
fn new(ease: u32, sizeshift: u32) -> Self;
|
||||
fn new(ease: u32,
|
||||
sizeshift: u32) -> Self;
|
||||
|
||||
//Actually perform a mining attempt on the given input and
|
||||
//return a proof if found
|
||||
|
@ -63,14 +65,15 @@ pub fn verify_size(bh: &BlockHeader, cuckoo_sz: u32) -> bool {
|
|||
|
||||
/// Uses the much easier Cuckoo20 (mostly for
|
||||
/// tests).
|
||||
pub fn pow20(bh: &mut BlockHeader, diff: Difficulty) -> Result<(), Error> {
|
||||
pow_size(bh, diff, 20)
|
||||
pub fn pow20<T: MiningWorker>(miner:&mut T, bh: &mut BlockHeader, diff: Difficulty) -> Result<(), Error> {
|
||||
pow_size(miner, bh, diff, 20)
|
||||
}
|
||||
|
||||
/// Runs a naive single-threaded proof of work computation over the provided
|
||||
/// block, until the required difficulty target is reached. May take a
|
||||
/// while for a low target...
|
||||
pub fn pow_size(bh: &mut BlockHeader, diff: Difficulty, sizeshift: u32) -> Result<(), Error> {
|
||||
/// Runs a proof of work computation over the provided block using the provided Mining Worker,
|
||||
/// until the required difficulty target is reached. May take a while for a low target...
|
||||
|
||||
pub fn pow_size<T: MiningWorker>(miner:&mut T, bh: &mut BlockHeader,
|
||||
diff: Difficulty, sizeshift: u32) -> Result<(), Error> {
|
||||
let start_nonce = bh.nonce;
|
||||
|
||||
// try to find a cuckoo cycle on that header hash
|
||||
|
@ -81,7 +84,8 @@ pub fn pow_size(bh: &mut BlockHeader, diff: Difficulty, sizeshift: u32) -> Resul
|
|||
|
||||
// if we found a cycle (not guaranteed) and the proof hash is higher that the
|
||||
// diff, we're all good
|
||||
if let Ok(proof) = Miner::new(EASINESS, sizeshift).mine(&pow_hash[..]) {
|
||||
|
||||
if let Ok(proof) = miner.mine(&pow_hash[..]) {
|
||||
if proof.to_difficulty() >= diff {
|
||||
bh.pow = proof;
|
||||
return Ok(());
|
||||
|
@ -109,7 +113,8 @@ mod test {
|
|||
fn genesis_pow() {
|
||||
let mut b = genesis::genesis();
|
||||
b.header.nonce = 310;
|
||||
pow_size(&mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), 12).unwrap();
|
||||
let mut internal_miner = cuckoo::Miner::new(EASINESS, 12);
|
||||
pow_size(&mut internal_miner, &mut b.header, Difficulty::from_num(MINIMUM_DIFFICULTY), 12).unwrap();
|
||||
assert!(b.header.nonce != 310);
|
||||
assert!(b.header.pow.to_difficulty() >= Difficulty::from_num(MINIMUM_DIFFICULTY));
|
||||
assert!(verify_size(&b.header, 12));
|
||||
|
|
45
grin.toml
45
grin.toml
|
@ -13,48 +13,81 @@
|
|||
[server]
|
||||
|
||||
#the address on which services will listen, e.g. Transaction Pool
|
||||
|
||||
api_http_addr = "127.0.0.1:13413"
|
||||
|
||||
#the directory, relative to current, in which the grin blockchain
|
||||
#is stored
|
||||
|
||||
db_root = ".grin"
|
||||
|
||||
#How to seed this server, can be None, List or WebStatic
|
||||
|
||||
seeding_type = "None"
|
||||
|
||||
#if seeding_type = List, the list of peers to connect to.
|
||||
#seeds = ["192.168.0.1:8080","192.168.0.2:8080"]
|
||||
|
||||
#Whether to run in test mode, which at the moment affects cuckoo_size
|
||||
#if this is false tries to use a slow cuckoo30 at the moment, not
|
||||
#recommended
|
||||
#Whether to run in test mode. This affects the size of
|
||||
#cuckoo graph.
|
||||
#If this is true, CONSENSUS::TEST_SIZESHIFT is used
|
||||
#If this is false, CONSENSUS::DEFAULT_SIZESHIFT is used
|
||||
|
||||
test_mode = true
|
||||
|
||||
#7 = Bit flags for FULL_NODE, this structure needs to be changed
|
||||
#internally to make it more configurable
|
||||
|
||||
capabilities = [7]
|
||||
|
||||
#The P2P server details (i.e. the server that communicates with other
|
||||
#grin server nodes
|
||||
|
||||
[server.p2p_config]
|
||||
host = "127.0.0.1"
|
||||
port = 13414
|
||||
|
||||
#Mining details. This section is optional. If it's not here, the server
|
||||
#will default to not mining.
|
||||
|
||||
[mining]
|
||||
|
||||
#flag whether mining is enabled
|
||||
|
||||
enable_mining = true
|
||||
|
||||
#Whether to use cuckoo-miner, and related parameters
|
||||
|
||||
use_cuckoo_miner = true
|
||||
|
||||
#If using cuckoo_miner, the directory in which plugins are installed
|
||||
#if not specified, grin will look in the directory /deps relative
|
||||
#to the executable
|
||||
|
||||
#cuckoo_miner_plugin_dir = "target/debug/deps"
|
||||
|
||||
#if using cuckoo_miner, the implementation to use.. currently
|
||||
#just filters for this word in the filenames in the plugin
|
||||
#directory
|
||||
#Plugins currently included are:
|
||||
#"simple" : the basic cuckoo algorithm
|
||||
#"edgetrim" : an algorithm trading speed for a much lower memory footpring
|
||||
#Not included but verified working:
|
||||
#"cuda" a gpu miner - which currently needs to bebuilt and installed
|
||||
#separately from#the cuckoo-miner repository. Instructions found there
|
||||
|
||||
cuckoo_miner_plugin_type = "simple"
|
||||
|
||||
#the list of parameters if you're using "edgetrim"
|
||||
#cuckoo_miner_parameter_list = {NUM_THREADS=4, NUM_TRIMS=7}
|
||||
|
||||
#the wallet reciever to which coinbase rewards will be sent
|
||||
|
||||
wallet_receiver_url = "http://127.0.0.1:13415"
|
||||
|
||||
#whether to ignore the reward (mostly for testing)
|
||||
|
||||
burn_reward = true
|
||||
|
||||
#testing value, optional
|
||||
#slow_down_in_millis = 30
|
||||
|
||||
#testing value, should really be removed and read from consensus instead, optional
|
||||
#cuckoo_size = 12
|
|
@ -4,10 +4,6 @@ version = "0.1.0"
|
|||
authors = ["Ignotus Peverell <igno.peverell@protonmail.com>"]
|
||||
workspace = ".."
|
||||
|
||||
[features]
|
||||
# Compliation flag whether to include the experimental cuckoo-miner crate
|
||||
use-cuckoo-miner = []
|
||||
|
||||
[dependencies]
|
||||
grin_api = { path = "../api" }
|
||||
grin_chain = { path = "../chain" }
|
||||
|
@ -19,7 +15,7 @@ grin_util = { path = "../util" }
|
|||
grin_wallet = { path = "../wallet" }
|
||||
secp256k1zkp = { path = "../secp256k1zkp" }
|
||||
|
||||
#cuckoo_miner = { version = "*", optional=true, path = "../../cuckoo-miner"}
|
||||
cuckoo_miner = { git = "https://github.com/mimblewimble/cuckoo-miner", tag="grin_integration"}
|
||||
|
||||
env_logger="^0.3.5"
|
||||
futures = "^0.1.9"
|
||||
|
|
|
@ -46,18 +46,16 @@ extern crate grin_util as util;
|
|||
extern crate grin_wallet as wallet;
|
||||
extern crate secp256k1zkp as secp;
|
||||
|
||||
#[cfg(feature = "use-cuckoo-miner")]
|
||||
extern crate cuckoo_miner;
|
||||
|
||||
mod adapters;
|
||||
mod miner;
|
||||
#[cfg(feature = "use-cuckoo-miner")]
|
||||
mod plugin;
|
||||
mod server;
|
||||
mod seed;
|
||||
mod sync;
|
||||
mod types;
|
||||
|
||||
|
||||
pub use server::{Server};
|
||||
pub use types::{ServerConfig, MinerConfig, Seeding, ServerStats};
|
||||
pub use plugin::PluginMiner;
|
||||
|
|
|
@ -78,7 +78,7 @@ impl Miner {
|
|||
|
||||
/// 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<T: MiningWorker>(&self, mut miner:T) {
|
||||
pub fn run_loop<T: MiningWorker>(&self, mut miner:T, cuckoo_size:u32) {
|
||||
|
||||
info!("(Server ID: {}) Starting miner loop.", self.debug_output_id);
|
||||
let mut coinbase = self.get_coinbase();
|
||||
|
@ -95,7 +95,7 @@ impl Miner {
|
|||
let mut sol = None;
|
||||
debug!("(Server ID: {}) Mining at Cuckoo{} for at most 2 secs on block {} at difficulty {}.",
|
||||
self.debug_output_id,
|
||||
self.config.cuckoo_size.unwrap(),
|
||||
cuckoo_size,
|
||||
latest_hash,
|
||||
b.header.difficulty);
|
||||
let mut iter_count = 0;
|
||||
|
@ -134,7 +134,7 @@ impl Miner {
|
|||
info!("(Server ID: {}) Found valid proof of work, adding block {}.",
|
||||
self.debug_output_id, b.hash());
|
||||
b.header.pow = proof;
|
||||
let opts = if self.config.cuckoo_size.unwrap() < consensus::DEFAULT_SIZESHIFT as u32 {
|
||||
let opts = if cuckoo_size < consensus::DEFAULT_SIZESHIFT as u32 {
|
||||
chain::EASY_POW
|
||||
} else {
|
||||
chain::NONE
|
||||
|
|
|
@ -22,9 +22,12 @@ use std::env;
|
|||
use core::pow::cuckoo;
|
||||
use core::pow::cuckoo::Error;
|
||||
use core::pow::MiningWorker;
|
||||
use core::consensus::TEST_SIZESHIFT;
|
||||
use core::consensus::{TEST_SIZESHIFT, DEFAULT_SIZESHIFT};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use core::core::Proof;
|
||||
use types::{MinerConfig, ServerConfig};
|
||||
|
||||
use cuckoo_miner::{
|
||||
CuckooMiner,
|
||||
|
@ -35,10 +38,84 @@ use cuckoo_miner::{
|
|||
CuckooPluginCapabilities};
|
||||
|
||||
pub struct PluginMiner {
|
||||
miner:CuckooMiner,
|
||||
miner:Option<CuckooMiner>,
|
||||
last_solution: CuckooMinerSolution,
|
||||
}
|
||||
|
||||
impl Default for PluginMiner {
|
||||
fn default() -> PluginMiner {
|
||||
PluginMiner {
|
||||
miner: None,
|
||||
last_solution: CuckooMinerSolution::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PluginMiner {
|
||||
pub fn init(&mut self, miner_config: MinerConfig, server_config: ServerConfig){
|
||||
//Get directory of executable
|
||||
let mut exe_path=env::current_exe().unwrap();
|
||||
exe_path.pop();
|
||||
let exe_path=exe_path.to_str().unwrap();
|
||||
|
||||
let plugin_install_path = match miner_config.cuckoo_miner_plugin_dir {
|
||||
Some(s) => s,
|
||||
None => String::from(format!("{}/deps", exe_path))
|
||||
};
|
||||
|
||||
let plugin_impl_filter = match miner_config.cuckoo_miner_plugin_type {
|
||||
Some(s) => s,
|
||||
None => String::from("simple")
|
||||
};
|
||||
|
||||
//First, load and query the plugins in the given directory
|
||||
//These should all be stored in 'deps' at the moment relative
|
||||
//to the executable path, though they should appear somewhere else
|
||||
//when packaging is more//thought out
|
||||
|
||||
let mut plugin_manager = CuckooPluginManager::new().unwrap();
|
||||
let result=plugin_manager.load_plugin_dir(plugin_install_path);
|
||||
|
||||
if let Err(e) = result {
|
||||
error!("Unable to load cuckoo-miner plugin directory, either from configuration or [exe_path]/deps.");
|
||||
panic!("Unable to load plugin directory... Please check configuration values");
|
||||
}
|
||||
|
||||
//The miner implementation needs to match what's in the consensus sizeshift value
|
||||
//
|
||||
let sz = if server_config.test_mode {
|
||||
TEST_SIZESHIFT
|
||||
} else {
|
||||
DEFAULT_SIZESHIFT
|
||||
};
|
||||
|
||||
//So this is built dynamically based on the plugin implementation
|
||||
//type and the consensus sizeshift
|
||||
let filter = format!("{}_{}", plugin_impl_filter, sz);
|
||||
|
||||
let caps = plugin_manager.get_available_plugins(&filter).unwrap();
|
||||
//insert it into the miner configuration being created below
|
||||
|
||||
|
||||
let mut config = CuckooMinerConfig::new();
|
||||
|
||||
info!("Mining using plugin: {}", caps[0].full_path.clone());
|
||||
config.plugin_full_path = caps[0].full_path.clone();
|
||||
if let Some(l) = miner_config.cuckoo_miner_parameter_list {
|
||||
config.parameter_list = l.clone();
|
||||
}
|
||||
|
||||
//this will load the associated plugin
|
||||
let result=CuckooMiner::new(config);
|
||||
if let Err(e) = result {
|
||||
error!("Error initializing mining plugin: {:?}", e);
|
||||
error!("Accepted values are: {:?}", caps[0].parameters);
|
||||
panic!("Unable to init mining plugin.");
|
||||
}
|
||||
self.miner=Some(result.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
impl MiningWorker for PluginMiner {
|
||||
|
||||
/// This will initialise a plugin according to what's currently
|
||||
|
@ -46,55 +123,16 @@ impl MiningWorker for PluginMiner {
|
|||
/// version of the miner for now, though this should become
|
||||
/// configurable somehow
|
||||
|
||||
fn new(ease: u32, sizeshift: u32) -> Self {
|
||||
|
||||
//Get directory of executable
|
||||
let mut exe_path=env::current_exe().unwrap();
|
||||
exe_path.pop();
|
||||
let exe_path=exe_path.to_str().unwrap();
|
||||
|
||||
//First, load and query the plugins in the given directory
|
||||
//These should all be stored in 'deps' at the moment relative, though
|
||||
//to the executable path, though they should appear somewhere else
|
||||
//when packaging is more//thought out
|
||||
|
||||
let mut plugin_manager = CuckooPluginManager::new().unwrap();
|
||||
let result=plugin_manager.load_plugin_dir(String::from(format!("{}/deps", exe_path))).expect("");
|
||||
|
||||
//Get a list of installed plugins and capabilities.. filtering for the one we want
|
||||
//Just use the baseline edgetrim (i.e. cuckoo_miner.cpp) for now
|
||||
//You need to change the value TEST_SIZESHIFT in consensus.rs for now to modify this,
|
||||
//so that blocks mined in this version will validate
|
||||
|
||||
let filter = format!("simple_{}", TEST_SIZESHIFT);
|
||||
|
||||
let caps = plugin_manager.get_available_plugins(&filter).unwrap();
|
||||
//insert it into the miner configuration being created below
|
||||
|
||||
let mut config = CuckooMinerConfig::new();
|
||||
|
||||
info!("Mining using plugin: {}", caps[0].full_path.clone());
|
||||
config.plugin_full_path = caps[0].full_path.clone();
|
||||
//Set threads, should read this from a configuration file
|
||||
//somewhere or query the system to determine a default
|
||||
config.num_threads=4;
|
||||
//let plugin decide number of trims
|
||||
config.num_trims=0;
|
||||
|
||||
//this will load the associated plugin
|
||||
let miner = CuckooMiner::new(config).expect("");
|
||||
|
||||
PluginMiner {
|
||||
miner: miner,
|
||||
last_solution: CuckooMinerSolution::new(),
|
||||
}
|
||||
fn new(ease: u32,
|
||||
sizeshift: u32) -> Self {
|
||||
PluginMiner::default()
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
let result = self.miner.mine(&header, &mut self.last_solution).unwrap();
|
||||
let result = self.miner.as_mut().unwrap().mine(&header, &mut self.last_solution).unwrap();
|
||||
if result == true {
|
||||
return Ok(Proof(self.last_solution.solution_nonces));
|
||||
}
|
||||
|
|
|
@ -41,7 +41,6 @@ use store;
|
|||
use sync;
|
||||
use types::*;
|
||||
|
||||
#[cfg(feature = "use-cuckoo-miner")]
|
||||
use plugin::PluginMiner;
|
||||
|
||||
/// Grin server holding internal structures.
|
||||
|
@ -58,8 +57,7 @@ pub struct Server {
|
|||
|
||||
impl Server {
|
||||
/// Instantiates and starts a new server.
|
||||
pub fn start(mut config: ServerConfig) -> Result<Server, Error> {
|
||||
check_config(&mut config);
|
||||
pub fn start(config: ServerConfig) -> Result<Server, Error> {
|
||||
let mut evtlp = reactor::Core::new().unwrap();
|
||||
|
||||
let mut mining_config = config.mining_config.clone();
|
||||
|
@ -82,7 +80,6 @@ impl Server {
|
|||
|
||||
/// Instantiates a new server associated with the provided future reactor.
|
||||
pub fn future(mut config: ServerConfig, evt_handle: &reactor::Handle) -> Result<Server, Error> {
|
||||
check_config(&mut config);
|
||||
|
||||
let pool_adapter = Arc::new(PoolToChainAdapter::new());
|
||||
let tx_pool = Arc::new(RwLock::new(pool::TransactionPool::new(pool_adapter.clone())));
|
||||
|
@ -145,26 +142,27 @@ impl Server {
|
|||
self.p2p.peer_count()
|
||||
}
|
||||
|
||||
/// Start mining for blocks on a separate thread. Relies on a toy miner,
|
||||
/// mostly for testing.
|
||||
#[cfg(not(feature = "use-cuckoo-miner"))]
|
||||
/// Start mining for blocks on a separate thread. Uses toy miner by default,
|
||||
/// mostly for testing, but can also load a plugin from cuckoo-miner
|
||||
pub fn start_miner(&self, config: MinerConfig) {
|
||||
let cuckoo_size = match self.config.test_mode {
|
||||
true => consensus::TEST_SIZESHIFT as u32,
|
||||
false => consensus::DEFAULT_SIZESHIFT as u32,
|
||||
};
|
||||
let mut miner = miner::Miner::new(config.clone(), self.chain.clone(), self.tx_pool.clone());
|
||||
miner.set_debug_output_id(format!("Port {}",self.config.p2p_config.unwrap().port));
|
||||
let server_config = self.config.clone();
|
||||
thread::spawn(move || {
|
||||
let test_cuckoo_miner = cuckoo::Miner::new(consensus::EASINESS, config.cuckoo_size.unwrap().clone());
|
||||
miner.run_loop(test_cuckoo_miner);
|
||||
});
|
||||
}
|
||||
|
||||
/// And a version we only get if we're using the cuckoo miner crate
|
||||
#[cfg(feature = "use-cuckoo-miner")]
|
||||
pub fn start_miner(&self, config: MinerConfig) {
|
||||
let mut miner = miner::Miner::new(config.clone(), self.chain.clone(), self.tx_pool.clone());
|
||||
miner.set_debug_output_id(format!("Port {}",self.config.p2p_config.port));
|
||||
thread::spawn(move || {
|
||||
let test_cuckoo_miner = PluginMiner::new(consensus::EASINESS, config.cuckoo_size.unwrap().clone());
|
||||
miner.run_loop(test_cuckoo_miner);
|
||||
if config.use_cuckoo_miner {
|
||||
let mut cuckoo_miner = PluginMiner::new(consensus::EASINESS,
|
||||
cuckoo_size);
|
||||
cuckoo_miner.init(config.clone(),server_config);
|
||||
miner.run_loop(cuckoo_miner, cuckoo_size);
|
||||
} else {
|
||||
let test_internal_miner = cuckoo::Miner::new(consensus::EASINESS, cuckoo_size);
|
||||
miner.run_loop(test_internal_miner, cuckoo_size);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -183,12 +181,3 @@ impl Server {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn check_config(config: &mut ServerConfig) {
|
||||
// applying test/normal config
|
||||
config.mining_config.as_mut().unwrap().cuckoo_size = if config.test_mode {
|
||||
Some(consensus::TEST_SIZESHIFT as u32)
|
||||
} else {
|
||||
Some(consensus::DEFAULT_SIZESHIFT as u32)
|
||||
};
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
use std::convert::From;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use api;
|
||||
use chain;
|
||||
|
@ -103,6 +104,19 @@ pub struct MinerConfig {
|
|||
/// Whether to start the miner with the server
|
||||
pub enable_mining: bool,
|
||||
|
||||
/// Whether to use the cuckoo-miner crate and plugin for mining
|
||||
pub use_cuckoo_miner: bool,
|
||||
|
||||
/// The location in which cuckoo miner plugins are stored
|
||||
pub cuckoo_miner_plugin_dir: Option<String>,
|
||||
|
||||
/// The type of plugin to use (ends up filtering the filename)
|
||||
pub cuckoo_miner_plugin_type: Option<String>,
|
||||
|
||||
/// Cuckoo-miner parameters... these vary according
|
||||
/// to the plugin being loaded
|
||||
pub cuckoo_miner_parameter_list: Option<HashMap<String, u32>>,
|
||||
|
||||
/// Base address to the HTTP wallet receiver
|
||||
pub wallet_receiver_url: String,
|
||||
|
||||
|
@ -114,10 +128,6 @@ pub struct MinerConfig {
|
|||
/// mining loop by adding a sleep to the thread
|
||||
pub slow_down_in_millis: Option<u64>,
|
||||
|
||||
/// Size of Cuckoo Cycle to mine on
|
||||
pub cuckoo_size: Option<u32>,
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
|
@ -139,10 +149,13 @@ impl Default for MinerConfig {
|
|||
fn default() -> MinerConfig {
|
||||
MinerConfig {
|
||||
enable_mining: false,
|
||||
use_cuckoo_miner: false,
|
||||
cuckoo_miner_plugin_dir: None,
|
||||
cuckoo_miner_plugin_type: None,
|
||||
cuckoo_miner_parameter_list: None,
|
||||
wallet_receiver_url: "http://localhost:13416".to_string(),
|
||||
burn_reward: false,
|
||||
slow_down_in_millis: Some(0),
|
||||
cuckoo_size: Some(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,9 +107,6 @@ pub struct LocalServerContainerConfig {
|
|||
//Whether to burn mining rewards
|
||||
pub burn_mining_rewards: bool,
|
||||
|
||||
//size of cuckoo graph for mining
|
||||
pub cuckoo_size: u32,
|
||||
|
||||
//full address to send coinbase rewards to
|
||||
pub coinbase_wallet_address: String,
|
||||
|
||||
|
@ -133,7 +130,6 @@ impl Default for LocalServerContainerConfig {
|
|||
is_seeding: false,
|
||||
start_miner: false,
|
||||
start_wallet: false,
|
||||
cuckoo_size: consensus::TEST_SIZESHIFT as u32,
|
||||
burn_mining_rewards: false,
|
||||
coinbase_wallet_address: String::from(""),
|
||||
wallet_validating_node_url: String::from(""),
|
||||
|
@ -232,12 +228,14 @@ impl LocalServerContainer {
|
|||
let mut miner_config = grin::MinerConfig {
|
||||
enable_mining: self.config.start_miner,
|
||||
burn_reward: self.config.burn_mining_rewards,
|
||||
cuckoo_size: Some(self.config.cuckoo_size),
|
||||
use_cuckoo_miner: true,
|
||||
cuckoo_miner_plugin_dir: Some(String::from("../target/debug/deps")),
|
||||
cuckoo_miner_plugin_type: Some(String::from("simple")),
|
||||
wallet_receiver_url : self.config.coinbase_wallet_address.clone(),
|
||||
slow_down_in_millis: Some(self.config.miner_slowdown_in_millis.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
||||
if self.config.start_miner == true {
|
||||
println!("starting Miner on port {}", self.config.p2p_server_port);
|
||||
s.start_miner(miner_config);
|
||||
|
|
|
@ -153,9 +153,6 @@ fn simulate_parallel_mining(){
|
|||
server_config.start_wallet = true;
|
||||
server_config.is_seeding = true;
|
||||
|
||||
//This is the default value, can play with this here for convenience
|
||||
server_config.cuckoo_size=consensus::TEST_SIZESHIFT as u32;
|
||||
|
||||
pool.create_server(&mut server_config);
|
||||
|
||||
//point next servers at first seed
|
||||
|
@ -199,7 +196,9 @@ fn simulate_block_propagation() {
|
|||
let miner_config = grin::MinerConfig{
|
||||
enable_mining: true,
|
||||
burn_reward: true,
|
||||
cuckoo_size: Some(consensus::TEST_SIZESHIFT as u32),
|
||||
use_cuckoo_miner: true,
|
||||
cuckoo_miner_plugin_dir: Some(String::from("../target/debug/deps")),
|
||||
cuckoo_miner_plugin_type: Some(String::from("simple")),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -256,7 +255,9 @@ fn simulate_full_sync() {
|
|||
let miner_config = grin::MinerConfig{
|
||||
enable_mining: true,
|
||||
burn_reward: true,
|
||||
cuckoo_size: Some(consensus::TEST_SIZESHIFT as u32),
|
||||
use_cuckoo_miner: true,
|
||||
cuckoo_miner_plugin_dir: Some(String::from("../target/debug/deps")),
|
||||
cuckoo_miner_plugin_type: Some(String::from("simple")),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -274,7 +275,7 @@ fn simulate_full_sync() {
|
|||
|
||||
// mine a few blocks on server 1
|
||||
servers[0].start_miner(miner_config);
|
||||
thread::sleep(time::Duration::from_secs(15));
|
||||
thread::sleep(time::Duration::from_secs(45));
|
||||
|
||||
// connect 1 and 2
|
||||
let addr = format!("{}:{}", "127.0.0.1", 11001);
|
||||
|
|
Loading…
Reference in a new issue