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

Commit ae4e75b

Browse files
skunertbkchr
authored andcommitted
Remove HeaderBackend from RelayChainRPCClient (#2385)
* Remove HeaderBackend from RelayChainRPCClient * update lockfile for {"substrate", "polkadot"} --------- Co-authored-by: parity-processbot <>
1 parent 12b4cf4 commit ae4e75b

File tree

6 files changed

+34
-124
lines changed

6 files changed

+34
-124
lines changed

Cargo.lock

Lines changed: 5 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/relay-chain-inprocess-interface/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkad
3838
# Polkadot
3939
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.41" }
4040
polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.41" }
41-
metered = { package = "prioritized-metered-channel", version = "0.4.0" }
41+
metered = { package = "prioritized-metered-channel", version = "0.2.0" }
4242

4343
# Cumulus
4444
cumulus-test-service = { path = "../../test/service" }

client/relay-chain-minimal-node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ polkadot-network-bridge = { git = "https://github.com/paritytech/polkadot", bran
1717
# substrate deps
1818
sc-authority-discovery = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }
1919
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }
20+
sc-utils = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }
2021
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }
2122
sc-network-common = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }
2223
sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }

client/relay-chain-minimal-node/src/blockchain_rpc_client.rs

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ use std::pin::Pin;
1818

1919
use cumulus_relay_chain_interface::{RelayChainError, RelayChainResult};
2020
use cumulus_relay_chain_rpc_interface::RelayChainRpcClient;
21-
use futures::{Future, Stream, StreamExt};
21+
use futures::{Stream, StreamExt};
2222
use polkadot_core_primitives::{Block, BlockNumber, Hash, Header};
2323
use polkadot_overseer::RuntimeApiSubsystemClient;
24-
use sc_authority_discovery::AuthorityDiscovery;
24+
use sc_authority_discovery::{AuthorityDiscovery, Error as AuthorityDiscoveryError};
2525
use sp_api::{ApiError, RuntimeApiInfo};
26-
use sp_blockchain::{HeaderBackend, Info};
27-
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
2826

2927
#[derive(Clone)]
3028
pub struct BlockChainRpcClient {
@@ -312,6 +310,14 @@ impl AuthorityDiscovery<Block> for BlockChainRpcClient {
312310
let result = self.rpc_client.authority_discovery_authorities(at).await?;
313311
Ok(result)
314312
}
313+
314+
async fn best_hash(&self) -> std::result::Result<Hash, AuthorityDiscoveryError> {
315+
self.block_get_hash(None)
316+
.await
317+
.ok()
318+
.flatten()
319+
.ok_or_else(|| AuthorityDiscoveryError::BestBlockFetchingError)
320+
}
315321
}
316322

317323
impl BlockChainRpcClient {
@@ -327,67 +333,3 @@ impl BlockChainRpcClient {
327333
Ok(self.rpc_client.get_finalized_heads_stream()?.boxed())
328334
}
329335
}
330-
331-
fn block_local<T>(fut: impl Future<Output = T>) -> T {
332-
let tokio_handle = tokio::runtime::Handle::current();
333-
tokio::task::block_in_place(|| tokio_handle.block_on(fut))
334-
}
335-
336-
impl HeaderBackend<Block> for BlockChainRpcClient {
337-
fn header(
338-
&self,
339-
hash: <Block as BlockT>::Hash,
340-
) -> sp_blockchain::Result<Option<<Block as BlockT>::Header>> {
341-
Ok(block_local(self.rpc_client.chain_get_header(Some(hash)))?)
342-
}
343-
344-
fn info(&self) -> Info<Block> {
345-
let best_header = block_local(self.rpc_client.chain_get_header(None))
346-
.expect("Unable to get header from relay chain.")
347-
.unwrap();
348-
let genesis_hash = block_local(self.rpc_client.chain_get_head(Some(0)))
349-
.expect("Unable to get header from relay chain.");
350-
let finalized_head = block_local(self.rpc_client.chain_get_finalized_head())
351-
.expect("Unable to get finalized head from relay chain.");
352-
let finalized_header = block_local(self.rpc_client.chain_get_header(Some(finalized_head)))
353-
.expect("Unable to get finalized header from relay chain.")
354-
.unwrap();
355-
Info {
356-
best_hash: best_header.hash(),
357-
best_number: best_header.number,
358-
genesis_hash,
359-
finalized_hash: finalized_head,
360-
finalized_number: finalized_header.number,
361-
finalized_state: None,
362-
number_leaves: 1,
363-
block_gap: None,
364-
}
365-
}
366-
367-
fn status(
368-
&self,
369-
hash: <Block as BlockT>::Hash,
370-
) -> sp_blockchain::Result<sp_blockchain::BlockStatus> {
371-
if self.header(hash)?.is_some() {
372-
Ok(sc_client_api::blockchain::BlockStatus::InChain)
373-
} else {
374-
Ok(sc_client_api::blockchain::BlockStatus::Unknown)
375-
}
376-
}
377-
378-
fn number(
379-
&self,
380-
hash: <Block as BlockT>::Hash,
381-
) -> sp_blockchain::Result<Option<<<Block as BlockT>::Header as HeaderT>::Number>> {
382-
let result = block_local(self.rpc_client.chain_get_header(Some(hash)))?
383-
.map(|maybe_header| maybe_header.number);
384-
Ok(result)
385-
}
386-
387-
fn hash(
388-
&self,
389-
number: NumberFor<Block>,
390-
) -> sp_blockchain::Result<Option<<Block as BlockT>::Hash>> {
391-
Ok(block_local(self.rpc_client.chain_get_block_hash(number.into()))?)
392-
}
393-
}

