2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-05 16:55:33 +01:00
|
|
|
// 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 provider for the LES protocol. This is typically a full node, who can
|
|
|
|
//! give as much data as necessary to its peers.
|
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2016-12-05 16:55:33 +01:00
|
|
|
use ethcore::blockchain_info::BlockChainInfo;
|
|
|
|
use ethcore::client::{BlockChainClient, ProvingBlockChainClient};
|
2016-12-15 18:19:19 +01:00
|
|
|
use ethcore::transaction::PendingTransaction;
|
2016-12-09 23:01:43 +01:00
|
|
|
use ethcore::ids::BlockId;
|
2016-12-28 13:44:51 +01:00
|
|
|
use ethcore::encoded;
|
2017-03-08 17:37:07 +01:00
|
|
|
use util::{RwLock, H256};
|
2016-12-05 16:55:33 +01:00
|
|
|
|
2017-02-03 18:47:03 +01:00
|
|
|
use cht::{self, BlockInfo};
|
2017-02-09 18:42:18 +01:00
|
|
|
use client::{LightChainClient, AsLightClient};
|
|
|
|
use transaction_queue::TransactionQueue;
|
2017-02-03 18:47:03 +01:00
|
|
|
|
2016-12-05 17:09:05 +01:00
|
|
|
use request;
|
2016-12-05 16:55:33 +01:00
|
|
|
|
2017-03-07 17:18:26 +01:00
|
|
|
/// Defines the operations that a provider for the light subprotocol must fulfill.
|
2016-12-08 23:21:47 +01:00
|
|
|
#[cfg_attr(feature = "ipc", ipc(client_ident="LightProviderClient"))]
|
2016-12-05 16:55:33 +01:00
|
|
|
pub trait Provider: Send + Sync {
|
|
|
|
/// Provide current blockchain info.
|
|
|
|
fn chain_info(&self) -> BlockChainInfo;
|
|
|
|
|
|
|
|
/// Find the depth of a common ancestor between two blocks.
|
|
|
|
/// If either block is unknown or an ancestor can't be found
|
|
|
|
/// then return `None`.
|
|
|
|
fn reorg_depth(&self, a: &H256, b: &H256) -> Option<u64>;
|
|
|
|
|
|
|
|
/// Earliest block where state queries are available.
|
|
|
|
/// If `None`, no state queries are servable.
|
|
|
|
fn earliest_state(&self) -> Option<u64>;
|
|
|
|
|
|
|
|
/// 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.
|
2017-03-06 17:36:56 +01:00
|
|
|
fn block_headers(&self, req: request::CompleteHeadersRequest) -> Option<request::HeadersResponse> {
|
2016-12-11 15:40:31 +01:00
|
|
|
use request::HashOrNumber;
|
2016-12-05 16:55:33 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
if req.max == 0 { return None }
|
2016-12-19 14:54:10 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
let best_num = self.chain_info().best_block_number;
|
|
|
|
let start_num = match req.start {
|
|
|
|
HashOrNumber::Number(start_num) => start_num,
|
|
|
|
HashOrNumber::Hash(hash) => match self.block_header(BlockId::Hash(hash)) {
|
|
|
|
None => {
|
2017-03-09 17:28:49 +01:00
|
|
|
trace!(target: "pip_provider", "Unknown block hash {} requested", hash);
|
2017-03-06 17:36:56 +01:00
|
|
|
return None;
|
2016-12-11 15:40:31 +01:00
|
|
|
}
|
|
|
|
Some(header) => {
|
2016-12-28 13:44:51 +01:00
|
|
|
let num = header.number();
|
2016-12-19 14:54:10 +01:00
|
|
|
let canon_hash = self.block_header(BlockId::Number(num))
|
2016-12-28 13:44:51 +01:00
|
|
|
.map(|h| h.hash());
|
2016-12-19 14:54:10 +01:00
|
|
|
|
|
|
|
if req.max == 1 || canon_hash != Some(hash) {
|
2016-12-11 15:40:31 +01:00
|
|
|
// Non-canonical header or single header requested.
|
2017-03-06 17:36:56 +01:00
|
|
|
return Some(::request::HeadersResponse {
|
|
|
|
headers: vec![header],
|
|
|
|
})
|
2016-12-11 15:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
num
|
|
|
|
}
|
2016-12-05 16:55:33 +01:00
|
|
|
}
|
2016-12-11 15:40:31 +01:00
|
|
|
};
|
2016-12-19 14:54:10 +01:00
|
|
|
|
2017-03-13 13:36:03 +01:00
|
|
|
let headers: Vec<_> = (0u64..req.max as u64)
|
2016-12-08 12:20:18 +01:00
|
|
|
.map(|x: u64| x.saturating_mul(req.skip + 1))
|
2017-01-11 14:39:03 +01:00
|
|
|
.take_while(|x| if req.reverse { x < &start_num } else { best_num.saturating_sub(start_num) >= *x })
|
2016-12-05 16:55:33 +01:00
|
|
|
.map(|x| if req.reverse { start_num - x } else { start_num + x })
|
2016-12-09 23:01:43 +01:00
|
|
|
.map(|x| self.block_header(BlockId::Number(x)))
|
2016-12-05 16:55:33 +01:00
|
|
|
.take_while(|x| x.is_some())
|
|
|
|
.flat_map(|x| x)
|
2017-03-06 17:36:56 +01:00
|
|
|
.collect();
|
|
|
|
|
2017-03-09 17:28:49 +01:00
|
|
|
if headers.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(::request::HeadersResponse { headers: headers })
|
|
|
|
}
|
2016-12-05 16:55:33 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:54:10 +01:00
|
|
|
/// Get a block header by id.
|
2016-12-28 13:44:51 +01:00
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header>;
|
2016-12-19 14:54:10 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
/// Fulfill a block body request.
|
|
|
|
fn block_body(&self, req: request::CompleteBodyRequest) -> Option<request::BodyResponse>;
|
2016-12-05 16:55:33 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
/// Fulfill a request for block receipts.
|
|
|
|
fn block_receipts(&self, req: request::CompleteReceiptsRequest) -> Option<request::ReceiptsResponse>;
|
2016-12-19 14:54:10 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
/// Get an account proof.
|
|
|
|
fn account_proof(&self, req: request::CompleteAccountRequest) -> Option<request::AccountResponse>;
|
2016-12-05 16:55:33 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
/// Get a storage proof.
|
|
|
|
fn storage_proof(&self, req: request::CompleteStorageRequest) -> Option<request::StorageResponse>;
|
2016-12-05 17:09:05 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
/// Provide contract code for the specified (block_hash, code_hash) pair.
|
|
|
|
fn contract_code(&self, req: request::CompleteCodeRequest) -> Option<request::CodeResponse>;
|
2016-12-19 14:54:10 +01:00
|
|
|
|
|
|
|
/// Provide a header proof from a given Canonical Hash Trie as well as the
|
2017-03-06 17:36:56 +01:00
|
|
|
/// corresponding header.
|
|
|
|
fn header_proof(&self, req: request::CompleteHeaderProofRequest) -> Option<request::HeaderProofResponse>;
|
2016-12-19 14:54:10 +01:00
|
|
|
|
|
|
|
/// Provide pending transactions.
|
|
|
|
fn ready_transactions(&self) -> Vec<PendingTransaction>;
|
2017-02-25 00:27:48 +01:00
|
|
|
|
|
|
|
/// Provide a proof-of-execution for the given transaction proof request.
|
|
|
|
/// Returns a vector of all state items necessary to execute the transaction.
|
2017-03-07 19:48:07 +01:00
|
|
|
fn transaction_proof(&self, req: request::CompleteExecutionRequest) -> Option<request::ExecutionResponse>;
|
2016-12-19 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of a light client data provider for a client.
|
|
|
|
impl<T: ProvingBlockChainClient + ?Sized> Provider for T {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
BlockChainClient::chain_info(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reorg_depth(&self, a: &H256, b: &H256) -> Option<u64> {
|
|
|
|
self.tree_route(a, b).map(|route| route.index as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn earliest_state(&self) -> Option<u64> {
|
|
|
|
Some(self.pruning_info().earliest_state)
|
|
|
|
}
|
|
|
|
|
2016-12-28 13:44:51 +01:00
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
2016-12-19 14:54:10 +01:00
|
|
|
BlockChainClient::block_header(self, id)
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:18:26 +01:00
|
|
|
fn block_body(&self, req: request::CompleteBodyRequest) -> Option<request::BodyResponse> {
|
2017-03-07 19:48:07 +01:00
|
|
|
BlockChainClient::block_body(self, BlockId::Hash(req.hash))
|
2017-03-06 17:36:56 +01:00
|
|
|
.map(|body| ::request::BodyResponse { body: body })
|
2016-12-19 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:18:26 +01:00
|
|
|
fn block_receipts(&self, req: request::CompleteReceiptsRequest) -> Option<request::ReceiptsResponse> {
|
2017-03-07 19:48:07 +01:00
|
|
|
BlockChainClient::block_receipts(self, &req.hash)
|
2017-03-22 15:40:58 +01:00
|
|
|
.map(|x| ::request::ReceiptsResponse { receipts: ::rlp::decode_list(&x) })
|
2016-12-19 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
fn account_proof(&self, req: request::CompleteAccountRequest) -> Option<request::AccountResponse> {
|
|
|
|
self.prove_account(req.address_hash, BlockId::Hash(req.block_hash)).map(|(proof, acc)| {
|
|
|
|
::request::AccountResponse {
|
|
|
|
proof: proof,
|
|
|
|
nonce: acc.nonce,
|
|
|
|
balance: acc.balance,
|
|
|
|
code_hash: acc.code_hash,
|
|
|
|
storage_root: acc.storage_root,
|
|
|
|
}
|
2017-03-07 17:18:26 +01:00
|
|
|
})
|
2016-12-19 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
fn storage_proof(&self, req: request::CompleteStorageRequest) -> Option<request::StorageResponse> {
|
2017-03-07 19:48:07 +01:00
|
|
|
self.prove_storage(req.address_hash, req.key_hash, BlockId::Hash(req.block_hash)).map(|(proof, item) | {
|
2017-03-06 17:36:56 +01:00
|
|
|
::request::StorageResponse {
|
|
|
|
proof: proof,
|
|
|
|
value: item,
|
|
|
|
}
|
2017-03-07 17:18:26 +01:00
|
|
|
})
|
2016-12-19 14:54:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 19:48:07 +01:00
|
|
|
fn contract_code(&self, req: request::CompleteCodeRequest) -> Option<request::CodeResponse> {
|
2017-03-06 17:36:56 +01:00
|
|
|
self.state_data(&req.code_hash)
|
|
|
|
.map(|code| ::request::CodeResponse { code: code })
|
|
|
|
}
|
2017-01-16 17:10:30 +01:00
|
|
|
|
2017-03-07 17:18:26 +01:00
|
|
|
fn header_proof(&self, req: request::CompleteHeaderProofRequest) -> Option<request::HeaderProofResponse> {
|
2017-03-06 17:36:56 +01:00
|
|
|
let cht_number = match cht::block_to_cht_number(req.num) {
|
|
|
|
Some(cht_num) => cht_num,
|
|
|
|
None => {
|
2017-03-09 17:28:49 +01:00
|
|
|
debug!(target: "pip_provider", "Requested CHT proof with invalid block number");
|
2017-03-06 17:36:56 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut needed = None;
|
2017-02-03 18:47:03 +01:00
|
|
|
|
|
|
|
// build the CHT, caching the requested header as we pass through it.
|
|
|
|
let cht = {
|
|
|
|
let block_info = |id| {
|
|
|
|
let hdr = self.block_header(id);
|
|
|
|
let td = self.block_total_difficulty(id);
|
|
|
|
|
|
|
|
match (hdr, td) {
|
|
|
|
(Some(hdr), Some(td)) => {
|
|
|
|
let info = BlockInfo {
|
|
|
|
hash: hdr.hash(),
|
|
|
|
parent_hash: hdr.parent_hash(),
|
|
|
|
total_difficulty: td,
|
|
|
|
};
|
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
if hdr.number() == req.num {
|
|
|
|
needed = Some((hdr, td));
|
2017-02-03 18:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(info)
|
2017-01-16 17:10:30 +01:00
|
|
|
}
|
2017-02-03 18:47:03 +01:00
|
|
|
_ => None,
|
2017-01-16 17:10:30 +01:00
|
|
|
}
|
2017-02-03 18:47:03 +01:00
|
|
|
};
|
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
match cht::build(cht_number, block_info) {
|
2017-02-03 18:47:03 +01:00
|
|
|
Some(cht) => cht,
|
|
|
|
None => return None, // incomplete CHT.
|
2017-01-16 17:10:30 +01:00
|
|
|
}
|
2017-02-03 18:47:03 +01:00
|
|
|
};
|
2017-01-16 17:10:30 +01:00
|
|
|
|
2017-03-06 17:36:56 +01:00
|
|
|
let (needed_hdr, needed_td) = needed.expect("`needed` always set in loop, number checked before; qed");
|
2017-01-16 17:10:30 +01:00
|
|
|
|
2017-02-03 18:47:03 +01:00
|
|
|
// prove our result.
|
2017-03-06 17:36:56 +01:00
|
|
|
match cht.prove(req.num, 0) {
|
|
|
|
Ok(Some(proof)) => Some(::request::HeaderProofResponse {
|
|
|
|
proof: proof,
|
|
|
|
hash: needed_hdr.hash(),
|
|
|
|
td: needed_td,
|
|
|
|
}),
|
2017-02-03 18:47:03 +01:00
|
|
|
Ok(None) => None,
|
|
|
|
Err(e) => {
|
2017-03-09 17:28:49 +01:00
|
|
|
debug!(target: "pip_provider", "Error looking up number in freshly-created CHT: {}", e);
|
2017-02-03 18:47:03 +01:00
|
|
|
None
|
|
|
|
}
|
2017-01-16 17:10:30 +01:00
|
|
|
}
|
2016-12-05 16:55:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:18:26 +01:00
|
|
|
fn transaction_proof(&self, req: request::CompleteExecutionRequest) -> Option<request::ExecutionResponse> {
|
2017-02-25 00:27:48 +01:00
|
|
|
use ethcore::transaction::Transaction;
|
|
|
|
|
2017-03-07 19:48:07 +01:00
|
|
|
let id = BlockId::Hash(req.block_hash);
|
2017-02-25 00:27:48 +01:00
|
|
|
let nonce = match self.nonce(&req.from, id.clone()) {
|
|
|
|
Some(nonce) => nonce,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
let transaction = Transaction {
|
|
|
|
nonce: nonce,
|
|
|
|
gas: req.gas,
|
|
|
|
gas_price: req.gas_price,
|
|
|
|
action: req.action,
|
|
|
|
value: req.value,
|
|
|
|
data: req.data,
|
|
|
|
}.fake_sign(req.from);
|
|
|
|
|
|
|
|
self.prove_transaction(transaction, id)
|
2017-03-07 17:18:26 +01:00
|
|
|
.map(|proof| ::request::ExecutionResponse { items: proof })
|
2017-02-25 00:27:48 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 14:54:26 +01:00
|
|
|
fn ready_transactions(&self) -> Vec<PendingTransaction> {
|
|
|
|
BlockChainClient::ready_transactions(self)
|
2016-12-05 16:55:33 +01:00
|
|
|
}
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
2017-01-16 17:42:39 +01:00
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
/// The light client "provider" implementation. This wraps a `LightClient` and
|
|
|
|
/// a light transaction queue.
|
|
|
|
pub struct LightProvider<L> {
|
|
|
|
client: Arc<L>,
|
|
|
|
txqueue: Arc<RwLock<TransactionQueue>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<L> LightProvider<L> {
|
|
|
|
/// Create a new `LightProvider` from the given client and transaction queue.
|
|
|
|
pub fn new(client: Arc<L>, txqueue: Arc<RwLock<TransactionQueue>>) -> Self {
|
|
|
|
LightProvider {
|
|
|
|
client: client,
|
|
|
|
txqueue: txqueue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: draw from cache (shared between this and the RPC layer)
|
|
|
|
impl<L: AsLightClient + Send + Sync> Provider for LightProvider<L> {
|
|
|
|
fn chain_info(&self) -> BlockChainInfo {
|
|
|
|
self.client.as_light_client().chain_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reorg_depth(&self, _a: &H256, _b: &H256) -> Option<u64> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn earliest_state(&self) -> Option<u64> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
|
|
|
self.client.as_light_client().block_header(id)
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn block_body(&self, _req: request::CompleteBodyRequest) -> Option<request::BodyResponse> {
|
2017-03-06 17:36:56 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn block_receipts(&self, _req: request::CompleteReceiptsRequest) -> Option<request::ReceiptsResponse> {
|
2017-02-09 18:42:18 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn account_proof(&self, _req: request::CompleteAccountRequest) -> Option<request::AccountResponse> {
|
2017-02-09 18:42:18 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn storage_proof(&self, _req: request::CompleteStorageRequest) -> Option<request::StorageResponse> {
|
2017-03-06 17:36:56 +01:00
|
|
|
None
|
2017-02-09 18:42:18 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn contract_code(&self, _req: request::CompleteCodeRequest) -> Option<request::CodeResponse> {
|
2017-03-06 17:36:56 +01:00
|
|
|
None
|
2017-02-09 18:42:18 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn header_proof(&self, _req: request::CompleteHeaderProofRequest) -> Option<request::HeaderProofResponse> {
|
2017-02-09 18:42:18 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:48:07 +01:00
|
|
|
fn transaction_proof(&self, _req: request::CompleteExecutionRequest) -> Option<request::ExecutionResponse> {
|
2017-02-25 00:27:48 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-02-09 18:42:18 +01:00
|
|
|
fn ready_transactions(&self) -> Vec<PendingTransaction> {
|
|
|
|
let chain_info = self.chain_info();
|
|
|
|
self.txqueue.read().ready_transactions(chain_info.best_block_number, chain_info.best_block_timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<L: AsLightClient> AsLightClient for LightProvider<L> {
|
|
|
|
type Client = L::Client;
|
|
|
|
|
|
|
|
fn as_light_client(&self) -> &L::Client {
|
|
|
|
self.client.as_light_client()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 17:42:39 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use ethcore::client::{EachBlockWith, TestBlockChainClient};
|
|
|
|
use super::Provider;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cht_proof() {
|
|
|
|
let client = TestBlockChainClient::new();
|
|
|
|
client.add_blocks(2000, EachBlockWith::Nothing);
|
|
|
|
|
2017-03-08 18:27:29 +01:00
|
|
|
let req = ::request::CompleteHeaderProofRequest {
|
|
|
|
num: 1500,
|
2017-01-16 17:42:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(client.header_proof(req.clone()).is_none());
|
|
|
|
|
|
|
|
client.add_blocks(48, EachBlockWith::Nothing);
|
|
|
|
|
|
|
|
assert!(client.header_proof(req.clone()).is_some());
|
|
|
|
}
|
|
|
|
}
|