From 2680a7da708d32b40372524d17f6ca4e0b6e1bed Mon Sep 17 00:00:00 2001 From: Revertron Date: Mon, 19 Apr 2021 21:00:32 +0200 Subject: [PATCH] Fixed consensus error. Added a delay before signing. --- src/blockchain/chain.rs | 16 ++++++++++++++++ src/miner.rs | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index b68d927..a2d57e3 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -212,6 +212,12 @@ impl Chain { trace!("Block {} has enough signing blocks", block.index); return None; } + if let Some(block) = &self.last_block { + if block.timestamp + 60 > Utc::now().timestamp() { + info!("Waiting for other blocks before signing."); + return None; + } + } let keystore = keystore.clone().unwrap().clone(); let signers: HashSet = self.get_block_signers(&block).into_iter().collect(); if signers.contains(&keystore.get_public()) { @@ -645,6 +651,10 @@ impl Chain { warn!("Ignoring block from the future:\n{:?}", &block); return Bad; } + if block.index > self.height() + 1 { + info!("Ignoring future block:\n{:?}", &block); + return Future; + } if !check_public_key_strength(&block.pub_key, KEYSTORE_DIFFICULTY) { warn!("Ignoring block with weak public key:\n{:?}", &block); return Bad; @@ -675,6 +685,12 @@ impl Chain { warn!("Block {:?} has wrong signature! Ignoring!", &block); return Bad; } + if let Some(prev_block) = self.get_block(block.index - 1) { + if block.prev_block_hash != prev_block.hash { + warn!("Ignoring block with wrong previous hash:\n{:?}", &block); + return Bad; + } + } let faulty_blocks = vec![ "0000133B790B61460D757E1F1F2D04480C8340D28CA73AE5AF27DBBF60548D00", diff --git a/src/miner.rs b/src/miner.rs index 5e1c0fe..321fb55 100644 --- a/src/miner.rs +++ b/src/miner.rs @@ -276,6 +276,10 @@ fn find_hash(context: Arc>, mut block: Block, running: Arc 1 { if let Ok(context) = context.lock() { if context.chain.height() >= block.index { + if !full { + info!("Blockchain changed while mining signing block, dropping work"); + return None; + } break; } }