mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 15:35:42 +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.
|
/// Return number of the best finalized block.
|
||||||
pub async fn best_finalized_header_number(&self) -> Result<C::BlockNumber> {
|
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.
|
/// Return header of the best finalized block.
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ use crate::{
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use finality_relay::TargetClient;
|
use finality_relay::TargetClient;
|
||||||
use relay_substrate_client::{
|
use relay_substrate_client::{
|
||||||
AccountIdOf, AccountKeyPairOf, Chain, Client, Error, HeaderIdOf, HeaderOf, SyncHeader,
|
AccountIdOf, AccountKeyPairOf, Client, Error, HeaderIdOf, HeaderOf, SyncHeader, TransactionEra,
|
||||||
TransactionEra, TransactionTracker, UnsignedTransaction,
|
TransactionTracker, UnsignedTransaction,
|
||||||
};
|
};
|
||||||
use relay_utils::relay_loop::Client as RelayClient;
|
use relay_utils::relay_loop::Client as RelayClient;
|
||||||
use sp_core::Pair;
|
use sp_core::Pair;
|
||||||
@@ -100,7 +100,6 @@ where
|
|||||||
Ok(crate::messages_source::read_client_state::<P::TargetChain, P::SourceChain>(
|
Ok(crate::messages_source::read_client_state::<P::TargetChain, P::SourceChain>(
|
||||||
&self.client,
|
&self.client,
|
||||||
None,
|
None,
|
||||||
P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD,
|
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
.best_finalized_peer_at_best_self)
|
.best_finalized_peer_at_best_self)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ use bp_messages::{
|
|||||||
};
|
};
|
||||||
use bp_runtime::{BasicOperatingMode, HeaderIdProvider};
|
use bp_runtime::{BasicOperatingMode, HeaderIdProvider};
|
||||||
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
|
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
|
||||||
use codec::{Decode, Encode};
|
use codec::Encode;
|
||||||
use frame_support::weights::Weight;
|
use frame_support::weights::Weight;
|
||||||
use messages_relay::{
|
use messages_relay::{
|
||||||
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
|
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
|
||||||
@@ -47,13 +47,12 @@ use messages_relay::{
|
|||||||
};
|
};
|
||||||
use num_traits::Zero;
|
use num_traits::Zero;
|
||||||
use relay_substrate_client::{
|
use relay_substrate_client::{
|
||||||
AccountIdOf, AccountKeyPairOf, BalanceOf, BlockNumberOf, Chain, ChainWithMessages, Client,
|
AccountIdOf, AccountKeyPairOf, BalanceOf, Chain, ChainWithMessages, Client,
|
||||||
Error as SubstrateError, HashOf, HeaderIdOf, TransactionEra, TransactionTracker,
|
Error as SubstrateError, HashOf, HeaderIdOf, TransactionEra, TransactionTracker,
|
||||||
UnsignedTransaction,
|
UnsignedTransaction,
|
||||||
};
|
};
|
||||||
use relay_utils::{relay_loop::Client as RelayClient, HeaderId};
|
use relay_utils::relay_loop::Client as RelayClient;
|
||||||
use sp_core::{Bytes, Pair};
|
use sp_core::Pair;
|
||||||
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned};
|
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
/// Intermediate message proof returned by the source Substrate node. Includes everything
|
/// 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
|
// we can't relay confirmations if messages pallet at source chain is halted
|
||||||
self.ensure_pallet_active().await?;
|
self.ensure_pallet_active().await?;
|
||||||
|
|
||||||
read_client_state(
|
read_client_state(&self.source_client, Some(&self.target_client)).await
|
||||||
&self.source_client,
|
|
||||||
Some(&self.target_client),
|
|
||||||
P::TargetChain::BEST_FINALIZED_HEADER_ID_METHOD,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn latest_generated_nonce(
|
async fn latest_generated_nonce(
|
||||||
@@ -408,31 +402,21 @@ where
|
|||||||
pub async fn read_client_state<SelfChain, PeerChain>(
|
pub async fn read_client_state<SelfChain, PeerChain>(
|
||||||
self_client: &Client<SelfChain>,
|
self_client: &Client<SelfChain>,
|
||||||
peer_client: Option<&Client<PeerChain>>,
|
peer_client: Option<&Client<PeerChain>>,
|
||||||
best_finalized_header_id_method_name: &str,
|
|
||||||
) -> Result<ClientState<HeaderIdOf<SelfChain>, HeaderIdOf<PeerChain>>, SubstrateError>
|
) -> Result<ClientState<HeaderIdOf<SelfChain>, HeaderIdOf<PeerChain>>, SubstrateError>
|
||||||
where
|
where
|
||||||
SelfChain: Chain,
|
SelfChain: Chain,
|
||||||
SelfChain::Header: DeserializeOwned,
|
|
||||||
SelfChain::Index: DeserializeOwned,
|
|
||||||
PeerChain: Chain,
|
PeerChain: Chain,
|
||||||
{
|
{
|
||||||
// let's read our state first: we need best finalized header hash on **this** 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_id = self_client.best_finalized_header().await?.id();
|
||||||
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();
|
|
||||||
|
|
||||||
// now let's read our best header on **this** chain
|
// now let's read our best header on **this** chain
|
||||||
let self_best_header = self_client.best_header().await?;
|
let self_best_id = self_client.best_header().await?.id();
|
||||||
let self_best_hash = self_best_header.hash();
|
|
||||||
let self_best_id = self_best_header.id();
|
|
||||||
|
|
||||||
// now let's read id of best finalized peer header at our best finalized block
|
// now let's read id of best finalized peer header at our best finalized block
|
||||||
let peer_on_self_best_finalized_id =
|
let peer_on_self_best_finalized_id =
|
||||||
best_finalized_peer_header_at_self::<SelfChain, PeerChain>(
|
best_finalized_peer_header_at_self::<SelfChain, PeerChain>(
|
||||||
self_client,
|
self_client,
|
||||||
self_best_hash,
|
self_best_id.hash(),
|
||||||
best_finalized_header_id_method_name,
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -440,7 +424,7 @@ where
|
|||||||
let actual_peer_on_self_best_finalized_id = match peer_client {
|
let actual_peer_on_self_best_finalized_id = match peer_client {
|
||||||
Some(peer_client) => {
|
Some(peer_client) => {
|
||||||
let actual_peer_on_self_best_finalized =
|
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()
|
actual_peer_on_self_best_finalized.id()
|
||||||
},
|
},
|
||||||
None => 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>(
|
pub async fn best_finalized_peer_header_at_self<SelfChain, PeerChain>(
|
||||||
self_client: &Client<SelfChain>,
|
self_client: &Client<SelfChain>,
|
||||||
at_self_hash: HashOf<SelfChain>,
|
at_self_hash: HashOf<SelfChain>,
|
||||||
best_finalized_header_id_method_name: &str,
|
|
||||||
) -> Result<HeaderIdOf<PeerChain>, SubstrateError>
|
) -> Result<HeaderIdOf<PeerChain>, SubstrateError>
|
||||||
where
|
where
|
||||||
SelfChain: Chain,
|
SelfChain: Chain,
|
||||||
PeerChain: Chain,
|
PeerChain: Chain,
|
||||||
{
|
{
|
||||||
// now let's read id of best finalized peer header at our best finalized block
|
// now let's read id of best finalized peer header at our best finalized block
|
||||||
let encoded_best_finalized_peer_on_self = self_client
|
self_client
|
||||||
.state_call(
|
.typed_state_call::<_, Option<_>>(
|
||||||
best_finalized_header_id_method_name.into(),
|
PeerChain::BEST_FINALIZED_HEADER_ID_METHOD.into(),
|
||||||
Bytes(Vec::new()),
|
(),
|
||||||
Some(at_self_hash),
|
Some(at_self_hash),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?
|
||||||
|
.ok_or(SubstrateError::BridgePalletIsNotInitialized)
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_out_msgs_details<C: Chain>(
|
fn validate_out_msgs_details<C: Chain>(
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ use messages_relay::{
|
|||||||
message_lane_loop::{NoncesSubmitArtifacts, TargetClient, TargetClientState},
|
message_lane_loop::{NoncesSubmitArtifacts, TargetClient, TargetClientState},
|
||||||
};
|
};
|
||||||
use relay_substrate_client::{
|
use relay_substrate_client::{
|
||||||
AccountIdOf, AccountKeyPairOf, BalanceOf, CallOf, Chain, ChainWithMessages, Client,
|
AccountIdOf, AccountKeyPairOf, BalanceOf, CallOf, ChainWithMessages, Client,
|
||||||
Error as SubstrateError, HashOf, TransactionEra, TransactionTracker, UnsignedTransaction,
|
Error as SubstrateError, HashOf, TransactionEra, TransactionTracker, UnsignedTransaction,
|
||||||
};
|
};
|
||||||
use relay_utils::relay_loop::Client as RelayClient;
|
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
|
// we can't relay messages if messages pallet at target chain is halted
|
||||||
self.ensure_pallet_active().await?;
|
self.ensure_pallet_active().await?;
|
||||||
|
|
||||||
read_client_state(
|
read_client_state(&self.target_client, Some(&self.source_client)).await
|
||||||
&self.target_client,
|
|
||||||
Some(&self.source_client),
|
|
||||||
P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn latest_received_nonce(
|
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 best_target_block_hash = target.best_block().await.map_err(map_target_err)?.1;
|
||||||
let para_header_at_target =
|
let para_header_at_target = best_finalized_peer_header_at_self::<
|
||||||
best_finalized_peer_header_at_self::<P::TargetChain, P::SourceParachain>(
|
P::TargetChain,
|
||||||
target.client(),
|
P::SourceParachain,
|
||||||
best_target_block_hash,
|
>(target.client(), best_target_block_hash)
|
||||||
P::SourceParachain::BEST_FINALIZED_HEADER_ID_METHOD,
|
.await;
|
||||||
)
|
|
||||||
.await;
|
|
||||||
// if there are no parachain heads at the target (`BridgePalletIsNotInitialized`), we'll need
|
// 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
|
// to submit at least one. Otherwise the pallet will be treated as uninitialized and messages
|
||||||
// sync will stall.
|
// sync will stall.
|
||||||
@@ -496,14 +494,12 @@ where
|
|||||||
.map_err(map_source_err)?;
|
.map_err(map_source_err)?;
|
||||||
|
|
||||||
let relay_header_at_source = best_finalized_relay_block_id.0;
|
let relay_header_at_source = best_finalized_relay_block_id.0;
|
||||||
let relay_header_at_target =
|
let relay_header_at_target = best_finalized_peer_header_at_self::<
|
||||||
best_finalized_peer_header_at_self::<P::TargetChain, P::SourceRelayChain>(
|
P::TargetChain,
|
||||||
target.client(),
|
P::SourceRelayChain,
|
||||||
best_target_block_hash,
|
>(target.client(), best_target_block_hash)
|
||||||
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD,
|
.await
|
||||||
)
|
.map_err(map_target_err)?;
|
||||||
.await
|
|
||||||
.map_err(map_target_err)?;
|
|
||||||
|
|
||||||
let para_header_at_relay_header_at_target = source
|
let para_header_at_relay_header_at_target = source
|
||||||
.on_chain_para_head_id(relay_header_at_target, P::SourceParachain::PARACHAIN_ID.into())
|
.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>(
|
Ok(crate::messages_source::read_client_state::<P::TargetChain, P::SourceRelayChain>(
|
||||||
&self.0.target_client,
|
&self.0.target_client,
|
||||||
None,
|
None,
|
||||||
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD,
|
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
.best_finalized_peer_at_best_self)
|
.best_finalized_peer_at_best_self)
|
||||||
|
|||||||
Reference in New Issue
Block a user