Skip to content

Commit

Permalink
Allow Substrate Pallet to be Halted (#485)
Browse files Browse the repository at this point in the history
* Copy-Pasta owner and freezing code from `message-lane`

* Halt pallet if bridge hasn't been initialized

* Make owner optional in `message-lane` pallet

* Add `is_halted` to `InitializationData`

* Fix initialization tests

* Only allow pallet to be initialized once

* Add some logging around halting and ownership changes

* Remove `target` in debugging calls
  • Loading branch information
HCastano authored and bkchr committed Apr 10, 2024
1 parent 6c0110c commit 3f7655a
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 48 deletions.
1 change: 1 addition & 0 deletions bridges/bin/millau/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fn testnet_genesis(
pallet_substrate_bridge: Some(BridgeRialtoConfig {
// We'll initialize the pallet with a dispatchable instead.
init_data: None,
owner: None,
}),
pallet_sudo: Some(SudoConfig { key: root_key }),
pallet_session: Some(SessionConfig {
Expand Down
1 change: 1 addition & 0 deletions bridges/bin/rialto/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,6 @@ fn load_kovan_bridge_config() -> Option<BridgeKovanConfig> {
fn load_millau_bridge_config() -> Option<BridgeMillauConfig> {
Some(BridgeMillauConfig {
init_data: Some(rialto_runtime::millau::init_data()),
owner: Some([0; 32].into()),
})
}
1 change: 1 addition & 0 deletions bridges/bin/rialto/runtime/src/millau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn init_data() -> InitializationData<Header> {
authority_list: authority_set.authorities,
set_id: authority_set.set_id,
scheduled_change: None,
is_halted: false,
}
}

Expand Down
30 changes: 17 additions & 13 deletions bridges/modules/message-lane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ decl_storage! {
/// `None`, then there are no direct ways to halt/resume pallet operations, but other
/// runtime methods may still be used to do that (i.e. democracy::referendum to update halt
/// flag directly or call the `halt_operations`).
pub ModuleOwner get(fn module_owner) config(): Option<T::AccountId>;
pub ModuleOwner get(fn module_owner): Option<T::AccountId>;
/// If true, all pallet transactions are failed immediately.
pub IsHalted get(fn is_halted) config(): bool;
/// Map of lane id => inbound lane data.
Expand All @@ -160,6 +160,12 @@ decl_storage! {
}
add_extra_genesis {
config(phantom): sp_std::marker::PhantomData<I>;
config(owner): Option<T::AccountId>;
build(|config| {
if let Some(ref owner) = config.owner {
<ModuleOwner<T, I>>::put(owner);
}
})
}
}

Expand Down Expand Up @@ -188,8 +194,14 @@ decl_module! {
pub fn set_owner(origin, new_owner: Option<T::AccountId>) {
ensure_owner_or_root::<T, I>(origin)?;
match new_owner {
Some(new_owner) => ModuleOwner::<T, I>::put(new_owner),
None => ModuleOwner::<T, I>::kill(),
Some(new_owner) => {
ModuleOwner::<T, I>::put(&new_owner);
frame_support::debug::info!("Setting pallet Owner to: {:?}", new_owner);
},
None => {
ModuleOwner::<T, I>::kill();
frame_support::debug::info!("Removed Owner of pallet.");
},
}
}

Expand All @@ -200,6 +212,7 @@ decl_module! {
pub fn halt_operations(origin) {
ensure_owner_or_root::<T, I>(origin)?;
IsHalted::<I>::put(true);
frame_support::debug::warn!("Stopping pallet operations.");
}

/// Resume all pallet operations. May be called even if pallet is halted.
Expand All @@ -209,6 +222,7 @@ decl_module! {
pub fn resume_operations(origin) {
ensure_owner_or_root::<T, I>(origin)?;
IsHalted::<I>::put(false);
frame_support::debug::info!("Resuming pallet operations.");
}

/// Send message over lane.
Expand All @@ -225,7 +239,6 @@ decl_module! {
// let's first check if message can be delivered to target chain
T::TargetHeaderChain::verify_message(&payload).map_err(|err| {
frame_support::debug::trace!(
target: "runtime",
"Message to lane {:?} is rejected by target chain: {:?}",
lane_id,
err,
Expand All @@ -242,7 +255,6 @@ decl_module! {
&payload,
).map_err(|err| {
frame_support::debug::trace!(
target: "runtime",
"Message to lane {:?} is rejected by lane verifier: {:?}",
lane_id,
err,
Expand All @@ -257,7 +269,6 @@ decl_module! {
&delivery_and_dispatch_fee,
).map_err(|err| {
frame_support::debug::trace!(
target: "runtime",
"Message to lane {:?} is rejected because submitter {:?} is unable to pay fee {:?}: {:?}",
lane_id,
submitter,
Expand All @@ -277,7 +288,6 @@ decl_module! {
lane.prune_messages(T::MaxMessagesToPruneAtOnce::get());

frame_support::debug::trace!(
target: "runtime",
"Accepted message {} to lane {:?}",
nonce,
lane_id,
Expand All @@ -303,7 +313,6 @@ decl_module! {
let messages = verify_and_decode_messages_proof::<T::SourceHeaderChain, T::InboundMessageFee, T::InboundPayload>(proof)
.map_err(|err| {
frame_support::debug::trace!(
target: "runtime",
"Rejecting invalid messages proof: {:?}",
err,
);
Expand All @@ -323,7 +332,6 @@ decl_module! {
.sum();
if dispatch_weight < actual_dispatch_weight {
frame_support::debug::trace!(
target: "runtime",
"Rejecting messages proof because of dispatch weight mismatch: declared={}, expected={}",
dispatch_weight,
actual_dispatch_weight,
Expand All @@ -342,7 +350,6 @@ decl_module! {
let updated_latest_confirmed_nonce = lane.receive_state_update(lane_state);
if let Some(updated_latest_confirmed_nonce) = updated_latest_confirmed_nonce {
frame_support::debug::trace!(
target: "runtime",
"Received lane {:?} state update: latest_confirmed_nonce={}",
lane_id,
updated_latest_confirmed_nonce,
Expand All @@ -361,7 +368,6 @@ decl_module! {
}

frame_support::debug::trace!(
target: "runtime",
"Received messages: total={}, valid={}",
total_messages,
valid_messages,
Expand All @@ -378,7 +384,6 @@ decl_module! {
let confirmation_relayer = ensure_signed(origin)?;
let (lane_id, lane_data) = T::TargetHeaderChain::verify_messages_delivery_proof(proof).map_err(|err| {
frame_support::debug::trace!(
target: "runtime",
"Rejecting invalid messages delivery proof: {:?}",
err,
);
Expand Down Expand Up @@ -414,7 +419,6 @@ decl_module! {
}

frame_support::debug::trace!(
target: "runtime",
"Received messages delivery proof up to (and including) {} at lane {:?}",
lane_data.latest_received_nonce,
lane_id,
Expand Down
Loading

0 comments on commit 3f7655a

Please sign in to comment.