mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-07 04:58:01 +00:00
Simplify read_client_state() (#1739)
This commit is contained in:
committed by
Bastian Köcher
parent
15e7d828ea
commit
41daa32acb
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
@@ -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)
|
||||
|
||||
@@ -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},
|
||||
@@ -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
|
||||
@@ -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(
|
||||
@@ -408,31 +402,21 @@ 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?;
|
||||
|
||||
@@ -440,7 +424,7 @@ where
|
||||
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,
|
||||
@@ -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>(
|
||||
|
||||
@@ -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;
|
||||
@@ -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(
|
||||
|
||||
@@ -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.
|
||||
@@ -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())
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user