diff --git a/core-processor/src/processing.rs b/core-processor/src/processing.rs index b5572e0e3a8..85fb4d91a36 100644 --- a/core-processor/src/processing.rs +++ b/core-processor/src/processing.rs @@ -90,6 +90,8 @@ where let dispatch = execution_context.dispatch; let balance = execution_context.balance; let program_id = execution_context.program.id; + let initial_reservations_amount = execution_context.gas_reserver.states().len(); + let execution_context = WasmExecutionContext { gas_counter: execution_context.gas_counter, gas_allowance_counter: execution_context.gas_allowance_counter, @@ -138,25 +140,52 @@ where }); match exec_result { - Ok(res) => Ok(match res.kind { - DispatchResultKind::Trap(reason) => process_execution_error( - res.dispatch, - program_id, - res.gas_amount.burned(), - res.system_reservation_context, - ActorExecutionErrorReplyReason::Trap(reason), - ), - DispatchResultKind::Success => process_success(Success, res), - DispatchResultKind::Wait(duration, ref waited_type) => { - process_success(Wait(duration, waited_type.clone()), res) - } - DispatchResultKind::Exit(value_destination) => { - process_success(Exit(value_destination), res) - } - DispatchResultKind::GasAllowanceExceed => { - process_allowance_exceed(dispatch, program_id, res.gas_amount.burned()) + Ok(res) => { + match res.kind { + DispatchResultKind::Success + | DispatchResultKind::Wait(_, _) + | DispatchResultKind::Exit(_) => { + // assert that after processing the initial reservation is less or equal to the current one. + // during execution reservation amount might increase due to `system_reserve_gas` calls + // thus making initial reservation less than current one. + debug_assert!( + res.context_store.system_reservation() + >= system_reservation_ctx.previous_reservation + ); + debug_assert!( + system_reservation_ctx.previous_reservation + == res.system_reservation_context.previous_reservation + ); + debug_assert!(res + .gas_reserver + .as_ref() + .map(|reserver| initial_reservations_amount <= reserver.states().len()) + .unwrap_or(true)); + } + // reservation does not change in case of failure + _ => (), } - }), + Ok(match res.kind { + DispatchResultKind::Trap(reason) => process_execution_error( + res.dispatch, + program_id, + res.gas_amount.burned(), + res.system_reservation_context, + ActorExecutionErrorReplyReason::Trap(reason), + ), + + DispatchResultKind::Success => process_success(Success, res), + DispatchResultKind::Wait(duration, ref waited_type) => { + process_success(Wait(duration, waited_type.clone()), res) + } + DispatchResultKind::Exit(value_destination) => { + process_success(Exit(value_destination), res) + } + DispatchResultKind::GasAllowanceExceed => { + process_allowance_exceed(dispatch, program_id, res.gas_amount.burned()) + } + }) + } Err(ExecutionError::Actor(e)) => Ok(process_execution_error( dispatch, program_id,