Fixed a problem with forks longer than one.
This commit is contained in:
+10
-3
@@ -19,6 +19,7 @@ use rand::random;
|
||||
use crate::{Block, Context, p2p::Message, p2p::Peer, p2p::Peers, p2p::State};
|
||||
use crate::blockchain::types::BlockQuality;
|
||||
use crate::commons::*;
|
||||
use std::cmp::max;
|
||||
|
||||
const SERVER: Token = Token(0);
|
||||
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(500));
|
||||
@@ -404,7 +405,8 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
||||
let mut context = context.lock().unwrap();
|
||||
if peer.is_higher(my_height) {
|
||||
context.chain.update_max_height(height);
|
||||
context.bus.post(crate::event::Event::Syncing { have: my_height, height});
|
||||
let event = crate::event::Event::Syncing { have: my_height, height: max(height, my_height) };
|
||||
context.bus.post(event);
|
||||
}
|
||||
if active_count < 5 || random::<u8>() < 10 {
|
||||
debug!("Requesting more peers from {}", peer.get_addr().ip());
|
||||
@@ -514,7 +516,8 @@ fn process_new_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &To
|
||||
if my_height == max_height {
|
||||
context.bus.post(crate::event::Event::SyncFinished);
|
||||
} else {
|
||||
context.bus.post(crate::event::Event::Syncing { have: my_height, height: max_height });
|
||||
let event = crate::event::Event::Syncing { have: my_height, height: max(max_height, my_height) };
|
||||
context.bus.post(event);
|
||||
}
|
||||
context.bus.post(crate::event::Event::NetworkStatus { nodes: peers_count, blocks: my_height });
|
||||
}
|
||||
@@ -528,11 +531,15 @@ fn process_new_block(context: Arc<Mutex<Context>>, peers: &mut Peers, token: &To
|
||||
context.bus.post(crate::event::Event::SyncFinished);
|
||||
return State::Banned;
|
||||
}
|
||||
BlockQuality::Rewind => {
|
||||
debug!("Got some orphan block, requesting its parent");
|
||||
return State::message(Message::GetBlock { index: block.index - 1 });
|
||||
}
|
||||
BlockQuality::Fork => {
|
||||
debug!("Got forked block {} with hash {:?}", block.index, block.hash);
|
||||
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");
|
||||
context.chain.replace_block(block).expect("Error replacing block with fork");
|
||||
let index = context.chain.get_height();
|
||||
context.bus.post(crate::event::Event::BlockchainChanged { index });
|
||||
}
|
||||
|
||||
+13
-2
@@ -23,6 +23,7 @@ pub struct Peers {
|
||||
ignored: HashSet<IpAddr>,
|
||||
my_id: String,
|
||||
block_asked_time: i64,
|
||||
behind_ping_sent_time: i64,
|
||||
}
|
||||
|
||||
impl Peers {
|
||||
@@ -32,7 +33,8 @@ impl Peers {
|
||||
new_peers: Vec::new(),
|
||||
ignored: HashSet::new(),
|
||||
my_id: commons::random_string(6),
|
||||
block_asked_time: 0
|
||||
block_asked_time: 0,
|
||||
behind_ping_sent_time: 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,7 +259,7 @@ impl Peers {
|
||||
}
|
||||
|
||||
// If someone has less blocks (we mined a new block) we send a ping with our height
|
||||
{
|
||||
if self.need_behind_ping() {
|
||||
let mut rng = rand::thread_rng();
|
||||
match self.peers
|
||||
.iter_mut()
|
||||
@@ -268,6 +270,7 @@ impl Peers {
|
||||
debug!("Found some peer lower than we are, sending ping");
|
||||
registry.reregister(peer.get_stream(), token.clone(), Interest::WRITABLE).unwrap();
|
||||
peer.set_state(State::message(Message::Ping { height, hash }));
|
||||
self.update_behind_ping_time();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -383,6 +386,14 @@ impl Peers {
|
||||
pub fn need_ask_block(&self) -> bool {
|
||||
self.block_asked_time + 5 < Utc::now().timestamp()
|
||||
}
|
||||
|
||||
pub fn update_behind_ping_time(&mut self) {
|
||||
self.behind_ping_sent_time = Utc::now().timestamp();
|
||||
}
|
||||
|
||||
pub fn need_behind_ping(&self) -> bool {
|
||||
self.behind_ping_sent_time + 5 < Utc::now().timestamp()
|
||||
}
|
||||
}
|
||||
|
||||
fn skip_private_addr(addr: &SocketAddr) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user