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 Jul 25, 2024
1 parent 2e0a43d commit ac8271f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
57 changes: 56 additions & 1 deletion bridges/modules/xcm-bridge-hub-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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 @@ -134,6 +137,10 @@ 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 @@ -202,10 +209,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 @@ -399,6 +422,7 @@ mod tests {
use mock::*;

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

#[test]
Expand All @@ -418,13 +442,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_xcm_channel_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 @@ -434,6 +461,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 @@ -568,6 +608,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 @@ -585,6 +627,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 { .. }
),
..
})
));
});
}

Expand Down
10 changes: 8 additions & 2 deletions bridges/modules/xcm-bridge-hub-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 @@ -72,6 +72,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 @@ -185,7 +186,12 @@ 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()
})
}

pub(crate) fn fake_message_hash<T>(message: &Xcm<T>) -> XcmHash {
Expand Down
1 change: 1 addition & 0 deletions bridges/modules/xcm-bridge-hub/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl pallet_xcm_bridge_hub::Config for TestRuntime {
}

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

type UniversalLocation = UniversalLocation;
Expand Down

0 comments on commit ac8271f

Please sign in to comment.