Skip to content

Commit

Permalink
added UpdatedBestFinalizedHeader event to pallet-bridge-grandpa (pari…
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent 28b1f69 commit 3ed617c
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 26 deletions.
6 changes: 4 additions & 2 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ impl pallet_bridge_relayers::Config for Runtime {

pub type RialtoGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = bp_rialto::Rialto;
// This is a pretty unscientific cap.
//
Expand All @@ -405,6 +406,7 @@ impl pallet_bridge_grandpa::Config for Runtime {

pub type WestendGrandpaInstance = pallet_bridge_grandpa::Instance1;
impl pallet_bridge_grandpa::Config<WestendGrandpaInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = bp_westend::Westend;
type MaxRequests = ConstU32<50>;
type HeadersToKeep = ConstU32<{ bp_westend::DAYS }>;
Expand Down Expand Up @@ -559,11 +561,11 @@ construct_runtime!(

// Rialto bridge modules.
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
BridgeRialtoGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
BridgeRialtoGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
BridgeRialtoMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},

// Westend bridge modules.
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>},
BridgeWestendParachains: pallet_bridge_parachains::<Instance1>::{Pallet, Call, Storage, Event<T>},

// RialtoParachain bridge modules.
Expand Down
3 changes: 2 additions & 1 deletion bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ impl pallet_bridge_relayers::Config for Runtime {

pub type MillauGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = bp_millau::Millau;
/// This is a pretty unscientific cap.
///
Expand Down Expand Up @@ -604,7 +605,7 @@ construct_runtime!(

// Millau bridge modules.
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
BridgeMillauMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
}
);
Expand Down
3 changes: 2 additions & 1 deletion bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl pallet_bridge_relayers::Config for Runtime {

pub type MillauGrandpaInstance = ();
impl pallet_bridge_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = bp_millau::Millau;
/// This is a pretty unscientific cap.
///
Expand Down Expand Up @@ -479,7 +480,7 @@ construct_runtime!(

// Millau bridge modules.
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
BridgeMillauGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
BridgeMillauMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},

// Millau bridge modules (BEEFY based).
Expand Down
3 changes: 2 additions & 1 deletion bridges/bin/runtime-common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ frame_support::construct_runtime! {
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
BridgeRelayers: pallet_bridge_relayers::{Pallet, Call, Storage, Event<T>},
BridgeGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
BridgeGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage, Event<T>},
BridgeParachains: pallet_bridge_parachains::{Pallet, Call, Storage, Event<T>},
BridgeMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>, Config<T>},
}
Expand Down Expand Up @@ -194,6 +194,7 @@ impl pallet_transaction_payment::Config for TestRuntime {
}

impl pallet_bridge_grandpa::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = BridgedUnderlyingChain;
type MaxRequests = ConstU32<50>;
type HeadersToKeep = ConstU32<8>;
Expand Down
42 changes: 37 additions & 5 deletions bridges/modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub mod pallet {

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The chain we are bridging to here.
type BridgedChain: ChainWithGrandpa;

Expand Down Expand Up @@ -164,19 +168,19 @@ pub mod pallet {

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

let (hash, number) = (finality_target.hash(), finality_target.number());
let (hash, number) = (finality_target.hash(), *finality_target.number());
log::trace!(
target: LOG_TARGET,
"Going to try and finalize header {:?}",
finality_target
);

SubmitFinalityProofHelper::<T, I>::check_obsolete(*number)?;
SubmitFinalityProofHelper::<T, I>::check_obsolete(number)?;

let authority_set = <CurrentAuthoritySet<T, I>>::get();
let unused_proof_size = authority_set.unused_proof_size();
let set_id = authority_set.set_id;
verify_justification::<T, I>(&justification, hash, *number, authority_set.into())?;
verify_justification::<T, I>(&justification, hash, number, authority_set.into())?;

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

Self::deposit_event(Event::UpdatedBestFinalizedHeader { number, hash });

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee })
}

Expand Down Expand Up @@ -370,6 +376,16 @@ pub mod pallet {
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
/// Best finalized chain header has been updated to the header with given number and hash.
UpdatedBestFinalizedHeader {
number: BridgedBlockNumber<T, I>,
hash: BridgedBlockHash<T, I>,
},
}

#[pallet::error]
pub enum Error<T, I = ()> {
/// The given justification is invalid for the given header.
Expand Down Expand Up @@ -635,8 +651,8 @@ pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader
mod tests {
use super::*;
use crate::mock::{
run_test, test_header, RuntimeOrigin, TestBridgedChain, TestHeader, TestNumber,
TestRuntime, MAX_BRIDGED_AUTHORITIES,
run_test, test_header, RuntimeEvent as TestEvent, RuntimeOrigin, System, TestBridgedChain,
TestHeader, TestNumber, TestRuntime, MAX_BRIDGED_AUTHORITIES,
};
use bp_header_chain::BridgeGrandpaCall;
use bp_runtime::BasicOperatingMode;
Expand All @@ -649,10 +665,14 @@ mod tests {
assert_err, assert_noop, assert_ok, dispatch::PostDispatchInfo,
storage::generator::StorageValue,
};
use frame_system::{EventRecord, Phase};
use sp_core::Get;
use sp_runtime::{Digest, DigestItem, DispatchError};

fn initialize_substrate_bridge() {
System::set_block_number(1);
System::reset_events();

assert_ok!(init_with_origin(RuntimeOrigin::root()));
}

Expand Down Expand Up @@ -847,6 +867,18 @@ mod tests {
let header = test_header(1);
assert_eq!(<BestFinalized<TestRuntime>>::get().unwrap().1, header.hash());
assert!(<ImportedHeaders<TestRuntime>>::contains_key(header.hash()));

assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
number: *header.number(),
hash: header.hash(),
}),
topics: vec![],
}],
);
})
}

