Skip to content

Commit

Permalink
Simplify operational extrinsics (paritytech#810)
Browse files Browse the repository at this point in the history
* Simplify operational extrinsics

* Remove old extrinsics from finality verifier
  • Loading branch information
HCastano authored and serban300 committed Apr 9, 2024
1 parent 748ce19 commit 835f179
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
49 changes: 21 additions & 28 deletions bridges/modules/finality-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,19 @@ pub mod pallet {
Ok(().into())
}

/// Halt all pallet operations. Operations may be resumed using `resume_operations` call.
/// Halt or resume all pallet operations.
///
/// May only be called either by root, or by `ModuleOwner`.
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
pub fn halt_operations(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
pub fn set_operational(origin: OriginFor<T>, operational: bool) -> DispatchResultWithPostInfo {
ensure_owner_or_root::<T>(origin)?;
<IsHalted<T>>::put(true);
log::warn!("Stopping pallet operations.");
<IsHalted<T>>::put(operational);

Ok(().into())
}

/// Resume all pallet operations. May be called even if pallet is halted.
///
/// May only be called either by root, or by `ModuleOwner`.
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
pub fn resume_operations(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_owner_or_root::<T>(origin)?;
<IsHalted<T>>::put(false);
log::info!("Resuming pallet operations.");
if operational {
log::info!("Resuming pallet operations.");
} else {
log::warn!("Stopping pallet operations.");
}

Ok(().into())
}
Expand Down Expand Up @@ -655,29 +648,29 @@ mod tests {

assert_ok!(Module::<TestRuntime>::set_owner(Origin::root(), Some(1)));
assert_noop!(
Module::<TestRuntime>::halt_operations(Origin::signed(2)),
Module::<TestRuntime>::set_operational(Origin::signed(2), false),
DispatchError::BadOrigin,
);
assert_ok!(Module::<TestRuntime>::halt_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), false));

assert_ok!(Module::<TestRuntime>::set_owner(Origin::signed(1), None));
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(2)),
Module::<TestRuntime>::set_operational(Origin::signed(2), true),
DispatchError::BadOrigin,
);
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), true));
});
}

#[test]
fn pallet_may_be_halted_by_root() {
run_test(|| {
assert_ok!(Module::<TestRuntime>::halt_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), false));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), true));
});
}

Expand All @@ -686,21 +679,21 @@ mod tests {
run_test(|| {
ModuleOwner::<TestRuntime>::put(2);

assert_ok!(Module::<TestRuntime>::halt_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), false));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), true));

assert_noop!(
Module::<TestRuntime>::halt_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), false),
DispatchError::BadOrigin,
);
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);

assert_ok!(Module::<TestRuntime>::halt_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), false));
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);
});
Expand Down
47 changes: 21 additions & 26 deletions bridges/modules/message-lane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,19 @@ decl_module! {
}
}

/// Halt all pallet operations. Operations may be resumed using `resume_operations` call.
/// Halt or resume all pallet operations.
///
/// May only be called either by root, or by `ModuleOwner`.
#[weight = (T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational)]
pub fn halt_operations(origin) {
pub fn set_operational(origin, operational: bool) {
ensure_owner_or_root::<T, I>(origin)?;
IsHalted::<I>::put(true);
log::warn!("Stopping pallet operations.");
}
<IsHalted<I>>::put(operational);

/// Resume all pallet operations. May be called even if pallet is halted.
///
/// May only be called either by root, or by `ModuleOwner`.
#[weight = (T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational)]
pub fn resume_operations(origin) {
ensure_owner_or_root::<T, I>(origin)?;
IsHalted::<I>::put(false);
log::info!("Resuming pallet operations.");
if operational {
log::info!("Resuming pallet operations.");
} else {
log::warn!("Stopping pallet operations.");
}
}

/// Update pallet parameter.
Expand Down Expand Up @@ -915,29 +910,29 @@ mod tests {

assert_ok!(Module::<TestRuntime>::set_owner(Origin::root(), Some(1)));
assert_noop!(
Module::<TestRuntime>::halt_operations(Origin::signed(2)),
Module::<TestRuntime>::set_operational(Origin::signed(2), false),
DispatchError::BadOrigin,
);
assert_ok!(Module::<TestRuntime>::halt_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), false));

assert_ok!(Module::<TestRuntime>::set_owner(Origin::signed(1), None));
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(2)),
Module::<TestRuntime>::set_operational(Origin::signed(2), true),
DispatchError::BadOrigin,
);
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), true));
});
}

#[test]
fn pallet_may_be_halted_by_root() {
run_test(|| {
assert_ok!(Module::<TestRuntime>::halt_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::root()));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), false));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::root(), true));
});
}

Expand All @@ -946,21 +941,21 @@ mod tests {
run_test(|| {
ModuleOwner::<TestRuntime>::put(2);

assert_ok!(Module::<TestRuntime>::halt_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::resume_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), false));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), true));

assert_noop!(
Module::<TestRuntime>::halt_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), false),
DispatchError::BadOrigin,
);
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);

assert_ok!(Module::<TestRuntime>::halt_operations(Origin::signed(2)));
assert_ok!(Module::<TestRuntime>::set_operational(Origin::signed(2), false));
assert_noop!(
Module::<TestRuntime>::resume_operations(Origin::signed(1)),
Module::<TestRuntime>::set_operational(Origin::signed(1), true),
DispatchError::BadOrigin,
);
});
Expand Down

0 comments on commit 835f179

Please sign in to comment.