diff --git a/substrate/bin/node/inspect/src/lib.rs b/substrate/bin/node/inspect/src/lib.rs index cd32f08e9f..c82682d602 100644 --- a/substrate/bin/node/inspect/src/lib.rs +++ b/substrate/bin/node/inspect/src/lib.rs @@ -30,7 +30,7 @@ use std::{ marker::PhantomData }; use codec::{Encode, Decode}; -use sc_client_api::BlockBody; +use sc_client_api::BlockBackend; use sp_blockchain::HeaderBackend; use sp_core::hexdisplay::HexDisplay; use sp_runtime::{ @@ -101,12 +101,12 @@ impl std::error::Error for Error { /// A helper trait to access block headers and bodies. pub trait ChainAccess: HeaderBackend + - BlockBody + BlockBackend {} impl ChainAccess for T where TBlock: Block, - T: sp_blockchain::HeaderBackend + sc_client_api::BlockBody, + T: sp_blockchain::HeaderBackend + sc_client_api::BlockBackend, {} /// Blockchain inspector. diff --git a/substrate/client/api/src/call_executor.rs b/substrate/client/api/src/call_executor.rs index f39d797157..3afd29be8d 100644 --- a/substrate/client/api/src/call_executor.rs +++ b/substrate/client/api/src/call_executor.rs @@ -35,6 +35,7 @@ use crate::execution_extensions::ExecutionExtensions; pub trait ExecutorProvider { /// executor instance type Executor: CallExecutor; + /// Get call executor reference. fn executor(&self) -> &Self::Executor; diff --git a/substrate/client/api/src/client.rs b/substrate/client/api/src/client.rs index 22503732be..06d49da640 100644 --- a/substrate/client/api/src/client.rs +++ b/substrate/client/api/src/client.rs @@ -21,7 +21,8 @@ use futures::channel::mpsc; use sp_core::storage::StorageKey; use sp_runtime::{ traits::{Block as BlockT, NumberFor}, - generic::{BlockId, SignedBlock} + generic::{BlockId, SignedBlock}, + Justification, }; use sp_consensus::BlockOrigin; @@ -73,8 +74,8 @@ pub trait BlockchainEvents { ) -> sp_blockchain::Result>; } -/// Fetch block body by ID. -pub trait BlockBody { +/// Interface for fetching block data. +pub trait BlockBackend { /// Get block body by ID. Returns `None` if the body is not stored. fn block_body( &self, @@ -83,6 +84,12 @@ pub trait BlockBody { /// Get full block by id. fn block(&self, id: &BlockId) -> sp_blockchain::Result>>; + + /// Get block status. + fn block_status(&self, id: &BlockId) -> sp_blockchain::Result; + + /// Get block justification set by id. + fn justification(&self, id: &BlockId) -> sp_blockchain::Result>; } /// Provide a list of potential uncle headers for a given block. diff --git a/substrate/client/consensus/babe/rpc/src/lib.rs b/substrate/client/consensus/babe/rpc/src/lib.rs index 1ea7e423dc..ddf6375880 100644 --- a/substrate/client/consensus/babe/rpc/src/lib.rs +++ b/substrate/client/consensus/babe/rpc/src/lib.rs @@ -50,8 +50,6 @@ pub trait BabeApi { } /// Implements the BabeRPC trait for interacting with Babe. -/// -/// Uses a background thread to calculate epoch_authorship data. pub struct BabeRPCHandler { /// shared reference to the client. client: Arc, @@ -74,7 +72,6 @@ impl BabeRPCHandler { babe_config: Config, select_chain: SC, ) -> Self { - Self { client, shared_epoch_changes, diff --git a/substrate/client/network/src/chain.rs b/substrate/client/network/src/chain.rs index 12033e1d51..4e7e28be93 100644 --- a/substrate/client/network/src/chain.rs +++ b/substrate/client/network/src/chain.rs @@ -16,77 +16,20 @@ //! Blockchain access trait -use sc_client::Client as SubstrateClient; -use sp_blockchain::{Error, Info as BlockchainInfo}; -use sc_client_api::{ChangesProof, StorageProof, CallExecutor, ProofProvider}; -use sp_consensus::{BlockImport, BlockStatus, Error as ConsensusError}; -use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor}; -use sp_runtime::generic::{BlockId}; -use sp_runtime::Justification; -use sp_core::storage::{StorageKey, ChildInfo}; +use sp_blockchain::{Error, HeaderBackend, HeaderMetadata}; +use sc_client_api::{BlockBackend, ProofProvider}; +use sp_runtime::traits::{Block as BlockT, BlockIdTo}; /// Local client abstraction for the network. -pub trait Client: Send + Sync { - /// Get blockchain info. - fn info(&self) -> BlockchainInfo; +pub trait Client: HeaderBackend + ProofProvider + BlockIdTo + + BlockBackend + HeaderMetadata + Send + Sync +{} - /// Get block status. - fn block_status(&self, id: &BlockId) -> Result; - - /// Get block hash by number. - fn block_hash(&self, block_number: NumberFor) -> Result, Error>; - - /// Get block header. - fn header(&self, id: &BlockId) -> Result, Error>; - - /// Get block body. - fn body(&self, id: &BlockId) -> Result>, Error>; - - /// Get block justification. - fn justification(&self, id: &BlockId) -> Result, Error>; - - /// Get block header proof. - fn header_proof(&self, block_number: NumberFor) - -> Result<(Block::Header, StorageProof), Error>; - - /// Get storage read execution proof. - fn read_proof( - &self, - block: &Block::Hash, - keys: &mut dyn Iterator, - ) -> Result; - - /// Get child storage read execution proof. - fn read_child_proof( - &self, - block: &Block::Hash, - storage_key: &[u8], - child_info: ChildInfo, - keys: &mut dyn Iterator, - ) -> Result; - - /// Get method execution proof. - fn execution_proof( - &self, - block: &Block::Hash, - method: &str, - data: &[u8], - ) -> Result<(Vec, StorageProof), Error>; - - /// Get key changes proof. - fn key_changes_proof( - &self, - first: Block::Hash, - last: Block::Hash, - min: Block::Hash, - max: Block::Hash, - storage_key: Option<&StorageKey>, - key: &StorageKey - ) -> Result, Error>; - - /// Returns `true` if the given `block` is a descendent of `base`. - fn is_descendent_of(&self, base: &Block::Hash, block: &Block::Hash) -> Result; -} +impl Client for T + where + T: HeaderBackend + ProofProvider + BlockIdTo + + BlockBackend + HeaderMetadata + Send + Sync +{} /// Finality proof provider. pub trait FinalityProofProvider: Send + Sync { @@ -99,99 +42,3 @@ impl FinalityProofProvider for () { Ok(None) } } - -impl Client for SubstrateClient where - B: sc_client_api::backend::Backend + Send + Sync + 'static, - E: CallExecutor + Send + Sync + 'static, - Self: BlockImport, - Block: BlockT, - RA: Send + Sync -{ - fn info(&self) -> BlockchainInfo { - (self as &SubstrateClient).chain_info() - } - - fn block_status(&self, id: &BlockId) -> Result { - (self as &SubstrateClient).block_status(id) - } - - fn block_hash( - &self, - block_number: ::Number, - ) -> Result, Error> { - (self as &SubstrateClient).block_hash(block_number) - } - - fn header(&self, id: &BlockId) -> Result, Error> { - (self as &SubstrateClient).header(id) - } - - fn body(&self, id: &BlockId) -> Result>, Error> { - (self as &SubstrateClient).body(id) - } - - fn justification(&self, id: &BlockId) -> Result, Error> { - (self as &SubstrateClient).justification(id) - } - - fn header_proof( - &self, - block_number: ::Number, - )-> Result<(Block::Header, StorageProof), Error> { - ProofProvider::::header_proof(self, &BlockId::Number(block_number)) - } - - fn read_proof( - &self, - block: &Block::Hash, - keys: &mut dyn Iterator, - ) -> Result { - ProofProvider::::read_proof(self, &BlockId::Hash(block.clone()), keys) - } - - fn read_child_proof( - &self, - block: &Block::Hash, - storage_key: &[u8], - child_info: ChildInfo, - keys: &mut dyn Iterator, - ) -> Result { - ProofProvider::::read_child_proof(self, &BlockId::Hash(block.clone()), storage_key, child_info, keys) - } - - fn execution_proof( - &self, - block: &Block::Hash, - method: &str, - data: &[u8], - ) -> Result<(Vec, StorageProof), Error> { - ProofProvider::::execution_proof( - self, - &BlockId::Hash(block.clone()), - method, - data, - ) - } - - fn key_changes_proof( - &self, - first: Block::Hash, - last: Block::Hash, - min: Block::Hash, - max: Block::Hash, - storage_key: Option<&StorageKey>, - key: &StorageKey, - ) -> Result, Error> { - ProofProvider::::key_changes_proof(self, first, last, min, max, storage_key, key) - } - - fn is_descendent_of(&self, base: &Block::Hash, block: &Block::Hash) -> Result { - if base == block { - return Ok(false); - } - - let ancestor = sp_blockchain::lowest_common_ancestor(self, *block, *base)?; - - Ok(ancestor.hash == *base) - } -} diff --git a/substrate/client/network/src/config.rs b/substrate/client/network/src/config.rs index b7a802b537..3f73d761ce 100644 --- a/substrate/client/network/src/config.rs +++ b/substrate/client/network/src/config.rs @@ -43,6 +43,7 @@ use std::{error::Error, fs, io::{self, Write}, net::Ipv4Addr, path::{Path, PathB use zeroize::Zeroize; use prometheus_endpoint::Registry; + /// Network initialization parameters. pub struct Params { /// Assigned roles for our node (full, light, ...). diff --git a/substrate/client/network/src/protocol.rs b/substrate/client/network/src/protocol.rs index a577513220..2c0cb9df2e 100644 --- a/substrate/client/network/src/protocol.rs +++ b/substrate/client/network/src/protocol.rs @@ -857,7 +857,7 @@ impl Protocol { let get_justification = request .fields .contains(message::BlockAttributes::JUSTIFICATION); - while let Some(header) = self.context_data.chain.header(&id).unwrap_or(None) { + while let Some(header) = self.context_data.chain.header(id).unwrap_or(None) { if blocks.len() >= max { break; } @@ -875,7 +875,7 @@ impl Protocol { body: if get_body { self.context_data .chain - .body(&BlockId::Hash(hash)) + .block_body(&BlockId::Hash(hash)) .unwrap_or(None) } else { None @@ -1300,7 +1300,7 @@ impl Protocol { /// In chain-based consensus, we often need to make sure non-best forks are /// at least temporarily synced. pub fn announce_block(&mut self, hash: B::Hash, data: Vec) { - let header = match self.context_data.chain.header(&BlockId::Hash(hash)) { + let header = match self.context_data.chain.header(BlockId::Hash(hash)) { Ok(Some(header)) => header, Ok(None) => { warn!("Trying to announce unknown block: {}", hash); @@ -1468,7 +1468,7 @@ impl Protocol { request.block ); let proof = match self.context_data.chain.execution_proof( - &request.block, + &BlockId::Hash(request.block), &request.method, &request.data, ) { @@ -1601,7 +1601,7 @@ impl Protocol { trace!(target: "sync", "Remote read request {} from {} ({} at {})", request.id, who, keys_str(), request.block); let proof = match self.context_data.chain.read_proof( - &request.block, + &BlockId::Hash(request.block), &mut request.keys.iter().map(AsRef::as_ref) ) { Ok(proof) => proof, @@ -1650,7 +1650,7 @@ impl Protocol { request.id, who, request.storage_key.to_hex::(), keys_str(), request.block); let proof = if let Some(child_info) = ChildInfo::resolve_child_info(request.child_type, &request.child_info[..]) { match self.context_data.chain.read_child_proof( - &request.block, + &BlockId::Hash(request.block), &request.storage_key, child_info, &mut request.keys.iter().map(AsRef::as_ref), @@ -1708,7 +1708,7 @@ impl Protocol { ) { trace!(target: "sync", "Remote header proof request {} from {} ({})", request.id, who, request.block); - let (header, proof) = match self.context_data.chain.header_proof(request.block) { + let (header, proof) = match self.context_data.chain.header_proof(&BlockId::Number(request.block)) { Ok((header, proof)) => (Some(header), proof), Err(error) => { trace!(target: "sync", "Remote header proof request {} from {} ({}) failed with: {}", diff --git a/substrate/client/network/src/protocol/block_requests.rs b/substrate/client/network/src/protocol/block_requests.rs index 20a99378b1..5a947c0b6b 100644 --- a/substrate/client/network/src/protocol/block_requests.rs +++ b/substrate/client/network/src/protocol/block_requests.rs @@ -192,7 +192,7 @@ where let mut blocks = Vec::new(); let mut block_id = from_block_id; - while let Some(header) = self.chain.header(&block_id).unwrap_or(None) { + while let Some(header) = self.chain.header(block_id).unwrap_or(None) { if blocks.len() >= max_blocks as usize { break } @@ -209,7 +209,7 @@ where Vec::new() }, body: if get_body { - self.chain.body(&BlockId::Hash(hash))? + self.chain.block_body(&BlockId::Hash(hash))? .unwrap_or(Vec::new()) .iter_mut() .map(|extrinsic| extrinsic.encode()) diff --git a/substrate/client/network/src/protocol/light_client_handler.rs b/substrate/client/network/src/protocol/light_client_handler.rs index af8fa89f51..d48bd4b91b 100644 --- a/substrate/client/network/src/protocol/light_client_handler.rs +++ b/substrate/client/network/src/protocol/light_client_handler.rs @@ -57,7 +57,10 @@ use sc_client_api::StorageProof; use sc_peerset::ReputationChange; use sp_core::storage::{ChildInfo, StorageKey}; use sp_blockchain::{Error as ClientError}; -use sp_runtime::traits::{Block, Header, NumberFor, Zero}; +use sp_runtime::{ + traits::{Block, Header, NumberFor, Zero}, + generic::BlockId, +}; use std::{ collections::{BTreeMap, VecDeque, HashMap}, iter, @@ -429,7 +432,7 @@ where let block = Decode::decode(&mut request.block.as_ref())?; - let proof = match self.chain.execution_proof(&block, &request.method, &request.data) { + let proof = match self.chain.execution_proof(&BlockId::Hash(block), &request.method, &request.data) { Ok((_, proof)) => proof, Err(e) => { log::trace!("remote call request from {} ({} at {:?}) failed with: {}", @@ -468,7 +471,7 @@ where let block = Decode::decode(&mut request.block.as_ref())?; - let proof = match self.chain.read_proof(&block, &mut request.keys.iter().map(AsRef::as_ref)) { + let proof = match self.chain.read_proof(&BlockId::Hash(block), &mut request.keys.iter().map(AsRef::as_ref)) { Ok(proof) => proof, Err(error) => { log::trace!("remote read request from {} ({} at {:?}) failed with: {}", @@ -510,7 +513,7 @@ where let proof = if let Some(info) = ChildInfo::resolve_child_info(request.child_type, &request.child_info[..]) { match self.chain.read_child_proof( - &block, + &BlockId::Hash(block), &request.storage_key, info, &mut request.keys.iter().map(AsRef::as_ref) @@ -554,8 +557,7 @@ where log::trace!("remote header proof request from {} ({:?})", peer, request.block); let block = Decode::decode(&mut request.block.as_ref())?; - - let (header, proof) = match self.chain.header_proof(block) { + let (header, proof) = match self.chain.header_proof(&BlockId::Number(block)) { Ok((header, proof)) => (header.encode(), proof), Err(error) => { log::trace!("remote header proof request from {} ({:?}) failed with: {}", diff --git a/substrate/client/network/src/protocol/sync.rs b/substrate/client/network/src/protocol/sync.rs index 04afc5d918..8157440dbc 100644 --- a/substrate/client/network/src/protocol/sync.rs +++ b/substrate/client/network/src/protocol/sync.rs @@ -28,7 +28,7 @@ //! use blocks::BlockCollection; -use sp_blockchain::{Error as ClientError, Info as BlockchainInfo}; +use sp_blockchain::{Error as ClientError, Info as BlockchainInfo, HeaderMetadata}; use sp_consensus::{BlockOrigin, BlockStatus, block_validation::{BlockAnnounceValidator, Validation}, import_queue::{IncomingBlock, BlockImportResult, BlockImportError} @@ -459,7 +459,7 @@ impl ChainSync { pub fn request_justification(&mut self, hash: &B::Hash, number: NumberFor) { let client = &self.client; self.extra_justifications.schedule((*hash, number), |base, block| { - client.is_descendent_of(base, block) + is_descendent_of(&**client, base, block) }) } @@ -467,7 +467,7 @@ impl ChainSync { pub fn request_finality_proof(&mut self, hash: &B::Hash, number: NumberFor) { let client = &self.client; self.extra_finality_proofs.schedule((*hash, number), |base, block| { - client.is_descendent_of(base, block) + is_descendent_of(&**client, base, block) }) } @@ -693,7 +693,7 @@ impl ChainSync { }).collect() } PeerSyncState::AncestorSearch(num, state) => { - let matching_hash = match (blocks.get(0), self.client.block_hash(*num)) { + let matching_hash = match (blocks.get(0), self.client.hash(*num)) { (Some(block), Ok(maybe_our_block_hash)) => { trace!(target: "sync", "Got ancestry block #{} ({}) from peer {}", num, block.hash, who); maybe_our_block_hash.filter(|x| x == &block.hash) @@ -1001,7 +1001,7 @@ impl ChainSync { pub fn on_block_finalized(&mut self, hash: &B::Hash, number: NumberFor) { let client = &self.client; let r = self.extra_finality_proofs.on_block_finalized(hash, number, |base, block| { - client.is_descendent_of(base, block) + is_descendent_of(&**client, base, block) }); if let Err(err) = r { @@ -1010,7 +1010,7 @@ impl ChainSync { let client = &self.client; let r = self.extra_justifications.on_block_finalized(hash, number, |base, block| { - client.is_descendent_of(base, block) + is_descendent_of(&**client, base, block) }); if let Err(err) = r { @@ -1383,6 +1383,21 @@ fn fork_sync_request( None } +/// Returns `true` if the given `block` is a descendent of `base`. +fn is_descendent_of(client: &T, base: &Block::Hash, block: &Block::Hash) -> sp_blockchain::Result + where + Block: BlockT, + T: HeaderMetadata + ?Sized, +{ + if base == block { + return Ok(false); + } + + let ancestor = sp_blockchain::lowest_common_ancestor(client, *block, *base)?; + + Ok(ancestor.hash == *base) +} + #[cfg(test)] mod test { use super::*; diff --git a/substrate/client/network/src/service.rs b/substrate/client/network/src/service.rs index c3b8c8577e..811690f433 100644 --- a/substrate/client/network/src/service.rs +++ b/substrate/client/network/src/service.rs @@ -50,6 +50,7 @@ use crate::protocol::{self, Protocol, PeerInfo}; use crate::protocol::{event::Event, light_dispatch::{AlwaysBadChecker, RequestData}}; use crate::protocol::sync::SyncState; + /// Minimum Requirements for a Hash within Networking pub trait ExHashT: std::hash::Hash + Eq + std::fmt::Debug + Clone + Send + Sync + 'static {} diff --git a/substrate/client/network/test/src/lib.rs b/substrate/client/network/test/src/lib.rs index 06e8c5c334..e98cf8bada 100644 --- a/substrate/client/network/test/src/lib.rs +++ b/substrate/client/network/test/src/lib.rs @@ -29,12 +29,7 @@ use sc_network::config::FinalityProofProvider; use sp_blockchain::{ Result as ClientResult, well_known_cache_keys::{self, Id as CacheKeyId}, Info as BlockchainInfo, }; -use sc_client_api::{ - BlockchainEvents, BlockImportNotification, - FinalityNotifications, ImportNotifications, - FinalityNotification, - backend::{TransactionFor, AuxStore, Backend, Finalizer}, -}; +use sc_client_api::{BlockchainEvents, BlockImportNotification, FinalityNotifications, ImportNotifications, FinalityNotification, backend::{TransactionFor, AuxStore, Backend, Finalizer}, BlockBackend}; use sc_block_builder::{BlockBuilder, BlockBuilderProvider}; use sc_client::LongestChain; use sc_network::config::Roles; diff --git a/substrate/client/rpc/src/chain/chain_full.rs b/substrate/client/rpc/src/chain/chain_full.rs index ea562d4774..c1b062754b 100644 --- a/substrate/client/rpc/src/chain/chain_full.rs +++ b/substrate/client/rpc/src/chain/chain_full.rs @@ -20,7 +20,7 @@ use std::sync::Arc; use rpc::futures::future::result; use sc_rpc_api::Subscriptions; -use sc_client_api::{BlockchainEvents, BlockBody}; +use sc_client_api::{BlockchainEvents, BlockBackend}; use sp_runtime::{generic::{BlockId, SignedBlock}, traits::{Block as BlockT}}; use super::{ChainBackend, client_err, error::FutureResult}; @@ -50,7 +50,7 @@ impl FullChain { impl ChainBackend for FullChain where Block: BlockT + 'static, - Client: BlockBody + HeaderBackend + BlockchainEvents + 'static, + Client: BlockBackend + HeaderBackend + BlockchainEvents + 'static, { fn client(&self) -> &Arc { &self.client diff --git a/substrate/client/rpc/src/chain/mod.rs b/substrate/client/rpc/src/chain/mod.rs index e7a927e780..fa97e21886 100644 --- a/substrate/client/rpc/src/chain/mod.rs +++ b/substrate/client/rpc/src/chain/mod.rs @@ -46,7 +46,7 @@ use self::error::{Result, Error, FutureResult}; pub use sc_rpc_api::chain::*; use sp_blockchain::HeaderBackend; -use sc_client_api::BlockBody; +use sc_client_api::BlockBackend; /// Blockchain backend API trait ChainBackend: Send + Sync + 'static @@ -182,7 +182,7 @@ pub fn new_full( ) -> Chain where Block: BlockT + 'static, - Client: BlockBody + HeaderBackend + BlockchainEvents + 'static, + Client: BlockBackend + HeaderBackend + BlockchainEvents + 'static, { Chain { backend: Box::new(self::chain_full::FullChain::new(client, subscriptions)), @@ -198,7 +198,7 @@ pub fn new_light>( ) -> Chain where Block: BlockT + 'static, - Client: BlockBody + HeaderBackend + BlockchainEvents + 'static, + Client: BlockBackend + HeaderBackend + BlockchainEvents + 'static, F: Send + Sync + 'static, { Chain { diff --git a/substrate/client/service/src/chain_ops.rs b/substrate/client/service/src/chain_ops.rs index 85328d8011..c58a2755dd 100644 --- a/substrate/client/service/src/chain_ops.rs +++ b/substrate/client/service/src/chain_ops.rs @@ -35,7 +35,7 @@ use sp_consensus::{ use sc_executor::{NativeExecutor, NativeExecutionDispatch}; use std::{io::{Read, Write, Seek}, pin::Pin}; -use sc_client_api::BlockBody; +use sc_client_api::BlockBackend; /// Build a chain spec json pub fn build_spec(spec: &dyn ChainSpec, raw: bool) -> error::Result { diff --git a/substrate/client/src/client.rs b/substrate/client/src/client.rs index 0ddd37adb2..dcb889c5fb 100644 --- a/substrate/client/src/client.rs +++ b/substrate/client/src/client.rs @@ -71,7 +71,7 @@ pub use sc_client_api::{ }, client::{ ImportNotifications, FinalityNotification, FinalityNotifications, BlockImportNotification, - ClientInfo, BlockchainEvents, BlockBody, ProvideUncles, BadBlocks, ForkBlocks, + ClientInfo, BlockchainEvents, BlockBackend, ProvideUncles, BadBlocks, ForkBlocks, BlockOf, }, execution_extensions::{ExecutionExtensions, ExecutionStrategies}, @@ -1058,11 +1058,6 @@ impl Client where self.backend.blockchain().body(*id) } - /// Get block justification set by id. - pub fn justification(&self, id: &BlockId) -> sp_blockchain::Result> { - self.backend.blockchain().justification(*id) - } - /// Gets the uncles of the block with `target_hash` going back `max_generation` ancestors. pub fn uncles(&self, target_hash: Block::Hash, max_generation: NumberFor) -> sp_blockchain::Result> { let load_header = |id: Block::Hash| -> sp_blockchain::Result { @@ -1865,7 +1860,7 @@ where } } -impl BlockBody for Client +impl BlockBackend for Client where B: backend::Backend, E: CallExecutor, @@ -1886,6 +1881,33 @@ impl BlockBody for Client _ => None, }) } + + fn block_status(&self, id: &BlockId) -> sp_blockchain::Result { + // this can probably be implemented more efficiently + if let BlockId::Hash(ref h) = id { + if self.importing_block.read().as_ref().map_or(false, |importing| h == importing) { + return Ok(BlockStatus::Queued); + } + } + let hash_and_number = match id.clone() { + BlockId::Hash(hash) => self.backend.blockchain().number(hash)?.map(|n| (hash, n)), + BlockId::Number(n) => self.backend.blockchain().hash(n)?.map(|hash| (hash, n)), + }; + match hash_and_number { + Some((hash, number)) => { + if self.backend.have_state_at(&hash, number) { + Ok(BlockStatus::InChainWithState) + } else { + Ok(BlockStatus::InChainPruned) + } + } + None => Ok(BlockStatus::Unknown), + } + } + + fn justification(&self, id: &BlockId) -> sp_blockchain::Result> { + self.backend.blockchain().justification(*id) + } } impl backend::AuxStore for Client diff --git a/substrate/client/src/lib.rs b/substrate/client/src/lib.rs index 5a15f6ebf7..e5365e2e64 100644 --- a/substrate/client/src/lib.rs +++ b/substrate/client/src/lib.rs @@ -98,7 +98,7 @@ pub use crate::{ client::{ new_with_backend, new_in_mem, - BlockBody, ImportNotifications, FinalityNotifications, BlockchainEvents, LockImportRun, + BlockBackend, ImportNotifications, FinalityNotifications, BlockchainEvents, LockImportRun, BlockImportNotification, Client, ClientInfo, ExecutionStrategies, FinalityNotification, LongestChain, BlockOf, ProvideUncles, BadBlocks, ForkBlocks, apply_aux, }, diff --git a/substrate/client/transaction-pool/src/api.rs b/substrate/client/transaction-pool/src/api.rs index d14f532435..a8888baa48 100644 --- a/substrate/client/transaction-pool/src/api.rs +++ b/substrate/client/transaction-pool/src/api.rs @@ -25,7 +25,7 @@ use futures::{ use sc_client_api::{ blockchain::HeaderBackend, light::{Fetcher, RemoteCallRequest, RemoteBodyRequest}, - BlockBody, + BlockBackend, }; use sp_runtime::{ generic::BlockId, traits::{self, Block as BlockT, BlockIdTo, Header as HeaderT, Hash as HashT}, @@ -63,7 +63,7 @@ impl FullChainApi where impl sc_transaction_graph::ChainApi for FullChainApi where Block: BlockT, - Client: ProvideRuntimeApi + BlockBody + BlockIdTo, + Client: ProvideRuntimeApi + BlockBackend + BlockIdTo, Client: Send + Sync + 'static, Client::Api: TaggedTransactionQueue, sp_api::ApiErrorFor: Send, diff --git a/substrate/primitives/blockchain/src/header_metadata.rs b/substrate/primitives/blockchain/src/header_metadata.rs index 32dd0bcf06..85a94624c9 100644 --- a/substrate/primitives/blockchain/src/header_metadata.rs +++ b/substrate/primitives/blockchain/src/header_metadata.rs @@ -30,7 +30,7 @@ const LRU_CACHE_SIZE: usize = 5_000; /// small branches, and because of our current query pattern: /// lca(best, final), lca(best + 1, final), lca(best + 2, final), etc. /// The first call is O(h) but the others are O(1). -pub fn lowest_common_ancestor>( +pub fn lowest_common_ancestor + ?Sized>( backend: &T, id_one: Block::Hash, id_two: Block::Hash,