Expand Down
5 changes: 3 additions & 2 deletions bridges/modules/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ construct_runtime! {
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Grandpa: grandpa::{Pallet, Call},
Grandpa: grandpa::{Pallet, Call, Event<T>},
}
}

Expand All @@ -69,7 +69,7 @@ impl frame_system::Config for TestRuntime {
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = ();
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
Expand All @@ -94,6 +94,7 @@ parameter_types! {
}

impl grandpa::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = TestBridgedChain;
type MaxRequests = MaxRequests;
type HeadersToKeep = HeadersToKeep;
Expand Down
72 changes: 60 additions & 12 deletions bridges/modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,14 +735,18 @@ mod tests {
},
)
.unwrap();

System::<TestRuntime>::set_block_number(1);
System::<TestRuntime>::reset_events();
}

fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) {
fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) -> ParaHash {
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::on_initialize(
0,
);

let header = test_relay_header(num, state_root);
let hash = header.hash();
let justification = make_default_justification(&header);
assert_ok!(
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::submit_finality_proof(
Expand All @@ -751,6 +755,8 @@ mod tests {
justification,
)
);

hash
}

fn prepare_parachain_heads_proof(
Expand Down Expand Up @@ -1010,7 +1016,7 @@ mod tests {
);

// import head#10 of parachain#1 at relay block #1
proceed(1, state_root_10);
let relay_1_hash = proceed(1, state_root_10);
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
assert_eq!(
ParasInfo::<TestRuntime>::get(ParaId(1)),
Expand Down Expand Up @@ -1043,6 +1049,16 @@ mod tests {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Grandpa1(
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
}
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Parachains(Event::UpdatedParachainHead {
Expand Down Expand Up @@ -1155,7 +1171,7 @@ mod tests {

// try to import head#0 of parachain#1 at relay block#1
// => call succeeds, but nothing is changed
proceed(1, state_root);
let relay_1_hash = proceed(1, state_root);
assert_ok!(import_parachain_1_head(1, state_root, parachains, proof));
assert_eq!(ParasInfo::<TestRuntime>::get(ParaId(1)), Some(initial_best_head(1)));
assert_eq!(
Expand All @@ -1169,6 +1185,16 @@ mod tests {
}),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Grandpa1(
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
}
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Parachains(Event::RejectedObsoleteParachainHead {
Expand All @@ -1193,7 +1219,7 @@ mod tests {
initialize(state_root_5);

// head#10 of parachain#1 at relay block#1
proceed(1, state_root_10);
let relay_1_hash = proceed(1, state_root_10);
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
assert_eq!(
ParasInfo::<TestRuntime>::get(ParaId(1)),
Expand All @@ -1207,14 +1233,26 @@ mod tests {
);
assert_eq!(
System::<TestRuntime>::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: TestEvent::Parachains(Event::UpdatedParachainHead {
parachain: ParaId(1),
parachain_head_hash: head_data(1, 10).hash(),
}),
topics: vec![],
}],
vec![
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Grandpa1(
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
}
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Parachains(Event::UpdatedParachainHead {
parachain: ParaId(1),
parachain_head_hash: head_data(1, 10).hash(),
}),
topics: vec![],
}
],
);

// now try to import head#5 at relay block#0
Expand All @@ -1233,6 +1271,16 @@ mod tests {
assert_eq!(
System::<TestRuntime>::events(),
vec![
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Grandpa1(
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
}
),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: TestEvent::Parachains(Event::UpdatedParachainHead {
Expand Down
6 changes: 4 additions & 2 deletions bridges/modules/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ construct_runtime! {
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Grandpa1: pallet_bridge_grandpa::<Instance1>::{Pallet},
Grandpa2: pallet_bridge_grandpa::<Instance2>::{Pallet},
Grandpa1: pallet_bridge_grandpa::<Instance1>::{Pallet, Event<T>},
Grandpa2: pallet_bridge_grandpa::<Instance2>::{Pallet, Event<T>},
Parachains: pallet_bridge_parachains::{Call, Pallet, Event<T>},
}
}
Expand Down Expand Up @@ -197,13 +197,15 @@ parameter_types! {
}

impl pallet_bridge_grandpa::Config<pallet_bridge_grandpa::Instance1> for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = TestBridgedChain;
type MaxRequests = ConstU32<2>;
type HeadersToKeep = HeadersToKeep;
type WeightInfo = ();
}

impl pallet_bridge_grandpa::Config<pallet_bridge_grandpa::Instance2> for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type BridgedChain = TestBridgedChain;
type MaxRequests = ConstU32<2>;
type HeadersToKeep = HeadersToKeep;
Expand Down

0 comments on commit 3ed617c

Please sign in to comment.