Move client consensus parts out of primitives and into client/consensus/api (#9319)

* moved client code out of primitives

* bump ci

* Fixup from merge.

* Removed unused deps thanks to review feedback

* Removing unneeded deps

* updating lock file

* note about rustfmt

* fixed typo to bump ci

* Move lonely CacheKeyId to parent

* cargo fmt

* updating import style

* Update docs/STYLE_GUIDE.md

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Squirrel
2021-07-30 14:27:17 +01:00
committed by GitHub
parent 8a44bec2dc
commit df59596ec0
69 changed files with 339 additions and 283 deletions
+1
View File
@@ -53,6 +53,7 @@ smallvec = "1.5.0"
sp-arithmetic = { version = "4.0.0-dev", path = "../../primitives/arithmetic" }
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
sc-consensus = { version = "0.10.0-dev", path = "../consensus/common" }
sp-core = { version = "4.0.0-dev", path = "../../primitives/core" }
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }
sp-utils = { version = "4.0.0-dev", path = "../../primitives/utils" }
+2 -4
View File
@@ -36,10 +36,8 @@ use libp2p::{
};
use log::debug;
use prost::Message;
use sp_consensus::{
import_queue::{IncomingBlock, Origin},
BlockOrigin,
};
use sc_consensus::import_queue::{IncomingBlock, Origin};
use sp_consensus::BlockOrigin;
use sp_runtime::{
traits::{Block as BlockT, NumberFor},
Justifications,
+2 -1
View File
@@ -19,7 +19,8 @@
//! Blockchain access trait
use sc_client_api::{BlockBackend, ProofProvider};
pub use sc_client_api::{ImportedState, StorageData, StorageKey};
pub use sc_client_api::{StorageData, StorageKey};
pub use sc_consensus::ImportedState;
use sp_blockchain::{Error, HeaderBackend, HeaderMetadata};
use sp_runtime::traits::{Block as BlockT, BlockIdTo};
+2 -1
View File
@@ -44,7 +44,8 @@ use libp2p::{
multiaddr, wasm_ext, Multiaddr, PeerId,
};
use prometheus_endpoint::Registry;
use sp_consensus::{block_validation::BlockAnnounceValidator, import_queue::ImportQueue};
use sc_consensus::ImportQueue;
use sp_consensus::block_validation::BlockAnnounceValidator;
use sp_runtime::traits::Block as BlockT;
use std::{
borrow::Cow,
+5 -5
View File
@@ -50,7 +50,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)
struct PassThroughVerifier(bool);
#[async_trait::async_trait]
impl<B: BlockT> sp_consensus::import_queue::Verifier<B> for PassThroughVerifier {
impl<B: BlockT> sc_consensus::Verifier<B> for PassThroughVerifier {
async fn verify(
&mut self,
origin: sp_consensus::BlockOrigin,
@@ -59,7 +59,7 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)
body: Option<Vec<B::Extrinsic>>,
) -> Result<
(
sp_consensus::BlockImportParams<B, ()>,
sc_consensus::BlockImportParams<B, ()>,
Option<Vec<(sp_blockchain::well_known_cache_keys::Id, Vec<u8>)>>,
),
String,
@@ -79,16 +79,16 @@ fn build_test_full_node(network_config: config::NetworkConfiguration)
)]
});
let mut import = sp_consensus::BlockImportParams::new(origin, header);
let mut import = sc_consensus::BlockImportParams::new(origin, header);
import.body = body;
import.finalized = self.0;
import.justifications = justifications;
import.fork_choice = Some(sp_consensus::ForkChoiceStrategy::LongestChain);
import.fork_choice = Some(sc_consensus::ForkChoiceStrategy::LongestChain);
Ok((import, maybe_keys))
}
}
let import_queue = Box::new(sp_consensus::import_queue::BasicQueue::new(
let import_queue = Box::new(sc_consensus::BasicQueue::new(
PassThroughVerifier(false),
Box::new(client.clone()),
None,
+3 -6
View File
@@ -48,12 +48,9 @@ use message::{
use notifications::{Notifications, NotificationsOut};
use prometheus_endpoint::{register, Gauge, GaugeVec, Opts, PrometheusError, Registry, U64};
use prost::Message as _;
use sc_consensus::import_queue::{BlockImportError, BlockImportStatus, IncomingBlock, Origin};
use sp_arithmetic::traits::SaturatedConversion;
use sp_consensus::{
block_validation::BlockAnnounceValidator,
import_queue::{BlockImportError, BlockImportResult, IncomingBlock, Origin},
BlockOrigin,
};
use sp_consensus::{block_validation::BlockAnnounceValidator, BlockOrigin};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, CheckedSub, Header as HeaderT, NumberFor, Zero},
@@ -1048,7 +1045,7 @@ impl<B: BlockT> Protocol<B> {
&mut self,
imported: usize,
count: usize,
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>,
results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
) {
let results = self.sync.on_blocks_processed(imported, count, results);
for result in results {
@@ -39,11 +39,11 @@ use extra_requests::ExtraRequests;
use futures::{stream::FuturesUnordered, task::Poll, Future, FutureExt, StreamExt};
use libp2p::PeerId;
use log::{debug, error, info, trace, warn};
use sc_consensus::{BlockImportError, BlockImportStatus, IncomingBlock};
use sp_arithmetic::traits::Saturating;
use sp_blockchain::{Error as ClientError, HeaderMetadata};
use sp_consensus::{
block_validation::{BlockAnnounceValidator, Validation},
import_queue::{BlockImportError, BlockImportResult, IncomingBlock},
BlockOrigin, BlockStatus,
};
use sp_runtime::{
@@ -1240,7 +1240,7 @@ impl<B: BlockT> ChainSync<B> {
&'a mut self,
imported: usize,
count: usize,
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>,
results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
) -> impl Iterator<Item = Result<(PeerId, BlockRequest<B>), BadPeer>> + 'a {
trace!(target: "sync", "Imported {} of {}", imported, count);
@@ -1260,12 +1260,12 @@ impl<B: BlockT> ChainSync<B> {
}
match result {
Ok(BlockImportResult::ImportedKnown(number, who)) => {
Ok(BlockImportStatus::ImportedKnown(number, who)) => {
if let Some(peer) = who.and_then(|p| self.peers.get_mut(&p)) {
peer.update_common_number(number);
}
},
Ok(BlockImportResult::ImportedUnknown(number, aux, who)) => {
Ok(BlockImportStatus::ImportedUnknown(number, aux, who)) => {
if aux.clear_justification_requests {
trace!(
target: "sync",
@@ -2454,7 +2454,7 @@ mod test {
///
/// The node is connected to multiple peers. Both of these peers are having a best block (1) that
/// is below our best block (3). Now peer 2 announces a fork of block 3 that we will
/// request from peer 2. After imporitng the fork, peer 2 and then peer 1 will announce block 4.
/// request from peer 2. After importing the fork, peer 2 and then peer 1 will announce block 4.
/// But as peer 1 in our view is still at block 1, we will request block 2 (which we already have)
/// from it. In the meanwhile peer 2 sends us block 4 and 3 and we send another request for block
/// 2 to peer 2. Peer 1 answers with block 2 and then peer 2. This will need to succeed, as we
@@ -2777,7 +2777,7 @@ mod test {
.rev()
.map(|b| {
(
Ok(BlockImportResult::ImportedUnknown(
Ok(BlockImportStatus::ImportedUnknown(
b.header().number().clone(),
Default::default(),
Some(peer_id1.clone()),
+3 -3
View File
@@ -68,8 +68,8 @@ use libp2p::{
use log::{debug, error, info, trace, warn};
use metrics::{Histogram, HistogramVec, MetricSources, Metrics};
use parking_lot::Mutex;
use sc_consensus::{BlockImportError, BlockImportStatus, ImportQueue, Link};
use sc_peerset::PeersetHandle;
use sp_consensus::import_queue::{BlockImportError, BlockImportResult, ImportQueue, Link};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{
@@ -1265,7 +1265,7 @@ impl<'a, B: BlockT + 'static, H: ExHashT> sp_consensus::SyncOracle for &'a Netwo
}
}
impl<B: BlockT, H: ExHashT> sp_consensus::JustificationSyncLink<B> for NetworkService<B, H> {
impl<B: BlockT, H: ExHashT> sc_consensus::JustificationSyncLink<B> for NetworkService<B, H> {
fn request_justification(&self, hash: &B::Hash, number: NumberFor<B>) {
NetworkService::request_justification(self, hash, number);
}
@@ -2104,7 +2104,7 @@ impl<'a, B: BlockT> Link<B> for NetworkLink<'a, B> {
&mut self,
imported: usize,
count: usize,
results: Vec<(Result<BlockImportResult<NumberFor<B>>, BlockImportError>, B::Hash)>,
results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
) {
self.protocol
.behaviour_mut()
@@ -47,7 +47,7 @@ fn build_test_full_node(
struct PassThroughVerifier(bool);
#[async_trait::async_trait]
impl<B: BlockT> sp_consensus::import_queue::Verifier<B> for PassThroughVerifier {
impl<B: BlockT> sc_consensus::Verifier<B> for PassThroughVerifier {
async fn verify(
&mut self,
origin: sp_consensus::BlockOrigin,
@@ -56,7 +56,7 @@ fn build_test_full_node(
body: Option<Vec<B::Extrinsic>>,
) -> Result<
(
sp_consensus::BlockImportParams<B, ()>,
sc_consensus::BlockImportParams<B, ()>,
Option<Vec<(sp_blockchain::well_known_cache_keys::Id, Vec<u8>)>>,
),
String,
@@ -75,16 +75,16 @@ fn build_test_full_node(
vec![(sp_blockchain::well_known_cache_keys::AUTHORITIES, blob.to_vec())]
});
let mut import = sp_consensus::BlockImportParams::new(origin, header);
let mut import = sc_consensus::BlockImportParams::new(origin, header);
import.body = body;
import.finalized = self.0;
import.justifications = justifications;
import.fork_choice = Some(sp_consensus::ForkChoiceStrategy::LongestChain);
import.fork_choice = Some(sc_consensus::ForkChoiceStrategy::LongestChain);
Ok((import, maybe_keys))
}
}
let import_queue = Box::new(sp_consensus::import_queue::BasicQueue::new(
let import_queue = Box::new(sc_consensus::BasicQueue::new(
PassThroughVerifier(false),
Box::new(client.clone()),
None,
@@ -21,12 +21,11 @@
use super::*;
use futures::executor::block_on;
use sc_block_builder::BlockBuilderProvider;
use sp_consensus::{
import_queue::{
import_single_block, BasicQueue, BlockImportError, BlockImportResult, IncomingBlock,
},
ImportedAux,
use sc_consensus::{
import_single_block, BasicQueue, BlockImportError, BlockImportStatus, ImportedAux,
IncomingBlock,
};
use sp_consensus::BlockOrigin;
use sp_runtime::generic::BlockId;
use substrate_test_runtime_client::{
self,
@@ -76,7 +75,7 @@ fn import_single_good_block_works() {
block,
&mut PassThroughVerifier::new(true),
)) {
Ok(BlockImportResult::ImportedUnknown(ref num, ref aux, ref org))
Ok(BlockImportStatus::ImportedUnknown(ref num, ref aux, ref org))
if *num == number && *aux == expected_aux && *org == Some(peer_id) => {},
r @ _ => panic!("{:?}", r),
}
@@ -91,7 +90,7 @@ fn import_single_good_known_block_is_ignored() {
block,
&mut PassThroughVerifier::new(true),
)) {
Ok(BlockImportResult::ImportedKnown(ref n, _)) if *n == number => {},
Ok(BlockImportStatus::ImportedKnown(ref n, _)) if *n == number => {},
_ => panic!(),
}
}
+18 -18
View File
@@ -40,7 +40,10 @@ use sc_client_api::{
BlockBackend, BlockImportNotification, BlockchainEvents, FinalityNotification,
FinalityNotifications, ImportNotifications,
};
use sc_consensus::LongestChain;
use sc_consensus::{
BasicQueue, BlockCheckParams, BlockImport, BlockImportParams, BoxJustificationImport,
ForkChoiceStrategy, ImportResult, JustificationImport, LongestChain, Verifier,
};
pub use sc_network::config::EmptyTransactionPool;
use sc_network::{
block_request_handler::{self, BlockRequestHandler},
@@ -58,11 +61,8 @@ use sp_blockchain::{
HeaderBackend, Info as BlockchainInfo, Result as ClientResult,
};
use sp_consensus::{
block_import::{BlockImport, ImportResult},
block_validation::{BlockAnnounceValidator, DefaultBlockAnnounceValidator},
import_queue::{BasicQueue, BoxJustificationImport, Verifier},
BlockCheckParams, BlockImportParams, BlockOrigin, Error as ConsensusError, ForkChoiceStrategy,
JustificationImport,
BlockOrigin, Error as ConsensusError,
};
use sp_core::H256;
use sp_runtime::{
@@ -152,7 +152,7 @@ pub enum PeersClient {
impl PeersClient {
pub fn as_full(&self) -> Option<Arc<PeersFullClient>> {
match *self {
PeersClient::Full(ref client, ref _backend) => Some(client.clone()),
PeersClient::Full(ref client, _) => Some(client.clone()),
_ => None,
}
}
@@ -163,15 +163,15 @@ impl PeersClient {
pub fn get_aux(&self, key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.get_aux(key),
PeersClient::Light(ref client, ref _backend) => client.get_aux(key),
PeersClient::Full(ref client, _) => client.get_aux(key),
PeersClient::Light(ref client, _) => client.get_aux(key),
}
}
pub fn info(&self) -> BlockchainInfo<Block> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.chain_info(),
PeersClient::Light(ref client, ref _backend) => client.chain_info(),
PeersClient::Full(ref client, _) => client.chain_info(),
PeersClient::Light(ref client, _) => client.chain_info(),
}
}
@@ -180,8 +180,8 @@ impl PeersClient {
block: &BlockId<Block>,
) -> ClientResult<Option<<Block as BlockT>::Header>> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.header(block),
PeersClient::Light(ref client, ref _backend) => client.header(block),
PeersClient::Full(ref client, _) => client.header(block),
PeersClient::Light(ref client, _) => client.header(block),
}
}
@@ -200,22 +200,22 @@ impl PeersClient {
pub fn justifications(&self, block: &BlockId<Block>) -> ClientResult<Option<Justifications>> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.justifications(block),
PeersClient::Light(ref client, ref _backend) => client.justifications(block),
PeersClient::Full(ref client, _) => client.justifications(block),
PeersClient::Light(ref client, _) => client.justifications(block),
}
}
pub fn finality_notification_stream(&self) -> FinalityNotifications<Block> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.finality_notification_stream(),
PeersClient::Light(ref client, ref _backend) => client.finality_notification_stream(),
PeersClient::Full(ref client, _) => client.finality_notification_stream(),
PeersClient::Light(ref client, _) => client.finality_notification_stream(),
}
}
pub fn import_notification_stream(&self) -> ImportNotifications<Block> {
match *self {
PeersClient::Full(ref client, ref _backend) => client.import_notification_stream(),
PeersClient::Light(ref client, ref _backend) => client.import_notification_stream(),
PeersClient::Full(ref client, _) => client.import_notification_stream(),
PeersClient::Light(ref client, _) => client.import_notification_stream(),
}
}