Skip to content

Commit

Permalink
emit events from bridge hub router (#2760)
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik authored and bkontur committed May 7, 2024
1 parent 5f631da commit b455290
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
56 changes: 55 additions & 1 deletion modules/xcm-bridge-hub-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ 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>;
/// Benchmarks results from runtime we're plugged into.
type WeightInfo: WeightInfo;

Expand Down Expand Up @@ -131,6 +134,9 @@ pub mod pallet {
previous_factor,
delivery_fee_factor,
);
Self::deposit_event(Event::DeliveryFeeFactorDecreased {
new_value: delivery_fee_factor,
});

DeliveryFeeFactor::<T, I>::put(delivery_fee_factor);
T::WeightInfo::on_initialize_when_non_congested()
Expand Down Expand Up @@ -199,10 +205,26 @@ pub mod pallet {
previous_factor,
f,
);
Self::deposit_event(Event::DeliveryFeeFactorIncreased { new_value: *f });
*f
});
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
/// Delivery fee factor has been decreased.
DeliveryFeeFactorDecreased {
/// New value of the `DeliveryFeeFactor`.
new_value: FixedU128,
},
/// Delivery fee factor has been increased.
DeliveryFeeFactorIncreased {
/// New value of the `DeliveryFeeFactor`.
new_value: FixedU128,
},
}
}

/// We'll be using `SovereignPaidRemoteExporter` to send remote messages over the sibling/child
Expand Down Expand Up @@ -369,6 +391,7 @@ mod tests {
use mock::*;

use frame_support::traits::Hooks;
use frame_system::{EventRecord, Phase};
use sp_runtime::traits::One;

#[test]
Expand All @@ -388,13 +411,16 @@ mod tests {
let old_delivery_fee_factor = XcmBridgeHubRouter::delivery_fee_factor();
XcmBridgeHubRouter::on_initialize(One::one());
assert_eq!(XcmBridgeHubRouter::delivery_fee_factor(), old_delivery_fee_factor);

assert_eq!(System::events(), vec![]);
})
}

#[test]
fn fee_factor_is_decreased_from_on_initialize_when_queue_is_uncongested() {
run_test(|| {
DeliveryFeeFactor::<TestRuntime, ()>::put(FixedU128::from_rational(125, 100));
let initial_fee_factor = FixedU128::from_rational(125, 100);
DeliveryFeeFactor::<TestRuntime, ()>::put(initial_fee_factor);

// it shold eventually decreased to one
while XcmBridgeHubRouter::delivery_fee_factor() > MINIMAL_DELIVERY_FEE_FACTOR {
Expand All @@ -404,6 +430,19 @@ mod tests {
// verify that it doesn't decreases anymore
XcmBridgeHubRouter::on_initialize(One::one());
assert_eq!(XcmBridgeHubRouter::delivery_fee_factor(), MINIMAL_DELIVERY_FEE_FACTOR);

// check emitted event
let first_system_event = System::events().first().cloned();
assert_eq!(
first_system_event,
Some(EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::XcmBridgeHubRouter(Event::DeliveryFeeFactorDecreased {
new_value: initial_fee_factor / EXPONENTIAL_FEE_BASE,
}),
topics: vec![],
})
);
})
}

Expand Down Expand Up @@ -497,6 +536,8 @@ mod tests {

assert!(TestToBridgeHubSender::is_message_sent());
assert_eq!(old_delivery_fee_factor, XcmBridgeHubRouter::delivery_fee_factor());

assert_eq!(System::events(), vec![]);
});
}

Expand All @@ -520,6 +561,19 @@ mod tests {

assert!(TestToBridgeHubSender::is_message_sent());
assert!(old_delivery_fee_factor < XcmBridgeHubRouter::delivery_fee_factor());

// check emitted event
let first_system_event = System::events().first().cloned();
assert!(matches!(
first_system_event,
Some(EventRecord {
phase: Phase::Initialization,
event: RuntimeEvent::XcmBridgeHubRouter(
Event::DeliveryFeeFactorIncreased { .. }
),
..
})
));
});
}
}
10 changes: 8 additions & 2 deletions modules/xcm-bridge-hub-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ construct_runtime! {
pub enum TestRuntime
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
XcmBridgeHubRouter: pallet_xcm_bridge_hub_router::{Pallet, Storage},
XcmBridgeHubRouter: pallet_xcm_bridge_hub_router::{Pallet, Storage, Event<T>},
}
}

Expand Down Expand Up @@ -68,6 +68,7 @@ impl frame_system::Config for TestRuntime {
}

impl pallet_xcm_bridge_hub_router::Config<()> for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();

type UniversalLocation = UniversalLocation;
Expand Down Expand Up @@ -152,5 +153,10 @@ pub fn new_test_ext() -> sp_io::TestExternalities {

/// Run pallet test.
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
new_test_ext().execute_with(test)
new_test_ext().execute_with(|| {
System::set_block_number(1);
System::reset_events();

test()
})
}

0 comments on commit b455290

Please sign in to comment.