leo_lang/cli/commands/
debug.rs

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
// Copyright (C) 2019-2025 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use std::{fs, path::PathBuf};

use snarkvm::prelude::{Network, ProgramID, TestnetV0};

#[cfg(not(feature = "only_testnet"))]
use snarkvm::prelude::{CanaryV0, MainnetV0};

use leo_errors::UtilError;
use leo_retriever::{Manifest, NetworkName, Retriever};
use leo_span::Symbol;

use super::*;

/// Debugs an Aleo program through the interpreter.
#[derive(Parser, Debug)]
pub struct LeoDebug {
    #[arg(long, help = "Use these source files instead of finding source files through the project structure.", num_args = 1..)]
    pub(crate) paths: Vec<String>,

    #[arg(long, help = "The block height, accessible via block.height.", default_value = "0")]
    pub(crate) block_height: u32,

    #[arg(long, action, help = "Use the text user interface.")]
    pub(crate) tui: bool,

    #[clap(flatten)]
    pub(crate) compiler_options: BuildOptions,
}

impl Command for LeoDebug {
    type Input = <LeoBuild as Command>::Output;
    type Output = ();

    fn log_span(&self) -> Span {
        tracing::span!(tracing::Level::INFO, "Leo")
    }

    fn prelude(&self, context: Context) -> Result<Self::Input> {
        if self.paths.is_empty() {
            (LeoBuild { options: self.compiler_options.clone() }).execute(context)
        } else {
            Ok(())
        }
    }

    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
        // Parse the network.
        let network = NetworkName::try_from(context.get_network(&self.compiler_options.network)?)?;
        match network {
            NetworkName::TestnetV0 => handle_debug::<TestnetV0>(&self, context),
            NetworkName::MainnetV0 => {
                #[cfg(feature = "only_testnet")]
                panic!("Mainnet chosen with only_testnet feature");
                #[cfg(not(feature = "only_testnet"))]
                return handle_debug::<MainnetV0>(&self, context);
            }
            NetworkName::CanaryV0 => {
                #[cfg(feature = "only_testnet")]
                panic!("Canary chosen with only_testnet feature");
                #[cfg(not(feature = "only_testnet"))]
                return handle_debug::<CanaryV0>(&self, context);
            }
        }
    }
}

fn handle_debug<N: Network>(command: &LeoDebug, context: Context) -> Result<()> {
    if command.paths.is_empty() {
        // Get the package path.
        let package_path = context.dir()?;
        let home_path = context.home()?;

        // Get the program id.
        let manifest = Manifest::read_from_dir(&package_path)?;
        let program_id = ProgramID::<N>::from_str(manifest.program())?;

        // Get the private key.
        let private_key = context.get_private_key(&None)?;
        let address = Address::try_from(&private_key)?;

        // Retrieve all local dependencies in post order
        let main_sym = Symbol::intern(&program_id.name().to_string());
        let mut retriever = Retriever::<N>::new(
            main_sym,
            &package_path,
            &home_path,
            context.get_endpoint(&command.compiler_options.endpoint)?.to_string(),
        )
        .map_err(|err| UtilError::failed_to_retrieve_dependencies(err, Default::default()))?;
        let mut local_dependencies =
            retriever.retrieve().map_err(|err| UtilError::failed_to_retrieve_dependencies(err, Default::default()))?;

        // Push the main program at the end of the list.
        local_dependencies.push(main_sym);

        let paths: Vec<PathBuf> = local_dependencies
            .into_iter()
            .map(|dependency| {
                let base_path = retriever.get_context(&dependency).full_path();
                base_path.join("src/main.leo")
            })
            .collect();

        let imports_directory = package_path.join("build/imports");

        let aleo_paths: Vec<PathBuf> = if let Ok(dir) = fs::read_dir(imports_directory) {
            dir.flat_map(|maybe_filename| maybe_filename.ok())
                .filter(|entry| entry.file_type().ok().map(|filetype| filetype.is_file()).unwrap_or(false))
                .flat_map(|entry| {
                    let path = entry.path();
                    if path.extension().map(|e| e == "aleo").unwrap_or(false) { Some(path) } else { None }
                })
                .collect()
        } else {
            Vec::new()
        };

        leo_interpreter::interpret(&paths, &aleo_paths, address, command.block_height, command.tui)
    } else {
        let private_key: PrivateKey<TestnetV0> = PrivateKey::from_str(leo_package::TEST_PRIVATE_KEY)?;
        let address = Address::try_from(&private_key)?;

        let leo_paths: Vec<PathBuf> = command
            .paths
            .iter()
            .filter(|path_str| path_str.ends_with(".leo"))
            .map(|path_str| path_str.into())
            .collect();
        let aleo_paths: Vec<PathBuf> = command
            .paths
            .iter()
            .filter(|path_str| !path_str.ends_with(".leo"))
            .map(|path_str| path_str.into())
            .collect();

        leo_interpreter::interpret(&leo_paths, &aleo_paths, address, command.block_height, command.tui)
    }
}