Move all light client work to own crate
This commit is contained in:
15
ethcore/light/Cargo.toml
Normal file
15
ethcore/light/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
description = "Parity LES primitives"
|
||||
homepage = "https://ethcore.io"
|
||||
license = "GPL-3.0"
|
||||
name = "ethcore-light"
|
||||
version = "1.5.0"
|
||||
authors = ["Ethcore <admin@ethcore.io>"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.3"
|
||||
ethcore = { path = ".." }
|
||||
ethcore-util = { path = "../../util" }
|
||||
ethcore-network = { path = "../../util/network" }
|
||||
ethcore-io = { path = "../../util/io" }
|
||||
rlp = { path = "../../util/rlp" }
|
||||
@@ -19,22 +19,23 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use engines::Engine;
|
||||
use ids::BlockID;
|
||||
use miner::TransactionQueue;
|
||||
use service::ClientIoMessage;
|
||||
use block_import_error::BlockImportError;
|
||||
use block_status::BlockStatus;
|
||||
use verification::queue::{Config as QueueConfig, HeaderQueue, QueueInfo, Status};
|
||||
use transaction::SignedTransaction;
|
||||
use types::blockchain_info::BlockChainInfo;
|
||||
|
||||
use super::provider::{CHTProofRequest, Provider, ProofRequest};
|
||||
use ethcore::engines::Engine;
|
||||
use ethcore::ids::BlockID;
|
||||
use ethcore::miner::TransactionQueue;
|
||||
use ethcore::service::ClientIoMessage;
|
||||
use ethcore::block_import_error::BlockImportError;
|
||||
use ethcore::block_status::BlockStatus;
|
||||
use ethcore::verification::queue::{Config as QueueConfig, HeaderQueue, QueueInfo, Status};
|
||||
use ethcore::transaction::SignedTransaction;
|
||||
use ethcore::blockchain_info::BlockChainInfo;
|
||||
|
||||
use io::IoChannel;
|
||||
use util::hash::H256;
|
||||
use util::{Bytes, Mutex};
|
||||
|
||||
use provider::Provider;
|
||||
use request;
|
||||
|
||||
/// Light client implementation.
|
||||
pub struct Client {
|
||||
engine: Arc<Engine>,
|
||||
@@ -78,27 +79,31 @@ impl Provider for Client {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn block_headers(&self, block: (u64, H256), skip: usize, max: usize, reverse: bool) -> Vec<Bytes> {
|
||||
fn block_headers(&self, _req: request::Headers) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn block_bodies(&self, blocks: Vec<H256>) -> Vec<Bytes> {
|
||||
fn block_bodies(&self, _req: request::Bodies) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn receipts(&self, blocks: Vec<H256>) -> Vec<Bytes> {
|
||||
fn receipts(&self, _req: request::Receipts) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn proofs(&self, requests: Vec<(H256, ProofRequest)>) -> Vec<Bytes> {
|
||||
fn proofs(&self, _req: request::StateProofs) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn code(&self, accounts: Vec<(H256, H256)>) -> Vec<Bytes> {
|
||||
fn code(&self, _req: request::ContractCodes) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn header_proofs(&self, requests: Vec<CHTProofRequest>) -> Vec<Bytes> {
|
||||
fn header_proofs(&self, _req: request::HeaderProofs) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn block_deltas(&self, _req: request::BlockDeltas) -> Vec<Bytes> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
@@ -28,9 +28,16 @@
|
||||
//! It starts by performing a header-only sync, verifying every header in
|
||||
//! the chain.
|
||||
|
||||
mod client;
|
||||
mod provider;
|
||||
mod sync;
|
||||
pub mod client;
|
||||
pub mod net;
|
||||
pub mod provider;
|
||||
pub mod request;
|
||||
|
||||
pub use self::client::Client;
|
||||
pub use self::provider::{CHTProofRequest, ProofRequest, Provider};
|
||||
extern crate ethcore_util as util;
|
||||
extern crate ethcore_network as network;
|
||||
extern crate ethcore_io as io;
|
||||
extern crate ethcore;
|
||||
extern crate rlp;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
370
ethcore/light/src/net/mod.rs
Normal file
370
ethcore/light/src/net/mod.rs
Normal file
@@ -0,0 +1,370 @@
|
||||
// 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/>.
|
||||
|
||||
//! LES Protocol Version 1 implementation.
|
||||
//!
|
||||
//! This uses a "Provider" to answer requests and syncs to a `Client`.
|
||||
//! See https://github.com/ethcore/parity/wiki/Light-Ethereum-Subprotocol-(LES)
|
||||
|
||||
use io::TimerToken;
|
||||
use network::{NetworkProtocolHandler, NetworkService, NetworkContext, NetworkError, PeerId};
|
||||
use rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View};
|
||||
use util::hash::H256;
|
||||
use util::{Mutex, RwLock};
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use provider::Provider;
|
||||
use request::Request;
|
||||
|
||||
const TIMEOUT: TimerToken = 0;
|
||||
const TIMEOUT_INTERVAL_MS: u64 = 1000;
|
||||
|
||||
// LPV1
|
||||
const PROTOCOL_VERSION: u32 = 1;
|
||||
|
||||
// TODO [rob] make configurable.
|
||||
const PROTOCOL_ID: [u8; 3] = *b"les";
|
||||
|
||||
// TODO [rob] Buffer flow.
|
||||
|
||||
// packet ID definitions.
|
||||
mod packet {
|
||||
// the status packet.
|
||||
pub const STATUS: u8 = 0x00;
|
||||
|
||||
// broadcast that new block hashes have appeared.
|
||||
pub const NEW_BLOCK_HASHES: u8 = 0x01;
|
||||
|
||||
// request and response for block headers
|
||||
pub const GET_BLOCK_HEADERS: u8 = 0x02;
|
||||
pub const BLOCK_HEADERS: u8 = 0x03;
|
||||
|
||||
// request and response for block bodies
|
||||
pub const GET_BLOCK_BODIES: u8 = 0x04;
|
||||
pub const BLOCK_BODIES: u8 = 0x05;
|
||||
|
||||
// request and response for transaction receipts.
|
||||
pub const GET_RECEIPTS: u8 = 0x06;
|
||||
pub const RECEIPTS: u8 = 0x07;
|
||||
|
||||
// request and response for merkle proofs.
|
||||
pub const GET_PROOFS: u8 = 0x08;
|
||||
pub const PROOFS: u8 = 0x09;
|
||||
|
||||
// request and response for contract code.
|
||||
pub const GET_CONTRACT_CODES: u8 = 0x0a;
|
||||
pub const CONTRACT_CODES: u8 = 0x0b;
|
||||
|
||||
// relay transactions to peers.
|
||||
pub const SEND_TRANSACTIONS: u8 = 0x0c;
|
||||
|
||||
// request and response for header proofs in a CHT.
|
||||
pub const GET_HEADER_PROOFS: u8 = 0x0d;
|
||||
pub const HEADER_PROOFS: u8 = 0x0e;
|
||||
|
||||
// broadcast dynamic capabilities.
|
||||
pub const CAPABILITIES: u8 = 0x0f;
|
||||
|
||||
// request and response for block-level state deltas.
|
||||
pub const GET_BLOCK_DELTAS: u8 = 0x10;
|
||||
pub const BLOCK_DELTAS: u8 = 0x11;
|
||||
|
||||
// request and response for transaction proofs.
|
||||
pub const GET_TRANSACTION_PROOFS: u8 = 0x12;
|
||||
pub const TRANSACTION_PROOFS: u8 = 0x13;
|
||||
}
|
||||
|
||||
// helper macro for disconnecting peer on error while returning
|
||||
// the value if ok.
|
||||
// requires that error types are debug.
|
||||
macro_rules! try_dc {
|
||||
($io: expr, $peer: expr, $e: expr) => {
|
||||
match $e {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
debug!(target: "les", "disconnecting peer {} due to error {:?}", $peer, e);
|
||||
$io.disconnect_peer($peer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Requested {
|
||||
timestamp: usize,
|
||||
req: Request,
|
||||
}
|
||||
|
||||
// data about each peer.
|
||||
struct Peer {
|
||||
buffer: u64, // remaining buffer value.
|
||||
current_asking: HashSet<usize>, // pending request ids.
|
||||
}
|
||||
|
||||
/// This is an implementation of the light ethereum network protocol, abstracted
|
||||
/// over a `Provider` of data and a p2p network.
|
||||
///
|
||||
/// This is simply designed for request-response purposes. Higher level uses
|
||||
/// of the protocol, such as synchronization, will function as wrappers around
|
||||
/// this system.
|
||||
pub struct LightProtocol {
|
||||
provider: Box<Provider>,
|
||||
genesis_hash: H256,
|
||||
mainnet: bool,
|
||||
peers: RwLock<HashMap<PeerId, Peer>>,
|
||||
pending_requests: RwLock<HashMap<usize, Requested>>,
|
||||
req_id: AtomicUsize,
|
||||
}
|
||||
|
||||
impl LightProtocol {
|
||||
// make a request to a given peer.
|
||||
fn request_from(&self, peer: &PeerId, req: Request) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// called when a peer connects.
|
||||
fn on_connect(&self, peer: &PeerId, io: &NetworkContext) {
|
||||
let peer = *peer;
|
||||
match self.send_status(peer, io) {
|
||||
Ok(()) => {
|
||||
self.peers.write().insert(peer, Peer {
|
||||
buffer: 0,
|
||||
current_asking: HashSet::new(),
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
trace!(target: "les", "Error while sending status: {}", e);
|
||||
io.disable_peer(peer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// called when a peer disconnects.
|
||||
fn on_disconnect(&self, peer: PeerId, io: &NetworkContext) {
|
||||
// TODO: reassign all requests assigned to this peer.
|
||||
self.peers.write().remove(&peer);
|
||||
}
|
||||
|
||||
fn send_status(&self, peer: PeerId, io: &NetworkContext) -> Result<(), NetworkError> {
|
||||
let chain_info = self.provider.chain_info();
|
||||
|
||||
// TODO [rob] use optional keys too.
|
||||
let mut stream = RlpStream::new_list(6);
|
||||
stream
|
||||
.begin_list(2)
|
||||
.append(&"protocolVersion")
|
||||
.append(&PROTOCOL_VERSION)
|
||||
.begin_list(2)
|
||||
.append(&"networkId")
|
||||
.append(&(self.mainnet as u8))
|
||||
.begin_list(2)
|
||||
.append(&"headTd")
|
||||
.append(&chain_info.total_difficulty)
|
||||
.begin_list(2)
|
||||
.append(&"headHash")
|
||||
.append(&chain_info.best_block_hash)
|
||||
.begin_list(2)
|
||||
.append(&"headNum")
|
||||
.append(&chain_info.best_block_number)
|
||||
.begin_list(2)
|
||||
.append(&"genesisHash")
|
||||
.append(&self.genesis_hash);
|
||||
|
||||
io.send(peer, packet::STATUS, stream.out())
|
||||
}
|
||||
|
||||
/// Check on the status of all pending requests.
|
||||
fn check_pending_requests(&self) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn status(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a new block hashes message.
|
||||
fn new_block_hashes(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
const MAX_NEW_HASHES: usize = 256;
|
||||
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for block headers.
|
||||
fn get_block_headers(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
const MAX_HEADERS: usize = 512;
|
||||
|
||||
let req_id: u64 = try_dc!(io, *peer, data.val_at(0));
|
||||
let block: (u64, H256) = try_dc!(io, *peer, data.at(1).and_then(|block_list| {
|
||||
Ok((try!(block_list.val_at(0)), try!(block_list.val_at(1))))
|
||||
}));
|
||||
let max = ::std::cmp::min(MAX_HEADERS, try_dc!(io, *peer, data.val_at(2)));
|
||||
let reverse: bool = try_dc!(io, *peer, data.val_at(3));
|
||||
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for block headers.
|
||||
fn block_headers(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for block bodies.
|
||||
fn get_block_bodies(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
const MAX_BODIES: usize = 512;
|
||||
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for block bodies.
|
||||
fn block_bodies(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for receipts.
|
||||
fn get_receipts(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for receipts.
|
||||
fn receipts(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for proofs.
|
||||
fn get_proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for proofs.
|
||||
fn proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for contract code.
|
||||
fn get_contract_code(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for contract code.
|
||||
fn contract_code(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for header proofs
|
||||
fn get_header_proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a response for header proofs
|
||||
fn header_proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive a set of transactions to relay.
|
||||
fn relay_transactions(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive updated capabilities from a peer.
|
||||
fn capabilities(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for block deltas.
|
||||
fn get_block_deltas(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive block deltas.
|
||||
fn block_deltas(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Handle a request for transaction proofs.
|
||||
fn get_transaction_proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// Receive transaction proofs.
|
||||
fn transaction_proofs(&self, peer: &PeerId, io: &NetworkContext, data: UntrustedRlp) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkProtocolHandler for LightProtocol {
|
||||
fn initialize(&self, io: &NetworkContext) {
|
||||
io.register_timer(TIMEOUT, TIMEOUT_INTERVAL_MS).expect("Error registering sync timer.");
|
||||
}
|
||||
|
||||
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
let rlp = UntrustedRlp::new(data);
|
||||
match packet_id {
|
||||
packet::STATUS => self.status(peer, io, rlp),
|
||||
packet::NEW_BLOCK_HASHES => self.new_block_hashes(peer, io, rlp),
|
||||
|
||||
packet::GET_BLOCK_HEADERS => self.get_block_headers(peer, io, rlp),
|
||||
packet::BLOCK_HEADERS => self.block_headers(peer, io, rlp),
|
||||
|
||||
packet::GET_BLOCK_BODIES => self.get_block_bodies(peer, io, rlp),
|
||||
packet::BLOCK_BODIES => self.block_bodies(peer, io, rlp),
|
||||
|
||||
packet::GET_RECEIPTS => self.get_receipts(peer, io, rlp),
|
||||
packet::RECEIPTS => self.receipts(peer, io, rlp),
|
||||
|
||||
packet::GET_PROOFS => self.get_proofs(peer, io, rlp),
|
||||
packet::PROOFS => self.proofs(peer, io, rlp),
|
||||
|
||||
packet::GET_CONTRACT_CODES => self.get_contract_code(peer, io, rlp),
|
||||
packet::CONTRACT_CODES => self.contract_code(peer, io, rlp),
|
||||
|
||||
packet::SEND_TRANSACTIONS => self.relay_transactions(peer, io, rlp),
|
||||
packet::CAPABILITIES => self.capabilities(peer, io, rlp),
|
||||
|
||||
packet::GET_HEADER_PROOFS => self.get_header_proofs(peer, io, rlp),
|
||||
packet::HEADER_PROOFS => self.header_proofs(peer, io, rlp),
|
||||
|
||||
packet::GET_BLOCK_DELTAS => self.get_block_deltas(peer, io, rlp),
|
||||
packet::BLOCK_DELTAS => self.block_deltas(peer, io, rlp),
|
||||
|
||||
packet::GET_TRANSACTION_PROOFS => self.get_transaction_proofs(peer, io, rlp),
|
||||
packet::TRANSACTION_PROOFS => self.transaction_proofs(peer, io, rlp),
|
||||
|
||||
other => {
|
||||
debug!(target: "les", "Disconnecting peer {} on unexpected packet {}", peer, other);
|
||||
io.disconnect_peer(*peer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn connected(&self, io: &NetworkContext, peer: &PeerId) {
|
||||
self.on_connect(peer, io);
|
||||
}
|
||||
|
||||
fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
|
||||
self.on_disconnect(*peer, io);
|
||||
}
|
||||
|
||||
fn timeout(&self, io: &NetworkContext, timer: TimerToken) {
|
||||
match timer {
|
||||
TIMEOUT => {
|
||||
// broadcast transactions to peers.
|
||||
}
|
||||
_ => warn!(target: "les", "received timeout on unknown token {}", timer),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,21 +17,20 @@
|
||||
//! A provider for the LES protocol. This is typically a full node, who can
|
||||
//! give as much data as necessary to its peers.
|
||||
|
||||
pub use proof_request::{CHTProofRequest, ProofRequest};
|
||||
|
||||
use transaction::SignedTransaction;
|
||||
use blockchain_info::BlockChainInfo;
|
||||
|
||||
use ethcore::transaction::SignedTransaction;
|
||||
use ethcore::blockchain_info::BlockChainInfo;
|
||||
use util::Bytes;
|
||||
use util::hash::H256;
|
||||
|
||||
use request;
|
||||
|
||||
/// Defines the operations that a provider for `LES` must fulfill.
|
||||
///
|
||||
/// These are defined at [1], but may be subject to change.
|
||||
/// Requests which can't be fulfilled should return an empty RLP list.
|
||||
///
|
||||
/// [1]: https://github.com/ethcore/parity/wiki/Light-Ethereum-Subprotocol-(LES)
|
||||
pub trait Provider: Sync {
|
||||
pub trait Provider: Send + Sync {
|
||||
/// Provide current blockchain info.
|
||||
fn chain_info(&self) -> BlockChainInfo;
|
||||
|
||||
@@ -40,27 +39,30 @@ pub trait Provider: Sync {
|
||||
///
|
||||
/// The returned vector may have any length in the range [0, `max`], but the
|
||||
/// results within must adhere to the `skip` and `reverse` parameters.
|
||||
fn block_headers(&self, block: (u64, H256), skip: usize, max: usize, reverse: bool) -> Vec<Bytes>;
|
||||
fn block_headers(&self, req: request::Headers) -> Vec<Bytes>;
|
||||
|
||||
/// Provide as many as possible of the requested blocks (minus the headers) encoded
|
||||
/// in RLP format.
|
||||
fn block_bodies(&self, blocks: Vec<H256>) -> Vec<Bytes>;
|
||||
fn block_bodies(&self, req: request::Bodies) -> Vec<Bytes>;
|
||||
|
||||
/// Provide the receipts as many as possible of the requested blocks.
|
||||
/// Returns a vector of RLP-encoded lists of receipts.
|
||||
fn receipts(&self, blocks: Vec<H256>) -> Vec<Bytes>;
|
||||
fn receipts(&self, req: request::Receipts) -> Vec<Bytes>;
|
||||
|
||||
/// Provide a set of merkle proofs, as requested. Each request is a
|
||||
/// block hash and request parameters.
|
||||
///
|
||||
/// Returns a vector to RLP-encoded lists satisfying the requests.
|
||||
fn proofs(&self, requests: Vec<(H256, ProofRequest)>) -> Vec<Bytes>;
|
||||
fn proofs(&self, req: request::StateProofs) -> Vec<Bytes>;
|
||||
|
||||
/// Provide contract code for the specified (block_hash, account_hash) pairs.
|
||||
fn code(&self, accounts: Vec<(H256, H256)>) -> Vec<Bytes>;
|
||||
fn code(&self, req: request::ContractCodes) -> Vec<Bytes>;
|
||||
|
||||
/// Provide header proofs from the Canonical Hash Tries.
|
||||
fn header_proofs(&self, requests: Vec<CHTProofRequest>) -> Vec<Bytes>;
|
||||
fn header_proofs(&self, req: request::HeaderProofs) -> Vec<Bytes>;
|
||||
|
||||
/// Provide block deltas.
|
||||
fn block_deltas(&self, req: request::BlockDeltas) -> Vec<Bytes>;
|
||||
|
||||
/// Provide pending transactions.
|
||||
fn pending_transactions(&self) -> Vec<SignedTransaction>;
|
||||
167
ethcore/light/src/request.rs
Normal file
167
ethcore/light/src/request.rs
Normal file
@@ -0,0 +1,167 @@
|
||||
// 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/>.
|
||||
|
||||
//! LES request types.
|
||||
|
||||
// TODO: make IPC compatible.
|
||||
|
||||
use ethcore::transaction::Transaction;
|
||||
use util::{Address, H256};
|
||||
|
||||
/// A request for block headers.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Headers {
|
||||
/// Block information for the request being made.
|
||||
pub block: (u64, H256),
|
||||
/// The maximum amount of headers which can be returned.
|
||||
pub max: u64,
|
||||
/// The amount of headers to skip between each response entry.
|
||||
pub skip: u64,
|
||||
/// Whether the headers should proceed in falling number from the initial block.
|
||||
pub reverse: bool,
|
||||
}
|
||||
|
||||
/// A request for specific block bodies.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Bodies {
|
||||
/// Hashes which bodies are being requested for.
|
||||
pub block_hashes: Vec<H256>
|
||||
}
|
||||
|
||||
/// A request for transaction receipts.
|
||||
///
|
||||
/// This request is answered with a list of transaction receipts for each block
|
||||
/// requested.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Receipts {
|
||||
/// Block hashes to return receipts for.
|
||||
pub block_hashes: Vec<H256>,
|
||||
}
|
||||
|
||||
/// A request for state proofs.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct StateProofs {
|
||||
/// Block hash to query state from.
|
||||
pub block: H256,
|
||||
/// Key of the state trie -- corresponds to account hash.
|
||||
pub key1: H256,
|
||||
/// Key in that account's storage trie; if empty, then the account RLP should be
|
||||
/// returned.
|
||||
pub key2: Option<H256>,
|
||||
/// if greater than zero, trie nodes beyond this level may be omitted.
|
||||
pub from_level: u32, // could even safely be u8; trie w/ 32-byte key can be at most 64-levels deep.
|
||||
}
|
||||
|
||||
/// A request for contract code.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ContractCodes {
|
||||
/// Block hash and account key (== sha3(address)) pairs to fetch code for.
|
||||
pub code_requests: Vec<(H256, H256)>,
|
||||
}
|
||||
|
||||
/// A request for header proofs from the Canonical Hash Trie.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct HeaderProofs {
|
||||
/// Number of the CHT.
|
||||
pub cht_number: u64,
|
||||
/// Block number requested.
|
||||
pub block_number: u64,
|
||||
/// If greater than zero, trie nodes beyond this level may be omitted.
|
||||
pub from_level: u32,
|
||||
}
|
||||
|
||||
/// A request for block deltas -- merkle proofs of all changed trie nodes and code.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct BlockDeltas {
|
||||
/// Block hashes deltas are being requested for.
|
||||
pub block_hashes: Vec<H256>,
|
||||
}
|
||||
|
||||
/// A request for a single transaction merkle proof.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TransactionProof {
|
||||
/// The block hash to use the initial state from.
|
||||
pub block_hash: H256,
|
||||
/// The address to treat as the sender of the transaction.
|
||||
pub sender: Address,
|
||||
/// The raw transaction request itself.
|
||||
pub transaction: Transaction,
|
||||
}
|
||||
|
||||
/// A request for transaction merkle proofs.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TransactionProofs {
|
||||
/// Transaction proof requests.
|
||||
pub tx_reqs: Vec<TransactionProof>,
|
||||
}
|
||||
|
||||
/// Kinds of requests.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
/// Requesting headers.
|
||||
Headers,
|
||||
/// Requesting block bodies.
|
||||
Bodies,
|
||||
/// Requesting transaction receipts.
|
||||
Receipts,
|
||||
/// Requesting proofs of state trie nodes.
|
||||
StateProofs,
|
||||
/// Requesting contract code by hash.
|
||||
Codes,
|
||||
/// Requesting header proofs (from the CHT).
|
||||
HeaderProofs,
|
||||
/// Requesting block deltas.
|
||||
Deltas,
|
||||
/// Requesting merkle proofs for transactions.
|
||||
TransactionProofs,
|
||||
}
|
||||
|
||||
/// Encompasses all possible types of requests in a single structure.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Request {
|
||||
/// Requesting headers.
|
||||
Headers(Headers),
|
||||
/// Requesting block bodies.
|
||||
Bodies(Bodies),
|
||||
/// Requesting transaction receipts.
|
||||
Receipts(Receipts),
|
||||
/// Requesting state proofs.
|
||||
StateProofs(StateProofs),
|
||||
/// Requesting contract codes.
|
||||
Codes(ContractCodes),
|
||||
/// Requesting header proofs.
|
||||
HeaderProofs(HeaderProofs),
|
||||
/// Requesting block deltas.
|
||||
Deltas(BlockDeltas),
|
||||
/// Requesting transaction proofs.
|
||||
TransactionProofs(TransactionProofs),
|
||||
}
|
||||
|
||||
impl Request {
|
||||
/// Get the kind of request this is.
|
||||
pub fn kind(&self) -> Kind {
|
||||
match *self {
|
||||
Request::Headers(_) => Kind::Headers,
|
||||
Request::Bodies(_) => Kind::Bodies,
|
||||
Request::Receipts(_) => Kind::Receipts,
|
||||
Request::StateProofs(_) => Kind::StateProofs,
|
||||
Request::Codes(_) => Kind::Codes,
|
||||
Request::HeaderProofs(_) => Kind::HeaderProofs,
|
||||
Request::Deltas(_) => Kind::Deltas,
|
||||
Request::TransactionProofs(_) => Kind::TransactionProofs,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// 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/>.
|
||||
|
||||
//! A light sync target.
|
||||
|
||||
use block_import_error::BlockImportError;
|
||||
use client::BlockChainInfo;
|
||||
|
||||
use util::hash::H256;
|
||||
|
||||
pub trait Sync {
|
||||
/// Whether syncing is enabled.
|
||||
fn enabled(&self) -> bool;
|
||||
|
||||
/// Current chain info.
|
||||
fn chain_info(&self) -> BlockChainInfo;
|
||||
|
||||
/// Import a header.
|
||||
fn import_header(&self, header_bytes: Vec<u8>) -> Result<H256, BlockImportError>;
|
||||
}
|
||||
@@ -33,5 +33,4 @@ pub mod transaction_import;
|
||||
pub mod block_import_error;
|
||||
pub mod restoration_status;
|
||||
pub mod snapshot_manifest;
|
||||
pub mod proof_request;
|
||||
pub mod mode;
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// 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/>.
|
||||
|
||||
//! Merkle proof request.
|
||||
use util::hash::H256;
|
||||
|
||||
/// A request for a state merkle proof.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Binary)]
|
||||
pub enum ProofRequest {
|
||||
/// Request a proof of the given account's (denoted by sha3(address))
|
||||
/// node in the state trie. Nodes with depth less than the second item
|
||||
/// may be omitted.
|
||||
Account(H256, usize),
|
||||
|
||||
/// Request a proof for a key in the given account's storage trie.
|
||||
/// Both values are hashes of their actual values. Nodes with depth
|
||||
/// less than the third item may be omitted.
|
||||
Storage(H256, H256, usize),
|
||||
}
|
||||
|
||||
/// A request for a Canonical Hash Trie proof for the given block number.
|
||||
/// Nodes with depth less than the second item may be omitted.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Binary)]
|
||||
pub struct CHTProofRequest {
|
||||
/// The number of the block the proof is requested for.
|
||||
/// The CHT's number can be deduced from this (`number` / 4096)
|
||||
pub number: u64,
|
||||
|
||||
/// Nodes with depth less than this can be omitted from the proof.
|
||||
pub depth: usize,
|
||||
}
|
||||
Reference in New Issue
Block a user