2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2018-03-03 18:42:13 +01:00
|
|
|
use ethcore::client::{TestBlockChainClient, BlockChainClient, BlockId, EachBlockWith, ChainInfo, BlockInfo};
|
2016-02-04 19:30:31 +01:00
|
|
|
use chain::{SyncState};
|
|
|
|
use super::helpers::*;
|
2018-03-29 11:20:27 +02:00
|
|
|
use {SyncConfig, WarpSync};
|
2016-02-04 19:30:31 +01:00
|
|
|
|
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn two_peers() {
|
2016-02-04 19:30:31 +01:00
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(3);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
2016-02-04 19:30:31 +01:00
|
|
|
net.sync();
|
2016-12-09 23:01:43 +01:00
|
|
|
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
|
2016-08-10 16:29:40 +02:00
|
|
|
assert_eq!(*net.peer(0).chain.blocks.read(), *net.peer(1).chain.blocks.read());
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
#[test]
|
|
|
|
fn long_chain() {
|
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing);
|
2016-05-16 14:41:41 +02:00
|
|
|
net.sync();
|
2016-12-09 23:01:43 +01:00
|
|
|
assert!(net.peer(0).chain.block(BlockId::Number(50000)).is_some());
|
2016-08-10 16:29:40 +02:00
|
|
|
assert_eq!(*net.peer(0).chain.blocks.read(), *net.peer(1).chain.blocks.read());
|
2016-05-16 14:41:41 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 19:30:31 +01:00
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn status_after_sync() {
|
2016-02-04 19:30:31 +01:00
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(3);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
2016-02-04 19:30:31 +01:00
|
|
|
net.sync();
|
2016-07-13 19:59:59 +02:00
|
|
|
let status = net.peer(0).sync.read().status();
|
2016-02-04 19:30:31 +01:00
|
|
|
assert_eq!(status.state, SyncState::Idle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn takes_few_steps() {
|
2016-02-04 19:30:31 +01:00
|
|
|
let mut net = TestNet::new(3);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(100, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(100, EachBlockWith::Uncle);
|
2016-02-04 19:30:31 +01:00
|
|
|
let total_steps = net.sync();
|
2016-05-16 14:41:41 +02:00
|
|
|
assert!(total_steps < 20);
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn empty_blocks() {
|
2016-02-04 19:30:31 +01:00
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(3);
|
|
|
|
for n in 0..200 {
|
2016-03-05 16:46:04 +01:00
|
|
|
let with = if n % 2 == 0 { EachBlockWith::Nothing } else { EachBlockWith::Uncle };
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(5, with.clone());
|
|
|
|
net.peer(2).chain.add_blocks(5, with);
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
net.sync();
|
2016-12-09 23:01:43 +01:00
|
|
|
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
|
2016-08-10 16:29:40 +02:00
|
|
|
assert_eq!(*net.peer(0).chain.blocks.read(), *net.peer(1).chain.blocks.read());
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn forked() {
|
2016-02-04 19:30:31 +01:00
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(3);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(30, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.add_blocks(30, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(30, EachBlockWith::Uncle);
|
|
|
|
net.peer(0).chain.add_blocks(10, EachBlockWith::Nothing); //fork
|
|
|
|
net.peer(1).chain.add_blocks(20, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(20, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle); //fork between 1 and 2
|
|
|
|
net.peer(2).chain.add_blocks(1, EachBlockWith::Nothing);
|
2016-02-04 19:30:31 +01:00
|
|
|
// peer 1 has the best chain of 601 blocks
|
2016-07-13 19:59:59 +02:00
|
|
|
let peer1_chain = net.peer(1).chain.numbers.read().clone();
|
2016-02-04 19:30:31 +01:00
|
|
|
net.sync();
|
2016-08-10 16:29:40 +02:00
|
|
|
assert_eq!(*net.peer(0).chain.difficulty.read(), *net.peer(1).chain.difficulty.read());
|
|
|
|
assert_eq!(&*net.peer(0).chain.numbers.read(), &peer1_chain);
|
|
|
|
assert_eq!(&*net.peer(1).chain.numbers.read(), &peer1_chain);
|
|
|
|
assert_eq!(&*net.peer(2).chain.numbers.read(), &peer1_chain);
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-10 23:05:41 +02:00
|
|
|
#[test]
|
|
|
|
fn forked_with_misbehaving_peer() {
|
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(3);
|
2017-01-11 14:39:03 +01:00
|
|
|
|
|
|
|
let mut alt_spec = ::ethcore::spec::Spec::new_test();
|
|
|
|
alt_spec.extra_data = b"fork".to_vec();
|
2016-10-10 23:05:41 +02:00
|
|
|
// peer 0 is on a totally different chain with higher total difficulty
|
2017-01-11 14:39:03 +01:00
|
|
|
net.peer_mut(0).chain = Arc::new(TestBlockChainClient::new_with_spec(alt_spec));
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(50, EachBlockWith::Nothing);
|
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
|
|
|
|
net.peer(2).chain.add_blocks(10, EachBlockWith::Nothing);
|
2016-10-10 23:05:41 +02:00
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Nothing);
|
|
|
|
net.peer(2).chain.add_blocks(20, EachBlockWith::Uncle);
|
2016-10-10 23:05:41 +02:00
|
|
|
// peer 1 should sync to peer 2, others should not change
|
|
|
|
let peer0_chain = net.peer(0).chain.numbers.read().clone();
|
|
|
|
let peer2_chain = net.peer(2).chain.numbers.read().clone();
|
|
|
|
net.sync();
|
|
|
|
assert_eq!(&*net.peer(0).chain.numbers.read(), &peer0_chain);
|
|
|
|
assert_eq!(&*net.peer(1).chain.numbers.read(), &peer2_chain);
|
|
|
|
assert_eq!(&*net.peer(2).chain.numbers.read(), &peer2_chain);
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:38:22 +02:00
|
|
|
#[test]
|
|
|
|
fn net_hard_fork() {
|
|
|
|
::env_logger::init().ok();
|
|
|
|
let ref_client = TestBlockChainClient::new();
|
|
|
|
ref_client.add_blocks(50, EachBlockWith::Uncle);
|
|
|
|
{
|
2016-12-09 23:01:43 +01:00
|
|
|
let mut net = TestNet::new_with_fork(2, Some((50, ref_client.block_hash(BlockId::Number(50)).unwrap())));
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
2016-07-27 21:38:22 +02:00
|
|
|
net.sync();
|
|
|
|
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 100);
|
|
|
|
}
|
|
|
|
{
|
2016-12-09 23:01:43 +01:00
|
|
|
let mut net = TestNet::new_with_fork(2, Some((50, ref_client.block_hash(BlockId::Number(50)).unwrap())));
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(100, EachBlockWith::Nothing);
|
2016-07-27 21:38:22 +02:00
|
|
|
net.sync();
|
|
|
|
assert_eq!(net.peer(1).chain.chain_info().best_block_number, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 19:30:31 +01:00
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn restart() {
|
2016-10-10 17:43:05 +02:00
|
|
|
::env_logger::init().ok();
|
2016-02-04 19:30:31 +01:00
|
|
|
let mut net = TestNet::new(3);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
|
|
|
|
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
|
2016-02-04 19:30:31 +01:00
|
|
|
|
2016-10-10 23:05:41 +02:00
|
|
|
net.sync();
|
2016-02-04 19:30:31 +01:00
|
|
|
|
|
|
|
// make sure that sync has actually happened
|
|
|
|
assert!(net.peer(0).chain.chain_info().best_block_number > 100);
|
|
|
|
net.restart_peer(0);
|
|
|
|
|
2016-07-13 19:59:59 +02:00
|
|
|
let status = net.peer(0).sync.read().status();
|
2016-07-23 17:27:23 +02:00
|
|
|
assert_eq!(status.state, SyncState::Idle);
|
2016-02-04 19:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-04 20:03:14 +01:00
|
|
|
fn status_empty() {
|
2016-02-04 19:30:31 +01:00
|
|
|
let net = TestNet::new(2);
|
2016-07-13 19:59:59 +02:00
|
|
|
assert_eq!(net.peer(0).sync.read().status().state, SyncState::Idle);
|
2016-10-29 13:07:06 +02:00
|
|
|
let mut config = SyncConfig::default();
|
2018-03-29 11:20:27 +02:00
|
|
|
config.warp_sync = WarpSync::Enabled;
|
2016-10-29 13:07:06 +02:00
|
|
|
let net = TestNet::new_with_config(2, config);
|
|
|
|
assert_eq!(net.peer(0).sync.read().status().state, SyncState::WaitingPeers);
|
2016-02-05 18:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn status_packet() {
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(100, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.add_blocks(1, EachBlockWith::Uncle);
|
2016-02-05 18:34:08 +01:00
|
|
|
|
|
|
|
net.start();
|
|
|
|
|
|
|
|
net.sync_step_peer(0);
|
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
assert_eq!(1, net.peer(0).queue.read().len());
|
|
|
|
assert_eq!(0x00, net.peer(0).queue.read()[0].packet_id);
|
2016-02-05 18:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-01 21:31:58 +01:00
|
|
|
fn propagate_hashes() {
|
2016-02-12 13:07:02 +01:00
|
|
|
let mut net = TestNet::new(6);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
2016-02-05 18:34:08 +01:00
|
|
|
net.sync();
|
2016-02-06 18:56:21 +01:00
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
|
2016-02-12 13:07:02 +01:00
|
|
|
net.sync();
|
2016-03-07 12:16:37 +01:00
|
|
|
net.trigger_chain_new_blocks(0); //first event just sets the marker
|
|
|
|
net.trigger_chain_new_blocks(0);
|
2016-02-05 18:34:08 +01:00
|
|
|
|
2016-08-08 17:18:10 +02:00
|
|
|
// 5 peers with NewHahses, 4 with blocks
|
2016-12-06 19:23:15 +01:00
|
|
|
assert_eq!(9, net.peer(0).queue.read().len());
|
2016-02-12 13:07:02 +01:00
|
|
|
let mut hashes = 0;
|
|
|
|
let mut blocks = 0;
|
2016-12-06 19:23:15 +01:00
|
|
|
for i in 0..net.peer(0).queue.read().len() {
|
|
|
|
if net.peer(0).queue.read()[i].packet_id == 0x1 {
|
2016-02-12 13:07:02 +01:00
|
|
|
hashes += 1;
|
|
|
|
}
|
2016-12-06 19:23:15 +01:00
|
|
|
if net.peer(0).queue.read()[i].packet_id == 0x7 {
|
2016-02-12 13:07:02 +01:00
|
|
|
blocks += 1;
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 17:18:10 +02:00
|
|
|
assert_eq!(blocks, 4);
|
|
|
|
assert_eq!(hashes, 5);
|
2016-02-07 01:00:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-01 21:31:58 +01:00
|
|
|
fn propagate_blocks() {
|
2016-05-29 00:38:10 +02:00
|
|
|
let mut net = TestNet::new(20);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
2016-02-07 01:00:43 +01:00
|
|
|
net.sync();
|
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(0).chain.add_blocks(10, EachBlockWith::Uncle);
|
2016-03-07 12:16:37 +01:00
|
|
|
net.trigger_chain_new_blocks(0); //first event just sets the marker
|
|
|
|
net.trigger_chain_new_blocks(0);
|
2016-02-07 01:00:43 +01:00
|
|
|
|
2016-12-06 19:23:15 +01:00
|
|
|
assert!(!net.peer(0).queue.read().is_empty());
|
2016-02-06 21:04:58 +01:00
|
|
|
// NEW_BLOCK_PACKET
|
2016-12-06 19:23:15 +01:00
|
|
|
let blocks = net.peer(0).queue.read().iter().filter(|p| p.packet_id == 0x7).count();
|
2016-05-29 00:38:10 +02:00
|
|
|
assert!(blocks > 0);
|
2016-02-10 22:16:25 +01:00
|
|
|
}
|
2016-02-15 13:34:58 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn restart_on_malformed_block() {
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.corrupt_block(6);
|
2016-05-16 14:41:41 +02:00
|
|
|
net.sync_steps(20);
|
2016-02-15 13:34:58 +01:00
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5);
|
2016-02-15 13:34:58 +01:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:41:41 +02:00
|
|
|
#[test]
|
|
|
|
fn restart_on_broken_chain() {
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.corrupt_block_parent(6);
|
2016-05-16 14:41:41 +02:00
|
|
|
net.sync_steps(20);
|
|
|
|
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5);
|
|
|
|
}
|
2016-06-17 16:01:33 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn high_td_attach() {
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-06 19:23:15 +01:00
|
|
|
net.peer(1).chain.add_blocks(10, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.corrupt_block_parent(6);
|
2016-06-17 16:01:33 +02:00
|
|
|
net.sync_steps(20);
|
|
|
|
|
|
|
|
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5);
|
|
|
|
}
|
2016-08-10 16:29:40 +02:00
|
|
|
|
2016-11-28 16:30:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn disconnect_on_unrelated_chain() {
|
|
|
|
::env_logger::init().ok();
|
|
|
|
let mut net = TestNet::new(2);
|
2016-12-23 18:43:40 +01:00
|
|
|
net.peer(0).chain.set_history(Some(20));
|
|
|
|
net.peer(1).chain.set_history(Some(20));
|
|
|
|
net.restart_peer(0);
|
|
|
|
net.restart_peer(1);
|
|
|
|
net.peer(0).chain.add_blocks(500, EachBlockWith::Uncle);
|
|
|
|
net.peer(1).chain.add_blocks(300, EachBlockWith::Nothing);
|
2016-11-28 16:30:36 +01:00
|
|
|
net.sync();
|
|
|
|
assert_eq!(net.disconnect_events, vec![(0, 0)]);
|
|
|
|
}
|
|
|
|
|