client/relay-chain-minimal-node/src/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use collator_overseer::{CollatorOverseerGenArgs, NewMinimalNode};
1818

1919
use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};
2020
use cumulus_relay_chain_rpc_interface::{RelayChainRpcInterface, Url};
21+
use network::build_collator_network;
2122
use polkadot_network_bridge::{peer_sets_info, IsAuthority};
2223
use polkadot_node_network_protocol::{
2324
peer_set::PeerSetProtocolNames,
@@ -149,14 +150,12 @@ async fn new_minimal_relay_chain(
149150
let (collation_req_receiver, available_data_req_receiver) =
150151
build_request_response_protocol_receivers(&request_protocol_names, &mut config);
151152

153+
let best_header = relay_chain_rpc_client.chain_get_header(None).await?.ok_or_else(|| {
154+
RelayChainError::RpcCallError("Unable to fetch best header".to_string().into())
155+
})?;
152156
let (network, network_starter, sync_oracle) =
153-
network::build_collator_network(network::BuildCollatorNetworkParams {
154-
config: &config,
155-
client: relay_chain_rpc_client.clone(),
156-
spawn_handle: task_manager.spawn_handle(),
157-
genesis_hash,
158-
})
159-
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;
157+
build_collator_network(&config, task_manager.spawn_handle(), genesis_hash, best_header)
158+
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;
160159

161160
let authority_discovery_service = build_authority_discovery_service(
162161
&task_manager,
@@ -180,12 +179,9 @@ async fn new_minimal_relay_chain(
180179
peer_set_protocol_names,
181180
};
182181

183-
let overseer_handle = collator_overseer::spawn_overseer(
184-
overseer_args,
185-
&task_manager,
186-
relay_chain_rpc_client.clone(),
187-
)
188-
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;
182+
let overseer_handle =
183+
collator_overseer::spawn_overseer(overseer_args, &task_manager, relay_chain_rpc_client)
184+
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;
189185

190186
network_starter.start_network();
191187

client/relay-chain-minimal-node/src/network.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use polkadot_core_primitives::{Block, Hash};
17+
use polkadot_core_primitives::{Block, Hash, Header};
1818
use sp_runtime::traits::{Block as BlockT, NumberFor};
1919

2020
use sc_network::{
@@ -24,42 +24,29 @@ use sc_network::{
2424
NetworkService,
2525
};
2626

27-
use sc_client_api::HeaderBackend;
2827
use sc_network_common::{role::Roles, sync::message::BlockAnnouncesHandshake};
2928
use sc_service::{error::Error, Configuration, NetworkStarter, SpawnTaskHandle};
3029
use sc_utils::mpsc::tracing_unbounded;
3130

3231
use std::{iter, sync::Arc};
3332

34-
use crate::BlockChainRpcClient;
35-
36-
pub(crate) struct BuildCollatorNetworkParams<'a> {
37-
/// The service configuration.
38-
pub config: &'a Configuration,
39-
/// A shared client returned by `new_full_parts`.
40-
pub client: Arc<BlockChainRpcClient>,
41-
/// A handle for spawning tasks.
42-
pub spawn_handle: SpawnTaskHandle,
43-
/// Genesis hash
44-
pub genesis_hash: Hash,
45-
}
46-
4733
/// Build the network service, the network status sinks and an RPC sender.
4834
pub(crate) fn build_collator_network(
49-
params: BuildCollatorNetworkParams,
35+
config: &Configuration,
36+
spawn_handle: SpawnTaskHandle,
37+
genesis_hash: Hash,
38+
best_header: Header,
5039
) -> Result<
5140
(Arc<NetworkService<Block, Hash>>, NetworkStarter, Box<dyn sp_consensus::SyncOracle + Send>),
5241
Error,
5342
> {
54-
let BuildCollatorNetworkParams { config, client, spawn_handle, genesis_hash } = params;
55-
5643
let protocol_id = config.protocol_id();
5744
let block_announce_config = get_block_announce_proto_config::<Block>(
5845
protocol_id.clone(),
5946
&None,
6047
Roles::from(&config.role),
61-
client.info().best_number,
62-
client.info().best_hash,
48+
best_header.number,
49+
best_header.hash(),
6350
genesis_hash,
6451
);
6552

@@ -75,7 +62,7 @@ pub(crate) fn build_collator_network(
7562
},
7663
fork_id: None,
7764
network_config: config.network.clone(),
78-
chain: client.clone(),
65+
genesis_hash,
7966
protocol_id,
8067
metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()),
8168
block_announce_config,

0 commit comments

Comments
 (0)