Backport: Block sync stopped without any errors. #277 (#286)

* Backport: Block sync stopped without any errors. #277
* fmt
This commit is contained in:
rakita
2021-03-09 11:20:32 +01:00
committed by GitHub
parent 7ea5707904
commit 5b904476cd
4 changed files with 76 additions and 50 deletions

View File

@@ -57,7 +57,7 @@ fn status_after_sync() {
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
net.sync();
let status = net.peer(0).sync.read().status();
let status = net.peer(0).sync.read().unwrap().status();
assert_eq!(status.state, SyncState::Idle);
}
@@ -177,19 +177,22 @@ fn restart() {
assert!(net.peer(0).chain.chain_info().best_block_number > 100);
net.restart_peer(0);
let status = net.peer(0).sync.read().status();
let status = net.peer(0).sync.read().unwrap().status();
assert_eq!(status.state, SyncState::Idle);
}
#[test]
fn status_empty() {
let net = TestNet::new(2);
assert_eq!(net.peer(0).sync.read().status().state, SyncState::Idle);
assert_eq!(
net.peer(0).sync.read().unwrap().status().state,
SyncState::Idle
);
let mut config = SyncConfig::default();
config.warp_sync = WarpSync::Enabled;
let net = TestNet::new_with_config(2, config);
assert_eq!(
net.peer(0).sync.read().status().state,
net.peer(0).sync.read().unwrap().status().state,
SyncState::WaitingPeers
);
}

View File

@@ -37,7 +37,10 @@ use network::{self, client_version::ClientVersion, PacketId, PeerId, ProtocolId,
use parking_lot::RwLock;
use std::{
collections::{HashMap, HashSet, VecDeque},
sync::Arc,
sync::{
Arc, RwLock as StdRwLock, RwLockReadGuard as StdRwLockReadGuard,
RwLockWriteGuard as StdRwLockWriteGuard,
},
};
use sync_io::SyncIo;
use tests::snapshot::*;
@@ -249,7 +252,7 @@ where
pub chain: Arc<C>,
pub miner: Arc<Miner>,
pub snapshot_service: Arc<TestSnapshotService>,
pub sync: RwLock<ChainSync>,
pub sync: StdRwLock<ChainSync>,
pub queue: RwLock<VecDeque<TestPacket>>,
pub io_queue: RwLock<VecDeque<ChainMessageType>>,
new_blocks_queue: RwLock<VecDeque<NewBlockMessage>>,
@@ -270,15 +273,17 @@ where
fn process_io_message(&self, message: ChainMessageType) {
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None);
match message {
ChainMessageType::Consensus(data) => {
self.sync.write().propagate_consensus_packet(&mut io, data)
}
ChainMessageType::Consensus(data) => self
.sync
.write()
.unwrap()
.propagate_consensus_packet(&mut io, data),
}
}
fn process_new_block_message(&self, message: NewBlockMessage) {
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None);
self.sync.write().chain_new_blocks(
self.sync.write().unwrap().chain_new_blocks(
&mut io,
&message.imported,
&message.invalid,
@@ -294,8 +299,8 @@ impl<C: FlushingBlockChainClient> Peer for EthPeer<C> {
type Message = TestPacket;
fn on_connect(&self, other: PeerId) {
self.sync.write().update_targets(&*self.chain);
self.sync.write().on_peer_connected(
self.sync.write().unwrap().update_targets(&*self.chain);
self.sync.write().unwrap().on_peer_connected(
&mut TestIo::new(
&*self.chain,
&self.snapshot_service,
@@ -313,7 +318,7 @@ impl<C: FlushingBlockChainClient> Peer for EthPeer<C> {
&self.queue,
Some(other),
);
self.sync.write().on_peer_aborting(&mut io, other);
self.sync.write().unwrap().on_peer_aborting(&mut io, other);
}
fn receive_message(&self, from: PeerId, msg: TestPacket) -> HashSet<PeerId> {
@@ -340,14 +345,17 @@ impl<C: FlushingBlockChainClient> Peer for EthPeer<C> {
fn sync_step(&self) {
let mut io = TestIo::new(&*self.chain, &self.snapshot_service, &self.queue, None);
self.chain.flush();
self.sync.write().maintain_peers(&mut io);
self.sync.write().maintain_sync(&mut io);
self.sync.write().continue_sync(&mut io);
self.sync.write().propagate_new_transactions(&mut io);
self.sync.write().unwrap().maintain_peers(&mut io);
self.sync.write().unwrap().maintain_sync(&mut io);
self.sync.write().unwrap().continue_sync(&mut io);
self.sync
.write()
.unwrap()
.propagate_new_transactions(&mut io);
}
fn restart_sync(&self) {
self.sync.write().restart(&mut TestIo::new(
self.sync.write().unwrap().restart(&mut TestIo::new(
&*self.chain,
&self.snapshot_service,
&self.queue,
@@ -400,7 +408,7 @@ impl TestNet<EthPeer<TestBlockChainClient>> {
let ss = Arc::new(TestSnapshotService::new());
let sync = ChainSync::new(config.clone(), &chain, ForkFilterApi::new_dummy(&chain));
net.peers.push(Arc::new(EthPeer {
sync: RwLock::new(sync),
sync: StdRwLock::new(sync),
snapshot_service: ss,
chain: Arc::new(chain),
miner: Arc::new(Miner::new_for_tests(&Spec::new_test(), None)),
@@ -449,7 +457,7 @@ impl TestNet<EthPeer<EthcoreClient>> {
let ss = Arc::new(TestSnapshotService::new());
let sync = ChainSync::new(config, &*client, ForkFilterApi::new_dummy(&*client));
let peer = Arc::new(EthPeer {
sync: RwLock::new(sync),
sync: StdRwLock::new(sync),
snapshot_service: ss,
chain: client,
miner,
@@ -558,7 +566,7 @@ where
impl<C: FlushingBlockChainClient> TestNet<EthPeer<C>> {
pub fn trigger_chain_new_blocks(&mut self, peer_id: usize) {
let peer = &mut self.peers[peer_id];
peer.sync.write().chain_new_blocks(
peer.sync.write().unwrap().chain_new_blocks(
&mut TestIo::new(&*peer.chain, &peer.snapshot_service, &peer.queue, None),
&[],
&[],