Optimized signing blocks scheduler.

This commit is contained in:
Revertron
2021-04-19 21:19:00 +02:00
parent 2680a7da70
commit 04ef49764a
5 changed files with 18 additions and 24 deletions
+3 -3
View File
@@ -233,7 +233,7 @@ impl Chain {
let keystore = Box::new(keystore);
// We start mining sign block after some time, not everyone in the same time
let start = Utc::now().timestamp() + (rand::random::<i64>() % BLOCK_SIGNERS_START_RANDOM);
return Some(Event::ActionMineLocker { start, index: block.index + 1, hash: block.hash, keystore });
return Some(Event::ActionMineSigning { start, index: block.index + 1, hash: block.hash, keystore });
} else if !signers.is_empty() {
info!("Signing block must be mined by other nodes");
}
@@ -664,7 +664,7 @@ impl Chain {
if block.index == 1 {
ZONE_DIFFICULTY
} else {
LOCKER_DIFFICULTY
SIGNER_DIFFICULTY
}
}
Some(t) => { self.get_difficulty_for_transaction(&t) }
@@ -816,7 +816,7 @@ impl Chain {
if self.can_sign_by_pos(sign_count, full_block.timestamp, block.timestamp, &block.pub_key) {
return true;
}
// If we got a locker/signing block
// If we got a signing block
let signers: HashSet<Bytes> = self.get_block_signers(full_block).into_iter().collect();
if !signers.contains(&block.pub_key) {
warn!("Ignoring block {} from '{:?}', as wrong signer!", block.index, &block.pub_key);
+1 -1
View File
@@ -3,7 +3,7 @@ pub const CHAIN_VERSION: u32 = 0;
pub const ZONE_DIFFICULTY: u32 = 28;
pub const ZONE_MIN_DIFFICULTY: u32 = 22;
pub const LOCKER_DIFFICULTY: u32 = 16;
pub const SIGNER_DIFFICULTY: u32 = 16;
pub const KEYSTORE_DIFFICULTY: u32 = 23;
/// Blocks start to be signed starting from this index
+1 -1
View File
@@ -13,7 +13,7 @@ pub enum Event {
NewBlockReceived,
BlockchainChanged { index: u64 },
ActionStopMining,
ActionMineLocker { start: i64, index: u64, hash: Bytes, keystore: Box<Keystore> },
ActionMineSigning { start: i64, index: u64, hash: Bytes, keystore: Box<Keystore> },
ActionQuit,
NetworkStatus { nodes: usize, blocks: u64 },
Syncing { have: u64, height: u64 },
+13 -11
View File
@@ -9,7 +9,7 @@ use log::{debug, error, info, trace, warn};
use num_cpus;
use crate::{Block, Bytes, Context, Keystore, setup_miner_thread};
use crate::commons::{CHAIN_VERSION, LOCKER_DIFFICULTY, KEYSTORE_DIFFICULTY};
use crate::commons::{CHAIN_VERSION, SIGNER_DIFFICULTY, KEYSTORE_DIFFICULTY};
use crate::blockchain::types::BlockQuality;
use crate::blockchain::hash_utils::*;
use crate::keys::check_public_key_strength;
@@ -37,7 +37,13 @@ impl Miner {
}
pub fn add_block(&mut self, block: Block, keystore: Keystore) {
self.jobs.lock().unwrap().push(MineJob { start: 0, block, keystore });
{
let mut jobs = self.jobs.lock().unwrap();
if block.transaction.is_none() {
jobs.retain(|job| job.block.transaction.is_some());
}
jobs.push(MineJob { start: 0, block, keystore });
}
self.cond_var.notify_one();
}
@@ -90,13 +96,13 @@ impl Miner {
Event::ActionStopMining => {
mining.store(false, Ordering::SeqCst);
}
Event::ActionMineLocker { start, index, hash, keystore } => {
Event::ActionMineSigning { start, index, hash, keystore } => {
if !mining.load(Ordering::SeqCst) {
let mut block = Block::new(None, Bytes::default(), hash, LOCKER_DIFFICULTY);
let mut block = Block::new(None, Bytes::default(), hash, SIGNER_DIFFICULTY);
block.index = index;
blocks.lock().unwrap().push(MineJob { start, block, keystore: keystore.deref().clone() });
cond_var.notify_all();
info!("Added a locker block to mine");
info!("Added a signing block to mine");
}
}
_ => {}
@@ -114,9 +120,9 @@ impl Miner {
job.block.signature = Bytes::default();
job.block.hash = Bytes::default();
job.block.version = CHAIN_VERSION;
// If this block needs to be a locker
// If this block needs to be a signer
if job.block.index > 0 && !job.block.prev_block_hash.is_empty() {
info!("Mining locker block");
info!("Mining signing block");
job.block.pub_key = job.keystore.get_public();
if !check_public_key_strength(&job.block.pub_key, KEYSTORE_DIFFICULTY) {
warn!("Can not mine block with weak public key!");
@@ -190,10 +196,6 @@ impl Miner {
context.settings.origin = block.hash.to_string();
}
context.chain.add_block(block);
let option = Some(job.keystore);
if let Some(event) = context.chain.update(&option) {
context.bus.post(event);
}
success = true;
}
context.bus.post(Event::MinerStopped { success, full });
-8
View File
@@ -492,10 +492,6 @@ fn process_new_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &To
match context.chain.check_new_block(&block) {
BlockQuality::Good => {
context.chain.add_block(block);
let keystore = context.keystore.clone();
if let Some(event) = context.chain.update(&keystore) {
context.bus.post(event);
}
let my_height = context.chain.height();
context.bus.post(crate::event::Event::BlockchainChanged { index: my_height });
// If it was the last block to sync
@@ -521,10 +517,6 @@ fn process_new_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &To
let last_block = context.chain.last_block().unwrap();
if block.is_better_than(&last_block) {
context.chain.replace_block(block.index, block).expect("Error replacing block with fork");
let keystore = context.keystore.clone();
if let Some(event) = context.chain.update(&keystore) {
context.bus.post(event);
}
let index = context.chain.height();
context.bus.post(crate::event::Event::BlockchainChanged { index });
}