Skip to content

Commit

Permalink
fix(miner): uncles in solo mining
Browse files Browse the repository at this point in the history
BREAKING CHANGE: miner config poll_interval unit: second -> millisecond
  • Loading branch information
zhangsoledad committed Mar 1, 2019
1 parent 5518401 commit abe7a8b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
46 changes: 28 additions & 18 deletions miner/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ impl Client {
}
}

pub fn run(&self) {
self.poll_block_template();
}

pub fn submit_block(
fn send_submit_block_request(
&self,
work_id: &str,
block: &Block,
Expand All @@ -138,24 +134,38 @@ impl Client {
self.rpc.request(method, params)
}

fn poll_block_template(&self) {
pub fn submit_block(&self, work_id: &str, block: &Block) {
let future = self.send_submit_block_request(work_id, block);
if self.config.block_on_submit {
let _ = future.wait();
}
}

pub fn poll_block_template(&self) {
loop {
debug!(target: "miner", "poll block template...");
match self.get_block_template().wait() {
Ok(new) => {
let work = self.current_work.upgradable_read();
if work.as_ref().map_or(true, |old| old.work_id != new.work_id) {
let mut write_guard = RwLockUpgradableReadGuard::upgrade(work);
*write_guard = Some(new);
let _ = self.new_work.send(());
}
}
Err(e) => {
error!(target: "miner", "rpc call get_block_template error: {:?}", e);
self.try_update_block_template();
thread::sleep(time::Duration::from_millis(self.config.poll_interval));
}
}

pub fn try_update_block_template(&self) -> bool {
let mut updated = false;
match self.get_block_template().wait() {
Ok(new) => {
let work = self.current_work.upgradable_read();
if work.as_ref().map_or(true, |old| old.work_id != new.work_id) {
let mut write_guard = RwLockUpgradableReadGuard::upgrade(work);
*write_guard = Some(new);
updated = true;
let _ = self.new_work.send(());
}
}
thread::sleep(time::Duration::from_secs(self.config.poll_interval));
Err(e) => {
error!(target: "miner", "rpc call get_block_template error: {:?}", e);
}
}
updated
}

fn get_block_template(&self) -> impl Future<Item = BlockTemplate, Error = RpcError> {
Expand Down
1 change: 1 addition & 0 deletions miner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct MinerConfig {
pub cycles_limit: Cycle,
pub bytes_limit: usize,
pub max_version: Version,
pub block_on_submit: bool,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Miner {
}
pub fn run(&self) {
loop {
self.client.try_update_block_template();
if let Some((work_id, block)) = self.mine() {
self.client.submit_block(&work_id, &block);
}
Expand Down
8 changes: 6 additions & 2 deletions nodes_template/miner.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"__comments__": {
"poll_interval": "poll block template interval, unit: millisecond"
},
"logger": {
"file": "miner.log",
"filter": "info",
Expand All @@ -7,8 +10,9 @@
"data_dir": "default",
"chain": "spec/dev.json",
"rpc_url": "http://127.0.0.1:8114/",
"poll_interval": 5,
"cycles_limit": 100000000,
"bytes_limit": 10000000,
"max_version": 0
"max_version": 0,
"poll_interval": 1000,
"block_on_submit": true
}
2 changes: 1 addition & 1 deletion src/cli/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub fn miner(matches: &ArgMatches) {

thread::Builder::new()
.name("client".to_string())
.spawn(move || client.run())
.spawn(move || client.poll_block_template())
.expect("Start client failed!");

miner.run()
Expand Down

0 comments on commit abe7a8b

Please sign in to comment.