diff --git a/src/blockchain/chain.rs b/src/blockchain/chain.rs index eaf2eff..41bd72f 100644 --- a/src/blockchain/chain.rs +++ b/src/blockchain/chain.rs @@ -254,17 +254,20 @@ impl Chain { let index = block.index; let timestamp = block.timestamp; let owner = block.pub_key.clone(); - self.last_block = Some(block.clone()); - if block.transaction.is_some() { - self.last_full_block = Some(block.clone()); - } let transaction = block.transaction.clone(); - if self.add_block_to_table(block).is_ok() { - if let Some(mut transaction) = transaction { - if transaction.signing.is_empty() { - transaction.signing = owner; + match self.add_block_to_table(block.clone()) { + Ok(_) => { + self.last_block = Some(block.clone()); + if let Some(mut transaction) = transaction { + self.last_full_block = Some(block); + if transaction.signing.is_empty() { + transaction.signing = owner; + } + self.add_transaction_to_table(index, timestamp, &transaction).expect("Error adding transaction"); } - self.add_transaction_to_table(index, timestamp, &transaction).expect("Error adding transaction"); + } + Err(e) => { + warn!("Error adding block to table ({}): {:?}", e, &block); } } } diff --git a/src/p2p/peers.rs b/src/p2p/peers.rs index 2ac75a1..d4377ee 100644 --- a/src/p2p/peers.rs +++ b/src/p2p/peers.rs @@ -288,8 +288,12 @@ impl Peers { // If someone has more blocks we sync if nodes >= MIN_CONNECTED_NODES_START_SYNC && height < max_height { - let count = min(max_height - height, nodes as u64); - self.ask_blocks_from_peers(registry, height, height + count, have_blocks); + // Give some opportunity to get more peers instead of requests for blocks only + let request_blocks = nodes >= 10 || random::(); + if request_blocks { + let count = min(max_height - height, nodes as u64); + self.ask_blocks_from_peers(registry, height, height + count, have_blocks); + } } // If someone has less blocks (we mined a new block) we send a ping with our height