Skip to content

Commit

Permalink
Simplify read_client_state() (paritytech#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Apr 9, 2024
1 parent f0dd96f commit dd0c16b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 65 deletions.
2 changes: 1 addition & 1 deletion bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<C: Chain> Client<C> {

/// Return number of the best finalized block.
pub async fn best_finalized_header_number(&self) -> Result<C::BlockNumber> {
Ok(*self.header_by_hash(self.best_finalized_header_hash().await?).await?.number())
Ok(*self.best_finalized_header().await?.number())
}

/// Return header of the best finalized block.
Expand Down
5 changes: 2 additions & 3 deletions bridges/relays/lib-substrate-relay/src/finality/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::{
use async_trait::async_trait;
use finality_relay::TargetClient;
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, Chain, Client, Error, HeaderIdOf, HeaderOf, SyncHeader,
TransactionEra, TransactionTracker, UnsignedTransaction,
AccountIdOf, AccountKeyPairOf, Client, Error, HeaderIdOf, HeaderOf, SyncHeader, TransactionEra,
TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
use sp_core::Pair;
Expand Down Expand Up @@ -100,7 +100,6 @@ where
Ok(crate::messages_source::read_client_state::<P::TargetChain, P::SourceChain>(
&self.client,
None,
P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await?
.best_finalized_peer_at_best_self)
Expand Down
53 changes: 15 additions & 38 deletions bridges/relays/lib-substrate-relay/src/messages_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use bp_messages::{
};
use bp_runtime::{BasicOperatingMode, HeaderIdProvider};
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use codec::{Decode, Encode};
use codec::Encode;
use frame_support::weights::Weight;
use messages_relay::{
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
Expand All @@ -47,13 +47,12 @@ use messages_relay::{
};
use num_traits::Zero;
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, BalanceOf, BlockNumberOf, Chain, ChainWithMessages, Client,
AccountIdOf, AccountKeyPairOf, BalanceOf, Chain, ChainWithMessages, Client,
Error as SubstrateError, HashOf, HeaderIdOf, TransactionEra, TransactionTracker,
UnsignedTransaction,
};
use relay_utils::{relay_loop::Client as RelayClient, HeaderId};
use sp_core::{Bytes, Pair};
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned};
use relay_utils::relay_loop::Client as RelayClient;
use sp_core::Pair;
use std::ops::RangeInclusive;

/// Intermediate message proof returned by the source Substrate node. Includes everything
Expand Down Expand Up @@ -155,12 +154,7 @@ where
// we can't relay confirmations if messages pallet at source chain is halted
self.ensure_pallet_active().await?;

read_client_state(
&self.source_client,
Some(&self.target_client),
P::TargetChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await
read_client_state(&self.source_client, Some(&self.target_client)).await
}

async fn latest_generated_nonce(
Expand Down Expand Up @@ -408,39 +402,29 @@ where
pub async fn read_client_state<SelfChain, PeerChain>(
self_client: &Client<SelfChain>,
peer_client: Option<&Client<PeerChain>>,
best_finalized_header_id_method_name: &str,
) -> Result<ClientState<HeaderIdOf<SelfChain>, HeaderIdOf<PeerChain>>, SubstrateError>
where
SelfChain: Chain,
SelfChain::Header: DeserializeOwned,
SelfChain::Index: DeserializeOwned,
PeerChain: Chain,
{
// let's read our state first: we need best finalized header hash on **this** chain
let self_best_finalized_header_hash = self_client.best_finalized_header_hash().await?;
let self_best_finalized_header =
self_client.header_by_hash(self_best_finalized_header_hash).await?;
let self_best_finalized_id = self_best_finalized_header.id();

let self_best_finalized_id = self_client.best_finalized_header().await?.id();
// now let's read our best header on **this** chain
let self_best_header = self_client.best_header().await?;
let self_best_hash = self_best_header.hash();
let self_best_id = self_best_header.id();
let self_best_id = self_client.best_header().await?.id();

// now let's read id of best finalized peer header at our best finalized block
let peer_on_self_best_finalized_id =
best_finalized_peer_header_at_self::<SelfChain, PeerChain>(
self_client,
self_best_hash,
best_finalized_header_id_method_name,
self_best_id.hash(),
)
.await?;

// read actual header, matching the `peer_on_self_best_finalized_id` from the peer chain
let actual_peer_on_self_best_finalized_id = match peer_client {
Some(peer_client) => {
let actual_peer_on_self_best_finalized =
peer_client.header_by_number(peer_on_self_best_finalized_id.0).await?;
peer_client.header_by_number(peer_on_self_best_finalized_id.number()).await?;
actual_peer_on_self_best_finalized.id()
},
None => peer_on_self_best_finalized_id,
Expand All @@ -460,27 +444,20 @@ where
pub async fn best_finalized_peer_header_at_self<SelfChain, PeerChain>(
self_client: &Client<SelfChain>,
at_self_hash: HashOf<SelfChain>,
best_finalized_header_id_method_name: &str,
) -> Result<HeaderIdOf<PeerChain>, SubstrateError>
where
SelfChain: Chain,
PeerChain: Chain,
{
// now let's read id of best finalized peer header at our best finalized block
let encoded_best_finalized_peer_on_self = self_client
.state_call(
best_finalized_header_id_method_name.into(),
Bytes(Vec::new()),
self_client
.typed_state_call::<_, Option<_>>(
PeerChain::BEST_FINALIZED_HEADER_ID_METHOD.into(),
(),
Some(at_self_hash),
)
.await?;

Option::<HeaderId<HashOf<PeerChain>, BlockNumberOf<PeerChain>>>::decode(
&mut &encoded_best_finalized_peer_on_self.0[..],
)
.map_err(SubstrateError::ResponseParseFailed)?
.map(Ok)
.unwrap_or(Err(SubstrateError::BridgePalletIsNotInitialized))
.await?
.ok_or(SubstrateError::BridgePalletIsNotInitialized)
}

fn validate_out_msgs_details<C: Chain>(
Expand Down
9 changes: 2 additions & 7 deletions bridges/relays/lib-substrate-relay/src/messages_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use messages_relay::{
message_lane_loop::{NoncesSubmitArtifacts, TargetClient, TargetClientState},
};
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, BalanceOf, CallOf, Chain, ChainWithMessages, Client,
AccountIdOf, AccountKeyPairOf, BalanceOf, CallOf, ChainWithMessages, Client,
Error as SubstrateError, HashOf, TransactionEra, TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
Expand Down Expand Up @@ -149,12 +149,7 @@ where
// we can't relay messages if messages pallet at target chain is halted
self.ensure_pallet_active().await?;

read_client_state(
&self.target_client,
Some(&self.source_client),
P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await
read_client_state(&self.target_client, Some(&self.source_client)).await
}

async fn latest_received_nonce(
Expand Down
27 changes: 11 additions & 16 deletions bridges/relays/lib-substrate-relay/src/on_demand/parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,11 @@ where
};

let best_target_block_hash = target.best_block().await.map_err(map_target_err)?.1;
let para_header_at_target =
best_finalized_peer_header_at_self::<P::TargetChain, P::SourceParachain>(
target.client(),
best_target_block_hash,
P::SourceParachain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await;
let para_header_at_target = best_finalized_peer_header_at_self::<
P::TargetChain,
P::SourceParachain,
>(target.client(), best_target_block_hash)
.await;
// if there are no parachain heads at the target (`BridgePalletIsNotInitialized`), we'll need
// to submit at least one. Otherwise the pallet will be treated as uninitialized and messages
// sync will stall.
Expand All @@ -496,14 +494,12 @@ where
.map_err(map_source_err)?;

let relay_header_at_source = best_finalized_relay_block_id.0;
let relay_header_at_target =
best_finalized_peer_header_at_self::<P::TargetChain, P::SourceRelayChain>(
target.client(),
best_target_block_hash,
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await
.map_err(map_target_err)?;
let relay_header_at_target = best_finalized_peer_header_at_self::<
P::TargetChain,
P::SourceRelayChain,
>(target.client(), best_target_block_hash)
.await
.map_err(map_target_err)?;

let para_header_at_relay_header_at_target = source
.on_chain_para_head_id(relay_header_at_target, P::SourceParachain::PARACHAIN_ID.into())
Expand Down Expand Up @@ -633,7 +629,6 @@ impl<'a, P: SubstrateParachainsPipeline>
Ok(crate::messages_source::read_client_state::<P::TargetChain, P::SourceRelayChain>(
&self.0.target_client,
None,
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await?
.best_finalized_peer_at_best_self)
Expand Down

0 comments on commit dd0c16b

Please sign in to comment.