Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 998d768

Browse files
skunertgpestana
authored andcommitted
Remove HeaderBackend requirement from AuthorityDiscovery and NetworkWorker (#13730)
* Remove `HeaderBackend` requirement from `NetworkWorker` * Remove HeaderBackend from authority-discovery
1 parent 9522b75 commit 998d768

File tree

8 files changed

+43
-30
lines changed

8 files changed

+43
-30
lines changed

client/authority-discovery/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub type Result<T> = std::result::Result<T, Error>;
2323

2424
/// Error type for the authority discovery module.
2525
#[derive(Debug, thiserror::Error)]
26+
#[allow(missing_docs)]
2627
pub enum Error {
2728
#[error("Received dht value found event with records with different keys.")]
2829
ReceivingDhtValueFoundEventWithDifferentKeys,
@@ -71,4 +72,7 @@ pub enum Error {
7172

7273
#[error("Received authority record without a valid signature for the remote peer id.")]
7374
MissingPeerIdSignature,
75+
76+
#[error("Unable to fetch best block.")]
77+
BestBlockFetchingError,
7478
}

client/authority-discovery/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
//! See [`Worker`] and [`Service`] for more documentation.
2929
3030
pub use crate::{
31+
error::Error,
3132
service::Service,
3233
worker::{AuthorityDiscovery, NetworkProvider, Role, Worker},
3334
};
@@ -148,7 +149,7 @@ pub fn new_worker_and_service_with_config<Client, Network, Block, DhtEventStream
148149
where
149150
Block: BlockT + Unpin + 'static,
150151
Network: NetworkProvider,
151-
Client: AuthorityDiscovery<Block> + HeaderBackend<Block> + 'static,
152+
Client: AuthorityDiscovery<Block> + 'static,
152153
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
153154
{
154155
let (to_worker, from_service) = mpsc::channel(0);

client/authority-discovery/src/worker.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ pub trait AuthorityDiscovery<Block: BlockT> {
157157
/// Retrieve authority identifiers of the current and next authority set.
158158
async fn authorities(&self, at: Block::Hash)
159159
-> std::result::Result<Vec<AuthorityId>, ApiError>;
160+
161+
/// Retrieve best block hash
162+
async fn best_hash(&self) -> std::result::Result<Block::Hash, Error>;
160163
}
161164

162165
#[async_trait::async_trait]
163166
impl<Block, T> AuthorityDiscovery<Block> for T
164167
where
165-
T: ProvideRuntimeApi<Block> + Send + Sync,
168+
T: ProvideRuntimeApi<Block> + HeaderBackend<Block> + Send + Sync,
166169
T::Api: AuthorityDiscoveryApi<Block>,
167170
Block: BlockT,
168171
{
@@ -172,13 +175,17 @@ where
172175
) -> std::result::Result<Vec<AuthorityId>, ApiError> {
173176
self.runtime_api().authorities(at)
174177
}
178+
179+
async fn best_hash(&self) -> std::result::Result<Block::Hash, Error> {
180+
Ok(self.info().best_hash)
181+
}
175182
}
176183

177184
impl<Client, Network, Block, DhtEventStream> Worker<Client, Network, Block, DhtEventStream>
178185
where
179186
Block: BlockT + Unpin + 'static,
180187
Network: NetworkProvider,
181-
Client: AuthorityDiscovery<Block> + HeaderBackend<Block> + 'static,
188+
Client: AuthorityDiscovery<Block> + 'static,
182189
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
183190
{
184191
/// Construct a [`Worker`].
@@ -376,7 +383,7 @@ where
376383
}
377384

378385
async fn refill_pending_lookups_queue(&mut self) -> Result<()> {
379-
let best_hash = self.client.info().best_hash;
386+
let best_hash = self.client.best_hash().await?;
380387

381388
let local_keys = match &self.role {
382389
Role::PublishAndDiscover(key_store) => key_store
@@ -594,7 +601,7 @@ where
594601
.into_iter()
595602
.collect::<HashSet<_>>();
596603

597-
let best_hash = client.info().best_hash;
604+
let best_hash = client.best_hash().await?;
598605
let authorities = client
599606
.authorities(best_hash)
600607
.await

client/network/src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use prometheus_endpoint::Registry;
3434
pub use sc_network_common::{role::Role, sync::warp::WarpSyncProvider, ExHashT};
3535
use zeroize::Zeroize;
3636

37+
use sp_runtime::traits::Block as BlockT;
3738
use std::{
3839
error::Error,
3940
fmt, fs,
@@ -44,7 +45,6 @@ use std::{
4445
path::{Path, PathBuf},
4546
pin::Pin,
4647
str::{self, FromStr},
47-
sync::Arc,
4848
};
4949

5050
pub use libp2p::{
@@ -688,7 +688,7 @@ impl NetworkConfiguration {
688688
}
689689

690690
/// Network initialization parameters.
691-
pub struct Params<Client> {
691+
pub struct Params<Block: BlockT> {
692692
/// Assigned role for our node (full, light, ...).
693693
pub role: Role,
694694

@@ -698,12 +698,12 @@ pub struct Params<Client> {
698698
/// Network layer configuration.
699699
pub network_config: NetworkConfiguration,
700700

701-
/// Client that contains the blockchain.
702-
pub chain: Arc<Client>,
703-
704701
/// Legacy name of the protocol to use on the wire. Should be different for each chain.
705702
pub protocol_id: ProtocolId,
706703

704+
/// Genesis hash of the chain
705+
pub genesis_hash: Block::Hash,
706+
707707
/// Fork ID to distinguish protocols of different hard forks. Part of the standard protocol
708708
/// name on the wire.
709709
pub fork_id: Option<String>,

client/network/src/service.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ use parking_lot::Mutex;
7373
use sc_network_common::ExHashT;
7474
use sc_peerset::PeersetHandle;
7575
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
76-
use sp_blockchain::HeaderBackend;
77-
use sp_runtime::traits::{Block as BlockT, Zero};
76+
use sp_runtime::traits::Block as BlockT;
7877

7978
use std::{
8079
cmp,
@@ -147,9 +146,7 @@ where
147146
/// Returns a `NetworkWorker` that implements `Future` and must be regularly polled in order
148147
/// for the network processing to advance. From it, you can extract a `NetworkService` using
149148
/// `worker.service()`. The `NetworkService` can be shared through the codebase.
150-
pub fn new<Client: HeaderBackend<B> + 'static>(
151-
mut params: Params<Client>,
152-
) -> Result<Self, Error> {
149+
pub fn new<Block: BlockT>(mut params: Params<Block>) -> Result<Self, Error> {
153150
// Private and public keys configuration.
154151
let local_identity = params.network_config.node_key.clone().into_keypair()?;
155152
let local_public = local_identity.public();
@@ -277,13 +274,11 @@ where
277274
config.discovery_limit(
278275
u64::from(params.network_config.default_peers_set.out_peers) + 15,
279276
);
280-
let genesis_hash = params
281-
.chain
282-
.hash(Zero::zero())
283-
.ok()
284-
.flatten()
285-
.expect("Genesis block exists; qed");
286-
config.with_kademlia(genesis_hash, params.fork_id.as_deref(), &params.protocol_id);
277+
config.with_kademlia(
278+
params.genesis_hash,
279+
params.fork_id.as_deref(),
280+
&params.protocol_id,
281+
);
287282
config.with_dht_random_walk(params.network_config.enable_dht_random_walk);
288283
config.allow_non_globals_in_dht(params.network_config.allow_non_globals_in_dht);
289284
config.use_kademlia_disjoint_query_paths(

client/network/test/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use sp_core::H256;
8383
use sp_runtime::{
8484
codec::{Decode, Encode},
8585
generic::BlockId,
86-
traits::{Block as BlockT, Header as HeaderT, NumberFor},
86+
traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero},
8787
Justification, Justifications,
8888
};
8989
use substrate_test_runtime_client::AccountKeyring;
@@ -916,13 +916,15 @@ where
916916
let sync_service_import_queue = Box::new(sync_service.clone());
917917
let sync_service = Arc::new(sync_service.clone());
918918

919-
let network = NetworkWorker::new(sc_network::config::Params {
919+
let genesis_hash =
920+
client.hash(Zero::zero()).ok().flatten().expect("Genesis block exists; qed");
921+
let network = NetworkWorker::new::<Block>(sc_network::config::Params {
920922
role: if config.is_authority { Role::Authority } else { Role::Full },
921923
executor: Box::new(|f| {
922924
tokio::spawn(f);
923925
}),
924926
network_config,
925-
chain: client.clone(),
927+
genesis_hash,
926928
protocol_id,
927929
fork_id,
928930
metrics_registry: None,

client/network/test/src/service.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use sc_network_sync::{
3434
service::network::{NetworkServiceHandle, NetworkServiceProvider},
3535
state_request_handler::StateRequestHandler,
3636
};
37-
use sp_runtime::traits::Block as BlockT;
37+
use sp_blockchain::HeaderBackend;
38+
use sp_runtime::traits::{Block as BlockT, Zero};
3839
use substrate_test_runtime_client::{
3940
runtime::{Block as TestBlock, Hash as TestHash},
4041
TestClientBuilder, TestClientBuilderExt as _,
@@ -194,17 +195,19 @@ impl TestNetworkBuilder {
194195
)
195196
.unwrap();
196197
let mut link = self.link.unwrap_or(Box::new(chain_sync_service.clone()));
198+
let genesis_hash =
199+
client.hash(Zero::zero()).ok().flatten().expect("Genesis block exists; qed");
197200
let worker = NetworkWorker::<
198201
substrate_test_runtime_client::runtime::Block,
199202
substrate_test_runtime_client::runtime::Hash,
200-
>::new(config::Params {
203+
>::new(config::Params::<substrate_test_runtime_client::runtime::Block> {
201204
block_announce_config,
202205
role: config::Role::Full,
203206
executor: Box::new(|f| {
204207
tokio::spawn(f);
205208
}),
209+
genesis_hash,
206210
network_config,
207-
chain: client.clone(),
208211
protocol_id,
209212
fork_id,
210213
metrics_registry: None,

client/service/src/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ where
850850
protocol_config
851851
}));
852852

853-
let mut network_params = sc_network::config::Params {
853+
let genesis_hash = client.hash(Zero::zero()).ok().flatten().expect("Genesis block exists; qed");
854+
let mut network_params = sc_network::config::Params::<TBl> {
854855
role: config.role.clone(),
855856
executor: {
856857
let spawn_handle = Clone::clone(&spawn_handle);
@@ -859,7 +860,7 @@ where
859860
})
860861
},
861862
network_config: config.network.clone(),
862-
chain: client.clone(),
863+
genesis_hash,
863864
protocol_id: protocol_id.clone(),
864865
fork_id: config.chain_spec.fork_id().map(ToOwned::to_owned),
865866
metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()),

0 commit comments

Comments
 (0)