Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Update bridges subtree #2903

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bridges/bin/runtime-common/src/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ macro_rules! assert_chain_types(
// if one of asserts fail, then either bridge isn't configured properly (or alternatively - non-standard
// configuration is used), or something has broke existing configuration (meaning that all bridged chains
// and relays will stop functioning)
use frame_system::{Config as SystemConfig, pallet_prelude::*};
use frame_system::{Config as SystemConfig, pallet_prelude::{BlockNumberFor, HeaderFor}};
use static_assertions::assert_type_eq_all;

assert_type_eq_all!(<$r as SystemConfig>::Nonce, bp_runtime::NonceOf<$this>);
Expand Down
15 changes: 9 additions & 6 deletions bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub type ThisChainHasher = BlakeTwo256;
pub type ThisChainRuntimeCall = RuntimeCall;
/// Runtime call origin at `ThisChain`.
pub type ThisChainCallOrigin = RuntimeOrigin;
// Block of `ThisChain`.
/// Header of `ThisChain`.
pub type ThisChainHeader = sp_runtime::generic::Header<ThisChainBlockNumber, ThisChainHasher>;
/// Block of `ThisChain`.
pub type ThisChainBlock = frame_system::mocking::MockBlockU32<TestRuntime>;

/// Account identifier at the `BridgedChain`.
Expand All @@ -79,8 +81,6 @@ pub type BridgedChainHasher = BlakeTwo256;
/// Header of the `BridgedChain`.
pub type BridgedChainHeader =
sp_runtime::generic::Header<BridgedChainBlockNumber, BridgedChainHasher>;
/// Block of the `BridgedChain`.
pub type BridgedChainBlock = frame_system::mocking::MockBlockU32<TestRuntime>;

/// Rewards payment procedure.
pub type TestPaymentProcedure = PayRewardFromAccount<Balances, ThisChainAccountId>;
Expand Down Expand Up @@ -312,9 +312,10 @@ impl From<BridgedChainOrigin>
pub struct ThisUnderlyingChain;

impl Chain for ThisUnderlyingChain {
type Block = ThisChainBlock;
type BlockNumber = ThisChainBlockNumber;
type Hash = ThisChainHash;
type Hasher = ThisChainHasher;
type Header = ThisChainHeader;
type AccountId = ThisChainAccountId;
type Balance = ThisChainBalance;
type Nonce = u32;
Expand Down Expand Up @@ -351,9 +352,10 @@ pub struct BridgedUnderlyingParachain;
pub struct BridgedChainCall;

impl Chain for BridgedUnderlyingChain {
type Block = BridgedChainBlock;
type BlockNumber = BridgedChainBlockNumber;
type Hash = BridgedChainHash;
type Hasher = BridgedChainHasher;
type Header = BridgedChainHeader;
type AccountId = BridgedChainAccountId;
type Balance = BridgedChainBalance;
type Nonce = u32;
Expand All @@ -376,9 +378,10 @@ impl ChainWithGrandpa for BridgedUnderlyingChain {
}

impl Chain for BridgedUnderlyingParachain {
type Block = BridgedChainBlock;
type BlockNumber = BridgedChainBlockNumber;
type Hash = BridgedChainHash;
type Hasher = BridgedChainHasher;
type Header = BridgedChainHeader;
type AccountId = BridgedChainAccountId;
type Balance = BridgedChainBalance;
type Nonce = u32;
Expand Down
2 changes: 1 addition & 1 deletion bridges/bin/runtime-common/src/parachains_benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
+ pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>,
PI: 'static,
<R as pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>>::BridgedChain:
bp_runtime::Chain<Block = pallet_bridge_parachains::RelayBlock, Hash = RelayBlockHash>,
bp_runtime::Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash>,
{
let parachain_head = ParaHead(vec![0u8; parachain_head_size as usize]);

Expand Down
9 changes: 2 additions & 7 deletions bridges/bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::messages_call_ext::{
};
use bp_messages::{LaneId, MessageNonce};
use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::{Chain, Parachain, ParachainIdOf, RangeInclusiveExt, StaticStrProvider};
use bp_runtime::{Parachain, ParachainIdOf, RangeInclusiveExt, StaticStrProvider};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{CallableCallFor, DispatchInfo, Dispatchable, PostDispatchInfo},
Expand All @@ -47,10 +47,7 @@ use pallet_transaction_payment::{Config as TransactionPaymentConfig, OnChargeTra
use pallet_utility::{Call as UtilityCall, Config as UtilityConfig, Pallet as UtilityPallet};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
Block as BlockT, DispatchInfoOf, Get, Header as HeaderT, PostDispatchInfoOf,
SignedExtension, Zero,
},
traits::{DispatchInfoOf, Get, PostDispatchInfoOf, SignedExtension, Zero},
transaction_validity::{
TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransactionBuilder,
},
Expand Down Expand Up @@ -281,7 +278,6 @@ where
+ GrandpaCallSubType<Runtime, Runtime::BridgesGrandpaPalletInstance>
+ ParachainsCallSubType<Runtime, Para::Instance>
+ MessagesCallSubType<Runtime, Msgs::Instance>,
<<<Runtime as BoundedBridgeGrandpaConfig<Runtime::BridgesGrandpaPalletInstance>>::BridgedRelayChain as Chain>::Block as BlockT>::Header: HeaderT<Number = RelayBlockNumber>
{
fn expand_call<'a>(&self, call: &'a CallOf<Runtime>) -> Vec<&'a CallOf<Runtime>> {
match call.is_sub_type() {
Expand Down Expand Up @@ -529,7 +525,6 @@ where
+ GrandpaCallSubType<Runtime, Runtime::BridgesGrandpaPalletInstance>
+ ParachainsCallSubType<Runtime, Para::Instance>
+ MessagesCallSubType<Runtime, Msgs::Instance>,
<<<Runtime as BoundedBridgeGrandpaConfig<Runtime::BridgesGrandpaPalletInstance>>::BridgedRelayChain as Chain>::Block as BlockT>::Header: HeaderT<Number = RelayBlockNumber>
{
const IDENTIFIER: &'static str = Id::STR;
type AccountId = Runtime::AccountId;
Expand Down
8 changes: 4 additions & 4 deletions bridges/modules/grandpa/src/call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use bp_header_chain::{justification::GrandpaJustification, ChainWithGrandpa};
use bp_runtime::BlockNumberOf;
use codec::Encode;
use frame_support::{dispatch::CallableCallFor, traits::IsSubType, weights::Weight, RuntimeDebug};
use frame_system::pallet_prelude::HeaderFor;
use sp_runtime::{
traits::{Header, Zero},
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
Expand Down Expand Up @@ -179,9 +178,10 @@ pub(crate) fn submit_finality_proof_info_from_args<T: Config<I>, I: 'static>(

/// Returns maximal expected size of `submit_finality_proof` call arguments.
fn max_expected_call_size<T: Config<I>, I: 'static>(required_precommits: u32) -> u32 {
let max_expected_justification_size = GrandpaJustification::<HeaderFor<T>>::max_reasonable_size::<
T::BridgedChain,
>(required_precommits);
let max_expected_justification_size =
GrandpaJustification::<BridgedHeader<T, I>>::max_reasonable_size::<T::BridgedChain>(
required_precommits,
);

// call arguments are header and justification
T::BridgedChain::MAX_HEADER_SIZE.saturating_add(max_expected_justification_size)
Expand Down
52 changes: 11 additions & 41 deletions bridges/modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@
pub use storage_types::StoredAuthoritySet;

use bp_header_chain::{
justification::GrandpaJustification, ChainWithGrandpa, HeaderChain, InitializationData,
StoredHeaderData, StoredHeaderDataBuilder,
justification::GrandpaJustification, ChainWithGrandpa, GrandpaConsensusLogReader, HeaderChain,
InitializationData, StoredHeaderData, StoredHeaderDataBuilder,
};
use bp_runtime::{BlockNumberOf, HashOf, HasherOf, HeaderId, HeaderOf, OwnedBridgeModule};
use finality_grandpa::voter_set::VoterSet;
use frame_support::{dispatch::PostDispatchInfo, ensure, DefaultNoBound};
use sp_consensus_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
use sp_runtime::{
traits::{Header as HeaderT, Zero},
SaturatedConversion,
Expand Down Expand Up @@ -443,11 +442,17 @@ pub mod pallet {

// We don't support forced changes - at that point governance intervention is required.
ensure!(
super::find_forced_change(header).is_none(),
GrandpaConsensusLogReader::<BridgedBlockNumber<T, I>>::find_forced_change(
header.digest()
)
.is_none(),
<Error<T, I>>::UnsupportedScheduledChange
);

if let Some(change) = super::find_scheduled_change(header) {
if let Some(change) =
GrandpaConsensusLogReader::<BridgedBlockNumber<T, I>>::find_scheduled_change(
header.digest(),
) {
// GRANDPA only includes a `delay` for forced changes, so this isn't valid.
ensure!(change.delay == Zero::zero(), <Error<T, I>>::UnsupportedScheduledChange);

Expand Down Expand Up @@ -616,42 +621,6 @@ impl<T: Config<I>, I: 'static> HeaderChain<BridgedChain<T, I>> for GrandpaChainH
}
}

pub(crate) fn find_scheduled_change<H: HeaderT>(
header: &H,
) -> Option<sp_consensus_grandpa::ScheduledChange<H::Number>> {
use sp_runtime::generic::OpaqueDigestItemId;

let id = OpaqueDigestItemId::Consensus(&GRANDPA_ENGINE_ID);

let filter_log = |log: ConsensusLog<H::Number>| match log {
ConsensusLog::ScheduledChange(change) => Some(change),
_ => None,
};

// find the first consensus digest with the right ID which converts to
// the right kind of consensus log.
header.digest().convert_first(|l| l.try_to(id).and_then(filter_log))
}

/// Checks the given header for a consensus digest signaling a **forced** scheduled change and
/// extracts it.
pub(crate) fn find_forced_change<H: HeaderT>(
header: &H,
) -> Option<(H::Number, sp_consensus_grandpa::ScheduledChange<H::Number>)> {
use sp_runtime::generic::OpaqueDigestItemId;

let id = OpaqueDigestItemId::Consensus(&GRANDPA_ENGINE_ID);

let filter_log = |log: ConsensusLog<H::Number>| match log {
ConsensusLog::ForcedChange(delay, change) => Some((delay, change)),
_ => None,
};

// find the first consensus digest with the right ID which converts to
// the right kind of consensus log.
header.digest().convert_first(|l| l.try_to(id).and_then(filter_log))
}

/// (Re)initialize bridge with given header for using it in `pallet-bridge-messages` benchmarks.
#[cfg(feature = "runtime-benchmarks")]
pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader<T, I>) {
Expand Down Expand Up @@ -685,6 +654,7 @@ mod tests {
storage::generator::StorageValue,
};
use frame_system::{EventRecord, Phase};
use sp_consensus_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
use sp_core::Get;
use sp_runtime::{Digest, DigestItem, DispatchError};

Expand Down
7 changes: 4 additions & 3 deletions bridges/modules/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use sp_runtime::{
};

pub type AccountId = u64;
pub type TestHeader = crate::BridgedHeader<TestRuntime, ()>;
pub type TestNumber = crate::BridgedBlockNumber<TestRuntime, ()>;
pub type TestHeader = sp_runtime::testing::Header;
pub type TestNumber = u64;

type Block = frame_system::mocking::MockBlock<TestRuntime>;

Expand Down Expand Up @@ -100,9 +100,10 @@ impl grandpa::Config for TestRuntime {
pub struct TestBridgedChain;

impl Chain for TestBridgedChain {
type Block = Block;
type BlockNumber = TestNumber;
type Hash = <TestRuntime as frame_system::Config>::Hash;
type Hasher = <TestRuntime as frame_system::Config>::Hashing;
type Header = TestHeader;

type AccountId = AccountId;
type Balance = u64;
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/parachains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ benchmarks_instance_pallet! {
where
<T as pallet_bridge_grandpa::Config<T::BridgesGrandpaPalletInstance>>::BridgedChain:
bp_runtime::Chain<
Block = crate::RelayBlock,
BlockNumber = RelayBlockNumber,
Hash = RelayBlockHash,
Hasher = RelayBlockHasher,
>,
Expand Down
15 changes: 8 additions & 7 deletions bridges/modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ pub type RelayBlockHash = bp_polkadot_core::Hash;
pub type RelayBlockNumber = bp_polkadot_core::BlockNumber;
/// Hasher of the bridged relay chain.
pub type RelayBlockHasher = bp_polkadot_core::Hasher;
/// Block type of the bridged relay chain.
pub type RelayBlock = bp_polkadot_core::Block;

/// Artifacts of the parachains head update.
struct UpdateParachainHeadArtifacts {
Expand Down Expand Up @@ -139,15 +137,18 @@ pub mod pallet {
pub trait BoundedBridgeGrandpaConfig<I: 'static>:
pallet_bridge_grandpa::Config<I, BridgedChain = Self::BridgedRelayChain>
{
type BridgedRelayChain: Chain<Hash = RelayBlockHash, Hasher = RelayBlockHasher>;
type BridgedRelayChain: Chain<
BlockNumber = RelayBlockNumber,
Hash = RelayBlockHash,
Hasher = RelayBlockHasher,
>;
}

impl<T, I: 'static> BoundedBridgeGrandpaConfig<I> for T
where
T: pallet_bridge_grandpa::Config<I>,
T::BridgedChain: Chain<Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
<<T::BridgedChain as Chain>::Block as sp_runtime::traits::Block>::Header:
sp_runtime::traits::Header<Number = RelayBlockNumber>,
T::BridgedChain:
Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
{
type BridgedRelayChain = T::BridgedChain;
}
Expand Down Expand Up @@ -322,7 +323,7 @@ pub mod pallet {
>::get(relay_block_hash)
.ok_or(Error::<T, I>::UnknownRelayChainBlock)?;
ensure!(
relay_block.number == relay_block_number.into(),
relay_block.number == relay_block_number,
Error::<T, I>::InvalidRelayChainBlockNumber,
);

Expand Down
24 changes: 14 additions & 10 deletions bridges/modules/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bp_runtime::{Chain, Parachain};
use frame_support::{construct_runtime, parameter_types, traits::ConstU32, weights::Weight};
use sp_runtime::{
testing::H256,
traits::{BlakeTwo256, Header, IdentityLookup},
traits::{BlakeTwo256, Header as HeaderT, IdentityLookup},
MultiSignature, Perbill,
};

Expand Down Expand Up @@ -48,9 +48,10 @@ pub type BigParachainHeader = sp_runtime::generic::Header<u128, BlakeTwo256>;
pub struct Parachain1;

impl Chain for Parachain1 {
type Block = Block;
type BlockNumber = u64;
type Hash = H256;
type Hasher = RegularParachainHasher;
type Header = RegularParachainHeader;
type AccountId = u64;
type Balance = u64;
type Nonce = u64;
Expand All @@ -71,9 +72,10 @@ impl Parachain for Parachain1 {
pub struct Parachain2;

impl Chain for Parachain2 {
type Block = Block;
type BlockNumber = u64;
type Hash = H256;
type Hasher = RegularParachainHasher;
type Header = RegularParachainHeader;
type AccountId = u64;
type Balance = u64;
type Nonce = u64;
Expand All @@ -94,9 +96,10 @@ impl Parachain for Parachain2 {
pub struct Parachain3;

impl Chain for Parachain3 {
type Block = Block;
type BlockNumber = u64;
type Hash = H256;
type Hasher = RegularParachainHasher;
type Header = RegularParachainHeader;
type AccountId = u64;
type Balance = u64;
type Nonce = u64;
Expand All @@ -117,12 +120,11 @@ impl Parachain for Parachain3 {
// this parachain is using u128 as block number and stored head data size exceeds limit
pub struct BigParachain;

type BigBlock = frame_system::mocking::MockBlockU128<TestRuntime>;

impl Chain for BigParachain {
type Block = BigBlock;
type BlockNumber = u128;
type Hash = H256;
type Hasher = RegularParachainHasher;
type Header = BigParachainHeader;
type AccountId = u64;
type Balance = u64;
type Nonce = u64;
Expand Down Expand Up @@ -161,11 +163,11 @@ impl frame_system::Config for TestRuntime {
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Block = Block;
type Hash = H256;
type Hashing = RegularParachainHasher;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
Expand Down Expand Up @@ -256,9 +258,10 @@ impl pallet_bridge_parachains::benchmarking::Config<()> for TestRuntime {
pub struct TestBridgedChain;

impl Chain for TestBridgedChain {
type Block = crate::RelayBlock;
type BlockNumber = crate::RelayBlockNumber;
type Hash = crate::RelayBlockHash;
type Hasher = crate::RelayBlockHasher;
type Header = RelayBlockHeader;

type AccountId = AccountId;
type Balance = u32;
Expand Down Expand Up @@ -286,9 +289,10 @@ impl ChainWithGrandpa for TestBridgedChain {
pub struct OtherBridgedChain;

impl Chain for OtherBridgedChain {
type Block = Block;
type BlockNumber = u64;
type Hash = crate::RelayBlockHash;
type Hasher = crate::RelayBlockHasher;
type Header = sp_runtime::generic::Header<u64, crate::RelayBlockHasher>;

type AccountId = AccountId;
type Balance = u32;
Expand Down
Loading