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

Commit 934d029

Browse files
authored
Bridges subtree sync (#2991)
* Squashed 'bridges/' changes from 0417308a48..278119fec2 278119fec2 Grandpa: Store the authority set changes (#2336) (#2337) 19f9c8ffdb remove message sender origin (#2322) 3c7c271d2e GRANDPA module: store accepted justifications (#2298) (#2301) fb7d12e793 GRANDPA justifications: equivocation detection primitives (#2295) (#2297) d03a2ed450 More backports from Cumulus subtree to polkadot-staging (#2283) 3c4ada921b Update dependecies (#2277) (#2281) 3e195c9e76 GRANDPA: optimize votes_ancestries when needed (#2262) (#2264) 7065bbabc6 Implement RuntimeDebug for GrandpaJustification (#2254) 8c9e59bcbc Define generate_grandpa_key_ownership_proof() (#2247) (#2248) 0b46956df7 Deduplicate Grandpa consensus log reading logic (#2245) (#2246) 96c9701710 Fix deps from Cumulus (#2244) git-subtree-dir: bridges git-subtree-split: 278119fec2b45990cf1271999b0c21befe7003d9 * Subtree update * Squashed 'bridges/' changes from 278119fec2..edf33a2c85 edf33a2c85 Backport fix (for wasm `std` env) (#2339) git-subtree-dir: bridges git-subtree-split: edf33a2c85399d366e008228f2d9e63e8a492d95
1 parent 4917e1a commit 934d029

File tree

39 files changed

+1375
-791
lines changed

39 files changed

+1375
-791
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridges/bin/runtime-common/src/messages.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ pub type HasherOf<C> = bp_runtime::HasherOf<UnderlyingChainOf<C>>;
7272
pub type AccountIdOf<C> = bp_runtime::AccountIdOf<UnderlyingChainOf<C>>;
7373
/// Type of balances that is used on the chain.
7474
pub type BalanceOf<C> = bp_runtime::BalanceOf<UnderlyingChainOf<C>>;
75-
/// Type of origin that is used on the chain.
76-
pub type OriginOf<C> = <C as ThisChainWithMessages>::RuntimeOrigin;
7775

7876
/// Sub-module that is declaring types required for processing This -> Bridged chain messages.
7977
pub mod source {
@@ -138,17 +136,11 @@ pub mod source {
138136
#[derive(RuntimeDebug)]
139137
pub struct FromThisChainMessageVerifier<B>(PhantomData<B>);
140138

141-
impl<B> LaneMessageVerifier<OriginOf<ThisChain<B>>, FromThisChainMessagePayload>
142-
for FromThisChainMessageVerifier<B>
139+
impl<B> LaneMessageVerifier<FromThisChainMessagePayload> for FromThisChainMessageVerifier<B>
143140
where
144141
B: MessageBridge,
145-
// matches requirements from the `frame_system::Config::Origin`
146-
OriginOf<ThisChain<B>>: Clone
147-
+ Into<Result<frame_system::RawOrigin<AccountIdOf<ThisChain<B>>>, OriginOf<ThisChain<B>>>>,
148-
AccountIdOf<ThisChain<B>>: PartialEq + Clone,
149142
{
150143
fn verify_message(
151-
_submitter: &OriginOf<ThisChain<B>>,
152144
_lane: &LaneId,
153145
_lane_outbound_data: &OutboundLaneData,
154146
_payload: &FromThisChainMessagePayload,

bridges/bin/runtime-common/src/messages_xcm_extension.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ impl<BlobDispatcher: DispatchBlob, Weights: MessagesPalletWeights> MessageDispat
110110
/// one side, where on the other it can be dispatched by [`XcmBlobMessageDispatch`].
111111
pub trait XcmBlobHauler {
112112
/// Runtime message sender adapter.
113-
type MessageSender: MessagesBridge<Self::MessageSenderOrigin, XcmAsPlainPayload>;
114-
115-
/// Runtime message sender origin, which is used by [`Self::MessageSender`].
116-
type MessageSenderOrigin;
117-
/// Our location within the Consensus Universe.
118-
fn message_sender_origin() -> Self::MessageSenderOrigin;
113+
type MessageSender: MessagesBridge<XcmAsPlainPayload>;
119114

120115
/// Return message lane (as "point-to-point link") used to deliver XCM messages.
121116
fn xcm_lane() -> LaneId;
@@ -124,12 +119,10 @@ pub trait XcmBlobHauler {
124119
/// XCM bridge adapter which connects [`XcmBlobHauler`] with [`XcmBlobHauler::MessageSender`] and
125120
/// makes sure that XCM blob is sent to the [`pallet_bridge_messages`] queue to be relayed.
126121
pub struct XcmBlobHaulerAdapter<XcmBlobHauler>(sp_std::marker::PhantomData<XcmBlobHauler>);
127-
impl<HaulerOrigin, H: XcmBlobHauler<MessageSenderOrigin = HaulerOrigin>> HaulBlob
128-
for XcmBlobHaulerAdapter<H>
129-
{
122+
impl<H: XcmBlobHauler> HaulBlob for XcmBlobHaulerAdapter<H> {
130123
fn haul_blob(blob: sp_std::prelude::Vec<u8>) -> Result<(), HaulBlobError> {
131124
let lane = H::xcm_lane();
132-
H::MessageSender::send_message(H::message_sender_origin(), lane, blob)
125+
H::MessageSender::send_message(lane, blob)
133126
.map(|artifacts| (lane, artifacts.nonce).using_encoded(sp_io::hashing::blake2_256))
134127
.map(|result| {
135128
log::info!(

bridges/modules/grandpa/src/lib.rs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
pub use storage_types::StoredAuthoritySet;
4040

4141
use bp_header_chain::{
42-
justification::GrandpaJustification, ChainWithGrandpa, GrandpaConsensusLogReader, HeaderChain,
43-
InitializationData, StoredHeaderData, StoredHeaderDataBuilder,
42+
justification::GrandpaJustification, AuthoritySet, ChainWithGrandpa, GrandpaConsensusLogReader,
43+
HeaderChain, HeaderGrandpaInfo, InitializationData, StoredHeaderData, StoredHeaderDataBuilder,
4444
};
4545
use bp_runtime::{BlockNumberOf, HashOf, HasherOf, HeaderId, HeaderOf, OwnedBridgeModule};
4646
use finality_grandpa::voter_set::VoterSet;
@@ -49,7 +49,7 @@ use sp_runtime::{
4949
traits::{Header as HeaderT, Zero},
5050
SaturatedConversion,
5151
};
52-
use sp_std::{boxed::Box, convert::TryInto};
52+
use sp_std::{boxed::Box, convert::TryInto, prelude::*};
5353

5454
mod call_ext;
5555
#[cfg(test)]
@@ -194,11 +194,12 @@ pub mod pallet {
194194
let authority_set = <CurrentAuthoritySet<T, I>>::get();
195195
let unused_proof_size = authority_set.unused_proof_size();
196196
let set_id = authority_set.set_id;
197-
verify_justification::<T, I>(&justification, hash, number, authority_set.into())?;
197+
let authority_set: AuthoritySet = authority_set.into();
198+
verify_justification::<T, I>(&justification, hash, number, authority_set)?;
198199

199-
let is_authorities_change_enacted =
200+
let maybe_new_authority_set =
200201
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
201-
let may_refund_call_fee = is_authorities_change_enacted &&
202+
let may_refund_call_fee = maybe_new_authority_set.is_some() &&
202203
// if we have seen too many mandatory headers in this block, we don't want to refund
203204
Self::free_mandatory_headers_remaining() > 0 &&
204205
// if arguments out of expected bounds, we don't want to refund
@@ -237,7 +238,14 @@ pub mod pallet {
237238
let actual_weight = pre_dispatch_weight
238239
.set_proof_size(pre_dispatch_weight.proof_size().saturating_sub(unused_proof_size));
239240

240-
Self::deposit_event(Event::UpdatedBestFinalizedHeader { number, hash });
241+
Self::deposit_event(Event::UpdatedBestFinalizedHeader {
242+
number,
243+
hash,
244+
grandpa_info: HeaderGrandpaInfo {
245+
justification,
246+
authority_set: maybe_new_authority_set,
247+
},
248+
});
241249

242250
Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee })
243251
}
@@ -402,6 +410,8 @@ pub mod pallet {
402410
UpdatedBestFinalizedHeader {
403411
number: BridgedBlockNumber<T, I>,
404412
hash: BridgedBlockHash<T, I>,
413+
/// The Grandpa info associated to the new best finalized header.
414+
grandpa_info: HeaderGrandpaInfo<BridgedHeader<T, I>>,
405415
},
406416
}
407417

@@ -437,9 +447,7 @@ pub mod pallet {
437447
pub(crate) fn try_enact_authority_change<T: Config<I>, I: 'static>(
438448
header: &BridgedHeader<T, I>,
439449
current_set_id: sp_consensus_grandpa::SetId,
440-
) -> Result<bool, sp_runtime::DispatchError> {
441-
let mut change_enacted = false;
442-
450+
) -> Result<Option<AuthoritySet>, DispatchError> {
443451
// We don't support forced changes - at that point governance intervention is required.
444452
ensure!(
445453
GrandpaConsensusLogReader::<BridgedBlockNumber<T, I>>::find_forced_change(
@@ -468,7 +476,6 @@ pub mod pallet {
468476
// Since our header schedules a change and we know the delay is 0, it must also enact
469477
// the change.
470478
<CurrentAuthoritySet<T, I>>::put(&next_authorities);
471-
change_enacted = true;
472479

473480
log::info!(
474481
target: LOG_TARGET,
@@ -477,9 +484,11 @@ pub mod pallet {
477484
current_set_id + 1,
478485
next_authorities,
479486
);
487+
488+
return Ok(Some(next_authorities.into()))
480489
};
481490

482-
Ok(change_enacted)
491+
Ok(None)
483492
}
484493

485494
/// Verify a GRANDPA justification (finality proof) for a given header.
@@ -603,10 +612,22 @@ pub mod pallet {
603612
}
604613
}
605614

606-
impl<T: Config<I>, I: 'static> Pallet<T, I> {
607-
/// Get the best finalized block number.
608-
pub fn best_finalized_number() -> Option<BridgedBlockNumber<T, I>> {
609-
BestFinalized::<T, I>::get().map(|id| id.number())
615+
impl<T: Config<I>, I: 'static> Pallet<T, I>
616+
where
617+
<T as frame_system::Config>::RuntimeEvent: TryInto<Event<T, I>>,
618+
{
619+
/// Get the GRANDPA justifications accepted in the current block.
620+
pub fn synced_headers_grandpa_info() -> Vec<HeaderGrandpaInfo<BridgedHeader<T, I>>> {
621+
frame_system::Pallet::<T>::read_events_no_consensus()
622+
.filter_map(|event| {
623+
if let Event::<T, I>::UpdatedBestFinalizedHeader { grandpa_info, .. } =
624+
event.event.try_into().ok()?
625+
{
626+
return Some(grandpa_info)
627+
}
628+
None
629+
})
630+
.collect()
610631
}
611632
}
612633

@@ -913,10 +934,18 @@ mod tests {
913934
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
914935
number: *header.number(),
915936
hash: header.hash(),
937+
grandpa_info: HeaderGrandpaInfo {
938+
justification: justification.clone(),
939+
authority_set: None,
940+
},
916941
}),
917942
topics: vec![],
918943
}],
919944
);
945+
assert_eq!(
946+
Pallet::<TestRuntime>::synced_headers_grandpa_info(),
947+
vec![HeaderGrandpaInfo { justification, authority_set: None }]
948+
);
920949
})
921950
}
922951

@@ -1022,7 +1051,7 @@ mod tests {
10221051
let result = Pallet::<TestRuntime>::submit_finality_proof(
10231052
RuntimeOrigin::signed(1),
10241053
Box::new(header.clone()),
1025-
justification,
1054+
justification.clone(),
10261055
);
10271056
assert_ok!(result);
10281057
assert_eq!(result.unwrap().pays_fee, frame_support::dispatch::Pays::No);
@@ -1037,6 +1066,30 @@ mod tests {
10371066
StoredAuthoritySet::<TestRuntime, ()>::try_new(next_authorities, next_set_id)
10381067
.unwrap(),
10391068
);
1069+
1070+
// Here
1071+
assert_eq!(
1072+
System::events(),
1073+
vec![EventRecord {
1074+
phase: Phase::Initialization,
1075+
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
1076+
number: *header.number(),
1077+
hash: header.hash(),
1078+
grandpa_info: HeaderGrandpaInfo {
1079+
justification: justification.clone(),
1080+
authority_set: Some(<CurrentAuthoritySet<TestRuntime>>::get().into()),
1081+
},
1082+
}),
1083+
topics: vec![],
1084+
}],
1085+
);
1086+
assert_eq!(
1087+
Pallet::<TestRuntime>::synced_headers_grandpa_info(),
1088+
vec![HeaderGrandpaInfo {
1089+
justification,
1090+
authority_set: Some(<CurrentAuthoritySet<TestRuntime>>::get().into()),
1091+
}]
1092+
);
10401093
})
10411094
}
10421095

bridges/modules/grandpa/src/storage_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{Config, Error};
2020

2121
use bp_header_chain::{AuthoritySet, ChainWithGrandpa};
2222
use codec::{Decode, Encode, MaxEncodedLen};
23-
use frame_support::{traits::Get, BoundedVec, RuntimeDebugNoBound};
23+
use frame_support::{traits::Get, BoundedVec, CloneNoBound, RuntimeDebugNoBound};
2424
use scale_info::TypeInfo;
2525
use sp_consensus_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
2626
use sp_std::marker::PhantomData;
@@ -39,7 +39,7 @@ impl<T: Config<I>, I: 'static> Get<u32> for StoredAuthorityListLimit<T, I> {
3939
}
4040

4141
/// A bounded GRANDPA Authority List and ID.
42-
#[derive(Clone, Decode, Encode, Eq, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound)]
42+
#[derive(CloneNoBound, Decode, Encode, Eq, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound)]
4343
#[scale_info(skip_type_params(T, I))]
4444
pub struct StoredAuthoritySet<T: Config<I>, I: 'static> {
4545
/// List of GRANDPA authorities for the current round.

0 commit comments

Comments
 (0)