multiversx_sc_scenario/api/impl_vh/
debug_api.rs1use multiversx_chain_vm::{
2 executor::{VMHooks, VMHooksEarlyExit},
3 host::context::TxContextRef,
4 host::vm_hooks::{TxVMHooksContext, VMHooksDispatcher},
5};
6use multiversx_sc::{chain_core::types::ReturnCode, err_msg};
7
8use crate::executor::debug::{
9 ContractDebugInstance, ContractDebugInstanceState, ContractDebugStack, StaticVarData,
10};
11
12use super::{DebugHandle, VMHooksApi, VMHooksApiBackend};
13
14#[derive(Clone)]
15pub struct DebugApiBackend;
16
17impl VMHooksApiBackend for DebugApiBackend {
18 type HandleType = DebugHandle;
19
20 fn with_vm_hooks<R, F>(f: F) -> R
21 where
22 F: FnOnce(&mut dyn VMHooks) -> Result<R, VMHooksEarlyExit>,
23 {
24 let instance = ContractDebugStack::static_peek();
25 let tx_context_ref = instance.tx_context_ref.clone();
26 let vh_context = TxVMHooksContext::new(tx_context_ref, ContractDebugInstanceState);
27 let mut dispatcher = VMHooksDispatcher::new(vh_context);
28 f(&mut dispatcher).unwrap_or_else(|err| ContractDebugInstanceState::early_exit_panic(err))
29 }
30
31 fn with_vm_hooks_ctx_1<R, F>(handle: Self::HandleType, f: F) -> R
32 where
33 F: FnOnce(&mut dyn VMHooks) -> Result<R, VMHooksEarlyExit>,
34 {
35 let tx_context_ref = TxContextRef(handle.context.clone());
36 let vh_context = TxVMHooksContext::new(tx_context_ref, ContractDebugInstanceState);
37 let mut dispatcher = VMHooksDispatcher::new(vh_context);
38 f(&mut dispatcher).unwrap_or_else(|err| ContractDebugInstanceState::early_exit_panic(err))
39 }
40
41 fn with_vm_hooks_ctx_2<R, F>(handle1: Self::HandleType, handle2: Self::HandleType, f: F) -> R
42 where
43 F: FnOnce(&mut dyn VMHooks) -> Result<R, VMHooksEarlyExit>,
44 {
45 assert_handles_on_same_context(&handle1, &handle2);
46 Self::with_vm_hooks_ctx_1(handle1, f)
47 }
48
49 fn with_vm_hooks_ctx_3<R, F>(
50 handle1: Self::HandleType,
51 handle2: Self::HandleType,
52 handle3: Self::HandleType,
53 f: F,
54 ) -> R
55 where
56 F: FnOnce(&mut dyn VMHooks) -> Result<R, VMHooksEarlyExit>,
57 {
58 assert_handles_on_same_context(&handle1, &handle2);
59 assert_handles_on_same_context(&handle1, &handle3);
60 Self::with_vm_hooks_ctx_1(handle1, f)
61 }
62
63 fn assert_live_handle(handle: &Self::HandleType) {
64 if !handle.is_on_current_context() {
65 ContractDebugInstanceState::early_exit_panic(
66 VMHooksEarlyExit::new(ReturnCode::DebugApiError.as_u64())
67 .with_const_message(err_msg::DEBUG_API_ERR_HANDLE_STALE),
68 );
69 }
70 }
71 fn with_static_data<R, F>(f: F) -> R
72 where
73 F: FnOnce(&StaticVarData) -> R,
74 {
75 let top_static_vars = ContractDebugStack::static_peek().static_var_ref;
76 f(&top_static_vars)
77 }
78}
79
80pub type DebugApi = VMHooksApi<DebugApiBackend>;
81
82impl DebugApi {
83 pub fn dummy() {
85 ContractDebugStack::static_push(ContractDebugInstance::dummy());
86 }
87}
88
89impl std::fmt::Debug for DebugApi {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 f.write_str("DebugApi")
92 }
93}
94
95fn assert_handles_on_same_context(handle1: &DebugHandle, handle2: &DebugHandle) {
96 if !handle1.is_on_same_context(handle2) {
97 ContractDebugInstanceState::early_exit_panic(
98 VMHooksEarlyExit::new(ReturnCode::DebugApiError.as_u64())
99 .with_const_message(err_msg::DEBUG_API_ERR_HANDLE_CONTEXT_MISMATCH),
100 );
101 }
102}