Skip to content

Commit

Permalink
runtime-sdk: support aborting in after_handle_call
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jun 6, 2023
1 parent 3b3e6ad commit 859d5b7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
8 changes: 7 additions & 1 deletion runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ impl<R: Runtime> Dispatcher<R> {
let (result, metadata) = Self::_dispatch_tx_call(ctx, call, opts);

// Unconditionally call after handle call hook.
R::Modules::after_handle_call(ctx, &result);
let result = match R::Modules::after_handle_call(ctx, result) {
Ok(result) => result,
Err(e) => {
// If the call failed, return the error.
return (e.into_call_result(), metadata);
}
};

// Make sure that a read-only call did not result in any modifications.
if read_only && ctx.runtime_state().has_pending_updates() {
Expand Down
16 changes: 13 additions & 3 deletions runtime-sdk/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,12 @@ pub trait TransactionHandler {
/// Perform any action after call, within the transaction context.
///
/// If an error is returned the transaction call fails and updates are rolled back.
fn after_handle_call<C: TxContext>(_ctx: &mut C, _result: &CallResult) {
fn after_handle_call<C: TxContext>(
_ctx: &mut C,
result: CallResult,
) -> Result<CallResult, modules::core::Error> {
// Default implementation doesn't do anything.
Ok(result)
}

/// Perform any action after dispatching the transaction, in batch context.
Expand Down Expand Up @@ -437,8 +441,14 @@ impl TransactionHandler for Tuple {
Ok(())
}

fn after_handle_call<C: TxContext>(ctx: &mut C, result: &CallResult) {
for_tuples!( #( Tuple::after_handle_call(ctx, result); )* );
fn after_handle_call<C: TxContext>(
ctx: &mut C,
mut result: CallResult,
) -> Result<CallResult, modules::core::Error> {
for_tuples!( #(
result = Tuple::after_handle_call(ctx, result)?;
)* );
Ok(result)
}

fn after_dispatch_tx<C: Context>(ctx: &mut C, tx_auth_info: &AuthInfo, result: &CallResult) {
Expand Down
7 changes: 6 additions & 1 deletion runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,10 @@ impl module::TransactionHandler for Module {
Ok(())
}

fn after_handle_call<C: TxContext>(ctx: &mut C, _result: &module::CallResult) {
fn after_handle_call<C: TxContext>(
ctx: &mut C,
result: module::CallResult,
) -> Result<module::CallResult, modules::core::Error> {
let acc = ctx
.value::<FeeAccumulator>(CONTEXT_KEY_TX_FEE_ACCUMULATOR)
.take()
Expand All @@ -974,6 +977,8 @@ impl module::TransactionHandler for Module {
for (denom, amount) in acc.total_fees.into_iter() {
fee_acc.add(&token::BaseUnits(amount, denom));
}

Ok(result)
}

fn after_dispatch_tx<C: Context>(
Expand Down
5 changes: 3 additions & 2 deletions runtime-sdk/src/modules/accounts/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,9 @@ fn test_fee_disbursement() {
// Run after call tx handler.
Accounts::after_handle_call(
&mut tx_ctx,
&module::CallResult::Ok(cbor::Value::Simple(cbor::SimpleValue::NullValue)),
);
module::CallResult::Ok(cbor::Value::Simple(cbor::SimpleValue::NullValue)),
)
.expect("after_handle_call should succeed");
tx_ctx.commit()
});

Expand Down
7 changes: 6 additions & 1 deletion runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,12 +999,17 @@ impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
Ok(())
}

fn after_handle_call<C: TxContext>(ctx: &mut C, _result: &module::CallResult) {
fn after_handle_call<C: TxContext>(
ctx: &mut C,
result: module::CallResult,
) -> Result<module::CallResult, Error> {
// Emit gas used event.
if Cfg::EMIT_GAS_USED_EVENTS {
let used_gas = Self::used_tx_gas(ctx);
ctx.emit_unconditional_event(Event::GasUsed { amount: used_gas });
}

Ok(result)
}
}

Expand Down
5 changes: 3 additions & 2 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,9 @@ fn test_gas_used_events() {
assert_eq!(Core::used_tx_gas(&mut tx_ctx), 10);
Core::after_handle_call(
&mut tx_ctx,
&module::CallResult::Ok(cbor::Value::Simple(cbor::SimpleValue::NullValue)),
);
module::CallResult::Ok(cbor::Value::Simple(cbor::SimpleValue::NullValue)),
)
.expect("after_handle_call should succeed");

let (etags, _) = tx_ctx.commit();
let tags = etags.clone().into_tags();
Expand Down

0 comments on commit 859d5b7

Please sign in to comment.