mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 21:01:02 +00:00
verify that GRANDPA pallet is not initialized before submitting initialization transaction (#1267)
* verify that GRANDPA pallet is not initialized before submitting initialization transaction * spelling
This commit is contained in:
committed by
Bastian Köcher
parent
988f6b1664
commit
bb249eff15
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithMessages, SignParam, TransactionSignScheme,
|
||||
UnsignedTransaction,
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -70,6 +70,10 @@ impl Chain for Kusama {
|
||||
type WeightToFee = bp_kusama::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Kusama {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = bp_kusama::WITH_KUSAMA_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Kusama {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_kusama::WITH_KUSAMA_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::{Compact, Decode, Encode};
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
BalanceOf, Chain, ChainBase, ChainWithBalances, ChainWithMessages, IndexOf, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
BalanceOf, Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, IndexOf,
|
||||
SignParam, TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -54,6 +54,10 @@ impl ChainBase for Millau {
|
||||
}
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Millau {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = bp_millau::WITH_MILLAU_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Millau {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_millau::WITH_MILLAU_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithMessages, SignParam, TransactionSignScheme,
|
||||
UnsignedTransaction,
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -70,6 +70,11 @@ impl Chain for Polkadot {
|
||||
type WeightToFee = bp_polkadot::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Polkadot {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str =
|
||||
bp_polkadot::WITH_POLKADOT_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Polkadot {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_polkadot::WITH_POLKADOT_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::{Compact, Decode, Encode};
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
BalanceOf, Chain, ChainBase, ChainWithBalances, ChainWithMessages, IndexOf, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
BalanceOf, Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, IndexOf,
|
||||
SignParam, TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -69,6 +69,10 @@ impl Chain for Rialto {
|
||||
type WeightToFee = bp_rialto::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Rialto {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = bp_rialto::WITH_RIALTO_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Rialto {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_rialto::WITH_RIALTO_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithMessages, SignParam, TransactionSignScheme,
|
||||
UnsignedTransaction,
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -73,6 +73,10 @@ impl Chain for Rococo {
|
||||
type WeightToFee = bp_rococo::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Rococo {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = bp_rococo::WITH_ROCOCO_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Rococo {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_rococo::WITH_ROCOCO_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -64,6 +64,20 @@ pub trait Chain: ChainBase + Clone {
|
||||
type WeightToFee: WeightToFeePolynomial<Balance = Self::Balance>;
|
||||
}
|
||||
|
||||
/// Substrate-based chain that is using direct GRANDPA finality from minimal relay-client point of
|
||||
/// view.
|
||||
///
|
||||
/// Keep in mind that parachains are relying on relay chain GRANDPA, so they should not implement
|
||||
/// this trait.
|
||||
pub trait ChainWithGrandpa: Chain {
|
||||
/// Name of the bridge GRANDPA pallet (used in `construct_runtime` macro call) that is deployed
|
||||
/// at some other chain to bridge with this `ChainWithGrandpa`.
|
||||
///
|
||||
/// We assume that all chains that are bridging with this `ChainWithGrandpa` are using
|
||||
/// the same name.
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str;
|
||||
}
|
||||
|
||||
/// Substrate-based chain with messaging support from minimal relay-client point of view.
|
||||
pub trait ChainWithMessages: Chain {
|
||||
/// Name of the bridge messages pallet (used in `construct_runtime` macro call) that is deployed
|
||||
|
||||
@@ -32,7 +32,7 @@ use std::time::Duration;
|
||||
pub use crate::{
|
||||
chain::{
|
||||
AccountKeyPairOf, BlockWithJustification, CallOf, Chain, ChainWithBalances,
|
||||
ChainWithMessages, SignParam, TransactionSignScheme, TransactionStatusOf,
|
||||
ChainWithGrandpa, ChainWithMessages, SignParam, TransactionSignScheme, TransactionStatusOf,
|
||||
UnsignedTransaction, WeightToFeeOf,
|
||||
},
|
||||
client::{ChainRuntimeVersion, Client, OpaqueGrandpaAuthoritiesSet, Subscription},
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//! Types used to connect to the Westend chain.
|
||||
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances};
|
||||
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa};
|
||||
use sp_core::storage::StorageKey;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -65,6 +65,11 @@ impl Chain for Westend {
|
||||
type WeightToFee = bp_westend::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Westend {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str =
|
||||
bp_westend::WITH_WESTEND_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithBalances for Westend {
|
||||
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
|
||||
StorageKey(bp_westend::account_info_storage_key(account_id))
|
||||
|
||||
@@ -20,8 +20,8 @@ use bp_messages::MessageNonce;
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use relay_substrate_client::{
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithMessages, SignParam, TransactionSignScheme,
|
||||
UnsignedTransaction,
|
||||
Chain, ChainBase, ChainWithBalances, ChainWithGrandpa, ChainWithMessages, SignParam,
|
||||
TransactionSignScheme, UnsignedTransaction,
|
||||
};
|
||||
use sp_core::{storage::StorageKey, Pair};
|
||||
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
|
||||
@@ -73,6 +73,10 @@ impl Chain for Wococo {
|
||||
type WeightToFee = bp_wococo::WeightToFee;
|
||||
}
|
||||
|
||||
impl ChainWithGrandpa for Wococo {
|
||||
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str = bp_wococo::WITH_WOCOCO_GRANDPA_PALLET_NAME;
|
||||
}
|
||||
|
||||
impl ChainWithMessages for Wococo {
|
||||
const WITH_CHAIN_MESSAGES_PALLET_NAME: &'static str =
|
||||
bp_wococo::WITH_WOCOCO_MESSAGES_PALLET_NAME;
|
||||
|
||||
@@ -55,4 +55,7 @@ pub enum Error<Hash: Debug + MaybeDisplay, HeaderNumber: Debug + MaybeDisplay> {
|
||||
/// Failed to retrieve header by the hash from the source chain.
|
||||
#[error("Failed to retrieve {0} header with hash {1}: {:?}")]
|
||||
RetrieveHeader(&'static str, Hash, client::Error),
|
||||
/// Failed to retrieve best finalized source header hash from the target chain.
|
||||
#[error("Failed to retrieve best finalized {0} header from the target chain: {1}")]
|
||||
RetrieveBestFinalizedHeaderHash(&'static str, client::Error),
|
||||
}
|
||||
|
||||
@@ -31,13 +31,13 @@ use bp_header_chain::{
|
||||
use codec::Decode;
|
||||
use finality_grandpa::voter_set::VoterSet;
|
||||
use num_traits::{One, Zero};
|
||||
use relay_substrate_client::{Chain, Client};
|
||||
use relay_substrate_client::{BlockNumberOf, Chain, ChainWithGrandpa, Client, HashOf};
|
||||
use sp_core::Bytes;
|
||||
use sp_finality_grandpa::AuthorityList as GrandpaAuthoritiesSet;
|
||||
use sp_runtime::traits::Header as HeaderT;
|
||||
|
||||
/// Submit headers-bridge initialization transaction.
|
||||
pub async fn initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
pub async fn initialize<SourceChain: ChainWithGrandpa, TargetChain: Chain>(
|
||||
source_client: Client<SourceChain>,
|
||||
target_client: Client<TargetChain>,
|
||||
target_transactions_signer: TargetChain::AccountId,
|
||||
@@ -54,13 +54,14 @@ pub async fn initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
.await;
|
||||
|
||||
match result {
|
||||
Ok(tx_hash) => log::info!(
|
||||
Ok(Some(tx_hash)) => log::info!(
|
||||
target: "bridge",
|
||||
"Successfully submitted {}-headers bridge initialization transaction to {}: {:?}",
|
||||
SourceChain::NAME,
|
||||
TargetChain::NAME,
|
||||
tx_hash,
|
||||
),
|
||||
Ok(None) => (),
|
||||
Err(err) => log::error!(
|
||||
target: "bridge",
|
||||
"Failed to submit {}-headers bridge initialization transaction to {}: {:?}",
|
||||
@@ -72,14 +73,28 @@ pub async fn initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
}
|
||||
|
||||
/// Craft and submit initialization transaction, returning any error that may occur.
|
||||
async fn do_initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
async fn do_initialize<SourceChain: ChainWithGrandpa, TargetChain: Chain>(
|
||||
source_client: Client<SourceChain>,
|
||||
target_client: Client<TargetChain>,
|
||||
target_transactions_signer: TargetChain::AccountId,
|
||||
prepare_initialize_transaction: impl FnOnce(TargetChain::Index, InitializationData<SourceChain::Header>) -> Bytes
|
||||
+ Send
|
||||
+ 'static,
|
||||
) -> Result<TargetChain::Hash, Error<SourceChain::Hash, <SourceChain::Header as HeaderT>::Number>> {
|
||||
) -> Result<
|
||||
Option<TargetChain::Hash>,
|
||||
Error<SourceChain::Hash, <SourceChain::Header as HeaderT>::Number>,
|
||||
> {
|
||||
let is_initialized = is_initialized::<SourceChain, TargetChain>(&target_client).await?;
|
||||
if is_initialized {
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
"{}-headers bridge at {} is already initialized. Skipping",
|
||||
SourceChain::NAME,
|
||||
TargetChain::NAME,
|
||||
);
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
let initialization_data = prepare_initialization_data(source_client).await?;
|
||||
log::info!(
|
||||
target: "bridge",
|
||||
@@ -95,7 +110,23 @@ async fn do_initialize<SourceChain: Chain, TargetChain: Chain>(
|
||||
})
|
||||
.await
|
||||
.map_err(|err| Error::SubmitTransaction(TargetChain::NAME, err))?;
|
||||
Ok(initialization_tx_hash)
|
||||
Ok(Some(initialization_tx_hash))
|
||||
}
|
||||
|
||||
/// Returns `Ok(true)` if bridge has already been initialized.
|
||||
async fn is_initialized<SourceChain: ChainWithGrandpa, TargetChain: Chain>(
|
||||
target_client: &Client<TargetChain>,
|
||||
) -> Result<bool, Error<HashOf<SourceChain>, BlockNumberOf<SourceChain>>> {
|
||||
Ok(target_client
|
||||
.raw_storage_value(
|
||||
bp_header_chain::storage_keys::best_finalized_hash_key(
|
||||
SourceChain::WITH_CHAIN_GRANDPA_PALLET_NAME,
|
||||
),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| Error::RetrieveBestFinalizedHeaderHash(SourceChain::NAME, err))?
|
||||
.is_some())
|
||||
}
|
||||
|
||||
/// Prepare initialization data for the GRANDPA verifier pallet.
|
||||
|
||||
Reference in New Issue
Block a user