From ed06572bd472219afc2f2f5bda9240eb4261a5ef Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 14 Sep 2016 19:23:29 +0200 Subject: [PATCH] Light provider trait --- ethcore/src/light/mod.rs | 2 +- ethcore/src/light/provider.rs | 57 ++++++++++++++++++++++++++++++ ethcore/src/types/mod.rs.in | 1 + ethcore/src/types/proof_request.rs | 44 +++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 ethcore/src/light/provider.rs create mode 100644 ethcore/src/types/proof_request.rs diff --git a/ethcore/src/light/mod.rs b/ethcore/src/light/mod.rs index 100548b46..32d4e9d8a 100644 --- a/ethcore/src/light/mod.rs +++ b/ethcore/src/light/mod.rs @@ -15,4 +15,4 @@ mod provider; mod client; -pub use self::provider::{CHTProofRequest, ProofRequest, Provider}; \ No newline at end of file +pub use self::provider::{CHTProofRequest, ProofRequest, Provider}; diff --git a/ethcore/src/light/provider.rs b/ethcore/src/light/provider.rs new file mode 100644 index 000000000..4833184eb --- /dev/null +++ b/ethcore/src/light/provider.rs @@ -0,0 +1,57 @@ +// 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 . + +//! 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 util::Bytes; +use util::hash::H256; + +/// Defines the operations that a provider for `LES` must fulfill. +/// +/// These are defined at [1], but may be subject to change. +/// +/// [1]: https://github.com/ethcore/parity/wiki/Light-Ethereum-Subprotocol-(LES) +pub trait Provider { + /// Provide a list of headers starting at the requested block, + /// possibly in reverse and skipping `skip` at a time. + /// + /// 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: H256, skip: usize, max: usize, reverse: bool) -> Vec; + + /// Provide as many as possible of the requested blocks (minus the headers) encoded + /// in RLP format. + fn block_bodies(&self, blocks: Vec) -> Vec; + + /// 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) -> Vec; + + /// 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; + + /// Provide contract code for the specified (block_hash, account_hash) pairs. + fn code(&self, accounts: Vec<(H256, H256)>) -> Vec; + + /// Provide header proofs from the Canonical Hash Tries. + fn header_proofs(&self, requests: Vec) -> Vec; +} \ No newline at end of file diff --git a/ethcore/src/types/mod.rs.in b/ethcore/src/types/mod.rs.in index 32c7faabe..c2537135d 100644 --- a/ethcore/src/types/mod.rs.in +++ b/ethcore/src/types/mod.rs.in @@ -33,3 +33,4 @@ pub mod transaction_import; pub mod block_import_error; pub mod restoration_status; pub mod snapshot_manifest; +pub mod proof_request; \ No newline at end of file diff --git a/ethcore/src/types/proof_request.rs b/ethcore/src/types/proof_request.rs new file mode 100644 index 000000000..20bddce21 --- /dev/null +++ b/ethcore/src/types/proof_request.rs @@ -0,0 +1,44 @@ +// 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 . + +//! 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, +} \ No newline at end of file