mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 10:01:17 +00:00
removes use of sc_client::Client from sc_network (#5147)
* removes use of sc_client::Client from sc_network * rename BlockProvider to BlockBackend * fix broken test
This commit is contained in:
@@ -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<Block: BlockT>: Send + Sync {
|
||||
/// Get blockchain info.
|
||||
fn info(&self) -> BlockchainInfo<Block>;
|
||||
pub trait Client<Block: BlockT>: HeaderBackend<Block> + ProofProvider<Block> + BlockIdTo<Block, Error = Error>
|
||||
+ BlockBackend<Block> + HeaderMetadata<Block, Error = Error> + Send + Sync
|
||||
{}
|
||||
|
||||
/// Get block status.
|
||||
fn block_status(&self, id: &BlockId<Block>) -> Result<BlockStatus, Error>;
|
||||
|
||||
/// Get block hash by number.
|
||||
fn block_hash(&self, block_number: NumberFor<Block>) -> 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: NumberFor<Block>)
|
||||
-> Result<(Block::Header, StorageProof), Error>;
|
||||
|
||||
/// Get storage read execution proof.
|
||||
fn read_proof(
|
||||
&self,
|
||||
block: &Block::Hash,
|
||||
keys: &mut dyn Iterator<Item=&[u8]>,
|
||||
) -> Result<StorageProof, Error>;
|
||||
|
||||
/// Get child storage read execution proof.
|
||||
fn read_child_proof(
|
||||
&self,
|
||||
block: &Block::Hash,
|
||||
storage_key: &[u8],
|
||||
child_info: ChildInfo,
|
||||
keys: &mut dyn Iterator<Item=&[u8]>,
|
||||
) -> Result<StorageProof, Error>;
|
||||
|
||||
/// Get method execution proof.
|
||||
fn execution_proof(
|
||||
&self,
|
||||
block: &Block::Hash,
|
||||
method: &str,
|
||||
data: &[u8],
|
||||
) -> Result<(Vec<u8>, 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<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>;
|
||||
}
|
||||
impl<Block: BlockT, T> Client<Block> for T
|
||||
where
|
||||
T: HeaderBackend<Block> + ProofProvider<Block> + BlockIdTo<Block, Error = Error>
|
||||
+ BlockBackend<Block> + HeaderMetadata<Block, Error = Error> + Send + Sync
|
||||
{}
|
||||
|
||||
/// Finality proof provider.
|
||||
pub trait FinalityProofProvider<Block: BlockT>: Send + Sync {
|
||||
@@ -99,99 +42,3 @@ impl<Block: BlockT> FinalityProofProvider<Block> for () {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
|
||||
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
|
||||
E: CallExecutor<Block> + Send + Sync + 'static,
|
||||
Self: BlockImport<Block, Error=ConsensusError>,
|
||||
Block: BlockT,
|
||||
RA: Send + Sync
|
||||
{
|
||||
fn info(&self) -> BlockchainInfo<Block> {
|
||||
(self as &SubstrateClient<B, E, Block, RA>).chain_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, StorageProof), Error> {
|
||||
ProofProvider::<Block>::header_proof(self, &BlockId::Number(block_number))
|
||||
}
|
||||
|
||||
fn read_proof(
|
||||
&self,
|
||||
block: &Block::Hash,
|
||||
keys: &mut dyn Iterator<Item=&[u8]>,
|
||||
) -> Result<StorageProof, Error> {
|
||||
ProofProvider::<Block>::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<Item=&[u8]>,
|
||||
) -> Result<StorageProof, Error> {
|
||||
ProofProvider::<Block>::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<u8>, StorageProof), Error> {
|
||||
ProofProvider::<Block>::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<ChangesProof<Block::Header>, Error> {
|
||||
ProofProvider::<Block>::key_changes_proof(self, first, last, min, max, storage_key, key)
|
||||
}
|
||||
|
||||
fn is_descendent_of(&self, base: &Block::Hash, block: &Block::Hash) -> Result<bool, Error> {
|
||||
if base == block {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let ancestor = sp_blockchain::lowest_common_ancestor(self, *block, *base)?;
|
||||
|
||||
Ok(ancestor.hash == *base)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<B: BlockT, H: ExHashT> {
|
||||
/// Assigned roles for our node (full, light, ...).
|
||||
|
||||
@@ -857,7 +857,7 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
/// 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<u8>) {
|
||||
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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
request.id, who, request.storage_key.to_hex::<String>(), 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<B: BlockT, H: ExHashT> Protocol<B, H> {
|
||||
) {
|
||||
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: {}",
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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: {}",
|
||||
|
||||
@@ -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<B: BlockT> ChainSync<B> {
|
||||
pub fn request_justification(&mut self, hash: &B::Hash, number: NumberFor<B>) {
|
||||
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<B: BlockT> ChainSync<B> {
|
||||
pub fn request_finality_proof(&mut self, hash: &B::Hash, number: NumberFor<B>) {
|
||||
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<B: BlockT> ChainSync<B> {
|
||||
}).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<B: BlockT> ChainSync<B> {
|
||||
pub fn on_block_finalized(&mut self, hash: &B::Hash, number: NumberFor<B>) {
|
||||
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<B: BlockT> ChainSync<B> {
|
||||
|
||||
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<B: BlockT>(
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns `true` if the given `block` is a descendent of `base`.
|
||||
fn is_descendent_of<Block, T>(client: &T, base: &Block::Hash, block: &Block::Hash) -> sp_blockchain::Result<bool>
|
||||
where
|
||||
Block: BlockT,
|
||||
T: HeaderMetadata<Block, Error = sp_blockchain::Error> + ?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::*;
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user