Skip to content

Commit c7b0207

Browse files
authored
added UpdatedBestFinalizedHeader event to pallet-bridge-grandpa (paritytech#1967)
1 parent a5b6bfc commit c7b0207

File tree

8 files changed

+114
-26
lines changed

8 files changed

+114
-26
lines changed

bridges/bin/millau/runtime/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ impl pallet_bridge_relayers::Config for Runtime {
393393

394394
pub type RialtoGrandpaInstance = ();
395395
impl pallet_bridge_grandpa::Config for Runtime {
396+
type RuntimeEvent = RuntimeEvent;
396397
type BridgedChain = bp_rialto::Rialto;
397398
// This is a pretty unscientific cap.
398399
//
@@ -405,6 +406,7 @@ impl pallet_bridge_grandpa::Config for Runtime {
405406

406407
pub type WestendGrandpaInstance = pallet_bridge_grandpa::Instance1;
407408
impl pallet_bridge_grandpa::Config<WestendGrandpaInstance> for Runtime {
409+
type RuntimeEvent = RuntimeEvent;
408410
type BridgedChain = bp_westend::Westend;
409411
type MaxRequests = ConstU32<50>;
410412
type HeadersToKeep = ConstU32<{ bp_westend::DAYS }>;
@@ -559,11 +561,11 @@ construct_runtime!(
559561

560562
// Rialto bridge modules.
561563
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
562-
BridgeRialtoGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
564+
BridgeRialtoGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
563565
BridgeRialtoMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
564566

565567
// Westend bridge modules.
566-
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},
568+
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>},
567569
BridgeWestendParachains: pallet_bridge_parachains::<Instance1>::{Pallet, Call, Storage, Event<T>},
568570

569571
// RialtoParachain bridge modules.

bridges/bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ impl pallet_bridge_relayers::Config for Runtime {
525525

526526
pub type MillauGrandpaInstance = ();
527527
impl pallet_bridge_grandpa::Config for Runtime {
528+
type RuntimeEvent = RuntimeEvent;
528529
type BridgedChain = bp_millau::Millau;
529530
/// This is a pretty unscientific cap.
530531
///
@@ -604,7 +605,7 @@ construct_runtime!(
604605

605606
// Millau bridge modules.
606607
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
607-
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
608+
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
608609
BridgeMillauMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
609610
}
610611
);

bridges/bin/rialto/runtime/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ impl pallet_bridge_relayers::Config for Runtime {
390390

391391
pub type MillauGrandpaInstance = ();
392392
impl pallet_bridge_grandpa::Config for Runtime {
393+
type RuntimeEvent = RuntimeEvent;
393394
type BridgedChain = bp_millau::Millau;
394395
/// This is a pretty unscientific cap.
395396
///
@@ -479,7 +480,7 @@ construct_runtime!(
479480

480481
// Millau bridge modules.
481482
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
482-
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
483+
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
483484
BridgeMillauMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
484485

485486
// Millau bridge modules (BEEFY based).

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ frame_support::construct_runtime! {
107107
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
108108
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
109109
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
110-
BridgeGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
110+
BridgeGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
111111
BridgeParachains: pallet_bridge_parachains::{Pallet, Call, Storage, Event<T>},
112112
BridgeMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
113113
}
@@ -194,6 +194,7 @@ impl pallet_transaction_payment::Config for TestRuntime {
194194
}
195195

196196
impl pallet_bridge_grandpa::Config for TestRuntime {
197+
type RuntimeEvent = RuntimeEvent;
197198
type BridgedChain = BridgedUnderlyingChain;
198199
type MaxRequests = ConstU32<50>;
199200
type HeadersToKeep = ConstU32<8>;

bridges/modules/grandpa/src/lib.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ pub mod pallet {
9696

9797
#[pallet::config]
9898
pub trait Config<I: 'static = ()>: frame_system::Config {
99+
/// The overarching event type.
100+
type RuntimeEvent: From<Event<Self, I>>
101+
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
102+
99103
/// The chain we are bridging to here.
100104
type BridgedChain: ChainWithGrandpa;
101105

@@ -164,19 +168,19 @@ pub mod pallet {
164168

165169
ensure!(Self::request_count() < T::MaxRequests::get(), <Error<T, I>>::TooManyRequests);
166170

167-
let (hash, number) = (finality_target.hash(), finality_target.number());
171+
let (hash, number) = (finality_target.hash(), *finality_target.number());
168172
log::trace!(
169173
target: LOG_TARGET,
170174
"Going to try and finalize header {:?}",
171175
finality_target
172176
);
173177

174-
SubmitFinalityProofHelper::<T, I>::check_obsolete(*number)?;
178+
SubmitFinalityProofHelper::<T, I>::check_obsolete(number)?;
175179

176180
let authority_set = <CurrentAuthoritySet<T, I>>::get();
177181
let unused_proof_size = authority_set.unused_proof_size();
178182
let set_id = authority_set.set_id;
179-
verify_justification::<T, I>(&justification, hash, *number, authority_set.into())?;
183+
verify_justification::<T, I>(&justification, hash, number, authority_set.into())?;
180184

181185
let is_authorities_change_enacted =
182186
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
@@ -212,6 +216,8 @@ pub mod pallet {
212216
let actual_weight = pre_dispatch_weight
213217
.set_proof_size(pre_dispatch_weight.proof_size().saturating_sub(unused_proof_size));
214218

219+
Self::deposit_event(Event::UpdatedBestFinalizedHeader { number, hash });
220+
215221
Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee })
216222
}
217223

@@ -370,6 +376,16 @@ pub mod pallet {
370376
}
371377
}
372378

379+
#[pallet::event]
380+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
381+
pub enum Event<T: Config<I>, I: 'static = ()> {
382+
/// Best finalized chain header has been updated to the header with given number and hash.
383+
UpdatedBestFinalizedHeader {
384+
number: BridgedBlockNumber<T, I>,
385+
hash: BridgedBlockHash<T, I>,
386+
},
387+
}
388+
373389
#[pallet::error]
374390
pub enum Error<T, I = ()> {
375391
/// The given justification is invalid for the given header.
@@ -635,8 +651,8 @@ pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader
635651
mod tests {
636652
use super::*;
637653
use crate::mock::{
638-
run_test, test_header, RuntimeOrigin, TestBridgedChain, TestHeader, TestNumber,
639-
TestRuntime, MAX_BRIDGED_AUTHORITIES,
654+
run_test, test_header, RuntimeEvent as TestEvent, RuntimeOrigin, System, TestBridgedChain,
655+
TestHeader, TestNumber, TestRuntime, MAX_BRIDGED_AUTHORITIES,
640656
};
641657
use bp_header_chain::BridgeGrandpaCall;
642658
use bp_runtime::BasicOperatingMode;
@@ -649,10 +665,14 @@ mod tests {
649665
assert_err, assert_noop, assert_ok, dispatch::PostDispatchInfo,
650666
storage::generator::StorageValue,
651667
};
668+
use frame_system::{EventRecord, Phase};
652669
use sp_core::Get;
653670
use sp_runtime::{Digest, DigestItem, DispatchError};
654671

655672
fn initialize_substrate_bridge() {
673+
System::set_block_number(1);
674+
System::reset_events();
675+
656676
assert_ok!(init_with_origin(RuntimeOrigin::root()));
657677
}
658678

@@ -847,6 +867,18 @@ mod tests {
847867
let header = test_header(1);
848868
assert_eq!(<BestFinalized<TestRuntime>>::get().unwrap().1, header.hash());
849869
assert!(<ImportedHeaders<TestRuntime>>::contains_key(header.hash()));
870+
871+
assert_eq!(
872+
System::events(),
873+
vec![EventRecord {
874+
phase: Phase::Initialization,
875+
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
876+
number: *header.number(),
877+
hash: header.hash(),
878+
}),
879+
topics: vec![],
880+
}],
881+
);
850882
})
851883
}
852884

bridges/modules/grandpa/src/mock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ construct_runtime! {
4949
UncheckedExtrinsic = UncheckedExtrinsic,
5050
{
5151
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
52-
Grandpa: grandpa::{Pallet, Call},
52+
Grandpa: grandpa::{Pallet, Call, Event<T>},
5353
}
5454
}
5555

@@ -69,7 +69,7 @@ impl frame_system::Config for TestRuntime {
6969
type AccountId = AccountId;
7070
type Lookup = IdentityLookup<Self::AccountId>;
7171
type Header = Header;
72-
type RuntimeEvent = ();
72+
type RuntimeEvent = RuntimeEvent;
7373
type BlockHashCount = ConstU64<250>;
7474
type Version = ();
7575
type PalletInfo = PalletInfo;
@@ -94,6 +94,7 @@ parameter_types! {
9494
}
9595

9696
impl grandpa::Config for TestRuntime {
97+
type RuntimeEvent = RuntimeEvent;
9798
type BridgedChain = TestBridgedChain;
9899
type MaxRequests = MaxRequests;
99100
type HeadersToKeep = HeadersToKeep;

bridges/modules/parachains/src/lib.rs

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,18 @@ mod tests {
735735
},
736736
)
737737
.unwrap();
738+
739+
System::<TestRuntime>::set_block_number(1);
740+
System::<TestRuntime>::reset_events();
738741
}
739742

740-
fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) {
743+
fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) -> ParaHash {
741744
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::on_initialize(
742745
0,
743746
);
744747

745748
let header = test_relay_header(num, state_root);
749+
let hash = header.hash();
746750
let justification = make_default_justification(&header);
747751
assert_ok!(
748752
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::submit_finality_proof(
@@ -751,6 +755,8 @@ mod tests {
751755
justification,
752756
)
753757
);
758+
759+
hash
754760
}
755761

756762
fn prepare_parachain_heads_proof(
@@ -1010,7 +1016,7 @@ mod tests {
10101016
);
10111017

10121018
// import head#10 of parachain#1 at relay block #1
1013-
proceed(1, state_root_10);
1019+
let relay_1_hash = proceed(1, state_root_10);
10141020
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
10151021
assert_eq!(
10161022
ParasInfo::<TestRuntime>::get(ParaId(1)),
@@ -1043,6 +1049,16 @@ mod tests {
10431049
}),
10441050
topics: vec![],
10451051
},
1052+
EventRecord {
1053+
phase: Phase::Initialization,
1054+
event: TestEvent::Grandpa1(
1055+
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
1056+
number: 1,
1057+
hash: relay_1_hash,
1058+
}
1059+
),
1060+
topics: vec![],
1061+
},
10461062
EventRecord {
10471063
phase: Phase::Initialization,
10481064
event: TestEvent::Parachains(Event::UpdatedParachainHead {
@@ -1155,7 +1171,7 @@ mod tests {
11551171

11561172
// try to import head#0 of parachain#1 at relay block#1
11571173
// => call succeeds, but nothing is changed
1158-
proceed(1, state_root);
1174+
let relay_1_hash = proceed(1, state_root);
11591175
assert_ok!(import_parachain_1_head(1, state_root, parachains, proof));
11601176
assert_eq!(ParasInfo::<TestRuntime>::get(ParaId(1)), Some(initial_best_head(1)));
11611177
assert_eq!(
@@ -1169,6 +1185,16 @@ mod tests {
11691185
}),
11701186
topics: vec![],
11711187
},
1188+
EventRecord {
1189+
phase: Phase::Initialization,
1190+
event: TestEvent::Grandpa1(
1191+
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
1192+
number: 1,
1193+
hash: relay_1_hash,
1194+
}
1195+
),
1196+
topics: vec![],
1197+
},
11721198
EventRecord {
11731199
phase: Phase::Initialization,
11741200
event: TestEvent::Parachains(Event::RejectedObsoleteParachainHead {
@@ -1193,7 +1219,7 @@ mod tests {
11931219
initialize(state_root_5);
11941220

11951221
// head#10 of parachain#1 at relay block#1
1196-
proceed(1, state_root_10);
1222+
let relay_1_hash = proceed(1, state_root_10);
11971223
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
11981224
assert_eq!(
11991225
ParasInfo::<TestRuntime>::get(ParaId(1)),
@@ -1207,14 +1233,26 @@ mod tests {
12071233
);
12081234
assert_eq!(
12091235
System::<TestRuntime>::events(),
1210-
vec![EventRecord {
1211-
phase: Phase::Initialization,
1212-
event: TestEvent::Parachains(Event::UpdatedParachainHead {
1213-
parachain: ParaId(1),
1214-
parachain_head_hash: head_data(1, 10).hash(),
1215-
}),
1216-
topics: vec![],
1217-
}],
1236+
vec![
1237+
EventRecord {
1238+
phase: Phase::Initialization,
1239+
event: TestEvent::Grandpa1(
1240+
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
1241+
number: 1,
1242+
hash: relay_1_hash,
1243+
}
1244+
),
1245+
topics: vec![],
1246+
},
1247+
EventRecord {
1248+
phase: Phase::Initialization,
1249+
event: TestEvent::Parachains(Event::UpdatedParachainHead {
1250+
parachain: ParaId(1),
1251+
parachain_head_hash: head_data(1, 10).hash(),
1252+
}),
1253+
topics: vec![],
1254+
}
1255+
],
12181256
);
12191257

12201258
// now try to import head#5 at relay block#0
@@ -1233,6 +1271,16 @@ mod tests {
12331271
assert_eq!(
12341272
System::<TestRuntime>::events(),
12351273
vec![
1274+
EventRecord {
1275+
phase: Phase::Initialization,
1276+
event: TestEvent::Grandpa1(
1277+
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
1278+
number: 1,
1279+
hash: relay_1_hash,
1280+
}
1281+
),
1282+
topics: vec![],
1283+
},
12361284
EventRecord {
12371285
phase: Phase::Initialization,
12381286
event: TestEvent::Parachains(Event::UpdatedParachainHead {

bridges/modules/parachains/src/mock.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ construct_runtime! {
150150
UncheckedExtrinsic = UncheckedExtrinsic,
151151
{
152152
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
153-
Grandpa1: pallet_bridge_grandpa::<Instance1>::{Pallet},
154-
Grandpa2: pallet_bridge_grandpa::<Instance2>::{Pallet},
153+
Grandpa1: pallet_bridge_grandpa::<Instance1>::{Pallet, Event<T>},
154+
Grandpa2: pallet_bridge_grandpa::<Instance2>::{Pallet, Event<T>},
155155
Parachains: pallet_bridge_parachains::{Call, Pallet, Event<T>},
156156
}
157157
}
@@ -197,13 +197,15 @@ parameter_types! {
197197
}
198198

199199
impl pallet_bridge_grandpa::Config<pallet_bridge_grandpa::Instance1> for TestRuntime {
200+
type RuntimeEvent = RuntimeEvent;
200201
type BridgedChain = TestBridgedChain;
201202
type MaxRequests = ConstU32<2>;
202203
type HeadersToKeep = HeadersToKeep;
203204
type WeightInfo = ();
204205
}
205206

206207
impl pallet_bridge_grandpa::Config<pallet_bridge_grandpa::Instance2> for TestRuntime {
208+
type RuntimeEvent = RuntimeEvent;
207209
type BridgedChain = TestBridgedChain;
208210
type MaxRequests = ConstU32<2>;
209211
type HeadersToKeep = HeadersToKeep;

0 commit comments

Comments
 (0)