openethereum/sync/src/chain.rs

1817 lines
62 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-15 00:51:50 +01:00
///
2016-04-06 10:07:24 +02:00
/// `BlockChain` synchronization strategy.
2016-02-15 00:51:50 +01:00
/// Syncs to peers and keeps up to date.
2016-01-09 18:40:13 +01:00
/// This implementation uses ethereum protocol v63
///
/// Syncing strategy.
///
/// 1. A peer arrives with a total difficulty better than ours
2016-02-15 00:51:50 +01:00
/// 2. Find a common best block between our an peer chain.
2016-01-09 18:40:13 +01:00
/// Start with out best block and request headers from peer backwards until a common block is found
2016-02-15 00:51:50 +01:00
/// 3. Download headers and block bodies from peers in parallel.
2016-01-09 18:40:13 +01:00
/// As soon as a set of the blocks is fully downloaded at the head of the queue it is fed to the blockchain
/// 4. Maintain sync by handling NewBlocks/NewHashes messages
///
2016-01-10 14:11:23 +01:00
use util::*;
2015-12-22 22:19:50 +01:00
use std::mem::{replace};
use ethcore::views::{HeaderView};
2016-02-03 21:42:30 +01:00
use ethcore::header::{BlockNumber, Header as BlockHeader};
use ethcore::client::{BlockChainClient, BlockStatus, BlockId, BlockChainInfo};
2016-02-03 21:42:30 +01:00
use range_collection::{RangeCollection, ToUsize, FromUsize};
use ethcore::error::*;
use ethcore::transaction::SignedTransaction;
use ethcore::block::Block;
use ethminer::{Miner, MinerService, AccountDetails};
2016-02-03 21:42:30 +01:00
use io::SyncIo;
use time;
2016-02-24 21:23:58 +01:00
use super::SyncConfig;
2015-12-22 22:19:50 +01:00
2016-02-24 22:37:28 +01:00
known_heap_size!(0, PeerInfo, Header, HeaderId);
2015-12-25 14:55:55 +01:00
impl ToUsize for BlockNumber {
fn to_usize(&self) -> usize {
*self as usize
}
}
impl FromUsize for BlockNumber {
fn from_usize(s: usize) -> BlockNumber {
s as BlockNumber
}
}
2016-01-08 17:52:25 +01:00
type PacketDecodeError = DecoderError;
2015-12-24 17:18:47 +01:00
const PROTOCOL_VERSION: u8 = 63u8;
const MAX_BODIES_TO_SEND: usize = 256;
2015-12-27 00:48:03 +01:00
const MAX_HEADERS_TO_SEND: usize = 512;
2015-12-24 17:18:47 +01:00
const MAX_NODE_DATA_TO_SEND: usize = 1024;
const MAX_RECEIPTS_TO_SEND: usize = 1024;
2016-03-13 15:59:25 +01:00
const MAX_RECEIPTS_HEADERS_TO_SEND: usize = 256;
2015-12-27 00:48:03 +01:00
const MAX_HEADERS_TO_REQUEST: usize = 512;
const MAX_BODIES_TO_REQUEST: usize = 64;
2016-02-05 18:34:08 +01:00
const MIN_PEERS_PROPAGATION: usize = 4;
const MAX_PEERS_PROPAGATION: usize = 128;
2016-02-06 18:56:21 +01:00
const MAX_PEER_LAG_PROPAGATION: BlockNumber = 20;
2015-12-24 17:18:47 +01:00
2015-12-22 22:19:50 +01:00
const STATUS_PACKET: u8 = 0x00;
const NEW_BLOCK_HASHES_PACKET: u8 = 0x01;
const TRANSACTIONS_PACKET: u8 = 0x02;
const GET_BLOCK_HEADERS_PACKET: u8 = 0x03;
const BLOCK_HEADERS_PACKET: u8 = 0x04;
const GET_BLOCK_BODIES_PACKET: u8 = 0x05;
const BLOCK_BODIES_PACKET: u8 = 0x06;
const NEW_BLOCK_PACKET: u8 = 0x07;
const GET_NODE_DATA_PACKET: u8 = 0x0d;
const NODE_DATA_PACKET: u8 = 0x0e;
const GET_RECEIPTS_PACKET: u8 = 0x0f;
const RECEIPTS_PACKET: u8 = 0x10;
const CONNECTION_TIMEOUT_SEC: f64 = 10f64;
2016-02-03 21:42:30 +01:00
2015-12-22 22:19:50 +01:00
struct Header {
2016-01-09 18:40:13 +01:00
/// Header data
2015-12-22 22:19:50 +01:00
data: Bytes,
/// Block hash
hash: H256,
/// Parent hash
parent: H256,
}
/// Used to identify header by transactions and uncles hashes
#[derive(Eq, PartialEq, Hash)]
struct HeaderId {
transactions_root: H256,
uncles: H256
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
2016-02-03 21:05:04 +01:00
/// Sync state
2015-12-22 22:19:50 +01:00
pub enum SyncState {
/// Initial chain sync has not started yet
NotSynced,
/// Initial chain sync complete. Waiting for new packets
Idle,
2016-01-10 23:37:09 +01:00
/// Block downloading paused. Waiting for block queue to process blocks and free some space
2015-12-22 22:19:50 +01:00
Waiting,
/// Downloading blocks
Blocks,
/// Downloading blocks learned from NewHashes packet
NewBlocks,
}
2016-01-09 18:40:13 +01:00
/// Syncing status and statistics
2016-03-11 10:17:20 +01:00
#[derive(Clone)]
2015-12-22 22:19:50 +01:00
pub struct SyncStatus {
2016-01-09 18:40:13 +01:00
/// State
2015-12-25 14:55:55 +01:00
pub state: SyncState,
2016-01-09 18:40:13 +01:00
/// Syncing protocol version. That's the maximum protocol version we connect to.
2015-12-25 14:55:55 +01:00
pub protocol_version: u8,
/// The underlying p2p network version.
pub network_id: U256,
2016-04-06 10:07:24 +02:00
/// `BlockChain` height for the moment the sync started.
2015-12-25 14:55:55 +01:00
pub start_block_number: BlockNumber,
2016-02-03 21:05:04 +01:00
/// Last fully downloaded and imported block number (if any).
pub last_imported_block_number: Option<BlockNumber>,
/// Highest block number in the download queue (if any).
pub highest_block_number: Option<BlockNumber>,
2016-01-09 18:40:13 +01:00
/// Total number of blocks for the sync process.
2016-02-03 21:05:04 +01:00
pub blocks_total: BlockNumber,
2016-01-09 18:40:13 +01:00
/// Number of blocks downloaded so far.
2016-02-03 21:05:04 +01:00
pub blocks_received: BlockNumber,
2016-01-22 04:54:38 +01:00
/// Total number of connected peers
pub num_peers: usize,
/// Total number of active peers
pub num_active_peers: usize,
2016-02-24 22:37:28 +01:00
/// Heap memory used in bytes
pub mem_used: usize,
2015-12-22 22:19:50 +01:00
}
2016-02-06 18:56:21 +01:00
#[derive(PartialEq, Eq, Debug, Clone)]
2016-01-10 23:37:09 +01:00
/// Peer data type requested
2016-01-08 16:26:00 +01:00
enum PeerAsking {
2015-12-22 22:19:50 +01:00
Nothing,
BlockHeaders,
BlockBodies,
}
2016-02-06 18:56:21 +01:00
#[derive(Clone)]
2016-01-10 23:37:09 +01:00
/// Syncing peer information
2015-12-22 22:19:50 +01:00
struct PeerInfo {
2016-01-10 23:37:09 +01:00
/// eth protocol version
2015-12-22 22:19:50 +01:00
protocol_version: u32,
2016-01-10 23:37:09 +01:00
/// Peer chain genesis hash
2015-12-22 22:19:50 +01:00
genesis: H256,
2016-02-03 21:05:04 +01:00
/// Peer network id
2015-12-22 22:19:50 +01:00
network_id: U256,
2016-01-10 23:37:09 +01:00
/// Peer best block hash
latest_hash: H256,
/// Peer best block number if known
latest_number: Option<BlockNumber>,
2016-01-10 23:37:09 +01:00
/// Peer total difficulty
2015-12-22 22:19:50 +01:00
difficulty: U256,
2016-01-10 23:37:09 +01:00
/// Type of data currenty being requested from peer.
2015-12-22 22:19:50 +01:00
asking: PeerAsking,
2016-01-10 23:37:09 +01:00
/// A set of block numbers being requested
2016-01-08 16:26:00 +01:00
asking_blocks: Vec<BlockNumber>,
2016-02-12 13:07:02 +01:00
/// Holds requested header hash if currently requesting block header by hash
asking_hash: Option<H256>,
2016-02-03 21:42:30 +01:00
/// Request timestamp
ask_time: f64,
2015-12-22 22:19:50 +01:00
}
2016-01-10 23:37:09 +01:00
/// Blockchain sync handler.
/// See module documentation for more details.
2015-12-22 22:19:50 +01:00
pub struct ChainSync {
/// Sync state
state: SyncState,
/// Last block number for the start of sync
starting_block: BlockNumber,
/// Highest block number seen
2016-02-03 21:05:04 +01:00
highest_block: Option<BlockNumber>,
2015-12-22 22:19:50 +01:00
/// Set of block header numbers being downloaded
downloading_headers: HashSet<BlockNumber>,
/// Set of block body numbers being downloaded
downloading_bodies: HashSet<BlockNumber>,
2016-02-12 13:07:02 +01:00
/// Set of block headers being downloaded by hash
downloading_hashes: HashSet<H256>,
2015-12-22 22:19:50 +01:00
/// Downloaded headers.
2016-01-08 16:26:00 +01:00
headers: Vec<(BlockNumber, Vec<Header>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
2015-12-22 22:19:50 +01:00
/// Downloaded bodies
2016-01-10 23:37:09 +01:00
bodies: Vec<(BlockNumber, Vec<Bytes>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
2015-12-22 22:19:50 +01:00
/// Peer info
peers: HashMap<PeerId, PeerInfo>,
/// Used to map body to header
header_ids: HashMap<HeaderId, BlockNumber>,
/// Last impoted block number
2016-02-03 21:05:04 +01:00
last_imported_block: Option<BlockNumber>,
2015-12-27 00:48:03 +01:00
/// Last impoted block hash
2016-02-03 21:05:04 +01:00
last_imported_hash: Option<H256>,
2015-12-22 22:19:50 +01:00
/// Syncing total difficulty
syncing_difficulty: U256,
/// True if common block for our and remote chain has been found
have_common_block: bool,
2016-02-12 13:07:02 +01:00
/// Last propagated block number
last_sent_block_number: BlockNumber,
2016-02-24 21:23:58 +01:00
/// Max blocks to download ahead
max_download_ahead_blocks: usize,
/// Network ID
network_id: U256,
/// Miner
miner: Arc<Miner>,
2015-12-22 22:19:50 +01:00
}
2016-02-04 23:24:36 +01:00
type RlpResponseResult = Result<Option<(PacketId, RlpStream)>, PacketDecodeError>;
2015-12-22 22:19:50 +01:00
impl ChainSync {
2016-01-09 19:13:58 +01:00
/// Create a new instance of syncing strategy.
pub fn new(config: SyncConfig, miner: Arc<Miner>) -> ChainSync {
2015-12-25 14:55:55 +01:00
ChainSync {
2015-12-22 22:19:50 +01:00
state: SyncState::NotSynced,
starting_block: 0,
2016-02-03 21:05:04 +01:00
highest_block: None,
2015-12-22 22:19:50 +01:00
downloading_headers: HashSet::new(),
downloading_bodies: HashSet::new(),
2016-02-12 13:07:02 +01:00
downloading_hashes: HashSet::new(),
2015-12-22 22:19:50 +01:00
headers: Vec::new(),
bodies: Vec::new(),
peers: HashMap::new(),
header_ids: HashMap::new(),
2016-02-03 21:05:04 +01:00
last_imported_block: None,
last_imported_hash: None,
2015-12-22 22:19:50 +01:00
syncing_difficulty: U256::from(0u64),
2016-01-08 16:26:00 +01:00
have_common_block: false,
last_sent_block_number: 0,
2016-02-24 21:23:58 +01:00
max_download_ahead_blocks: max(MAX_HEADERS_TO_REQUEST, config.max_download_ahead_blocks),
network_id: config.network_id,
miner: miner,
2015-12-25 14:55:55 +01:00
}
2015-12-22 22:19:50 +01:00
}
/// @returns Synchonization status
pub fn status(&self) -> SyncStatus {
SyncStatus {
state: self.state.clone(),
protocol_version: 63,
network_id: self.network_id,
2015-12-22 22:19:50 +01:00
start_block_number: self.starting_block,
2016-01-08 16:26:00 +01:00
last_imported_block_number: self.last_imported_block,
2015-12-22 22:19:50 +01:00
highest_block_number: self.highest_block,
2016-02-29 14:59:10 +01:00
blocks_received: match self.last_imported_block { Some(x) if x > self.starting_block => x - self.starting_block, _ => 0 },
blocks_total: match self.highest_block { Some(x) if x > self.starting_block => x - self.starting_block, _ => 0 },
2016-01-22 04:54:38 +01:00
num_peers: self.peers.len(),
num_active_peers: self.peers.values().filter(|p| p.asking != PeerAsking::Nothing).count(),
mem_used:
2016-02-24 22:37:28 +01:00
// TODO: https://github.com/servo/heapsize/pull/50
// self.downloading_hashes.heap_size_of_children()
//+ self.downloading_bodies.heap_size_of_children()
//+ self.downloading_hashes.heap_size_of_children()
self.headers.heap_size_of_children()
+ self.bodies.heap_size_of_children()
+ self.peers.heap_size_of_children()
2016-02-24 22:37:28 +01:00
+ self.header_ids.heap_size_of_children(),
2015-12-22 22:19:50 +01:00
}
}
/// Abort all sync activity
pub fn abort(&mut self, io: &mut SyncIo) {
self.restart(io);
self.peers.clear();
}
2016-03-11 11:16:49 +01:00
#[cfg_attr(feature="dev", allow(for_kv_map))] // Because it's not possible to get `values_mut()`
2016-01-09 18:40:13 +01:00
/// Rest sync. Clear all downloaded data but keep the queue
2015-12-22 22:19:50 +01:00
fn reset(&mut self) {
self.downloading_headers.clear();
self.downloading_bodies.clear();
self.headers.clear();
self.bodies.clear();
2016-01-17 15:56:09 +01:00
for (_, ref mut p) in &mut self.peers {
2015-12-22 22:19:50 +01:00
p.asking_blocks.clear();
2016-02-12 13:07:02 +01:00
p.asking_hash = None;
2015-12-22 22:19:50 +01:00
}
self.header_ids.clear();
self.syncing_difficulty = From::from(0u64);
self.state = SyncState::Idle;
}
/// Restart sync
pub fn restart(&mut self, io: &mut SyncIo) {
self.reset();
self.starting_block = 0;
2016-02-03 21:05:04 +01:00
self.highest_block = None;
2015-12-22 22:19:50 +01:00
self.have_common_block = false;
2016-01-07 20:43:37 +01:00
self.starting_block = io.chain().chain_info().best_block_number;
2015-12-22 22:19:50 +01:00
self.state = SyncState::NotSynced;
}
2016-03-26 12:00:05 +01:00
/// Restart sync after bad block has been detected. May end up re-downloading up to QUEUE_SIZE blocks
pub fn restart_on_bad_block(&mut self, io: &mut SyncIo) {
self.restart(io);
// Do not assume that the block queue/chain still has our last_imported_block
self.last_imported_block = None;
self.last_imported_hash = None;
}
2015-12-22 22:19:50 +01:00
/// Called by peer to report status
2016-01-14 19:03:48 +01:00
fn on_peer_status(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
2016-02-06 21:08:20 +01:00
let peer = PeerInfo {
2016-01-08 17:52:25 +01:00
protocol_version: try!(r.val_at(0)),
network_id: try!(r.val_at(1)),
difficulty: try!(r.val_at(2)),
latest_hash: try!(r.val_at(3)),
latest_number: None,
2016-01-08 17:52:25 +01:00
genesis: try!(r.val_at(4)),
2015-12-22 22:19:50 +01:00
asking: PeerAsking::Nothing,
asking_blocks: Vec::new(),
2016-02-12 13:07:02 +01:00
asking_hash: None,
2016-02-03 21:42:30 +01:00
ask_time: 0f64,
2015-12-22 22:19:50 +01:00
};
2015-12-25 14:55:55 +01:00
trace!(target: "sync", "New peer {} (protocol: {}, network: {:?}, difficulty: {:?}, latest:{}, genesis:{})", peer_id, peer.protocol_version, peer.network_id, peer.difficulty, peer.latest_hash, peer.genesis);
2016-02-03 21:05:04 +01:00
2016-02-12 13:07:02 +01:00
if self.peers.contains_key(&peer_id) {
warn!("Unexpected status packet from {}:{}", peer_id, io.peer_info(peer_id));
return Ok(());
}
2016-01-10 14:11:23 +01:00
let chain_info = io.chain().chain_info();
if peer.genesis != chain_info.genesis_hash {
io.disable_peer(peer_id);
trace!(target: "sync", "Peer {} genesis hash not matched", peer_id);
return Ok(());
}
2016-02-24 21:23:58 +01:00
if peer.network_id != self.network_id {
2016-01-10 14:11:23 +01:00
io.disable_peer(peer_id);
trace!(target: "sync", "Peer {} network id not matched", peer_id);
return Ok(());
}
2015-12-25 14:55:55 +01:00
2016-02-12 13:07:02 +01:00
self.peers.insert(peer_id.clone(), peer);
2016-02-14 01:03:48 +01:00
debug!(target: "sync", "Connected {}:{}", peer_id, io.peer_info(peer_id));
2015-12-22 22:19:50 +01:00
self.sync_peer(io, peer_id, false);
2016-01-08 17:52:25 +01:00
Ok(())
2015-12-22 22:19:50 +01:00
}
2016-03-11 11:16:49 +01:00
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
2015-12-22 22:19:50 +01:00
/// Called by peer once it has new block headers during sync
2016-01-14 19:03:48 +01:00
fn on_peer_block_headers(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
2015-12-27 00:48:03 +01:00
self.reset_peer_asking(peer_id, PeerAsking::BlockHeaders);
2015-12-22 22:19:50 +01:00
let item_count = r.item_count();
2016-01-08 16:26:00 +01:00
trace!(target: "sync", "{} -> BlockHeaders ({} entries)", peer_id, item_count);
2015-12-22 22:19:50 +01:00
self.clear_peer_download(peer_id);
if self.state != SyncState::Blocks && self.state != SyncState::NewBlocks && self.state != SyncState::Waiting {
trace!(target: "sync", "Ignored unexpected block headers");
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-22 22:19:50 +01:00
}
2016-01-08 16:26:00 +01:00
if self.state == SyncState::Waiting {
2015-12-22 22:19:50 +01:00
trace!(target: "sync", "Ignored block headers while waiting");
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-22 22:19:50 +01:00
}
2015-12-24 17:18:47 +01:00
for i in 0..item_count {
2016-01-08 17:52:25 +01:00
let info: BlockHeader = try!(r.val_at(i));
let number = BlockNumber::from(info.number);
if (number <= self.current_base_block() && self.have_common_block) || self.headers.have_item(&number) {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Skipping existing block header");
continue;
}
2016-02-03 21:05:04 +01:00
if self.highest_block == None || number > self.highest_block.unwrap() {
self.highest_block = Some(number);
2015-12-24 17:18:47 +01:00
}
2016-01-08 17:52:25 +01:00
let hash = info.hash();
match io.chain().block_status(BlockId::Hash(hash.clone())) {
BlockStatus::InChain | BlockStatus::Queued => {
if !self.have_common_block || self.current_base_block() < number {
2016-03-11 18:50:29 +01:00
self.last_imported_block = Some(number);
self.last_imported_hash = Some(hash.clone());
2016-03-11 21:23:36 +01:00
}
if !self.have_common_block {
self.have_common_block = true;
2016-03-11 18:50:29 +01:00
trace!(target: "sync", "Found common header {} ({})", number, hash);
} else {
2016-03-29 23:29:43 +02:00
trace!(target: "sync", "Header already in chain {} ({}), restarting", number, hash);
self.restart(io);
self.continue_sync(io);
return Ok(());
2016-03-11 18:50:29 +01:00
}
2015-12-24 17:18:47 +01:00
},
_ => {
if self.have_common_block {
//validate chain
2016-02-03 21:05:04 +01:00
let base_hash = self.last_imported_hash.clone().unwrap();
if self.have_common_block && number == self.current_base_block() + 1 && info.parent_hash != base_hash {
2016-02-04 13:16:31 +01:00
// Part of the forked chain. Restart to find common block again
debug!(target: "sync", "Mismatched block header {} {}, restarting sync", number, hash);
self.restart(io);
return Ok(());
2015-12-27 00:48:03 +01:00
}
2016-01-08 17:52:25 +01:00
if self.headers.find_item(&(number - 1)).map_or(false, |p| p.hash != info.parent_hash) {
2015-12-24 17:18:47 +01:00
// mismatching parent id, delete the previous block and don't add this one
2015-12-28 12:03:05 +01:00
debug!(target: "sync", "Mismatched block header {} {}", number, hash);
2015-12-24 17:18:47 +01:00
self.remove_downloaded_blocks(number - 1);
continue;
}
2015-12-28 12:03:05 +01:00
if self.headers.find_item(&(number + 1)).map_or(false, |p| p.parent != hash) {
2015-12-24 17:18:47 +01:00
// mismatching parent id for the next block, clear following headers
debug!(target: "sync", "Mismatched block header {}", number + 1);
self.remove_downloaded_blocks(number + 1);
}
if self.have_common_block && number < self.current_base_block() + 1 {
// unkown header
debug!(target: "sync", "Old block header {:?} ({}) is unknown, restarting sync", hash, number);
self.restart(io);
return Ok(());
}
2015-12-24 17:18:47 +01:00
}
let hdr = Header {
2016-01-08 17:52:25 +01:00
data: try!(r.at(i)).as_raw().to_vec(),
2016-01-04 13:25:32 +01:00
hash: hash.clone(),
2016-01-08 17:52:25 +01:00
parent: info.parent_hash,
2015-12-24 17:18:47 +01:00
};
self.headers.insert_item(number, hdr);
let header_id = HeaderId {
2016-01-08 17:52:25 +01:00
transactions_root: info.transactions_root,
uncles: info.uncles_hash
2015-12-24 17:18:47 +01:00
};
2015-12-28 12:03:05 +01:00
trace!(target: "sync", "Got header {} ({})", number, hash);
2015-12-24 17:18:47 +01:00
if header_id.transactions_root == rlp::SHA3_NULL_RLP && header_id.uncles == rlp::SHA3_EMPTY_LIST_RLP {
//empty body, just mark as downloaded
let mut body_stream = RlpStream::new_list(2);
2015-12-26 15:47:07 +01:00
body_stream.append_raw(&rlp::NULL_RLP, 1);
2015-12-27 00:48:03 +01:00
body_stream.append_raw(&rlp::EMPTY_LIST_RLP, 1);
2015-12-24 17:18:47 +01:00
self.bodies.insert_item(number, body_stream.out());
}
else {
self.header_ids.insert(header_id, number);
}
}
}
}
2015-12-27 00:48:03 +01:00
self.collect_blocks(io);
self.continue_sync(io);
2016-01-08 17:52:25 +01:00
Ok(())
2015-12-22 22:19:50 +01:00
}
/// Called by peer once it has new block bodies
2016-01-14 19:03:48 +01:00
fn on_peer_block_bodies(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
2016-01-08 17:52:25 +01:00
use util::triehash::ordered_trie_root;
2015-12-27 00:48:03 +01:00
self.reset_peer_asking(peer_id, PeerAsking::BlockBodies);
2015-12-24 17:18:47 +01:00
let item_count = r.item_count();
2016-01-08 16:26:00 +01:00
trace!(target: "sync", "{} -> BlockBodies ({} entries)", peer_id, item_count);
2015-12-24 17:18:47 +01:00
self.clear_peer_download(peer_id);
if self.state != SyncState::Blocks && self.state != SyncState::NewBlocks && self.state != SyncState::Waiting {
trace!(target: "sync", "Ignored unexpected block bodies");
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-24 17:18:47 +01:00
}
2016-02-04 02:11:27 +01:00
if self.state == SyncState::Waiting {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Ignored block bodies while waiting");
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-24 17:18:47 +01:00
}
2016-03-29 23:29:43 +02:00
if item_count == 0 {
trace!(target: "sync", "No bodies returned, restarting");
self.restart(io);
self.continue_sync(io);
return Ok(());
}
2015-12-24 17:18:47 +01:00
for i in 0..item_count {
2016-01-08 17:52:25 +01:00
let body = try!(r.at(i));
let tx = try!(body.at(0));
let tx_root = ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here
let uncles = try!(body.at(1)).as_raw().sha3();
2015-12-24 17:18:47 +01:00
let header_id = HeaderId {
transactions_root: tx_root,
uncles: uncles
};
2016-01-17 15:56:09 +01:00
match self.header_ids.get(&header_id).cloned() {
2015-12-24 17:18:47 +01:00
Some(n) => {
self.header_ids.remove(&header_id);
2016-01-08 16:00:32 +01:00
self.bodies.insert_item(n, body.as_raw().to_vec());
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "Got body {}", n);
2015-12-24 17:18:47 +01:00
}
None => {
2016-02-24 21:23:58 +01:00
trace!(target: "sync", "Ignored unknown/stale block body");
2015-12-24 17:18:47 +01:00
}
}
}
self.collect_blocks(io);
self.continue_sync(io);
2016-01-08 17:52:25 +01:00
Ok(())
2015-12-22 22:19:50 +01:00
}
/// Called by peer once it has new block bodies
2016-03-11 11:16:49 +01:00
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
2016-01-14 19:03:48 +01:00
fn on_peer_new_block(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
2016-01-08 17:52:25 +01:00
let block_rlp = try!(r.at(0));
let header_rlp = try!(block_rlp.at(0));
2016-01-08 16:00:32 +01:00
let h = header_rlp.as_raw().sha3();
2016-01-08 16:26:00 +01:00
trace!(target: "sync", "{} -> NewBlock ({})", peer_id, h);
2016-03-15 01:22:58 +01:00
if !self.have_common_block {
trace!(target: "sync", "NewBlock ignored while seeking");
2016-03-15 18:14:52 +01:00
return Ok(());
2016-03-15 01:22:58 +01:00
}
2016-02-07 22:08:15 +01:00
let header: BlockHeader = try!(header_rlp.as_val());
2016-02-03 21:42:30 +01:00
let mut unknown = false;
2016-02-12 13:07:02 +01:00
{
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
peer.latest_hash = header.hash();
peer.latest_number = Some(header.number());
2016-02-12 13:07:02 +01:00
}
2016-01-10 15:08:57 +01:00
// TODO: Decompose block and add to self.headers and self.bodies instead
if header.number <= From::from(self.current_base_block() + 1) {
2016-01-17 23:07:58 +01:00
match io.chain().import_block(block_rlp.as_raw().to_vec()) {
Err(Error::Import(ImportError::AlreadyInChain)) => {
2016-01-10 15:08:57 +01:00
trace!(target: "sync", "New block already in chain {:?}", h);
},
Err(Error::Import(ImportError::AlreadyQueued)) => {
2016-01-10 15:08:57 +01:00
trace!(target: "sync", "New block already queued {:?}", h);
},
Ok(_) => {
if self.current_base_block() < header.number {
self.last_imported_block = Some(header.number);
2016-03-15 01:22:58 +01:00
self.last_imported_hash = Some(header.hash());
self.remove_downloaded_blocks(header.number);
}
2016-03-15 01:22:58 +01:00
trace!(target: "sync", "New block queued {:?} ({})", h, header.number);
2016-01-10 15:08:57 +01:00
},
Err(Error::Block(BlockError::UnknownParent(p))) => {
2016-02-03 21:42:30 +01:00
unknown = true;
trace!(target: "sync", "New block with unknown parent ({:?}) {:?}", p, h);
2016-02-03 21:42:30 +01:00
},
2016-01-10 23:37:09 +01:00
Err(e) => {
debug!(target: "sync", "Bad new block {:?} : {:?}", h, e);
2016-01-10 15:08:57 +01:00
io.disable_peer(peer_id);
2015-12-24 17:18:47 +01:00
}
2016-01-10 15:08:57 +01:00
};
2016-02-02 12:12:32 +01:00
}
2016-02-03 21:42:30 +01:00
else {
unknown = true;
}
if unknown {
2016-01-10 23:37:09 +01:00
trace!(target: "sync", "New block unknown {:?}", h);
//TODO: handle too many unknown blocks
let difficulty: U256 = try!(r.val_at(1));
2016-02-12 14:20:18 +01:00
let peer_difficulty = self.peers.get_mut(&peer_id).unwrap().difficulty;
2016-01-10 23:37:09 +01:00
if difficulty > peer_difficulty {
trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h);
self.sync_peer(io, peer_id, true);
}
}
2016-01-08 17:52:25 +01:00
Ok(())
2015-12-22 22:19:50 +01:00
}
2016-02-03 21:05:04 +01:00
/// Handles NewHashes packet. Initiates headers download for any unknown hashes.
2016-01-14 19:03:48 +01:00
fn on_peer_new_hashes(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
2016-02-12 14:20:18 +01:00
if self.peers.get_mut(&peer_id).unwrap().asking != PeerAsking::Nothing {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Ignoring new hashes since we're already downloading.");
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-24 17:18:47 +01:00
}
2016-01-08 16:26:00 +01:00
trace!(target: "sync", "{} -> NewHashes ({} entries)", peer_id, r.item_count());
2016-02-12 13:07:02 +01:00
let hashes = r.iter().map(|item| (item.val_at::<H256>(0), item.val_at::<BlockNumber>(1)));
let mut max_height: BlockNumber = 0;
2016-01-08 17:52:25 +01:00
for (rh, rd) in hashes {
let h = try!(rh);
let d = try!(rd);
2016-02-12 13:07:02 +01:00
if self.downloading_hashes.contains(&h) {
continue;
}
match io.chain().block_status(BlockId::Hash(h.clone())) {
2015-12-24 17:18:47 +01:00
BlockStatus::InChain => {
trace!(target: "sync", "New block hash already in chain {:?}", h);
},
2016-01-10 23:37:09 +01:00
BlockStatus::Queued => {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "New hash block already queued {:?}", h);
},
BlockStatus::Unknown => {
if d > max_height {
2016-02-12 13:07:02 +01:00
trace!(target: "sync", "New unknown block hash {:?}", h);
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
peer.latest_hash = h.clone();
peer.latest_number = Some(d);
2015-12-24 17:18:47 +01:00
max_height = d;
}
},
BlockStatus::Bad =>{
debug!(target: "sync", "Bad new block hash {:?}", h);
io.disable_peer(peer_id);
2016-01-08 17:52:25 +01:00
return Ok(());
2015-12-24 17:18:47 +01:00
}
}
2016-01-08 17:52:25 +01:00
};
2016-02-12 13:07:02 +01:00
if max_height != 0 {
2016-01-23 18:44:45 +01:00
self.sync_peer(io, peer_id, true);
}
2016-01-08 17:52:25 +01:00
Ok(())
2015-12-22 22:19:50 +01:00
}
/// Called by peer when it is disconnecting
2016-01-14 19:03:48 +01:00
pub fn on_peer_aborting(&mut self, io: &mut SyncIo, peer: PeerId) {
trace!(target: "sync", "== Disconnecting {}", peer);
2016-01-10 14:11:23 +01:00
if self.peers.contains_key(&peer) {
2016-02-14 01:03:48 +01:00
debug!(target: "sync", "Disconnected {}", peer);
2016-01-10 14:11:23 +01:00
self.clear_peer_download(peer);
2016-01-15 12:26:04 +01:00
self.peers.remove(&peer);
2016-01-10 14:11:23 +01:00
self.continue_sync(io);
}
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Called when a new peer is connected
2016-01-14 19:03:48 +01:00
pub fn on_peer_connected(&mut self, io: &mut SyncIo, peer: PeerId) {
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "== Connected {}", peer);
if let Err(e) = self.send_status(io) {
2016-03-13 10:33:55 +01:00
debug!(target:"sync", "Error sending status request: {:?}", e);
io.disable_peer(peer);
}
2015-12-22 22:19:50 +01:00
}
2015-12-27 02:27:15 +01:00
/// Resume downloading
2015-12-24 17:18:47 +01:00
fn continue_sync(&mut self, io: &mut SyncIo) {
2015-12-27 02:27:15 +01:00
let mut peers: Vec<(PeerId, U256)> = self.peers.iter().map(|(k, p)| (*k, p.difficulty)).collect();
peers.sort_by(|&(_, d1), &(_, d2)| d1.cmp(&d2).reverse()); //TODO: sort by rating
for (p, _) in peers {
2016-01-14 19:03:48 +01:00
self.sync_peer(io, p, false);
2015-12-24 17:18:47 +01:00
}
2015-12-22 22:19:50 +01:00
}
/// Called after all blocks have been donloaded
fn complete_sync(&mut self) {
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "Sync complete");
2015-12-24 17:18:47 +01:00
self.reset();
self.state = SyncState::Idle;
2015-12-22 22:19:50 +01:00
}
/// Enter waiting state
fn pause_sync(&mut self) {
2015-12-25 14:55:55 +01:00
trace!(target: "sync", "Block queue full, pausing sync");
2015-12-24 17:18:47 +01:00
self.state = SyncState::Waiting;
2015-12-22 22:19:50 +01:00
}
2016-01-09 18:40:13 +01:00
/// Find something to do for a peer. Called for a new peer or when a peer is done with it's task.
2016-01-14 19:03:48 +01:00
fn sync_peer(&mut self, io: &mut SyncIo, peer_id: PeerId, force: bool) {
2015-12-22 22:19:50 +01:00
let (peer_latest, peer_difficulty) = {
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
2015-12-27 00:48:03 +01:00
if peer.asking != PeerAsking::Nothing {
2015-12-22 22:19:50 +01:00
return;
}
2015-12-27 00:48:03 +01:00
if self.state == SyncState::Waiting {
2015-12-25 14:55:55 +01:00
trace!(target: "sync", "Waiting for block queue");
2015-12-22 22:19:50 +01:00
return;
}
(peer.latest_hash.clone(), peer.difficulty.clone())
2015-12-22 22:19:50 +01:00
};
2016-01-07 20:43:37 +01:00
let td = io.chain().chain_info().pending_total_difficulty;
2015-12-22 22:19:50 +01:00
let syncing_difficulty = max(self.syncing_difficulty, td);
if force || peer_difficulty > syncing_difficulty {
// start sync
self.syncing_difficulty = peer_difficulty;
if self.state == SyncState::Idle || self.state == SyncState::NotSynced {
self.state = SyncState::Blocks;
}
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "Starting sync with better chain");
2016-02-12 14:20:18 +01:00
self.peers.get_mut(&peer_id).unwrap().asking_hash = Some(peer_latest.clone());
2016-02-12 13:07:02 +01:00
self.downloading_hashes.insert(peer_latest.clone());
2015-12-22 22:19:50 +01:00
self.request_headers_by_hash(io, peer_id, &peer_latest, 1, 0, false);
}
2016-02-11 21:10:41 +01:00
else if self.state == SyncState::Blocks && io.chain().block_status(BlockId::Hash(peer_latest)) == BlockStatus::Unknown {
2016-02-24 21:23:58 +01:00
self.request_blocks(io, peer_id, false);
2015-12-22 22:19:50 +01:00
}
}
2016-02-03 21:05:04 +01:00
fn current_base_block(&self) -> BlockNumber {
match self.last_imported_block { None => 0, Some(x) => x }
}
2016-03-01 22:03:29 +01:00
fn find_block_bodies_hashes_to_request(&self, ignore_others: bool) -> (Vec<H256>, Vec<BlockNumber>) {
2015-12-22 22:19:50 +01:00
let mut needed_bodies: Vec<H256> = Vec::new();
let mut needed_numbers: Vec<BlockNumber> = Vec::new();
2015-12-25 14:55:55 +01:00
2016-02-03 21:05:04 +01:00
if self.have_common_block && !self.headers.is_empty() && self.headers.range_iter().next().unwrap().0 == self.current_base_block() + 1 {
if let Some((start, ref items)) = self.headers.range_iter().next() {
2015-12-25 14:55:55 +01:00
let mut index: BlockNumber = 0;
while index != items.len() as BlockNumber && needed_bodies.len() < MAX_BODIES_TO_REQUEST {
let block = start + index;
2016-02-24 21:23:58 +01:00
if ignore_others || (!self.downloading_bodies.contains(&block) && !self.bodies.have_item(&block)) {
2015-12-25 14:55:55 +01:00
needed_bodies.push(items[index as usize].hash.clone());
needed_numbers.push(block);
}
index += 1;
2015-12-22 22:19:50 +01:00
}
}
}
2016-03-01 22:03:29 +01:00
(needed_bodies, needed_numbers)
}
/// Find some headers or blocks to download for a peer.
fn request_blocks(&mut self, io: &mut SyncIo, peer_id: PeerId, ignore_others: bool) {
self.clear_peer_download(peer_id);
if io.chain().queue_info().is_full() {
self.pause_sync();
return;
}
// check to see if we need to download any block bodies first
let (needed_bodies, needed_numbers) = self.find_block_bodies_hashes_to_request(ignore_others);
2015-12-22 22:19:50 +01:00
if !needed_bodies.is_empty() {
2016-02-24 21:23:58 +01:00
let (head, _) = self.headers.range_iter().next().unwrap();
if needed_numbers.first().unwrap() - head > self.max_download_ahead_blocks as BlockNumber {
trace!(target: "sync", "{}: Stalled download ({} vs {}), helping with downloading block bodies", peer_id, needed_numbers.first().unwrap(), head);
self.request_blocks(io, peer_id, true);
2016-03-01 22:03:29 +01:00
} else {
self.downloading_bodies.extend(needed_numbers.iter());
replace(&mut self.peers.get_mut(&peer_id).unwrap().asking_blocks, needed_numbers);
self.request_bodies(io, peer_id, needed_bodies);
2016-02-24 21:23:58 +01:00
}
2016-03-01 22:03:29 +01:00
return;
2015-12-22 22:19:50 +01:00
}
2016-03-01 22:03:29 +01:00
// check if need to download headers
let mut start = 0;
if !self.have_common_block {
// download backwards until common block is found 1 header at a time
let chain_info = io.chain().chain_info();
start = match self.last_imported_block {
Some(n) => n,
None => chain_info.best_block_number,
};
2016-03-01 22:03:29 +01:00
if !self.headers.is_empty() {
start = min(start, self.headers.range_iter().next().unwrap().0 - 1);
2015-12-22 22:19:50 +01:00
}
2016-03-01 22:03:29 +01:00
if start == 0 {
self.have_common_block = true; //reached genesis
self.last_imported_hash = Some(chain_info.genesis_hash);
self.last_imported_block = Some(0);
}
}
if self.have_common_block {
let mut headers: Vec<BlockNumber> = Vec::new();
let mut prev = self.current_base_block() + 1;
let head = self.headers.range_iter().next().map(|(h, _)| h);
for (next, ref items) in self.headers.range_iter() {
if !headers.is_empty() {
break;
}
if next <= prev {
2015-12-27 00:48:03 +01:00
prev = next + items.len() as BlockNumber;
2016-03-01 22:03:29 +01:00
continue;
2015-12-22 22:19:50 +01:00
}
2016-03-01 22:03:29 +01:00
let mut block = prev;
while block < next && headers.len() < MAX_HEADERS_TO_REQUEST {
if ignore_others || !self.downloading_headers.contains(&(block as BlockNumber)) {
headers.push(block as BlockNumber);
2016-02-24 21:23:58 +01:00
}
2016-03-01 22:03:29 +01:00
block += 1;
2015-12-22 22:19:50 +01:00
}
2016-03-01 22:03:29 +01:00
prev = next + items.len() as BlockNumber;
2015-12-22 22:19:50 +01:00
}
2016-03-01 22:03:29 +01:00
if !headers.is_empty() {
start = headers[0];
if head.is_some() && start > head.unwrap() && start - head.unwrap() > self.max_download_ahead_blocks as BlockNumber {
trace!(target: "sync", "{}: Stalled download ({} vs {}), helping with downloading headers", peer_id, start, head.unwrap());
self.request_blocks(io, peer_id, true);
return;
}
let count = headers.len();
self.downloading_headers.extend(headers.iter());
replace(&mut self.peers.get_mut(&peer_id).unwrap().asking_blocks, headers);
assert!(!self.headers.have_item(&start));
self.request_headers_by_number(io, peer_id, start, count, 0, false);
2015-12-22 22:19:50 +01:00
}
}
2016-03-01 22:03:29 +01:00
else {
// continue search for common block
self.downloading_headers.insert(start);
self.request_headers_by_number(io, peer_id, start, 1, 0, false);
}
2015-12-22 22:19:50 +01:00
}
2016-01-09 18:40:13 +01:00
/// Clear all blocks/headers marked as being downloaded by a peer.
2016-01-14 19:03:48 +01:00
fn clear_peer_download(&mut self, peer_id: PeerId) {
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
2016-02-12 13:07:02 +01:00
if let Some(hash) = peer.asking_hash.take() {
self.downloading_hashes.remove(&hash);
}
2015-12-24 17:18:47 +01:00
for b in &peer.asking_blocks {
self.downloading_headers.remove(&b);
self.downloading_bodies.remove(&b);
}
peer.asking_blocks.clear();
2015-12-22 22:19:50 +01:00
}
2015-12-24 17:18:47 +01:00
2016-01-09 18:40:13 +01:00
/// Checks if there are blocks fully downloaded that can be imported into the blockchain and does the import.
2015-12-24 17:18:47 +01:00
fn collect_blocks(&mut self, io: &mut SyncIo) {
if !self.have_common_block || self.headers.is_empty() || self.bodies.is_empty() {
return;
}
let mut restart = false;
// merge headers and bodies
{
2015-12-25 14:55:55 +01:00
let headers = self.headers.range_iter().next().unwrap();
let bodies = self.bodies.range_iter().next().unwrap();
2016-03-15 01:22:58 +01:00
if headers.0 != bodies.0 || headers.0 > self.current_base_block() + 1 {
2015-12-24 17:18:47 +01:00
return;
}
2015-12-27 00:48:03 +01:00
let count = min(headers.1.len(), bodies.1.len());
let mut imported = 0;
for i in 0..count {
2015-12-24 17:18:47 +01:00
let mut block_rlp = RlpStream::new_list(3);
block_rlp.append_raw(&headers.1[i].data, 1);
2015-12-27 00:48:03 +01:00
let body = Rlp::new(&bodies.1[i]);
2016-01-08 16:00:32 +01:00
block_rlp.append_raw(body.at(0).as_raw(), 1);
block_rlp.append_raw(body.at(1).as_raw(), 1);
2015-12-24 17:18:47 +01:00
let h = &headers.1[i].hash;
2016-02-03 21:42:30 +01:00
// Perform basic block verification
if !Block::is_good(block_rlp.as_raw()) {
debug!(target: "sync", "Bad block rlp {:?} : {:?}", h, block_rlp.as_raw());
restart = true;
break;
}
2016-01-17 23:07:58 +01:00
match io.chain().import_block(block_rlp.out()) {
Err(Error::Import(ImportError::AlreadyInChain)) => {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Block already in chain {:?}", h);
2016-02-03 21:05:04 +01:00
self.last_imported_block = Some(headers.0 + i as BlockNumber);
self.last_imported_hash = Some(h.clone());
2015-12-24 17:18:47 +01:00
},
Err(Error::Import(ImportError::AlreadyQueued)) => {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Block already queued {:?}", h);
2016-02-03 21:05:04 +01:00
self.last_imported_block = Some(headers.0 + i as BlockNumber);
self.last_imported_hash = Some(h.clone());
2015-12-24 17:18:47 +01:00
},
Ok(_) => {
2015-12-24 17:18:47 +01:00
trace!(target: "sync", "Block queued {:?}", h);
2016-02-03 21:05:04 +01:00
self.last_imported_block = Some(headers.0 + i as BlockNumber);
self.last_imported_hash = Some(h.clone());
2015-12-27 00:48:03 +01:00
imported += 1;
2015-12-24 17:18:47 +01:00
},
2016-01-10 23:37:09 +01:00
Err(e) => {
debug!(target: "sync", "Bad block {:?} : {:?}", h, e);
2015-12-24 17:18:47 +01:00
restart = true;
}
}
}
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "Imported {} of {}", imported, count);
2015-12-24 17:18:47 +01:00
}
if restart {
2016-03-26 12:00:05 +01:00
self.restart_on_bad_block(io);
2015-12-24 17:18:47 +01:00
return;
}
2016-02-03 21:05:04 +01:00
self.headers.remove_head(&(self.last_imported_block.unwrap() + 1));
self.bodies.remove_head(&(self.last_imported_block.unwrap() + 1));
2015-12-24 17:18:47 +01:00
if self.headers.is_empty() {
assert!(self.bodies.is_empty());
self.complete_sync();
}
2015-12-22 22:19:50 +01:00
}
2015-12-24 17:18:47 +01:00
2016-02-03 21:05:04 +01:00
/// Remove downloaded bocks/headers starting from specified number.
2016-01-09 18:40:13 +01:00
/// Used to recover from an error and re-download parts of the chain detected as bad.
2015-12-24 17:18:47 +01:00
fn remove_downloaded_blocks(&mut self, start: BlockNumber) {
2016-03-11 21:23:36 +01:00
let ids = self.header_ids.drain().filter(|&(_, v)| v < start).collect();
self.header_ids = ids;
let hdrs = self.downloading_headers.drain().filter(|v| *v < start).collect();
self.downloading_headers = hdrs;
let bodies = self.downloading_bodies.drain().filter(|v| *v < start).collect();
self.downloading_bodies = bodies;
2016-03-09 23:55:56 +01:00
self.headers.remove_from(&start);
self.bodies.remove_from(&start);
2015-12-22 22:19:50 +01:00
}
2016-01-09 18:40:13 +01:00
/// Request headers from a peer by block hash
2016-01-14 19:03:48 +01:00
fn request_headers_by_hash(&mut self, sync: &mut SyncIo, peer_id: PeerId, h: &H256, count: usize, skip: usize, reverse: bool) {
2016-01-09 18:40:13 +01:00
trace!(target: "sync", "{} <- GetBlockHeaders: {} entries starting from {}", peer_id, count, h);
2015-12-22 22:19:50 +01:00
let mut rlp = RlpStream::new_list(4);
rlp.append(h);
rlp.append(&count);
rlp.append(&skip);
rlp.append(&if reverse {1u32} else {0u32});
self.send_request(sync, peer_id, PeerAsking::BlockHeaders, GET_BLOCK_HEADERS_PACKET, rlp.out());
}
2016-01-09 18:40:13 +01:00
/// Request headers from a peer by block number
2016-01-14 19:03:48 +01:00
fn request_headers_by_number(&mut self, sync: &mut SyncIo, peer_id: PeerId, n: BlockNumber, count: usize, skip: usize, reverse: bool) {
2015-12-22 22:19:50 +01:00
let mut rlp = RlpStream::new_list(4);
2016-01-09 18:40:13 +01:00
trace!(target: "sync", "{} <- GetBlockHeaders: {} entries starting from {}", peer_id, count, n);
2015-12-22 22:19:50 +01:00
rlp.append(&n);
rlp.append(&count);
rlp.append(&skip);
rlp.append(&if reverse {1u32} else {0u32});
self.send_request(sync, peer_id, PeerAsking::BlockHeaders, GET_BLOCK_HEADERS_PACKET, rlp.out());
}
2016-01-09 18:40:13 +01:00
/// Request block bodies from a peer
2016-01-14 19:03:48 +01:00
fn request_bodies(&mut self, sync: &mut SyncIo, peer_id: PeerId, hashes: Vec<H256>) {
2015-12-22 22:19:50 +01:00
let mut rlp = RlpStream::new_list(hashes.len());
2016-01-09 18:40:13 +01:00
trace!(target: "sync", "{} <- GetBlockBodies: {} entries", peer_id, hashes.len());
2015-12-22 22:19:50 +01:00
for h in hashes {
rlp.append(&h);
}
2015-12-25 14:55:55 +01:00
self.send_request(sync, peer_id, PeerAsking::BlockBodies, GET_BLOCK_BODIES_PACKET, rlp.out());
2015-12-22 22:19:50 +01:00
}
2016-01-09 18:40:13 +01:00
/// Reset peer status after request is complete.
2016-01-14 19:03:48 +01:00
fn reset_peer_asking(&mut self, peer_id: PeerId, asking: PeerAsking) {
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
2015-12-27 00:48:03 +01:00
if peer.asking != asking {
warn!(target:"sync", "Asking {:?} while expected {:?}", peer.asking, asking);
}
else {
peer.asking = PeerAsking::Nothing;
}
}
2016-01-09 18:40:13 +01:00
/// Generic request sender
2016-01-14 19:03:48 +01:00
fn send_request(&mut self, sync: &mut SyncIo, peer_id: PeerId, asking: PeerAsking, packet_id: PacketId, packet: Bytes) {
2015-12-22 22:19:50 +01:00
{
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
2015-12-22 22:19:50 +01:00
if peer.asking != PeerAsking::Nothing {
2016-02-12 13:07:02 +01:00
warn!(target:"sync", "Asking {:?} while requesting {:?}", peer.asking, asking);
2015-12-22 22:19:50 +01:00
}
}
2016-01-14 19:03:48 +01:00
match sync.send(peer_id, packet_id, packet) {
2015-12-22 22:19:50 +01:00
Err(e) => {
debug!(target:"sync", "Error sending request: {:?}", e);
2015-12-22 22:19:50 +01:00
sync.disable_peer(peer_id);
}
Ok(_) => {
let mut peer = self.peers.get_mut(&peer_id).unwrap();
peer.asking = asking;
2016-02-03 21:42:30 +01:00
peer.ask_time = time::precise_time_s();
2015-12-22 22:19:50 +01:00
}
}
}
2015-12-24 17:18:47 +01:00
2016-02-12 13:07:02 +01:00
/// Generic packet sender
fn send_packet(&mut self, sync: &mut SyncIo, peer_id: PeerId, packet_id: PacketId, packet: Bytes) {
if let Err(e) = sync.send(peer_id, packet_id, packet) {
debug!(target:"sync", "Error sending packet: {:?}", e);
2016-02-12 13:07:02 +01:00
sync.disable_peer(peer_id);
}
}
2016-01-09 18:40:13 +01:00
/// Called when peer sends us new transactions
fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
// accepting transactions once only fully synced
2016-03-17 14:56:19 +01:00
if !io.is_chain_queue_empty() {
return Ok(());
}
let item_count = r.item_count();
trace!(target: "sync", "{} -> Transactions ({} entries)", peer_id, item_count);
2016-03-06 11:04:13 +01:00
let mut transactions = Vec::with_capacity(item_count);
for i in 0..item_count {
let tx: SignedTransaction = try!(r.val_at(i));
transactions.push(tx);
}
let chain = io.chain();
let fetch_account = |a: &Address| AccountDetails {
nonce: chain.nonce(a),
2016-03-17 11:49:56 +01:00
balance: chain.balance(a),
};
let _ = self.miner.import_transactions(transactions, fetch_account);
Ok(())
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Send Status message
fn send_status(&mut self, io: &mut SyncIo) -> Result<(), UtilError> {
2015-12-24 17:18:47 +01:00
let mut packet = RlpStream::new_list(5);
2016-01-07 20:43:37 +01:00
let chain = io.chain().chain_info();
2015-12-24 17:18:47 +01:00
packet.append(&(PROTOCOL_VERSION as u32));
2016-02-24 21:23:58 +01:00
packet.append(&self.network_id);
2015-12-24 17:18:47 +01:00
packet.append(&chain.total_difficulty);
2016-01-07 16:08:12 +01:00
packet.append(&chain.best_block_hash);
2015-12-24 17:18:47 +01:00
packet.append(&chain.genesis_hash);
io.respond(STATUS_PACKET, packet.out())
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Respond to GetBlockHeaders request
2016-02-04 23:24:36 +01:00
fn return_block_headers(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
2015-12-24 17:18:47 +01:00
// Packet layout:
// [ block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ]
2016-01-08 17:52:25 +01:00
let max_headers: usize = try!(r.val_at(1));
let skip: usize = try!(r.val_at(2));
let reverse: bool = try!(r.val_at(3));
2016-01-07 20:43:37 +01:00
let last = io.chain().chain_info().best_block_number;
2016-01-08 17:52:25 +01:00
let mut number = if try!(r.at(0)).size() == 32 {
2015-12-24 17:18:47 +01:00
// id is a hash
2016-01-08 17:52:25 +01:00
let hash: H256 = try!(r.val_at(0));
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "-> GetBlockHeaders (hash: {}, max: {}, skip: {}, reverse:{})", hash, max_headers, skip, reverse);
match io.chain().block_header(BlockId::Hash(hash)) {
2015-12-28 12:03:05 +01:00
Some(hdr) => From::from(HeaderView::new(&hdr).number()),
2015-12-24 17:18:47 +01:00
None => last
}
}
else {
2016-01-08 17:52:25 +01:00
trace!(target: "sync", "-> GetBlockHeaders (number: {}, max: {}, skip: {}, reverse:{})", try!(r.val_at::<BlockNumber>(0)), max_headers, skip, reverse);
try!(r.val_at(0))
2015-12-24 17:18:47 +01:00
};
2015-12-27 00:48:03 +01:00
if reverse {
number = min(last, number);
} else {
number = max(0, number);
2015-12-27 00:48:03 +01:00
}
2015-12-24 17:18:47 +01:00
let max_count = min(MAX_HEADERS_TO_SEND, max_headers);
let mut count = 0;
let mut data = Bytes::new();
2016-01-08 16:26:00 +01:00
let inc = (skip + 1) as BlockNumber;
2016-02-24 13:01:45 +01:00
while number <= last && count < max_count {
if let Some(mut hdr) = io.chain().block_header(BlockId::Number(number)) {
2016-01-17 15:56:09 +01:00
data.append(&mut hdr);
count += 1;
2015-12-24 17:18:47 +01:00
}
2015-12-25 14:55:55 +01:00
if reverse {
2016-02-24 13:01:45 +01:00
if number <= inc || number == 0 {
2016-01-08 16:26:00 +01:00
break;
}
number -= inc;
2015-12-25 14:55:55 +01:00
}
else {
2016-01-08 16:26:00 +01:00
number += inc;
2015-12-25 14:55:55 +01:00
}
2015-12-24 17:18:47 +01:00
}
let mut rlp = RlpStream::new_list(count as usize);
rlp.append_raw(&data, count as usize);
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "-> GetBlockHeaders: returned {} entries", count);
2016-02-04 23:24:36 +01:00
Ok(Some((BLOCK_HEADERS_PACKET, rlp)))
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Respond to GetBlockBodies request
2016-02-04 23:24:36 +01:00
fn return_block_bodies(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
2015-12-24 17:18:47 +01:00
let mut count = r.item_count();
if count == 0 {
debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
2016-02-04 23:24:36 +01:00
return Ok(None);
2015-12-24 17:18:47 +01:00
}
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "-> GetBlockBodies: {} entries", count);
2015-12-24 17:18:47 +01:00
count = min(count, MAX_BODIES_TO_SEND);
let mut added = 0usize;
let mut data = Bytes::new();
for i in 0..count {
if let Some(mut hdr) = io.chain().block_body(BlockId::Hash(try!(r.val_at::<H256>(i)))) {
2016-01-19 11:10:38 +01:00
data.append(&mut hdr);
added += 1;
2015-12-24 17:18:47 +01:00
}
}
let mut rlp = RlpStream::new_list(added);
rlp.append_raw(&data, added);
2015-12-27 00:48:03 +01:00
trace!(target: "sync", "-> GetBlockBodies: returned {} entries", added);
2016-02-04 23:24:36 +01:00
Ok(Some((BLOCK_BODIES_PACKET, rlp)))
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Respond to GetNodeData request
2016-02-04 23:24:36 +01:00
fn return_node_data(io: &SyncIo, r: &UntrustedRlp) -> RlpResponseResult {
2015-12-24 17:18:47 +01:00
let mut count = r.item_count();
if count == 0 {
debug!(target: "sync", "Empty GetNodeData request, ignoring.");
2016-02-04 23:24:36 +01:00
return Ok(None);
2015-12-24 17:18:47 +01:00
}
count = min(count, MAX_NODE_DATA_TO_SEND);
let mut added = 0usize;
let mut data = Bytes::new();
for i in 0..count {
2016-01-19 11:10:38 +01:00
if let Some(mut hdr) = io.chain().state_data(&try!(r.val_at::<H256>(i))) {
data.append(&mut hdr);
added += 1;
2015-12-24 17:18:47 +01:00
}
}
let mut rlp = RlpStream::new_list(added);
rlp.append_raw(&data, added);
2016-02-04 23:24:36 +01:00
Ok(Some((NODE_DATA_PACKET, rlp)))
2015-12-24 17:18:47 +01:00
}
2016-02-04 23:24:36 +01:00
fn return_receipts(io: &SyncIo, rlp: &UntrustedRlp) -> RlpResponseResult {
let mut count = rlp.item_count();
2015-12-24 17:18:47 +01:00
if count == 0 {
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
2016-02-04 23:24:36 +01:00
return Ok(None);
2015-12-24 17:18:47 +01:00
}
count = min(count, MAX_RECEIPTS_HEADERS_TO_SEND);
let mut added_headers = 0usize;
let mut added_receipts = 0usize;
2015-12-24 17:18:47 +01:00
let mut data = Bytes::new();
for i in 0..count {
if let Some(mut receipts_bytes) = io.chain().block_receipts(&try!(rlp.val_at::<H256>(i))) {
data.append(&mut receipts_bytes);
added_receipts += receipts_bytes.len();
added_headers += 1;
if added_receipts > MAX_RECEIPTS_TO_SEND { break; }
2015-12-24 17:18:47 +01:00
}
}
let mut rlp_result = RlpStream::new_list(added_headers);
rlp_result.append_raw(&data, added_headers);
2016-02-04 23:24:36 +01:00
Ok(Some((RECEIPTS_PACKET, rlp_result)))
}
fn return_rlp<FRlp, FError>(&self, io: &mut SyncIo, rlp: &UntrustedRlp, rlp_func: FRlp, error_func: FError) -> Result<(), PacketDecodeError>
where FRlp : Fn(&SyncIo, &UntrustedRlp) -> RlpResponseResult,
FError : FnOnce(UtilError) -> String
{
let response = rlp_func(io, rlp);
match response {
Err(e) => Err(e),
Ok(Some((packet_id, rlp_stream))) => {
io.respond(packet_id, rlp_stream.out()).unwrap_or_else(
|e| debug!(target: "sync", "{:?}", error_func(e)));
Ok(())
}
_ => Ok(())
}
2015-12-24 17:18:47 +01:00
}
2016-01-09 18:40:13 +01:00
/// Dispatch incoming requests and responses
2016-01-14 19:03:48 +01:00
pub fn on_packet(&mut self, io: &mut SyncIo, peer: PeerId, packet_id: u8, data: &[u8]) {
2016-01-08 17:52:25 +01:00
let rlp = UntrustedRlp::new(data);
2016-02-12 14:20:18 +01:00
if packet_id != STATUS_PACKET && !self.peers.contains_key(&peer) {
2016-03-13 10:33:55 +01:00
debug!(target:"sync", "Unexpected packet from unregistered peer: {}:{}", peer, io.peer_info(peer));
2016-02-12 14:20:18 +01:00
return;
}
2016-01-08 17:52:25 +01:00
let result = match packet_id {
2015-12-24 17:18:47 +01:00
STATUS_PACKET => self.on_peer_status(io, peer, &rlp),
TRANSACTIONS_PACKET => self.on_peer_transactions(io, peer, &rlp),
BLOCK_HEADERS_PACKET => self.on_peer_block_headers(io, peer, &rlp),
BLOCK_BODIES_PACKET => self.on_peer_block_bodies(io, peer, &rlp),
NEW_BLOCK_PACKET => self.on_peer_new_block(io, peer, &rlp),
NEW_BLOCK_HASHES_PACKET => self.on_peer_new_hashes(io, peer, &rlp),
2016-02-04 23:24:36 +01:00
GET_BLOCK_BODIES_PACKET => self.return_rlp(io, &rlp,
ChainSync::return_block_bodies,
|e| format!("Error sending block bodies: {:?}", e)),
GET_BLOCK_HEADERS_PACKET => self.return_rlp(io, &rlp,
ChainSync::return_block_headers,
|e| format!("Error sending block headers: {:?}", e)),
GET_RECEIPTS_PACKET => self.return_rlp(io, &rlp,
2016-02-05 01:01:50 +01:00
ChainSync::return_receipts,
|e| format!("Error sending receipts: {:?}", e)),
2016-02-04 23:24:36 +01:00
GET_NODE_DATA_PACKET => self.return_rlp(io, &rlp,
2016-02-05 01:01:50 +01:00
ChainSync::return_node_data,
|e| format!("Error sending nodes: {:?}", e)),
2016-02-15 00:51:50 +01:00
2016-02-03 21:05:04 +01:00
_ => {
2016-01-08 17:52:25 +01:00
debug!(target: "sync", "Unknown packet {}", packet_id);
Ok(())
}
};
result.unwrap_or_else(|e| {
debug!(target:"sync", "{} -> Malformed packet {} : {}", peer, packet_id, e);
})
2015-12-24 17:18:47 +01:00
}
2015-12-22 22:19:50 +01:00
2016-02-03 21:42:30 +01:00
pub fn maintain_peers(&self, io: &mut SyncIo) {
let tick = time::precise_time_s();
for (peer_id, peer) in &self.peers {
if peer.asking != PeerAsking::Nothing && (tick - peer.ask_time) > CONNECTION_TIMEOUT_SEC {
io.disconnect_peer(*peer_id);
}
}
}
2016-02-05 18:34:08 +01:00
fn check_resume(&mut self, io: &mut SyncIo) {
if !io.chain().queue_info().is_full() && self.state == SyncState::Waiting {
2016-02-11 21:10:41 +01:00
self.state = SyncState::Blocks;
2016-02-04 02:28:16 +01:00
self.continue_sync(io);
}
2015-12-22 22:19:50 +01:00
}
2016-02-05 18:34:08 +01:00
2016-02-07 22:52:56 +01:00
/// creates rlp to send for the tree defined by 'from' and 'to' hashes
2016-02-05 18:34:08 +01:00
fn create_new_hashes_rlp(chain: &BlockChainClient, from: &H256, to: &H256) -> Option<Bytes> {
match chain.tree_route(from, to) {
Some(route) => {
match route.blocks.len() {
0 => None,
_ => {
2016-02-07 23:39:02 +01:00
let mut rlp_stream = RlpStream::new_list(route.blocks.len());
for block_hash in route.blocks {
let mut hash_rlp = RlpStream::new_list(2);
let difficulty = chain.block_total_difficulty(BlockId::Hash(block_hash.clone())).expect("Mallformed block without a difficulty on the chain!");
2016-02-07 23:39:02 +01:00
hash_rlp.append(&block_hash);
hash_rlp.append(&difficulty);
rlp_stream.append_raw(&hash_rlp.out(), 1);
}
Some(rlp_stream.out())
2016-02-05 18:34:08 +01:00
}
}
},
None => None
}
}
2016-02-07 22:52:56 +01:00
/// creates latest block rlp for the given client
2016-02-06 21:00:52 +01:00
fn create_latest_block_rlp(chain: &BlockChainClient) -> Bytes {
2016-02-07 23:39:02 +01:00
let mut rlp_stream = RlpStream::new_list(2);
2016-02-12 14:20:18 +01:00
rlp_stream.append_raw(&chain.block(BlockId::Hash(chain.chain_info().best_block_hash)).unwrap(), 1);
2016-02-07 23:39:02 +01:00
rlp_stream.append(&chain.chain_info().total_difficulty);
rlp_stream.out()
2016-02-06 21:00:52 +01:00
}
2016-02-07 22:52:56 +01:00
/// returns peer ids that have less blocks than our chain
fn get_lagging_peers(&mut self, chain_info: &BlockChainInfo, io: &SyncIo) -> Vec<(PeerId, BlockNumber)> {
2016-02-05 23:45:25 +01:00
let latest_hash = chain_info.best_block_hash;
2016-02-06 18:56:21 +01:00
let latest_number = chain_info.best_block_number;
self.peers.iter_mut().filter_map(|(&id, ref mut peer_info)|
match io.chain().block_status(BlockId::Hash(peer_info.latest_hash.clone())) {
2016-02-06 20:16:59 +01:00
BlockStatus::InChain => {
if peer_info.latest_number.is_none() {
peer_info.latest_number = Some(HeaderView::new(&io.chain().block_header(BlockId::Hash(peer_info.latest_hash.clone())).unwrap()).number());
}
if peer_info.latest_hash != latest_hash && latest_number > peer_info.latest_number.unwrap() {
Some((id, peer_info.latest_number.unwrap()))
} else { None }
2016-02-06 20:16:59 +01:00
},
_ => None
2016-02-06 18:56:21 +01:00
})
.collect::<Vec<_>>()
2016-02-05 23:45:25 +01:00
}
/// propagates latest block to lagging peers
fn propagate_blocks(&mut self, chain_info: &BlockChainInfo, io: &mut SyncIo) -> usize {
2016-02-05 18:34:08 +01:00
let updated_peers = {
let lagging_peers = self.get_lagging_peers(chain_info, io);
2016-02-05 18:34:08 +01:00
2016-02-06 18:56:21 +01:00
// sqrt(x)/x scaled to max u32
let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32;
2016-02-06 20:16:59 +01:00
let lucky_peers = match lagging_peers.len() {
2016-02-05 18:34:08 +01:00
0 ... MIN_PEERS_PROPAGATION => lagging_peers,
_ => lagging_peers.into_iter().filter(|_| ::rand::random::<u32>() < fraction).collect::<Vec<_>>()
2016-02-05 18:34:08 +01:00
};
2016-02-06 18:56:21 +01:00
// taking at max of MAX_PEERS_PROPAGATION
lucky_peers.iter().map(|&(id, _)| id.clone()).take(min(lucky_peers.len(), MAX_PEERS_PROPAGATION)).collect::<Vec<PeerId>>()
2016-02-05 18:34:08 +01:00
};
2016-02-06 21:00:52 +01:00
let mut sent = 0;
for peer_id in updated_peers {
let rlp = ChainSync::create_latest_block_rlp(io.chain());
2016-02-12 13:07:02 +01:00
self.send_packet(io, peer_id, NEW_BLOCK_PACKET, rlp);
self.peers.get_mut(&peer_id).unwrap().latest_hash = chain_info.best_block_hash.clone();
self.peers.get_mut(&peer_id).unwrap().latest_number = Some(chain_info.best_block_number);
2016-02-06 21:00:52 +01:00
sent = sent + 1;
}
sent
}
/// propagates new known hashes to all peers
fn propagate_new_hashes(&mut self, chain_info: &BlockChainInfo, io: &mut SyncIo) -> usize {
let updated_peers = self.get_lagging_peers(chain_info, io);
2016-02-05 18:34:08 +01:00
let mut sent = 0;
let last_parent = HeaderView::new(&io.chain().block_header(BlockId::Hash(chain_info.best_block_hash.clone())).unwrap()).parent_hash();
for (peer_id, peer_number) in updated_peers {
let mut peer_best = self.peers.get(&peer_id).unwrap().latest_hash.clone();
if chain_info.best_block_number - peer_number > MAX_PEERS_PROPAGATION as BlockNumber {
// If we think peer is too far behind just send one latest hash
peer_best = last_parent.clone();
}
sent = sent + match ChainSync::create_new_hashes_rlp(io.chain(), &peer_best, &chain_info.best_block_hash) {
2016-02-05 18:34:08 +01:00
Some(rlp) => {
2016-02-06 20:23:25 +01:00
{
2016-02-12 14:20:18 +01:00
let peer = self.peers.get_mut(&peer_id).unwrap();
peer.latest_hash = chain_info.best_block_hash.clone();
peer.latest_number = Some(chain_info.best_block_number);
2016-02-06 20:23:25 +01:00
}
2016-02-12 13:07:02 +01:00
self.send_packet(io, peer_id, NEW_BLOCK_HASHES_PACKET, rlp);
2016-02-05 18:34:08 +01:00
1
},
None => 0
}
}
sent
}
/// propagates new transactions to all peers
fn propagate_new_transactions(&mut self, io: &mut SyncIo) -> usize {
// Early out of nobody to send to.
2016-04-06 10:07:24 +02:00
if self.peers.is_empty() {
return 0;
}
2016-04-06 23:03:07 +02:00
let mut transactions = self.miner.pending_transactions();
if transactions.is_empty() {
return 0;
}
let mut packet = RlpStream::new_list(transactions.len());
let tx_count = transactions.len();
for tx in transactions.drain(..) {
packet.append(&tx);
}
let rlp = packet.out();
let lucky_peers = {
// sqrt(x)/x scaled to max u32
let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32;
let small = self.peers.len() < MIN_PEERS_PROPAGATION;
let lucky_peers = self.peers.iter()
.filter_map(|(&p, _)| if small || ::rand::random::<u32>() < fraction { Some(p.clone()) } else { None })
.collect::<Vec<_>>();
// taking at max of MAX_PEERS_PROPAGATION
2016-04-06 10:07:24 +02:00
lucky_peers.iter().cloned().take(min(lucky_peers.len(), MAX_PEERS_PROPAGATION)).collect::<Vec<PeerId>>()
};
let sent = lucky_peers.len();
for peer_id in lucky_peers {
self.send_packet(io, peer_id, TRANSACTIONS_PACKET, rlp.clone());
}
2016-04-06 23:03:07 +02:00
trace!(target: "sync", "Sent {} transactions to {} peers.", tx_count, sent);
sent
}
fn propagate_latest_blocks(&mut self, io: &mut SyncIo) {
2016-04-06 23:03:07 +02:00
self.propagate_new_transactions(io);
let chain_info = io.chain().chain_info();
if (((chain_info.best_block_number as i64) - (self.last_sent_block_number as i64)).abs() as BlockNumber) < MAX_PEER_LAG_PROPAGATION {
let blocks = self.propagate_blocks(&chain_info, io);
let hashes = self.propagate_new_hashes(&chain_info, io);
2016-02-12 13:07:02 +01:00
if blocks != 0 || hashes != 0 {
trace!(target: "sync", "Sent latest {} blocks and {} hashes to peers.", blocks, hashes);
}
}
self.last_sent_block_number = chain_info.best_block_number;
2016-02-05 18:34:08 +01:00
}
/// Maintain other peers. Send out any new blocks and transactions
pub fn maintain_sync(&mut self, io: &mut SyncIo) {
self.check_resume(io);
}
/// called when block is imported to chain, updates transactions queue and propagates the blocks
2016-03-13 15:29:55 +01:00
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) {
2016-03-17 14:56:19 +01:00
if io.is_chain_queue_empty() {
// Notify miner
self.miner.chain_new_blocks(io.chain(), imported, invalid, enacted, retracted);
2016-03-17 15:02:18 +01:00
// Propagate latests blocks
self.propagate_latest_blocks(io);
}
2016-03-29 23:29:43 +02:00
if !invalid.is_empty() {
trace!(target: "sync", "Bad blocks in the queue, restarting");
self.restart_on_bad_block(io);
}
// TODO [todr] propagate transactions?
}
2016-03-09 12:54:07 +01:00
pub fn chain_new_head(&mut self, io: &mut SyncIo) {
self.miner.update_sealing(io.chain());
2016-03-05 21:47:28 +01:00
}
2015-12-22 22:19:50 +01:00
}
2016-02-04 19:30:31 +01:00
#[cfg(test)]
mod tests {
2016-02-04 20:03:14 +01:00
use tests::helpers::*;
use super::*;
2016-02-24 21:23:58 +01:00
use ::SyncConfig;
2016-02-04 20:03:14 +01:00
use util::*;
2016-02-05 23:45:25 +01:00
use super::{PeerInfo, PeerAsking};
2016-03-17 10:20:35 +01:00
use ethcore::views::BlockView;
2016-02-07 22:08:15 +01:00
use ethcore::header::*;
use ethcore::client::*;
use ethminer::{Miner, MinerService};
2016-02-07 22:08:15 +01:00
fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes {
let mut header = Header::new();
header.gas_limit = x!(0);
header.difficulty = x!(order * 100);
header.timestamp = (order * 10) as u64;
header.number = order as u64;
header.parent_hash = parent_hash;
header.state_root = H256::zero();
let mut rlp = RlpStream::new_list(3);
rlp.append(&header);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1);
rlp.out()
}
fn get_dummy_blocks(order: u32, parent_hash: H256) -> Bytes {
let mut rlp = RlpStream::new_list(1);
rlp.append_raw(&get_dummy_block(order, parent_hash), 1);
let difficulty: U256 = x!(100 * order);
rlp.append(&difficulty);
rlp.out()
}
2016-02-04 20:03:14 +01:00
2016-02-07 22:20:59 +01:00
fn get_dummy_hashes() -> Bytes {
let mut rlp = RlpStream::new_list(5);
for _ in 0..5 {
let mut hash_d_rlp = RlpStream::new_list(2);
let hash: H256 = H256::from(0u64);
let diff: U256 = U256::from(1u64);
hash_d_rlp.append(&hash);
hash_d_rlp.append(&diff);
rlp.append_raw(&hash_d_rlp.out(), 1);
}
rlp.out()
}
2016-02-04 20:03:14 +01:00
2016-02-04 23:24:36 +01:00
#[test]
fn return_receipts_empty() {
let mut client = TestBlockChainClient::new();
let mut queue = VecDeque::new();
let io = TestIo::new(&mut client, &mut queue, None);
let result = ChainSync::return_receipts(&io, &UntrustedRlp::new(&[0xc0]));
assert!(result.is_ok());
}
2016-02-04 20:03:14 +01:00
#[test]
fn return_receipts() {
let mut client = TestBlockChainClient::new();
2016-02-04 23:24:36 +01:00
let mut queue = VecDeque::new();
2016-02-04 23:58:32 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
2016-02-04 20:03:14 +01:00
2016-02-04 23:24:36 +01:00
let mut receipt_list = RlpStream::new_list(4);
receipt_list.append(&H256::from("0000000000000000000000000000000000000000000000005555555555555555"));
receipt_list.append(&H256::from("ff00000000000000000000000000000000000000000000000000000000000000"));
receipt_list.append(&H256::from("fff0000000000000000000000000000000000000000000000000000000000000"));
receipt_list.append(&H256::from("aff0000000000000000000000000000000000000000000000000000000000000"));
2016-02-04 20:03:14 +01:00
2016-02-04 23:58:32 +01:00
let receipts_request = receipt_list.out();
2016-02-04 23:24:36 +01:00
// it returns rlp ONLY for hashes started with "f"
2016-02-04 23:58:32 +01:00
let result = ChainSync::return_receipts(&io, &UntrustedRlp::new(&receipts_request.clone()));
2016-02-04 20:03:14 +01:00
assert!(result.is_ok());
2016-02-04 23:24:36 +01:00
let rlp_result = result.unwrap();
assert!(rlp_result.is_some());
// the length of two rlp-encoded receipts
assert_eq!(603, rlp_result.unwrap().1.out().len());
2016-02-04 23:58:32 +01:00
2016-02-12 14:20:18 +01:00
let mut sync = dummy_sync_with_peer(H256::new());
2016-02-04 23:58:32 +01:00
io.sender = Some(2usize);
2016-02-12 14:20:18 +01:00
sync.on_packet(&mut io, 0usize, super::GET_RECEIPTS_PACKET, &receipts_request);
2016-02-05 00:57:51 +01:00
assert_eq!(1, io.queue.len());
2016-02-04 20:03:14 +01:00
}
2016-02-04 23:58:32 +01:00
2016-02-05 00:57:51 +01:00
#[test]
fn return_nodes() {
let mut client = TestBlockChainClient::new();
let mut queue = VecDeque::new();
let mut io = TestIo::new(&mut client, &mut queue, None);
2016-02-04 23:58:32 +01:00
2016-02-05 00:57:51 +01:00
let mut node_list = RlpStream::new_list(3);
node_list.append(&H256::from("0000000000000000000000000000000000000000000000005555555555555555"));
node_list.append(&H256::from("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa"));
node_list.append(&H256::from("aff0000000000000000000000000000000000000000000000000000000000000"));
let node_request = node_list.out();
// it returns rlp ONLY for hashes started with "f"
let result = ChainSync::return_node_data(&io, &UntrustedRlp::new(&node_request.clone()));
assert!(result.is_ok());
let rlp_result = result.unwrap();
assert!(rlp_result.is_some());
// the length of one rlp-encoded hashe
assert_eq!(34, rlp_result.unwrap().1.out().len());
2016-02-12 14:20:18 +01:00
let mut sync = dummy_sync_with_peer(H256::new());
2016-02-05 00:57:51 +01:00
io.sender = Some(2usize);
2016-02-12 14:20:18 +01:00
sync.on_packet(&mut io, 0usize, super::GET_NODE_DATA_PACKET, &node_request);
2016-02-05 00:57:51 +01:00
assert_eq!(1, io.queue.len());
}
2016-02-05 23:45:25 +01:00
fn dummy_sync_with_peer(peer_latest_hash: H256) -> ChainSync {
let mut sync = ChainSync::new(SyncConfig::default(), Miner::new(false));
2016-02-05 23:45:25 +01:00
sync.peers.insert(0,
2016-02-06 18:56:21 +01:00
PeerInfo {
protocol_version: 0,
genesis: H256::zero(),
network_id: U256::zero(),
latest_hash: peer_latest_hash,
latest_number: None,
2016-02-06 18:56:21 +01:00
difficulty: U256::zero(),
asking: PeerAsking::Nothing,
asking_blocks: Vec::<BlockNumber>::new(),
2016-02-12 13:07:02 +01:00
asking_hash: None,
2016-02-06 18:56:21 +01:00
ask_time: 0f64,
});
2016-02-05 23:45:25 +01:00
sync
}
#[test]
fn finds_lagging_peers() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-05 23:45:25 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(10));
let chain_info = client.chain_info();
2016-02-05 23:45:25 +01:00
let io = TestIo::new(&mut client, &mut queue, None);
let lagging_peers = sync.get_lagging_peers(&chain_info, &io);
2016-02-05 23:45:25 +01:00
assert_eq!(1, lagging_peers.len())
}
#[test]
fn calculates_tree_for_lagging_peer() {
let mut client = TestBlockChainClient::new();
client.add_blocks(15, EachBlockWith::Uncle);
2016-02-05 23:45:25 +01:00
let start = client.block_hash_delta_minus(4);
let end = client.block_hash_delta_minus(2);
// wrong way end -> start, should be None
let rlp = ChainSync::create_new_hashes_rlp(&client, &end, &start);
assert!(rlp.is_none());
let rlp = ChainSync::create_new_hashes_rlp(&client, &start, &end).unwrap();
2016-02-07 23:39:02 +01:00
// size of three rlp encoded hash-difficulty
assert_eq!(107, rlp.len());
2016-02-05 23:45:25 +01:00
}
#[test]
2016-02-06 21:00:52 +01:00
fn sends_new_hashes_to_lagging_peer() {
2016-02-05 23:45:25 +01:00
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-05 23:45:25 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let chain_info = client.chain_info();
2016-02-05 23:45:25 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
let peer_count = sync.propagate_new_hashes(&chain_info, &mut io);
2016-02-05 23:45:25 +01:00
// 1 message should be send
assert_eq!(1, io.queue.len());
// 1 peer should be updated
2016-02-06 18:56:21 +01:00
assert_eq!(1, peer_count);
2016-02-05 23:45:25 +01:00
// NEW_BLOCK_HASHES_PACKET
assert_eq!(0x01, io.queue[0].packet_id);
}
2016-02-06 21:00:52 +01:00
#[test]
fn sends_latest_block_to_lagging_peer() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-06 21:00:52 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let chain_info = client.chain_info();
2016-02-06 21:00:52 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
let peer_count = sync.propagate_blocks(&chain_info, &mut io);
2016-02-06 21:00:52 +01:00
// 1 message should be send
assert_eq!(1, io.queue.len());
// 1 peer should be updated
assert_eq!(1, peer_count);
// NEW_BLOCK_PACKET
assert_eq!(0x07, io.queue[0].packet_id);
}
2016-02-07 21:01:09 +01:00
#[test]
2016-02-07 22:08:15 +01:00
fn handles_peer_new_block_mallformed() {
let mut client = TestBlockChainClient::new();
client.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 22:08:15 +01:00
let block_data = get_dummy_block(11, client.chain_info().best_block_hash);
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
2016-03-15 18:14:52 +01:00
sync.have_common_block = true;
2016-02-07 22:08:15 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
let block = UntrustedRlp::new(&block_data);
let result = sync.on_peer_new_block(&mut io, 0, &block);
assert!(result.is_err());
}
#[test]
fn handles_peer_new_block() {
let mut client = TestBlockChainClient::new();
client.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 22:08:15 +01:00
let block_data = get_dummy_blocks(11, client.chain_info().best_block_hash);
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let mut io = TestIo::new(&mut client, &mut queue, None);
let block = UntrustedRlp::new(&block_data);
let result = sync.on_peer_new_block(&mut io, 0, &block);
assert!(result.is_ok());
}
#[test]
fn handles_peer_new_block_empty() {
2016-02-07 21:01:09 +01:00
let mut client = TestBlockChainClient::new();
client.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 21:01:09 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let mut io = TestIo::new(&mut client, &mut queue, None);
let empty_data = vec![];
let block = UntrustedRlp::new(&empty_data);
let result = sync.on_peer_new_block(&mut io, 0, &block);
assert!(result.is_err());
}
2016-02-07 22:20:59 +01:00
#[test]
fn handles_peer_new_hashes() {
let mut client = TestBlockChainClient::new();
client.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 22:20:59 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let mut io = TestIo::new(&mut client, &mut queue, None);
let hashes_data = get_dummy_hashes();
let hashes_rlp = UntrustedRlp::new(&hashes_data);
let result = sync.on_peer_new_hashes(&mut io, 0, &hashes_rlp);
assert!(result.is_ok());
}
#[test]
fn handles_peer_new_hashes_empty() {
let mut client = TestBlockChainClient::new();
client.add_blocks(10, EachBlockWith::Uncle);
2016-02-07 22:20:59 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let mut io = TestIo::new(&mut client, &mut queue, None);
let empty_hashes_data = vec![];
let hashes_rlp = UntrustedRlp::new(&empty_hashes_data);
let result = sync.on_peer_new_hashes(&mut io, 0, &hashes_rlp);
assert!(result.is_ok());
}
2016-02-07 23:39:02 +01:00
// idea is that what we produce when propagading latest hashes should be accepted in
// on_peer_new_hashes in our code as well
2016-02-07 23:39:02 +01:00
#[test]
fn hashes_rlp_mutually_acceptable() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-07 23:39:02 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let chain_info = client.chain_info();
2016-02-07 23:39:02 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
sync.propagate_new_hashes(&chain_info, &mut io);
2016-02-07 23:39:02 +01:00
let data = &io.queue[0].data.clone();
let result = sync.on_peer_new_hashes(&mut io, 0, &UntrustedRlp::new(&data));
assert!(result.is_ok());
}
// idea is that what we produce when propagading latest block should be accepted in
// on_peer_new_block in our code as well
2016-02-07 23:39:02 +01:00
#[test]
fn block_rlp_mutually_acceptable() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-07 23:39:02 +01:00
let mut queue = VecDeque::new();
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let chain_info = client.chain_info();
2016-02-07 23:39:02 +01:00
let mut io = TestIo::new(&mut client, &mut queue, None);
sync.propagate_blocks(&chain_info, &mut io);
2016-02-07 23:39:02 +01:00
let data = &io.queue[0].data.clone();
let result = sync.on_peer_new_block(&mut io, 0, &UntrustedRlp::new(&data));
assert!(result.is_ok());
}
2016-02-24 13:01:45 +01:00
#[test]
fn should_add_transactions_to_queue() {
// given
let mut client = TestBlockChainClient::new();
client.add_blocks(98, EachBlockWith::Uncle);
client.add_blocks(1, EachBlockWith::UncleAndTransaction);
client.add_blocks(1, EachBlockWith::Transaction);
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let good_blocks = vec![client.block_hash_delta_minus(2)];
let retracted_blocks = vec![client.block_hash_delta_minus(1)];
// Add some balance to clients and reset nonces
2016-04-06 10:07:24 +02:00
for h in &[good_blocks[0], retracted_blocks[0]] {
let block = client.block(BlockId::Hash(*h)).unwrap();
2016-03-17 10:20:35 +01:00
let view = BlockView::new(&block);
2016-03-17 11:27:38 +01:00
client.set_balance(view.transactions()[0].sender().unwrap(), U256::from(1_000_000_000));
client.set_nonce(view.transactions()[0].sender().unwrap(), U256::from(0));
2016-03-17 10:20:35 +01:00
}
// when
{
let mut queue = VecDeque::new();
let mut io = TestIo::new(&mut client, &mut queue, None);
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks);
assert_eq!(sync.miner.status().transactions_in_future_queue, 0);
assert_eq!(sync.miner.status().transactions_in_pending_queue, 1);
}
// We need to update nonce status (because we say that the block has been imported)
for h in &[good_blocks[0]] {
let block = client.block(BlockId::Hash(*h)).unwrap();
let view = BlockView::new(&block);
client.set_nonce(view.transactions()[0].sender().unwrap(), U256::from(1));
}
{
let mut queue = VecDeque::new();
let mut io = TestIo::new(&mut client, &mut queue, None);
sync.chain_new_blocks(&mut io, &[], &[], &good_blocks, &retracted_blocks);
}
// then
let status = sync.miner.status();
assert_eq!(status.transactions_in_pending_queue, 1);
assert_eq!(status.transactions_in_future_queue, 0);
}
#[test]
fn should_not_add_transactions_to_queue_if_not_synced() {
// given
let mut client = TestBlockChainClient::new();
client.add_blocks(98, EachBlockWith::Uncle);
client.add_blocks(1, EachBlockWith::UncleAndTransaction);
client.add_blocks(1, EachBlockWith::Transaction);
let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(5));
let good_blocks = vec![client.block_hash_delta_minus(2)];
let retracted_blocks = vec![client.block_hash_delta_minus(1)];
let mut queue = VecDeque::new();
let mut io = TestIo::new(&mut client, &mut queue, None);
// when
2016-03-17 14:56:19 +01:00
sync.chain_new_blocks(&mut io, &[], &[], &[], &good_blocks);
assert_eq!(sync.miner.status().transactions_in_future_queue, 0);
assert_eq!(sync.miner.status().transactions_in_pending_queue, 0);
sync.chain_new_blocks(&mut io, &[], &[], &good_blocks, &retracted_blocks);
// then
let status = sync.miner.status();
assert_eq!(status.transactions_in_pending_queue, 0);
assert_eq!(status.transactions_in_future_queue, 0);
}
2016-02-24 13:01:45 +01:00
#[test]
fn returns_requested_block_headers() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-24 13:01:45 +01:00
let mut queue = VecDeque::new();
let io = TestIo::new(&mut client, &mut queue, None);
let mut rlp = RlpStream::new_list(4);
rlp.append(&0u64);
rlp.append(&10u64);
rlp.append(&0u64);
rlp.append(&0u64);
let data = rlp.out();
let response = ChainSync::return_block_headers(&io, &UntrustedRlp::new(&data));
assert!(response.is_ok());
let (_, rlp_stream) = response.unwrap().unwrap();
let response_data = rlp_stream.out();
let rlp = UntrustedRlp::new(&response_data);
assert!(rlp.at(0).is_ok());
assert!(rlp.at(9).is_ok());
}
#[test]
fn returns_requested_block_headers_reverse() {
let mut client = TestBlockChainClient::new();
client.add_blocks(100, EachBlockWith::Uncle);
2016-02-24 13:01:45 +01:00
let mut queue = VecDeque::new();
let io = TestIo::new(&mut client, &mut queue, None);
let mut rlp = RlpStream::new_list(4);
rlp.append(&15u64);
rlp.append(&15u64);
rlp.append(&0u64);
rlp.append(&1u64);
let data = rlp.out();
let response = ChainSync::return_block_headers(&io, &UntrustedRlp::new(&data));
assert!(response.is_ok());
let (_, rlp_stream) = response.unwrap().unwrap();
let response_data = rlp_stream.out();
let rlp = UntrustedRlp::new(&response_data);
assert!(rlp.at(0).is_ok());
assert!(rlp.at(14).is_ok());
assert!(!rlp.at(15).is_ok());
}
}