handling sync message

This commit is contained in:
Nikolay Volf 2016-02-07 03:00:43 +03:00
parent d40d4ef87c
commit efef36b5e8
4 changed files with 32 additions and 15 deletions

View File

@ -1137,13 +1137,13 @@ impl ChainSync {
pub fn maintain_sync(&mut self, io: &mut SyncIo) { pub fn maintain_sync(&mut self, io: &mut SyncIo) {
self.check_resume(io); self.check_resume(io);
if self.state == SyncState::Idle { let peers = self.propagade_new_hashes(io);
let peers = self.propagade_blocks(io); trace!(target: "sync", "Sent new hashes to peers: {:?}", peers);
trace!(target: "sync", "Sent latest block to peers: {:?}", peers); }
let peers = self.propagade_new_hashes(io); pub fn chain_blocks_verified(&mut self, io: &mut SyncIo) {
trace!(target: "sync", "Sent new hashes to peers: {:?}", peers); let peers = self.propagade_blocks(io);
} trace!(target: "sync", "Sent latest block to peers: {:?}", peers);
} }
} }

View File

@ -126,4 +126,10 @@ impl NetworkProtocolHandler<SyncMessage> for EthSync {
self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref())); self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref()));
self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref())); self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref()));
} }
fn message(&self, io: &NetworkContext<SyncMessage>, message: &SyncMessage) {
if let SyncMessage::BlockVerified = *message {
self.sync.write().unwrap().chain_blocks_verified(&mut NetSyncIo::new(io, self.chain.deref()));
}
}
} }

View File

@ -121,25 +121,31 @@ fn status_packet() {
} }
#[test] #[test]
fn propagade() { fn propagade_hashes() {
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer_mut(1).chain.add_blocks(1000, false); net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false); net.peer_mut(2).chain.add_blocks(1000, false);
net.sync(); net.sync();
let status = net.peer(0).sync.status();
assert_eq!(status.state, SyncState::Idle);
net.peer_mut(0).chain.add_blocks(10, false); net.peer_mut(0).chain.add_blocks(10, false);
assert_eq!(1010, net.peer(0).chain.chain_info().best_block_number);
assert_eq!(1000, net.peer(1).chain.chain_info().best_block_number);
assert_eq!(1000, net.peer(2).chain.chain_info().best_block_number);
net.sync_step_peer(0); net.sync_step_peer(0);
// 2 peers to sync // 2 peers to sync
assert_eq!(2, net.peer(0).queue.len()); assert_eq!(2, net.peer(0).queue.len());
// NEW_BLOCK_HASHES_PACKET
assert_eq!(0x01, net.peer(0).queue[0].packet_id);
}
#[test]
fn propagade_blocks() {
let mut net = TestNet::new(10);
net.peer_mut(1).chain.add_blocks(10, false);
net.sync();
net.peer_mut(0).chain.add_blocks(10, false);
net.trigger_block_verified(0);
assert!(!net.peer(0).queue.is_empty());
// NEW_BLOCK_PACKET // NEW_BLOCK_PACKET
assert_eq!(0x07, net.peer(0).queue[0].packet_id); assert_eq!(0x07, net.peer(0).queue[0].packet_id);
} }

View File

@ -395,4 +395,9 @@ impl TestNet {
pub fn done(&self) -> bool { pub fn done(&self) -> bool {
self.peers.iter().all(|p| p.queue.is_empty()) self.peers.iter().all(|p| p.queue.is_empty())
} }
pub fn trigger_block_verified(&mut self, peer_id: usize) {
let mut peer = self.peer_mut(peer_id);
peer.sync.chain_blocks_verified(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None));
}
} }