1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::rc::Rc;

use soroban_env_common::{
    xdr::{ContractEventType, Hash, ScBytes, ScString, ScVal, StringM},
    Error, Symbol, SymbolSmall, VecObject,
};

use crate::{budget::AsBudget, host::Frame, Host, HostError, Val};

use super::{
    internal::{InternalDiagnosticArg, InternalDiagnosticEvent},
    InternalEvent, InternalEventsBuffer,
};

#[derive(Clone, Default)]
pub enum DiagnosticLevel {
    #[default]
    None,
    Debug,
}

/// None of these functions are metered, which is why they're behind the is_debug check
impl Host {
    pub fn set_diagnostic_level(&self, diagnostic_level: DiagnosticLevel) {
        *self.0.diagnostic_level.borrow_mut() = diagnostic_level;
    }

    // As above, avoids having to import DiagnosticLevel.
    pub fn enable_debug(&self) {
        self.set_diagnostic_level(DiagnosticLevel::Debug)
    }

    pub fn is_debug(&self) -> bool {
        matches!(*self.0.diagnostic_level.borrow(), DiagnosticLevel::Debug)
    }

    /// Records a `System` contract event. `topics` is expected to be a `SCVec`
    /// length <= 4 that cannot contain `Vec`, `Map`, or `Bytes` with length > 32
    pub fn system_event(&self, topics: VecObject, data: Val) -> Result<(), HostError> {
        self.record_contract_event(ContractEventType::System, topics, data)?;
        Ok(())
    }

    pub(crate) fn record_diagnostic_event(
        &self,
        contract_id: Option<Hash>,
        topics: Vec<InternalDiagnosticArg>,
        args: Vec<InternalDiagnosticArg>,
    ) -> Result<(), HostError> {
        let de = Rc::new(InternalDiagnosticEvent {
            contract_id,
            topics,
            args,
        });
        self.with_events_mut(|events| {
            Ok(events.record(InternalEvent::Diagnostic(de), self.as_budget()))
        })?
    }

    // Will not return error if frame is missing
    pub(crate) fn get_current_contract_id_unmetered(&self) -> Result<Option<Hash>, HostError> {
        self.with_current_frame_opt(|frame| match frame {
            Some(Frame::ContractVM(vm, ..)) => Ok(Some(vm.contract_id.clone())),
            Some(Frame::HostFunction(_)) => Ok(None),
            Some(Frame::Token(id, ..)) => Ok(Some(id.clone())),
            #[cfg(any(test, feature = "testutils"))]
            Some(Frame::TestContract(tc)) => Ok(Some(tc.id.clone())),
            None => Ok(None),
        })
    }

    pub fn log_diagnostics(&self, msg: &str, args: &[Val]) -> Result<(), HostError> {
        if !self.is_debug() {
            return Ok(());
        }
        let calling_contract = self.get_current_contract_id_unmetered()?;
        self.as_budget().with_free_budget(|| {
            let log_sym = SymbolSmall::try_from_str("log")?;
            let topics = vec![InternalDiagnosticArg::HostVal(log_sym.to_val())];
            let msg = ScVal::String(ScString::from(StringM::try_from(msg.as_bytes().to_vec())?));
            let args: Vec<_> = std::iter::once(InternalDiagnosticArg::XdrVal(msg))
                .chain(args.iter().map(|rv| InternalDiagnosticArg::HostVal(*rv)))
                .collect();
            self.record_diagnostic_event(calling_contract, topics, args)
        })
    }

    pub(crate) fn err_diagnostics(
        &self,
        events: &mut InternalEventsBuffer,
        error: Error,
        msg: &str,
        args: &[Val],
    ) -> Result<(), HostError> {
        if !self.is_debug() {
            return Ok(());
        }

        self.as_budget().with_free_budget(|| {
            let error_sym = SymbolSmall::try_from_str("error")?;
            let contract_id = self.get_current_contract_id_unmetered()?;
            let topics = vec![
                InternalDiagnosticArg::HostVal(error_sym.to_val()),
                InternalDiagnosticArg::HostVal(error.to_val()),
            ];
            let msg = ScVal::String(ScString::from(StringM::try_from(msg.as_bytes().to_vec())?));
            let args: Vec<_> = std::iter::once(InternalDiagnosticArg::XdrVal(msg))
                .chain(args.iter().map(|rv| InternalDiagnosticArg::HostVal(*rv)))
                .collect();

            // We do the event-recording ourselves here rather than calling
            // self.record_system_debug_contract_event because we can/should
            // only be called with an already-borrowed events buffer (to
            // insulate against double-faulting).
            let ce = Rc::new(InternalDiagnosticEvent {
                contract_id,
                topics,
                args,
            });
            events.record(InternalEvent::Diagnostic(ce), self.as_budget())
        })
    }

    // Emits an event with topic = ["fn_call", called_contract_id, function_name] and
    // data = [arg1, args2, ...]
    // Should called prior to opening a frame for the next call so the calling contract can be inferred correctly
    pub fn fn_call_diagnostics(
        &self,
        called_contract_id: &Hash,
        func: &Symbol,
        args: &[Val],
    ) -> Result<(), HostError> {
        if !self.is_debug() {
            return Ok(());
        }

        let calling_contract = self.get_current_contract_id_unmetered()?;

        self.as_budget().with_free_budget(|| {
            let topics = vec![
                InternalDiagnosticArg::HostVal(SymbolSmall::try_from_str("fn_call")?.into()),
                InternalDiagnosticArg::XdrVal(ScVal::Bytes(ScBytes::try_from(
                    called_contract_id.as_slice().to_vec(),
                )?)),
                InternalDiagnosticArg::HostVal(func.into()),
            ];
            self.record_diagnostic_event(
                calling_contract,
                topics,
                args.iter()
                    .map(|rv| InternalDiagnosticArg::HostVal(*rv))
                    .collect(),
            )
        })
    }

    // Emits an event with topic = ["fn_return", function_name] and
    // data = [return_val]
    pub fn fn_return_diagnostics(
        &self,
        contract_id: &Hash,
        func: &Symbol,
        res: &Val,
    ) -> Result<(), HostError> {
        if !self.is_debug() {
            return Ok(());
        }

        self.as_budget().with_free_budget(|| {
            let topics = vec![
                InternalDiagnosticArg::HostVal(SymbolSmall::try_from_str("fn_return")?.into()),
                InternalDiagnosticArg::HostVal(func.into()),
            ];

            self.record_diagnostic_event(
                Some(contract_id.clone()),
                topics,
                vec![InternalDiagnosticArg::HostVal(*res)],
            )
        })
    }
}