-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Interpreter.
1 parent
d9fe41a
commit dd68615
Showing
26 changed files
with
5,349 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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::fmt; | ||
|
||
use leo_span::Span; | ||
|
||
/// Represents the interpreter halting, which should not be considered an | ||
/// actual runtime error. | ||
#[derive(Clone, Debug, Error)] | ||
pub struct InterpreterHalt { | ||
/// Optional Span where the halt occurred. | ||
span: Option<Span>, | ||
|
||
/// User visible message. | ||
message: String, | ||
} | ||
|
||
impl InterpreterHalt { | ||
pub fn new(message: String) -> Self { | ||
InterpreterHalt { span: None, message } | ||
} | ||
|
||
pub fn new_spanned(message: String, span: Span) -> Self { | ||
InterpreterHalt { span: Some(span), message } | ||
} | ||
|
||
pub fn span(&self) -> Option<Span> { | ||
self.span | ||
} | ||
} | ||
|
||
impl fmt::Display for InterpreterHalt { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
[package] | ||
name = "leo-interpreter" | ||
version = "2.3.1" | ||
authors = [ "The Leo Team <[email protected]>" ] | ||
description = "Interpreter for the Leo programming language" | ||
homepage = "https://leo-lang.org" | ||
repository = "https://github.com/ProvableHQ/leo" | ||
keywords = [ | ||
"aleo", | ||
"cryptography", | ||
"leo", | ||
"programming-language", | ||
"zero-knowledge" | ||
] | ||
categories = [ "compilers", "cryptography", "web-programming" ] | ||
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ] | ||
license = "GPL-3.0" | ||
edition = "2021" | ||
rust-version = "1.82.0" | ||
|
||
[dependencies.snarkvm] | ||
workspace = true | ||
|
||
[dependencies.snarkvm-circuit] | ||
version = "1.0.0" | ||
|
||
[dependencies.snarkvm-synthesizer-program] | ||
version = "1.0.0" | ||
|
||
[dependencies.leo-ast] | ||
workspace = true | ||
|
||
[dependencies.leo-passes] | ||
workspace = true | ||
|
||
[dependencies.leo-errors] | ||
workspace = true | ||
|
||
[dependencies.leo-package] | ||
workspace = true | ||
|
||
[dependencies.leo-parser] | ||
workspace = true | ||
|
||
[dependencies.leo-span] | ||
workspace = true | ||
|
||
[dependencies.colored] | ||
workspace = true | ||
|
||
[dependencies.indexmap] | ||
workspace = true | ||
|
||
[dependencies.dialoguer] | ||
version = "0.11.0" | ||
features = [ "history" ] | ||
|
||
[dependencies.rand] | ||
workspace = true | ||
|
||
[dependencies.rand_chacha] | ||
workspace = true | ||
|
||
[dependencies.toml] | ||
workspace = true | ||
|
||
[dev-dependencies.leo-test-framework] | ||
path = "../tests/test-framework" | ||
|
||
[dev-dependencies.serial_test] | ||
version = "3.1.1" | ||
|
||
[dev-dependencies.tempfile] | ||
workspace = true |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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 leo_ast::{Ast, Node as _, NodeBuilder}; | ||
use leo_errors::{InterpreterHalt, LeoError, Result}; | ||
use leo_span::{Span, source_map::FileName, symbol::with_session_globals}; | ||
|
||
use snarkvm::prelude::{Program, TestnetV0}; | ||
|
||
use colored::*; | ||
use std::{ | ||
collections::HashMap, | ||
fmt::{Display, Write as _}, | ||
fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
||
mod util; | ||
use util::*; | ||
|
||
mod cursor; | ||
use cursor::*; | ||
|
||
mod interpreter; | ||
use interpreter::*; | ||
|
||
mod cursor_aleo; | ||
|
||
mod value; | ||
use value::*; | ||
|
||
const INTRO: &str = "This is the Leo Interpreter. Try the command `#help`."; | ||
|
||
const HELP: &str = " | ||
You probably want to start by running a function or transition. | ||
For instance | ||
#into program.aleo/main() | ||
Once a function is running, commands include | ||
#into to evaluate into the next expression or statement; | ||
#step to take one step towards evaluating the current expression or statement; | ||
#over to complete evaluating the current expression or statement; | ||
#run to finish evaluating | ||
#quit to quit the interpreter. | ||
You can set a breakpoint with | ||
#break program_name line_number | ||
When executing Aleo VM code, you can print the value of a register like this: | ||
#print 2 | ||
You may also use one letter abbreviations for these commands, such as #i. | ||
Note that this interpreter is not line oriented as in many common debuggers; | ||
rather it is oriented around expressions and statements. | ||
As you step into code, individual expressions or statements will | ||
be evaluated one by one, including arguments of function calls. | ||
You may simply enter Leo expressions or statements on the command line | ||
to evaluate. For instance, if you want to see the value of a variable w: | ||
w | ||
If you want to set w to a new value: | ||
w = z + 2u8; | ||
Note that statements (like the assignment above) must end with a semicolon. | ||
If there are futures available to be executed, they will be listed by | ||
numerical index, and you may run them using `#future` (or `#f`); for instance | ||
#future 0 | ||
Input history is available - use the up and down arrow keys. | ||
"; | ||
|
||
fn parse_breakpoint(s: &str) -> Option<Breakpoint> { | ||
let strings: Vec<&str> = s.split_whitespace().collect(); | ||
if strings.len() == 2 { | ||
let mut program = strings[0].to_string(); | ||
if program.ends_with(".aleo") { | ||
program.truncate(program.len() - 5); | ||
} | ||
if let Ok(line) = strings[1].parse::<usize>() { | ||
return Some(Breakpoint { program, line }); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Load all the Leo source files indicated and open the interpreter | ||
/// to commands from the user. | ||
pub fn interpret( | ||
leo_filenames: &[PathBuf], | ||
aleo_filenames: &[PathBuf], | ||
signer: SvmAddress, | ||
block_height: u32, | ||
) -> Result<()> { | ||
let mut interpreter = Interpreter::new(leo_filenames.iter(), aleo_filenames.iter(), signer, block_height)?; | ||
println!("{}", INTRO); | ||
|
||
let mut history = dialoguer::BasicHistory::new(); | ||
loop { | ||
if let Some(v) = interpreter.view_current_in_context() { | ||
println!("{}:\n{v}", "Prepared to evaluate".bold()); | ||
} else if let Some(v) = interpreter.view_current() { | ||
println!("{}:\n{v}", "Prepared to evaluate".bold()); | ||
} | ||
for (i, future) in interpreter.cursor.futures.iter().enumerate() { | ||
println!("{i}: {future}"); | ||
} | ||
|
||
let user_input: String = dialoguer::Input::with_theme(&dialoguer::theme::ColorfulTheme::default()) | ||
.with_prompt("Command?") | ||
.history_with(&mut history) | ||
.interact_text() | ||
.unwrap(); | ||
|
||
let action = match user_input.trim() { | ||
"" => continue, | ||
"#h" | "#help" => { | ||
println!("{}", HELP); | ||
continue; | ||
} | ||
"#i" | "#into" => InterpreterAction::Into, | ||
"#s" | "#step" => InterpreterAction::Step, | ||
"#o" | "#over" => InterpreterAction::Over, | ||
"#r" | "#run" => InterpreterAction::Run, | ||
"#q" | "#quit" => return Ok(()), | ||
s => { | ||
if let Some(rest) = s.strip_prefix("#future ").or(s.strip_prefix("#f ")) { | ||
if let Ok(num) = rest.trim().parse::<usize>() { | ||
if num >= interpreter.cursor.futures.len() { | ||
println!("No such future"); | ||
continue; | ||
} | ||
InterpreterAction::RunFuture(num) | ||
} else { | ||
println!("Failed to parse future"); | ||
continue; | ||
} | ||
} else if let Some(rest) = s.strip_prefix("#break ").or(s.strip_prefix("#b ")) { | ||
let Some(breakpoint) = parse_breakpoint(rest) else { | ||
println!("Failed to parse breakpoint"); | ||
continue; | ||
}; | ||
InterpreterAction::Breakpoint(breakpoint) | ||
} else if let Some(rest) = s.strip_prefix("#into ").or(s.strip_prefix("#i ")) { | ||
InterpreterAction::LeoInterpretInto(rest.trim().into()) | ||
} else if let Some(rest) = s.strip_prefix("#print ").or(s.strip_prefix("#p ")) { | ||
let trimmed = rest.trim(); | ||
let without_r = trimmed.strip_prefix("r").unwrap_or(trimmed); | ||
if let Ok(num) = without_r.parse::<u64>() { | ||
InterpreterAction::PrintRegister(num) | ||
} else { | ||
println!("failed to parse register number {trimmed}"); | ||
continue; | ||
} | ||
} else { | ||
InterpreterAction::LeoInterpretOver(s.trim().into()) | ||
} | ||
} | ||
}; | ||
|
||
match interpreter.action(action) { | ||
Ok(Some(value)) => { | ||
println!("{}: {}\n", "Result".bold(), format!("{value}").bright_cyan()); | ||
} | ||
Ok(None) => {} | ||
Err(LeoError::InterpreterHalt(interpreter_halt)) => println!("Halted: {interpreter_halt}"), | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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 serial_test::serial; | ||
|
||
mod runner; | ||
|
||
#[test] | ||
#[serial] | ||
pub fn tests() { | ||
leo_test_framework::run_tests(&runner::InterpreterRunner, "interpreter"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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 crate::*; | ||
|
||
use snarkvm::prelude::{Address, PrivateKey}; | ||
|
||
use leo_span::symbol::create_session_if_not_set_then; | ||
use leo_test_framework::runner::{Namespace, ParseType, Runner, Test}; | ||
|
||
use std::{fs, path::PathBuf, str::FromStr as _}; | ||
|
||
pub struct LeoNamespace; | ||
|
||
impl Namespace for LeoNamespace { | ||
fn parse_type(&self) -> ParseType { | ||
ParseType::Whole | ||
} | ||
|
||
fn run_test(&self, test: Test) -> Result<toml::Value, String> { | ||
create_session_if_not_set_then(|_| run_leo_test(test).map(|v| toml::Value::String(format!("{v}")))) | ||
} | ||
} | ||
|
||
pub struct AleoNamespace; | ||
|
||
impl Namespace for AleoNamespace { | ||
fn parse_type(&self) -> ParseType { | ||
ParseType::Whole | ||
} | ||
|
||
fn run_test(&self, test: Test) -> Result<toml::Value, String> { | ||
create_session_if_not_set_then(|_| run_aleo_test(test).map(|v| toml::Value::String(format!("{v}")))) | ||
} | ||
} | ||
|
||
pub struct InterpreterRunner; | ||
|
||
impl Runner for InterpreterRunner { | ||
fn resolve_namespace(&self, name: &str) -> Option<Box<dyn Namespace>> { | ||
match name { | ||
"Leo" => Some(Box::new(LeoNamespace)), | ||
"Aleo" => Some(Box::new(AleoNamespace)), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
fn run_leo_test(test: Test) -> Result<Value, String> { | ||
let tempdir = tempfile::tempdir().map_err(|e| format!("{e}"))?; | ||
let mut filename = PathBuf::from(tempdir.path()); | ||
filename.push("main.leo"); | ||
fs::write(&filename, &test.content).map_err(|e| format!("{e}"))?; | ||
|
||
let private_key: PrivateKey<TestnetV0> = | ||
PrivateKey::from_str(leo_package::VALIDATOR_0_PRIVATE_KEY).expect("should be able to parse private key"); | ||
let address = Address::try_from(&private_key).expect("should be able to create address"); | ||
let empty: [&PathBuf; 0] = []; | ||
let mut interpreter = Interpreter::new([filename].iter(), empty, address, 0).map_err(|e| format!("{e}"))?; | ||
let v = interpreter.action(InterpreterAction::LeoInterpretOver("test.aleo/main()".into())); | ||
println!("got {v:?}"); | ||
match v { | ||
Err(e) => Err(format!("{e}")), | ||
Ok(None) => Err("no value received".to_string()), | ||
Ok(Some(v)) => Ok(v), | ||
} | ||
} | ||
|
||
fn run_aleo_test(test: Test) -> Result<Value, String> { | ||
let tempdir = tempfile::tempdir().map_err(|e| format!("{e}"))?; | ||
let mut filename = PathBuf::from(tempdir.path()); | ||
filename.push("main.aleo"); | ||
fs::write(&filename, &test.content).map_err(|e| format!("{e}"))?; | ||
|
||
let private_key: PrivateKey<TestnetV0> = | ||
PrivateKey::from_str(leo_package::VALIDATOR_0_PRIVATE_KEY).expect("should be able to parse private key"); | ||
let address = Address::try_from(&private_key).expect("should be able to create address"); | ||
let empty: [&PathBuf; 0] = []; | ||
let mut interpreter = Interpreter::new(empty, [filename].iter(), address, 0).map_err(|e| format!("{e}"))?; | ||
match interpreter.action(InterpreterAction::LeoInterpretOver("test.aleo/main()".into())) { | ||
Err(e) => Err(format!("{e}")), | ||
Ok(None) => Err("no value received".to_string()), | ||
Ok(Some(v)) => Ok(v), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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 leo_errors::{InterpreterHalt, Result}; | ||
use leo_span::{Span, Symbol}; | ||
|
||
use snarkvm::prelude::{Identifier, TestnetV0}; | ||
|
||
#[macro_export] | ||
macro_rules! tc_fail { | ||
() => { | ||
panic!("type checker failure") | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! halt_no_span { | ||
($($x:tt)*) => { | ||
return Err(InterpreterHalt::new(format!($($x)*)).into()) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! halt { | ||
($span: expr) => { | ||
return Err(InterpreterHalt::new_spanned(String::new(), $span).into()) | ||
|
||
}; | ||
|
||
($span: expr, $($x:tt)*) => { | ||
return Err(InterpreterHalt::new_spanned(format!($($x)*), $span).into()) | ||
}; | ||
} | ||
|
||
pub trait ExpectTc { | ||
type T; | ||
fn expect_tc(self, span: Span) -> Result<Self::T>; | ||
} | ||
|
||
impl<T> ExpectTc for Option<T> { | ||
type T = T; | ||
|
||
fn expect_tc(self, span: Span) -> Result<Self::T> { | ||
match self { | ||
Some(t) => Ok(t), | ||
None => Err(InterpreterHalt::new_spanned("type failure".into(), span).into()), | ||
} | ||
} | ||
} | ||
|
||
impl<T, U: std::fmt::Debug> ExpectTc for Result<T, U> { | ||
type T = T; | ||
|
||
fn expect_tc(self, span: Span) -> Result<Self::T> { | ||
self.map_err(|_e| InterpreterHalt::new_spanned("type failure".into(), span).into()) | ||
} | ||
} | ||
|
||
pub fn snarkvm_identifier_to_symbol(id: &Identifier<TestnetV0>) -> Symbol { | ||
let s = id.to_string(); | ||
Symbol::intern(&s) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright (C) 2019-2024 Aleo Systems 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::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, | ||
|
||
#[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(); | ||
|
||
leo_interpreter::interpret(&paths, &[], address, command.block_height) | ||
} else { | ||
let private_key: PrivateKey<TestnetV0> = PrivateKey::from_str(leo_package::VALIDATOR_0_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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace = "Leo" | ||
expectation = "Pass" | ||
outputs = ["1712u32"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace = "Leo" | ||
expectation = "Pass" | ||
outputs = ["7649508962193807282860816486231709561414143880166292770947785297592281622433field"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
namespace = "Leo" | ||
expectation = "Pass" | ||
*/ | ||
|
||
program test.aleo { | ||
function f(x: u32, y: u32) -> u32 { | ||
return x + (17u32 * y); | ||
|
||
} | ||
|
||
transition main() -> u32 { | ||
return f(12u32, 100u32); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
namespace = "Leo" | ||
expectation = "Pass" | ||
*/ | ||
|
||
program test.aleo { | ||
transition main() -> field { | ||
let a: field = 1234567890field; | ||
return BHP256::hash_to_field(a) | ||
* SHA3_256::hash_to_field(a) | ||
* Poseidon2::hash_to_field(a) | ||
* Poseidon4::hash_to_field(a) | ||
* Poseidon8::hash_to_field(a); | ||
} | ||
} |