Skip to content

Commit

Permalink
make remove from context infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoka committed Jan 6, 2025
1 parent 78970de commit 81053ac
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pallets/dca/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ impl<T: Config> Pallet<T> {
}
};

pallet_support::Pallet::<T>::remove_from_context(|id| ExecutionType::DCA(schedule_id, id))?;
pallet_support::Pallet::<T>::remove_from_context(|id| ExecutionType::DCA(schedule_id, id));

trade_result
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/omnipool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ pub mod pallet {
trade_fees,
);

pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Omnipool)?;
pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Omnipool);

#[cfg(feature = "try-runtime")]
Self::ensure_trade_invariant(
Expand Down Expand Up @@ -1369,7 +1369,7 @@ pub mod pallet {
trade_fees,
);

pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Omnipool)?;
pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Omnipool);

#[cfg(feature = "try-runtime")]
Self::ensure_trade_invariant(
Expand Down
4 changes: 2 additions & 2 deletions pallets/route-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub mod pallet {
event_id: next_event_id,
});

pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Router)?;
pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Router);

Ok(())
}
Expand Down Expand Up @@ -541,7 +541,7 @@ impl<T: Config> Pallet<T> {
event_id: next_event_id,
});

pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Router)?;
pallet_support::Pallet::<T>::remove_from_context(ExecutionType::Router);

Ok(())
}
Expand Down
10 changes: 3 additions & 7 deletions pallets/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ impl<T: Config> Pallet<T> {
// The `expected_last_stack_entry` parameter ensures the stack behaves as intended.
// It prevents issues where exceeding the stack size results in ignored actions,
// which could lead to unexpected stack data when the stack is decreased.
pub fn remove_from_context<F>(expected_last_stack_entry: F) -> DispatchResult
pub fn remove_from_context<F>(expected_last_stack_entry: F)
where
F: FnOnce(u32) -> ExecutionType
{
ExecutionContext::<T>::try_mutate(|stack| -> DispatchResult {
ExecutionContext::<T>::mutate(|stack| {
//We make it fire and forget, and it should fail only in test and when if wrongly used
debug_assert_ne!(stack.len(), 0, "The stack should not be empty when decreased");

Expand All @@ -164,11 +164,7 @@ impl<T: Config> Pallet<T> {
}
}
}

Ok(())
})?;

Ok(())
});
}

pub fn get_context() -> Vec<ExecutionType> {
Expand Down
6 changes: 3 additions & 3 deletions pallets/support/src/tests/incremental_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn stack_should_be_reduced_when_poped() {
assert_ok!(AmmSupport::add_to_context(ExecutionType::Router));
assert_ok!(AmmSupport::add_to_context(ExecutionType::Omnipool));

AmmSupport::remove_from_context(ExecutionType::Omnipool).unwrap();
AmmSupport::remove_from_context(ExecutionType::Omnipool);
assert_eq!(
AmmSupport::execution_context(),
vec![ExecutionType::Router(0), ExecutionType::Router(1)]
Expand Down Expand Up @@ -128,7 +128,7 @@ fn nothing_is_removed_when_type_not_matched_with_last_stack_item() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(AmmSupport::add_to_context(ExecutionType::Router));

AmmSupport::remove_from_context(ExecutionType::Batch).unwrap();
AmmSupport::remove_from_context(ExecutionType::Batch);

assert_eq!(AmmSupport::execution_context(), vec![ExecutionType::Router(0)]);
assert_eq!(
Expand All @@ -143,7 +143,7 @@ fn entry_is_removed_when_type_matched_with_last_stack_item() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(AmmSupport::add_to_context(ExecutionType::Router));

AmmSupport::remove_from_context(ExecutionType::Router).unwrap();
AmmSupport::remove_from_context(ExecutionType::Router);

assert_eq!(AmmSupport::execution_context().into_inner(), vec![]);
});
Expand Down
2 changes: 1 addition & 1 deletion runtime/adapters/src/xcm_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
.map_err(|_| give.clone())
};

pallet_support::Pallet::<Runtime>::remove_from_context(ExecutionType::XcmExchange).map_err(|_| give)?;
pallet_support::Pallet::<Runtime>::remove_from_context(ExecutionType::XcmExchange);

trade_result
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl BatchHook for ManageExecutionTypeForUnifiedEvent {
}

fn on_batch_end() -> DispatchResult {
AmmSupport::remove_from_context(ExecutionType::Batch)?;
AmmSupport::remove_from_context(ExecutionType::Batch);

Ok(())
}
Expand Down
8 changes: 2 additions & 6 deletions runtime/hydradx/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<Inner: ExecuteXcm<<XcmConfig as Config>::RuntimeCall>> ExecuteXcm<<XcmConfi

//In case of error we need to clean context as xcm execution won't happen
if prepare_result.is_err() {
let _ = pallet_support::Pallet::<Runtime>::remove_from_context(|event_id| ExecutionType::Xcm(unique_id, event_id));
pallet_support::Pallet::<Runtime>::remove_from_context(|event_id| ExecutionType::Xcm(unique_id, event_id));
}

prepare_result
Expand All @@ -268,11 +268,7 @@ impl<Inner: ExecuteXcm<<XcmConfig as Config>::RuntimeCall>> ExecuteXcm<<XcmConfi
let outcome = Inner::execute(origin, pre, id, weight_credit);

let dummy_topic_id = [1u8; 32];//We use dummy as the enum field values are irrelevant when removing from context
let Ok(_) = pallet_support::Pallet::<Runtime>::remove_from_context(|id| ExecutionType::Xcm(dummy_topic_id, id)) else {
return Outcome::Error {
error: XcmError::FailedToTransactAsset("Unexpected error at modifying unified events stack"),
};
};
pallet_support::Pallet::<Runtime>::remove_from_context(|id| ExecutionType::Xcm(dummy_topic_id, id));

outcome
}
Expand Down

0 comments on commit 81053ac

Please sign in to comment.