openethereum/sync/src/tests/chain.rs

163 lines
4.6 KiB
Rust
Raw Normal View History

2016-02-05 13:40:41 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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/>.
2016-02-04 19:30:31 +01:00
use util::*;
use ethcore::client::{BlockChainClient, BlockId};
2016-02-04 19:30:31 +01:00
use io::SyncIo;
use chain::{SyncState};
use super::helpers::*;
#[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);
net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false);
net.sync();
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
2016-02-04 19:30:31 +01:00
assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
}
#[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);
net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false);
net.sync();
let status = net.peer(0).sync.status();
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);
net.peer_mut(1).chain.add_blocks(100, false);
net.peer_mut(2).chain.add_blocks(100, false);
let total_steps = net.sync();
assert!(total_steps < 7);
}
#[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 {
net.peer_mut(1).chain.add_blocks(5, n % 2 == 0);
net.peer_mut(2).chain.add_blocks(5, n % 2 == 0);
}
net.sync();
assert!(net.peer(0).chain.block(BlockId::Number(1000)).is_some());
2016-02-04 19:30:31 +01:00
assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
}
#[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);
net.peer_mut(0).chain.add_blocks(300, false);
net.peer_mut(1).chain.add_blocks(300, false);
net.peer_mut(2).chain.add_blocks(300, false);
net.peer_mut(0).chain.add_blocks(100, true); //fork
net.peer_mut(1).chain.add_blocks(200, false);
net.peer_mut(2).chain.add_blocks(200, false);
net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2
net.peer_mut(2).chain.add_blocks(10, true);
// peer 1 has the best chain of 601 blocks
let peer1_chain = net.peer(1).chain.numbers.read().unwrap().clone();
net.sync();
assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain);
assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain);
assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain);
}
#[test]
2016-02-04 20:03:14 +01:00
fn restart() {
2016-02-04 19:30:31 +01:00
let mut net = TestNet::new(3);
net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false);
net.sync_steps(8);
// make sure that sync has actually happened
assert!(net.peer(0).chain.chain_info().best_block_number > 100);
net.restart_peer(0);
let status = net.peer(0).sync.status();
assert_eq!(status.state, SyncState::NotSynced);
}
#[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);
assert_eq!(net.peer(0).sync.status().state, SyncState::NotSynced);
2016-02-05 18:34:08 +01:00
}
#[test]
fn status_packet() {
let mut net = TestNet::new(2);
2016-02-12 13:07:02 +01:00
net.peer_mut(0).chain.add_blocks(100, false);
2016-02-05 18:34:08 +01:00
net.peer_mut(1).chain.add_blocks(1, false);
net.start();
net.sync_step_peer(0);
assert_eq!(1, net.peer(0).queue.len());
assert_eq!(0x00, net.peer(0).queue[0].packet_id);
}
#[test]
2016-02-07 01:00:43 +01:00
fn propagade_hashes() {
2016-02-12 13:07:02 +01:00
let mut net = TestNet::new(6);
net.peer_mut(1).chain.add_blocks(10, false);
2016-02-05 18:34:08 +01:00
net.sync();
2016-02-06 18:56:21 +01:00
2016-02-05 18:34:08 +01:00
net.peer_mut(0).chain.add_blocks(10, false);
2016-02-12 13:07:02 +01:00
net.sync();
net.trigger_block_verified(0); //first event just sets the marker
net.trigger_block_verified(0);
2016-02-05 18:34:08 +01:00
2016-02-12 13:07:02 +01:00
// 5 peers to sync
assert_eq!(5, net.peer(0).queue.len());
let mut hashes = 0;
let mut blocks = 0;
for i in 0..5 {
if net.peer(0).queue[i].packet_id == 0x1 {
hashes += 1;
}
if net.peer(0).queue[i].packet_id == 0x7 {
blocks += 1;
}
}
2016-02-12 15:48:26 +01:00
assert!(blocks + hashes == 5);
2016-02-07 01:00:43 +01:00
}
#[test]
fn propagade_blocks() {
2016-02-07 21:01:09 +01:00
let mut net = TestNet::new(2);
2016-02-07 01:00:43 +01:00
net.peer_mut(1).chain.add_blocks(10, false);
net.sync();
net.peer_mut(0).chain.add_blocks(10, false);
2016-02-12 13:07:02 +01:00
net.trigger_block_verified(0); //first event just sets the marker
2016-02-07 01:00:43 +01:00
net.trigger_block_verified(0);
assert!(!net.peer(0).queue.is_empty());
2016-02-06 21:04:58 +01:00
// NEW_BLOCK_PACKET
assert_eq!(0x07, net.peer(0).queue[0].packet_id);
}