mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 21:11:07 +00:00
0cae7217d8
* generalize tree_root to remove client.backend dependency * replace client.backend.blockchain.header with client.header * move used_state_cache_size into client info * Create intermediate Setup State. Fixes #1134 * remove client.backend from finality proof * update node-template * move memory backend into test helper mode * move test helper into client * starting the big refactor, remove unused functions * apply_finality * apply_finality * replacing more .backend from environment with client directly * remove .backend from grandpa by using traits * remove .backend from babe * remove .backend from tests where it is not needed * remove .backend from tests * fixing tests * fixing tests * fixing more tests * fixing tests * fix all forks test * fix style * fixing unnecessary allocation * remove old test. * fix service docs * apply suggestion * minor clean ups * turns out the test-helper features actually is being used! * fixing line length. * fix line length * minor cleaning * Apply suggestions from code review thanks, @Basti Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * address grumbles * simplify finalize block on client * move block back into inner function * Apply suggestions from code review Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com> * use as.ref instead of match * Update core/client/src/backend.rs Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>
147 lines
5.3 KiB
Rust
147 lines
5.3 KiB
Rust
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate 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.
|
|
|
|
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Blockchain access trait
|
|
|
|
use client::{self, Client as SubstrateClient, ClientInfo, BlockStatus, CallExecutor};
|
|
use client::error::Error;
|
|
use client::light::fetcher::ChangesProof;
|
|
use consensus::{BlockImport, Error as ConsensusError};
|
|
use sr_primitives::traits::{Block as BlockT, Header as HeaderT};
|
|
use sr_primitives::generic::{BlockId};
|
|
use sr_primitives::Justification;
|
|
use primitives::{H256, Blake2Hasher, storage::StorageKey};
|
|
|
|
/// Local client abstraction for the network.
|
|
pub trait Client<Block: BlockT>: Send + Sync {
|
|
/// Get blockchain info.
|
|
fn info(&self) -> ClientInfo<Block>;
|
|
|
|
/// Get block status.
|
|
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error>;
|
|
|
|
/// Get block hash by number.
|
|
fn block_hash(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<Option<Block::Hash>, Error>;
|
|
|
|
/// Get block header.
|
|
fn header(&self, id: &BlockId<Block>) -> Result<Option<Block::Header>, Error>;
|
|
|
|
/// Get block body.
|
|
fn body(&self, id: &BlockId<Block>) -> Result<Option<Vec<Block::Extrinsic>>, Error>;
|
|
|
|
/// Get block justification.
|
|
fn justification(&self, id: &BlockId<Block>) -> Result<Option<Justification>, Error>;
|
|
|
|
/// Get block header proof.
|
|
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<(Block::Header, Vec<Vec<u8>>), Error>;
|
|
|
|
/// Get storage read execution proof.
|
|
fn read_proof(&self, block: &Block::Hash, key: &[u8]) -> Result<Vec<Vec<u8>>, Error>;
|
|
|
|
/// Get method execution proof.
|
|
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, Vec<Vec<u8>>), Error>;
|
|
|
|
/// Get key changes proof.
|
|
fn key_changes_proof(
|
|
&self,
|
|
first: Block::Hash,
|
|
last: Block::Hash,
|
|
min: Block::Hash,
|
|
max: Block::Hash,
|
|
key: &StorageKey
|
|
) -> Result<ChangesProof<Block::Header>, Error>;
|
|
|
|
/// Returns `true` if the given `block` is a descendent of `base`.
|
|
fn is_descendent_of(&self, base: &Block::Hash, block: &Block::Hash) -> Result<bool, Error>;
|
|
}
|
|
|
|
/// Finality proof provider.
|
|
pub trait FinalityProofProvider<Block: BlockT>: Send + Sync {
|
|
/// Prove finality of the block.
|
|
fn prove_finality(&self, for_block: Block::Hash, request: &[u8]) -> Result<Option<Vec<u8>>, Error>;
|
|
}
|
|
|
|
impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
|
|
B: client::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
|
|
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static,
|
|
Self: BlockImport<Block, Error=ConsensusError>,
|
|
Block: BlockT<Hash=H256>,
|
|
RA: Send + Sync
|
|
{
|
|
fn info(&self) -> ClientInfo<Block> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).info()
|
|
}
|
|
|
|
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).block_status(id)
|
|
}
|
|
|
|
fn block_hash(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<Option<Block::Hash>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).block_hash(block_number)
|
|
}
|
|
|
|
fn header(&self, id: &BlockId<Block>) -> Result<Option<Block::Header>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).header(id)
|
|
}
|
|
|
|
fn body(&self, id: &BlockId<Block>) -> Result<Option<Vec<Block::Extrinsic>>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).body(id)
|
|
}
|
|
|
|
fn justification(&self, id: &BlockId<Block>) -> Result<Option<Justification>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).justification(id)
|
|
}
|
|
|
|
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<(Block::Header, Vec<Vec<u8>>), Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).header_proof(&BlockId::Number(block_number))
|
|
}
|
|
|
|
fn read_proof(&self, block: &Block::Hash, key: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).read_proof(&BlockId::Hash(block.clone()), key)
|
|
}
|
|
|
|
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, Vec<Vec<u8>>), Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).execution_proof(&BlockId::Hash(block.clone()), method, data)
|
|
}
|
|
|
|
fn key_changes_proof(
|
|
&self,
|
|
first: Block::Hash,
|
|
last: Block::Hash,
|
|
min: Block::Hash,
|
|
max: Block::Hash,
|
|
key: &StorageKey
|
|
) -> Result<ChangesProof<Block::Header>, Error> {
|
|
(self as &SubstrateClient<B, E, Block, RA>).key_changes_proof(first, last, min, max, key)
|
|
}
|
|
|
|
fn is_descendent_of(&self, base: &Block::Hash, block: &Block::Hash) -> Result<bool, Error> {
|
|
if base == block {
|
|
return Ok(false);
|
|
}
|
|
|
|
let tree_route = ::client::blockchain::tree_route(
|
|
|id| self.header(&id)?.ok_or_else(||
|
|
client::error::Error::UnknownBlock(format!("{:?}", id))
|
|
),
|
|
BlockId::Hash(*block),
|
|
BlockId::Hash(*base),
|
|
)?;
|
|
|
|
Ok(tree_route.common_block().hash == *base)
|
|
}
|
|
}
|