2016-12-13 21:09:43 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (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/>.
|
|
|
|
|
|
|
|
//! Light client synchronization.
|
|
|
|
//!
|
|
|
|
//! This will synchronize the header chain using LES messages.
|
|
|
|
//! Dataflow is largely one-directional as headers are pushed into
|
|
|
|
//! the light client queue for import. Where possible, they are batched
|
|
|
|
//! in groups.
|
|
|
|
//!
|
|
|
|
//! This is written assuming that the client and sync service are running
|
2016-12-14 23:26:15 +01:00
|
|
|
//! in the same binary; unlike a full node which might communicate via IPC.
|
2016-12-13 21:09:43 +01:00
|
|
|
|
2016-12-13 22:26:06 +01:00
|
|
|
use std::collections::HashMap;
|
2016-12-13 21:09:43 +01:00
|
|
|
use std::fmt;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2016-12-13 21:09:57 +01:00
|
|
|
use ethcore::header::Header;
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
use light::client::LightChainClient;
|
2016-12-13 21:09:57 +01:00
|
|
|
use light::net::{Announcement, Error as NetError, Handler, EventContext, Capabilities, ReqId, Status};
|
2016-12-13 21:09:43 +01:00
|
|
|
use light::request;
|
|
|
|
use network::PeerId;
|
2016-12-14 23:26:15 +01:00
|
|
|
use rlp::{DecoderError, UntrustedRlp, View};
|
2016-12-13 21:09:57 +01:00
|
|
|
use util::{Bytes, U256, H256, Mutex, RwLock};
|
2016-12-13 21:09:43 +01:00
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
mod response;
|
|
|
|
mod sync_round;
|
2016-12-13 21:09:43 +01:00
|
|
|
|
|
|
|
/// Peer chain info.
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct ChainInfo {
|
|
|
|
head_td: U256,
|
|
|
|
head_hash: H256,
|
|
|
|
head_num: u64,
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
struct Peer {
|
|
|
|
first_status: ChainInfo,
|
|
|
|
status: ChainInfo,
|
2016-12-13 21:09:43 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
impl Peer {
|
|
|
|
/// Create a peer object.
|
|
|
|
fn new(chain_info: ChainInfo) -> Self {
|
|
|
|
Peer {
|
|
|
|
first_status: chain_info.clone(),
|
|
|
|
status: chain_info.clone(),
|
2016-12-13 21:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Light client synchronization manager. See module docs for more details.
|
2016-12-14 23:26:15 +01:00
|
|
|
pub struct LightSync<L: LightChainClient> {
|
2016-12-13 21:09:43 +01:00
|
|
|
best_seen: Mutex<Option<(H256, U256)>>, // best seen block on the network.
|
2016-12-13 22:26:06 +01:00
|
|
|
peers: RwLock<HashMap<PeerId, Mutex<Peer>>>, // peers which are relevant to synchronization.
|
2016-12-14 23:26:15 +01:00
|
|
|
client: Arc<L>,
|
2016-12-13 21:09:43 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
impl<L: LightChainClient> Handler for LightSync<L> {
|
2016-12-13 21:09:43 +01:00
|
|
|
fn on_connect(&self, ctx: &EventContext, status: &Status, capabilities: &Capabilities) {
|
2016-12-13 21:09:57 +01:00
|
|
|
let our_best = self.client.chain_info().best_block_number;
|
|
|
|
|
|
|
|
if !capabilities.serve_headers || status.head_num <= our_best {
|
2016-12-13 21:09:43 +01:00
|
|
|
trace!(target: "sync", "Ignoring irrelevant peer: {}", ctx.peer());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let chain_info = ChainInfo {
|
|
|
|
head_td: status.head_td,
|
|
|
|
head_hash: status.head_hash,
|
|
|
|
head_num: status.head_num,
|
|
|
|
};
|
2016-12-13 22:26:06 +01:00
|
|
|
|
2016-12-14 23:26:15 +01:00
|
|
|
self.peers.write().insert(ctx.peer(), Mutex::new(Peer::new(chain_info)));
|
2016-12-13 22:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_disconnect(&self, ctx: &EventContext, _unfulfilled: &[ReqId]) {
|
2016-12-14 23:26:15 +01:00
|
|
|
let peer_id = ctx.peer();
|
2016-12-13 22:26:06 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_announcement(&self, ctx: &EventContext, announcement: &Announcement) {
|
|
|
|
// restart search for common ancestor if necessary.
|
|
|
|
// restart download if necessary.
|
|
|
|
// if this is a peer we found irrelevant earlier, we may want to
|
|
|
|
// re-evaluate their usefulness.
|
|
|
|
if !self.peers.read().contains_key(&ctx.peer()) { return }
|
|
|
|
|
|
|
|
trace!(target: "sync", "Announcement from peer {}: new chain head {:?}, reorg depth {}",
|
|
|
|
ctx.peer(), (announcement.head_hash, announcement.head_num), announcement.reorg_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_block_headers(&self, ctx: &EventContext, req_id: ReqId, headers: &[Bytes]) {
|
2016-12-14 23:26:15 +01:00
|
|
|
let peer_id = ctx.peer();
|
2016-12-13 21:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public API
|
2016-12-14 23:26:15 +01:00
|
|
|
impl<L: LightChainClient> LightSync<L> {
|
2016-12-13 21:09:43 +01:00
|
|
|
/// Create a new instance of `LightSync`.
|
|
|
|
///
|
|
|
|
/// This won't do anything until registered as a handler
|
2016-12-13 22:26:06 +01:00
|
|
|
/// so it can act on events.
|
2016-12-14 23:26:15 +01:00
|
|
|
pub fn new(client: Arc<L>) -> Self {
|
2016-12-13 21:09:43 +01:00
|
|
|
LightSync {
|
|
|
|
best_seen: Mutex::new(None),
|
2016-12-13 21:09:57 +01:00
|
|
|
peers: RwLock::new(HashMap::new()),
|
2016-12-13 21:09:43 +01:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|