From 2944e1e40faadac6b86c9a5d8d501c7af5f44308 Mon Sep 17 00:00:00 2001 From: Serban Iorga Date: Fri, 30 Dec 2022 10:09:58 +0200 Subject: [PATCH] Simplify read_client_state() (#1739) --- bridges/relays/client-substrate/src/client.rs | 2 +- .../src/finality/target.rs | 5 +- .../src/messages_source.rs | 53 ++++++------------- .../src/messages_target.rs | 9 +--- .../src/on_demand/parachains.rs | 27 ++++------ 5 files changed, 31 insertions(+), 65 deletions(-) diff --git a/bridges/relays/client-substrate/src/client.rs b/bridges/relays/client-substrate/src/client.rs index c6387d6bce6f8..dd763150267ae 100644 --- a/bridges/relays/client-substrate/src/client.rs +++ b/bridges/relays/client-substrate/src/client.rs @@ -254,7 +254,7 @@ impl Client { /// Return number of the best finalized block. pub async fn best_finalized_header_number(&self) -> Result { - 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. diff --git a/bridges/relays/lib-substrate-relay/src/finality/target.rs b/bridges/relays/lib-substrate-relay/src/finality/target.rs index c255c07d148e0..951123ae07c7c 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/target.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/target.rs @@ -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::( &self.client, None, - P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD, ) .await? .best_finalized_peer_at_best_self) diff --git a/bridges/relays/lib-substrate-relay/src/messages_source.rs b/bridges/relays/lib-substrate-relay/src/messages_source.rs index 0e4a2caa258a4..8dceb6df53be7 100644 --- a/bridges/relays/lib-substrate-relay/src/messages_source.rs +++ b/bridges/relays/lib-substrate-relay/src/messages_source.rs @@ -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( self_client: &Client, peer_client: Option<&Client>, - best_finalized_header_id_method_name: &str, ) -> Result, HeaderIdOf>, 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::( 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( self_client: &Client, at_self_hash: HashOf, - best_finalized_header_id_method_name: &str, ) -> Result, 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::, BlockNumberOf>>::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( diff --git a/bridges/relays/lib-substrate-relay/src/messages_target.rs b/bridges/relays/lib-substrate-relay/src/messages_target.rs index 679f9fc140803..75119a1de9768 100644 --- a/bridges/relays/lib-substrate-relay/src/messages_target.rs +++ b/bridges/relays/lib-substrate-relay/src/messages_target.rs @@ -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( diff --git a/bridges/relays/lib-substrate-relay/src/on_demand/parachains.rs b/bridges/relays/lib-substrate-relay/src/on_demand/parachains.rs index cfb9d9c7a9423..6be3fcb9886e0 100644 --- a/bridges/relays/lib-substrate-relay/src/on_demand/parachains.rs +++ b/bridges/relays/lib-substrate-relay/src/on_demand/parachains.rs @@ -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::( - 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::( - 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::( &self.0.target_client, None, - P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD, ) .await? .best_finalized_peer_at_best_self)