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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! Helper methods to display transaction data in more human readable way.
use crate::{node::ShowCalls, resolver};

use colored::Colorize;
use serde::Deserialize;
use std::collections::HashMap;

use crate::fork::block_on;
use zksync_basic_types::H160;

use vm::VmExecutionResultAndLogs;
use zksync_types::{vm_trace::Call, StorageLogQuery, StorageLogQueryType, VmEvent};

use lazy_static::lazy_static;

#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub enum ContractType {
    System,
    Precompile,
    Popular,
    Unknown,
}

#[derive(Debug, Deserialize, Clone)]
pub struct KnownAddress {
    address: H160,
    name: String,
    contract_type: ContractType,
}

lazy_static! {
    /// Loads the known contact addresses from the JSON file.
    static ref KNOWN_ADDRESSES: HashMap<H160, KnownAddress> = {
        let json_value = serde_json::from_slice(include_bytes!("data/address_map.json")).unwrap();
        let pairs: Vec<KnownAddress> = serde_json::from_value(json_value).unwrap();

        pairs
            .into_iter()
            .map(|entry| (entry.address, entry))
            .collect()
    };
}

fn address_to_human_readable(address: H160) -> Option<String> {
    KNOWN_ADDRESSES
        .get(&address)
        .map(|known_address| match known_address.contract_type {
            ContractType::System => known_address.name.to_string(),
            ContractType::Precompile => format!("{}", known_address.name.dimmed()),
            ContractType::Popular => format!("{}", known_address.name.green()),
            ContractType::Unknown => known_address.name.to_string(),
        })
}

/// Pretty-prints event object
/// if skip_resolve is false, will try to contact openchain to resolve the topic hashes.
pub fn print_event(event: &VmEvent, resolve_hashes: bool) {
    let event = event.clone();
    block_on(async move {
        let mut tt: Vec<String> = vec![];
        if !resolve_hashes {
            tt = event
                .indexed_topics
                .iter()
                .map(|t| format!("{:#x}", t))
                .collect();
        } else {
            for topic in event.indexed_topics {
                let selector = resolver::decode_event_selector(&format!("{:#x}", topic))
                    .await
                    .unwrap();
                tt.push(selector.unwrap_or(format!("{:#x}", topic)));
            }
        }

        log::info!(
            "{} {}",
            address_to_human_readable(event.address)
                .map(|x| format!("{:42}", x.blue()))
                .unwrap_or(format!("{:42}", format!("{:?}", event.address).blue())),
            tt.join(", ")
        );
    });
}

/// Pretty-prints contents of a 'call' - including subcalls.
/// If skip_resolve is false, will try to contact openchain to resolve the ABI names.
pub fn print_call(call: &Call, padding: usize, show_calls: &ShowCalls, resolve_hashes: bool) {
    let contract_type = KNOWN_ADDRESSES
        .get(&call.to)
        .cloned()
        .map(|known_address| known_address.contract_type)
        .unwrap_or(ContractType::Unknown);

    let should_print = match (&contract_type, &show_calls) {
        (_, ShowCalls::All) => true,
        (_, ShowCalls::None) => false,
        // now we're left only with 'user' and 'system'
        (ContractType::Unknown, _) => true,
        (ContractType::Popular, _) => true,
        (ContractType::Precompile, _) => false,
        // Now we're left with System
        (ContractType::System, ShowCalls::User) => false,
        (ContractType::System, ShowCalls::System) => true,
    };
    if should_print {
        let function_signature = if call.input.len() >= 4 {
            let sig = call.input.as_slice()[..4]
                .iter()
                .map(|byte| format!("{:02x}", byte))
                .collect::<Vec<_>>()
                .join("");

            if contract_type == ContractType::Precompile || !resolve_hashes {
                format!("{:>16}", sig)
            } else {
                block_on(async move {
                    let fetch = resolver::decode_function_selector(&sig).await.unwrap();
                    fetch.unwrap_or(format!("{:>16}", format!("0x{}", sig).dimmed()))
                })
            }
        } else {
            format!(
                "0x{}",
                call.input
                    .as_slice()
                    .iter()
                    .map(|byte| format!("{:02x}", byte))
                    .collect::<Vec<_>>()
                    .join("")
            )
        };

        let pretty_print = format!(
            "{}{:?} {} {} {} {} {}",
            " ".repeat(padding),
            call.r#type,
            address_to_human_readable(call.to)
                .map(|x| format!("{:<52}", x))
                .unwrap_or(format!("{:<52}", format!("{:?}", call.to).bold())),
            function_signature,
            call.revert_reason
                .as_ref()
                .map(|s| format!("Revert: {}", s))
                .unwrap_or_default(),
            call.error
                .as_ref()
                .map(|s| format!("Error: {}", s))
                .unwrap_or_default(),
            call.gas
        );

        if call.revert_reason.as_ref().is_some() || call.error.as_ref().is_some() {
            log::info!("{}", pretty_print.on_red());
        } else {
            log::info!("{}", pretty_print);
        }
    }
    for subcall in &call.calls {
        print_call(subcall, padding + 2, show_calls, resolve_hashes);
    }
}

pub fn print_logs(log_query: &StorageLogQuery) {
    let separator = "─".repeat(82);
    log::info!("{:<15} {:?}", "Type:", log_query.log_type);
    log::info!(
        "{:<15} {}",
        "Address:",
        address_to_human_readable(log_query.log_query.address)
            .unwrap_or(format!("{}", log_query.log_query.address))
    );
    log::info!("{:<15} {:#066x}", "Key:", log_query.log_query.key);

    log::info!(
        "{:<15} {:#066x}",
        "Read Value:",
        log_query.log_query.read_value
    );

    if log_query.log_type != StorageLogQueryType::Read {
        log::info!(
            "{:<15} {:#066x}",
            "Written Value:",
            log_query.log_query.written_value
        );
    }
    log::info!("{}", separator);
}

pub fn print_vm_details(result: &VmExecutionResultAndLogs) {
    log::info!("");
    log::info!("┌──────────────────────────┐");
    log::info!("│   VM EXECUTION RESULTS   │");
    log::info!("└──────────────────────────┘");

    log::info!("Cycles Used:          {}", result.statistics.cycles_used);
    log::info!(
        "Computation Gas Used: {}",
        result.statistics.computational_gas_used
    );
    log::info!("Contracts Used:       {}", result.statistics.contracts_used);
    match &result.result {
        vm::ExecutionResult::Success { .. } => {}
        vm::ExecutionResult::Revert { output } => {
            log::info!("");
            log::info!(
                "{}",
                format!(
                    "\n[!] Revert Reason:    {}",
                    output.to_user_friendly_string()
                )
                .on_red()
            );
        }
        vm::ExecutionResult::Halt { reason } => {
            log::info!("");
            log::info!("{}", format!("\n[!] Halt Reason:    {}", reason).on_red());
        }
    }

    log::info!("════════════════════════════");
}