diff --git a/Cargo.lock b/Cargo.lock index aaa1f87a48..a4224833e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1972,6 +1972,7 @@ dependencies = [ "toml 0.8.12", "tracing", "tracing-subscriber", + "ureq", "walkdir", "zip", ] diff --git a/Cargo.toml b/Cargo.toml index 59ac10119d..670c706ded 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,9 @@ default = [ ] ci_skip = [ "leo-compiler/ci_skip" ] noconfig = [ ] +[dependencies] +ureq = "2.9.7" + [dependencies.leo-ast] path = "./compiler/ast" version = "=1.11.0" diff --git a/errors/src/errors/package/package_errors.rs b/errors/src/errors/package/package_errors.rs index b1d044cd17..f328648a9c 100644 --- a/errors/src/errors/package/package_errors.rs +++ b/errors/src/errors/package/package_errors.rs @@ -383,4 +383,18 @@ create_messages!( msg: format!("The dependency program `{name}` was not found among the manifest's dependencies."), help: None, } + + @backtraced + conflicting_on_chain_program_name { + args: (first: impl Display, second: impl Display), + msg: format!("Conflicting program names given to execute on chain: `{first}` and `{second}`."), + help: Some("Either set `--local` to execute the local program on chain, or set `--program `.".to_string()), + } + + @backtraced + missing_on_chain_program_name { + args: (), + msg: "The name of the program to execute on-chain is missing.".to_string(), + help: Some("Either set `--local` to execute the local program on chain, or set `--program `.".to_string()), + } ); diff --git a/errors/src/errors/utils/util_errors.rs b/errors/src/errors/utils/util_errors.rs index 903a721945..b5dacb4286 100644 --- a/errors/src/errors/utils/util_errors.rs +++ b/errors/src/errors/utils/util_errors.rs @@ -140,8 +140,8 @@ create_messages!( @formatted failed_to_retrieve_from_endpoint { - args: (endpoint: impl Display, error: impl ErrorArg), - msg: format!("Failed to retrieve from endpoint `{endpoint}`. Error: {error}"), + args: (error: impl ErrorArg), + msg: format!("{error}"), help: None, } @@ -151,4 +151,46 @@ create_messages!( msg: format!("Compiled file at `{path}` does not exist, cannot compile parent."), help: Some("If you were using the `--non-recursive` flag, remove it and try again.".to_string()), } + + @backtraced + invalid_input_id_len { + args: (input: impl Display, expected_type: impl Display), + msg: format!("Invalid input: {input}."), + help: Some(format!("Type `{expected_type}` must contain exactly 61 lowercase characters or numbers.")), + } + + @backtraced + invalid_input_id { + args: (input: impl Display, expected_type: impl Display, expected_preface: impl Display), + msg: format!("Invalid input: {input}."), + help: Some(format!("Type `{expected_type}` must start with \"{expected_preface}\".")), + } + + @backtraced + invalid_numerical_input { + args: (input: impl Display), + msg: format!("Invalid numerical input: {input}."), + help: Some("Input must be a valid u32.".to_string()), + } + + @backtraced + invalid_range { + args: (), + msg: "The range must be less than or equal to 50 blocks.".to_string(), + help: None, + } + + @backtraced + invalid_height_or_hash { + args: (input: impl Display), + msg: format!("Invalid input: {input}."), + help: Some("Input must be a valid height or hash. Valid hashes are 61 characters long, composed of only numbers and lower case letters, and be prefaced with \"ab1\".".to_string()), + } + + @backtraced + invalid_field { + args: (field: impl Display), + msg: format!("Invalid field: {field}."), + help: Some("Field element must be numerical string with optional \"field\" suffix.".to_string()), + } ); diff --git a/leo/cli/cli.rs b/leo/cli/cli.rs index c6dc0d9f82..631ee5063e 100644 --- a/leo/cli/cli.rs +++ b/leo/cli/cli.rs @@ -36,7 +36,7 @@ pub struct CLI { #[clap(long, global = true, help = "Path to Leo program root folder")] path: Option, - #[clap(long, global = true, help = "Path to aleo program registry.")] + #[clap(long, global = true, help = "Path to aleo program registry")] pub home: Option, } @@ -73,6 +73,11 @@ enum Commands { #[clap(flatten)] command: Deploy, }, + #[clap(about = "Query live data from the Aleo network")] + Query { + #[clap(flatten)] + command: Query, + }, #[clap(about = "Compile the current package as a program")] Build { #[clap(flatten)] @@ -144,6 +149,7 @@ pub fn run_with_args(cli: CLI) -> Result<()> { command.try_execute(context) } + Commands::Query { command } => command.try_execute(context), Commands::Clean { command } => command.try_execute(context), Commands::Deploy { command } => command.try_execute(context), Commands::Example { command } => command.try_execute(context), diff --git a/leo/cli/commands/add.rs b/leo/cli/commands/add.rs index 0783f0fc7c..46618b7cee 100755 --- a/leo/cli/commands/add.rs +++ b/leo/cli/commands/add.rs @@ -60,11 +60,11 @@ impl Command for Add { // Allow both `credits.aleo` and `credits` syntax. let name: String = match &self.name { name if name.ends_with(".aleo") - && Package::::is_program_name_valid(&name[0..self.name.len() - 5]) => + && Package::::is_aleo_name_valid(&name[0..self.name.len() - 5]) => { name.clone() } - name if Package::::is_program_name_valid(name) => format!("{name}.aleo"), + name if Package::::is_aleo_name_valid(name) => format!("{name}.aleo"), name => return Err(PackageError::invalid_file_name_dependency(name).into()), }; diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index 26c963d121..b4d7dc92c4 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -35,6 +35,9 @@ pub use example::Example; pub mod execute; pub use execute::Execute; +pub mod query; +pub use query::Query; + pub mod new; pub use new::New; diff --git a/leo/cli/commands/query/block.rs b/leo/cli/commands/query/block.rs new file mode 100644 index 0000000000..84ef81f9e8 --- /dev/null +++ b/leo/cli/commands/query/block.rs @@ -0,0 +1,93 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use crate::cli::context::Context; +use clap::Parser; + +// Query on-chain information related to blocks. +#[derive(Parser, Debug)] +pub struct Block { + #[clap(help = "Fetch a block by specifying its height or hash", required_unless_present_any = &["latest", "latest_hash", "latest_height", "range"])] + pub(crate) id: Option, + #[arg(short, long, help = "Get the latest block", default_value = "false", conflicts_with_all(["latest_hash", "latest_height", "range", "transactions", "to_height"]))] + pub(crate) latest: bool, + #[arg(short, long, help = "Get the latest block hash", default_value = "false", conflicts_with_all(["latest", "latest_height", "range", "transactions", "to_height"]))] + pub(crate) latest_hash: bool, + #[arg(short, long, help = "Get the latest block height", default_value = "false", conflicts_with_all(["latest", "latest_hash", "range", "transactions", "to_height"]))] + pub(crate) latest_height: bool, + #[arg(short, long, help = "Get up to 50 consecutive blocks", number_of_values = 2, value_names = &["START_HEIGHT", "END_HEIGHT"], conflicts_with_all(["latest", "latest_hash", "latest_height", "transactions", "to_height"]))] + pub(crate) range: Option>, + #[arg( + short, + long, + help = "Get all transactions at the specified block height", + conflicts_with("to_height"), + default_value = "false" + )] + pub(crate) transactions: bool, + #[arg(short, long, help = "Lookup the block height corresponding to a hash value", default_value = "false")] + pub(crate) to_height: bool, +} + +impl Command for Block { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _input: Self::Input) -> Result { + // Build custom url to fetch from based on the flags and user's input. + let url = if self.latest_height { + "block/height/latest".to_string() + } else if self.latest_hash { + "block/hash/latest".to_string() + } else if self.latest { + "block/latest".to_string() + } else if let Some(range) = self.range { + // Make sure the range is composed of valid numbers. + is_valid_numerical_input(&range[0])?; + is_valid_numerical_input(&range[1])?; + + // Make sure the range is not too large. + if range[1].parse::().unwrap() - range[0].parse::().unwrap() > 50 { + return Err(UtilError::invalid_range().into()); + } + format!("blocks?start={}&end={}", range[0], range[1]) + } else if self.transactions { + is_valid_numerical_input(&self.id.clone().unwrap())?; + format!("block/{}/transactions", self.id.unwrap()).to_string() + } else if self.to_height { + let id = self.id.unwrap(); + is_valid_hash(&id)?; + format!("height/{}", id).to_string() + } else if let Some(id) = self.id { + is_valid_height_or_hash(&id)?; + format!("block/{}", id) + } else { + unreachable!("All cases are covered") + }; + + Ok(url) + } +} diff --git a/leo/cli/commands/query/committee.rs b/leo/cli/commands/query/committee.rs new file mode 100644 index 0000000000..86bf67b74f --- /dev/null +++ b/leo/cli/commands/query/committee.rs @@ -0,0 +1,40 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use clap::Parser; + +/// Query the committee. +#[derive(Parser, Debug)] +pub struct Committee {} + +impl Command for Committee { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _: Self::Input) -> Result { + Ok("/committee/latest".to_string()) + } +} diff --git a/leo/cli/commands/query/mempool.rs b/leo/cli/commands/query/mempool.rs new file mode 100644 index 0000000000..cd461d6f7d --- /dev/null +++ b/leo/cli/commands/query/mempool.rs @@ -0,0 +1,69 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use crate::cli::context::Context; +use clap::Parser; + +// Query transactions and transmissions from the memory pool. +#[derive(Parser, Debug)] +pub struct Mempool { + #[arg( + short, + long, + help = "Get the memory pool transactions", + default_value = "false", + required_unless_present = "transmissions", + conflicts_with("transmissions") + )] + pub(crate) transactions: bool, + #[arg( + short, + long, + help = "Get the memory pool transmissions", + default_value = "false", + required_unless_present = "transactions", + conflicts_with("transactions") + )] + pub(crate) transmissions: bool, +} + +impl Command for Mempool { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _input: Self::Input) -> Result { + // Build custom url to fetch from based on the flags and user's input. + let url = if self.transactions { + "memoryPool/transactions".to_string() + } else if self.transmissions { + "memoryPool/transmissions".to_string() + } else { + unreachable!("All cases are covered") + }; + + Ok(url) + } +} diff --git a/leo/cli/commands/query/mod.rs b/leo/cli/commands/query/mod.rs new file mode 100644 index 0000000000..f5030f378f --- /dev/null +++ b/leo/cli/commands/query/mod.rs @@ -0,0 +1,153 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +mod block; +use block::Block; + +mod program; +use program::Program; + +mod state_root; +use state_root::StateRoot; + +mod committee; +use committee::Committee; + +mod mempool; +use mempool::Mempool; + +mod peers; +use peers::Peers; + +mod transaction; +use transaction::Transaction; + +mod utils; +use utils::*; + +use leo_errors::UtilError; + +/// Query live data from the Aleo network. +#[derive(Parser, Debug)] +pub struct Query { + #[clap( + short, + long, + global = true, + help = "Endpoint to retrieve network state from. Defaults to http://api.explorer.aleo.org/v1.", + default_value = "http://api.explorer.aleo.org/v1" + )] + pub endpoint: String, + #[clap(short, long, global = true, help = "Network to use. Defaults to testnet3.", default_value = "testnet3")] + pub(crate) network: String, + #[clap(subcommand)] + command: QueryCommands, +} + +impl Command for Query { + type Input = (); + type Output = (); + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, context: Context, _: Self::Input) -> Result { + let output = match self.command { + QueryCommands::Block { command } => command.apply(context, ())?, + QueryCommands::Transaction { command } => command.apply(context, ())?, + QueryCommands::Program { command } => command.apply(context, ())?, + QueryCommands::Stateroot { command } => command.apply(context, ())?, + QueryCommands::Committee { command } => command.apply(context, ())?, + QueryCommands::Mempool { command } => { + if self.endpoint == "http://api.explorer.aleo.org/v1" { + tracing::warn!( + "⚠️ `leo query mempool` is only valid when using a custom endpoint. Specify one using `--endpoint`." + ); + } + command.apply(context, ())? + } + QueryCommands::Peers { command } => { + if self.endpoint == "http://api.explorer.aleo.org/v1" { + tracing::warn!( + "⚠️ `leo query peers` is only valid when using a custom endpoint. Specify one using `--endpoint`." + ); + } + command.apply(context, ())? + } + }; + + // Make GET request to retrieve on-chain state. + let url = format!("{}/{}/{}", self.endpoint, self.network, output); + let response = ureq::get(&url.clone()) + .set(&format!("X-Aleo-Leo-{}", env!("CARGO_PKG_VERSION")), "true") + .call() + .map_err(|err| UtilError::failed_to_retrieve_from_endpoint(err, Default::default()))?; + if response.status() == 200 { + tracing::info!("✅ Successfully retrieved data from '{url}'.\n"); + // Unescape the newlines. + println!("{}\n", response.into_string().unwrap().replace("\\n", "\n")); + Ok(()) + } else { + Err(UtilError::network_error(url, response.status(), Default::default()).into()) + } + } +} + +#[derive(Parser, Debug)] +enum QueryCommands { + #[clap(about = "Query block information")] + Block { + #[clap(flatten)] + command: Block, + }, + #[clap(about = "Query transaction information")] + Transaction { + #[clap(flatten)] + command: Transaction, + }, + #[clap(about = "Query program source code and live mapping values")] + Program { + #[clap(flatten)] + command: Program, + }, + #[clap(about = "Query the latest stateroot")] + Stateroot { + #[clap(flatten)] + command: StateRoot, + }, + #[clap(about = "Query the current committee")] + Committee { + #[clap(flatten)] + command: Committee, + }, + #[clap(about = "Query transactions and transmissions from the memory pool")] + Mempool { + #[clap(flatten)] + command: Mempool, + }, + #[clap(about = "Query peer information")] + Peers { + #[clap(flatten)] + command: Peers, + }, +} diff --git a/leo/cli/commands/query/peers.rs b/leo/cli/commands/query/peers.rs new file mode 100644 index 0000000000..9ed4e0fdd7 --- /dev/null +++ b/leo/cli/commands/query/peers.rs @@ -0,0 +1,61 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use crate::cli::context::Context; +use clap::Parser; + +// Query information about network peers. +#[derive(Parser, Debug)] +pub struct Peers { + #[arg(short, long, help = "Get all peer metrics", default_value = "false", conflicts_with("count"))] + pub(crate) metrics: bool, + #[arg( + short, + long, + help = "Get the count of all participating peers", + default_value = "false", + conflicts_with("metrics") + )] + pub(crate) count: bool, +} + +impl Command for Peers { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _input: Self::Input) -> Result { + // Build custom url to fetch from based on the flags and user's input. + let url = if self.metrics { + "peers/all/metrics".to_string() + } else if self.count { + "peers/count".to_string() + } else { + "peers/all".to_string() + }; + + Ok(url) + } +} diff --git a/leo/cli/commands/query/program.rs b/leo/cli/commands/query/program.rs new file mode 100644 index 0000000000..2eee0e8225 --- /dev/null +++ b/leo/cli/commands/query/program.rs @@ -0,0 +1,67 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use clap::Parser; +use leo_package::package::Package; + +/// Query program source code and live mapping values. +#[derive(Parser, Debug)] +pub struct Program { + #[clap(name = "NAME", help = "The name of the program to fetch")] + pub(crate) name: String, + #[arg( + short, + long, + help = "Get all mappings defined in the program", + default_value = "false", + conflicts_with = "mapping_value" + )] + pub(crate) mappings: bool, + #[arg(short, long, help = "Get the value corresponding to the specified mapping and key.", number_of_values = 2, value_names = &["MAPPING", "KEY"], conflicts_with = "mappings")] + pub(crate) mapping_value: Option>, +} + +impl Command for Program { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _: Self::Input) -> Result { + // Check that the program name is valid. + let program = check_valid_program_name(self.name); + // Build custom url to fetch from based on the flags and user's input. + let url = if let Some(mapping_info) = self.mapping_value { + // Check that the mapping name is valid. + Package::::is_aleo_name_valid(&mapping_info[0]); + format!("program/{}/mapping/{}/{}", program, mapping_info[0], mapping_info[1]) + } else if self.mappings { + format!("program/{}/mappings", program) + } else { + format!("program/{}", program) + }; + + Ok(url) + } +} diff --git a/leo/cli/commands/query/state_root.rs b/leo/cli/commands/query/state_root.rs new file mode 100644 index 0000000000..b53a9e6e27 --- /dev/null +++ b/leo/cli/commands/query/state_root.rs @@ -0,0 +1,40 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use clap::Parser; + +/// Query the latest stateroot. +#[derive(Parser, Debug)] +pub struct StateRoot {} + +impl Command for StateRoot { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _: Self::Input) -> Result { + Ok("/stateRoot/latest".to_string()) + } +} diff --git a/leo/cli/commands/query/transaction.rs b/leo/cli/commands/query/transaction.rs new file mode 100644 index 0000000000..5913f3662c --- /dev/null +++ b/leo/cli/commands/query/transaction.rs @@ -0,0 +1,68 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use clap::Parser; + +/// Query transaction information. +#[derive(Parser, Debug)] +pub struct Transaction { + #[clap(name = "ID", help = "The id of the transaction to fetch", required_unless_present_any = &["from_program", "from_transition", "from_io", "range"])] + pub(crate) id: Option, + #[arg(short, long, help = "Get the transaction only if it has been confirmed", default_value = "false", conflicts_with_all(["from_io", "from_transition", "from_program"]))] + pub(crate) confirmed: bool, + #[arg(value_name = "INPUT_OR_OUTPUT_ID", short, long, help = "Get the transition id that an input or output id occurred in", conflicts_with_all(["from_program", "from_transition", "confirmed", "id"]))] + pub(crate) from_io: Option, + #[arg(value_name = "TRANSITION_ID", short, long, help = "Get the id of the transaction containing the specified transition", conflicts_with_all(["from_io", "from_program", "confirmed", "id"]))] + pub(crate) from_transition: Option, + #[arg(value_name = "PROGRAM", short, long, help = "Get the id of the transaction id that the specified program was deployed in", conflicts_with_all(["from_io", "from_transition", "confirmed", "id"]))] + pub(crate) from_program: Option, +} + +impl Command for Transaction { + type Input = (); + type Output = String; + + fn log_span(&self) -> Span { + tracing::span!(tracing::Level::INFO, "Leo") + } + + fn prelude(&self, _context: Context) -> Result { + Ok(()) + } + + fn apply(self, _context: Context, _: Self::Input) -> Result { + // Build custom url to fetch from based on the flags and user's input. + let url = if let Some(io_id) = self.from_io { + let field = is_valid_field(&io_id)?; + format!("find/transitionID/{field}") + } else if let Some(transition) = self.from_transition { + is_valid_transition_id(&transition)?; + format!("find/transactionID/{transition}") + } else if let Some(program) = self.from_program { + // Check that the program name is valid. + format!("find/transactionID/deployment/{}", check_valid_program_name(program)) + } else if let Some(id) = self.id { + is_valid_transaction_id(&id)?; + if self.confirmed { format!("transaction/confirmed/{}", id) } else { format!("transaction/{}", id) } + } else { + unreachable!("All command paths covered.") + }; + + Ok(url) + } +} diff --git a/leo/cli/commands/query/utils.rs b/leo/cli/commands/query/utils.rs new file mode 100644 index 0000000000..c038c14356 --- /dev/null +++ b/leo/cli/commands/query/utils.rs @@ -0,0 +1,94 @@ +// Copyright (C) 2019-2023 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 . + +use super::*; + +use leo_errors::{LeoError, Result, UtilError}; + +use leo_package::package::Package; + +// A valid hash is 61 characters long, with preface "ab1" and all characters lowercase or numbers. +pub fn is_valid_hash(hash: &str) -> Result<(), LeoError> { + if hash.len() != 61 { + Err(UtilError::invalid_input_id_len(hash, "hash").into()) + } else if !hash.starts_with("ab1") && hash.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()) { + Err(UtilError::invalid_input_id(hash, "hash", "ab1").into()) + } else { + Ok(()) + } +} + +// A valid transaction id is 61 characters long, with preface "at1" and all characters lowercase or numbers. +pub fn is_valid_transaction_id(transaction: &str) -> Result<(), LeoError> { + if transaction.len() != 61 { + Err(UtilError::invalid_input_id_len(transaction, "transaction").into()) + } else if !transaction.starts_with("at1") + && transaction.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()) + { + Err(UtilError::invalid_input_id(transaction, "transaction", "at1").into()) + } else { + Ok(()) + } +} + +// A valid transition id is 61 characters long, with preface "au1" and all characters lowercase or numbers. +pub fn is_valid_transition_id(transition: &str) -> Result<(), LeoError> { + if transition.len() != 61 { + Err(UtilError::invalid_input_id_len(transition, "transition").into()) + } else if !transition.starts_with("au1") && transition.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()) + { + Err(UtilError::invalid_input_id(transition, "transition", "au1").into()) + } else { + Ok(()) + } +} + +// A valid numerical input is a u32. +pub fn is_valid_numerical_input(num: &str) -> Result<(), LeoError> { + if num.parse::().is_err() { Err(UtilError::invalid_numerical_input(num).into()) } else { Ok(()) } +} + +// A valid height or hash. +pub fn is_valid_height_or_hash(input: &str) -> Result<(), LeoError> { + match (is_valid_hash(input), is_valid_numerical_input(input)) { + (Ok(_), _) | (_, Ok(_)) => Ok(()), + _ => Err(UtilError::invalid_height_or_hash(input).into()), + } +} + +// Checks if the string is a valid field, allowing for optional `field` suffix. +pub fn is_valid_field(field: &str) -> Result { + let split = field.split("field").collect::>(); + + if split.len() == 1 && split[0].chars().all(|c| c.is_numeric()) { + Ok(format!("{}field", field)) + } else if split.len() == 2 && split[0].chars().all(|c| c.is_numeric()) && split[1].is_empty() { + Ok(field.to_string()) + } else { + Err(UtilError::invalid_field(field).into()) + } +} + +// Checks if the string is a valid program name in Aleo. +pub fn check_valid_program_name(name: String) -> String { + if name.ends_with(".aleo") { + Package::::is_aleo_name_valid(&name[0..name.len() - 5]); + name + } else { + Package::::is_aleo_name_valid(&name); + format!("{}.aleo", name) + } +} diff --git a/leo/cli/commands/remove.rs b/leo/cli/commands/remove.rs index a3df3c0d6c..07170fc119 100644 --- a/leo/cli/commands/remove.rs +++ b/leo/cli/commands/remove.rs @@ -67,11 +67,11 @@ impl Command for Remove { let name: String = match &self.name { Some(name) if name.ends_with(".aleo") - && Package::::is_program_name_valid(&name[0..name.len() - 5]) => + && Package::::is_aleo_name_valid(&name[0..name.len() - 5]) => { name.clone() } - Some(name) if Package::::is_program_name_valid(name) => format!("{name}.aleo"), + Some(name) if Package::::is_aleo_name_valid(name) => format!("{name}.aleo"), name => return Err(PackageError::invalid_file_name_dependency(name.clone().unwrap()).into()), }; diff --git a/leo/package/src/package.rs b/leo/package/src/package.rs index c8bdbe4b5d..5c95d6465c 100644 --- a/leo/package/src/package.rs +++ b/leo/package/src/package.rs @@ -38,7 +38,7 @@ pub struct Package { impl Package { pub fn new(package_name: &str) -> Result { // Check that the package name is a valid Aleo program name. - if !Self::is_program_name_valid(package_name) { + if !Self::is_aleo_name_valid(package_name) { return Err(PackageError::invalid_package_name(package_name).into()); } @@ -51,35 +51,35 @@ impl Package { }) } - /// Returns `true` if the program name is valid. + /// Returns `true` if it is a valid Aleo name. /// - /// Program names can only contain ASCII alphanumeric characters and underscores. - pub fn is_program_name_valid(program_name: &str) -> bool { - // Check that the program name is nonempty. - if program_name.is_empty() { - tracing::error!("Program names must be nonempty"); + /// Aleo names can only contain ASCII alphanumeric characters and underscores. + pub fn is_aleo_name_valid(name: &str) -> bool { + // Check that the name is nonempty. + if name.is_empty() { + tracing::error!("Aleo names must be nonempty"); return false; } - let first = program_name.chars().next().unwrap(); + let first = name.chars().next().unwrap(); // Check that the first character is not an underscore. if first == '_' { - tracing::error!("Program names cannot begin with an underscore"); + tracing::error!("Aleo names cannot begin with an underscore"); return false; } // Check that the first character is not a number. if first.is_numeric() { - tracing::error!("Program names cannot begin with a number"); + tracing::error!("Aleo names cannot begin with a number"); return false; } - // Iterate and check that the program name is valid. - for current in program_name.chars() { + // Iterate and check that the name is valid. + for current in name.chars() { // Check that the program name contains only ASCII alphanumeric or underscores. if !current.is_ascii_alphanumeric() && current != '_' { - tracing::error!("Program names must can only contain ASCII alphanumeric characters and underscores."); + tracing::error!("Aleo names must can only contain ASCII alphanumeric characters and underscores."); return false; } } @@ -90,7 +90,7 @@ impl Package { /// Returns `true` if a package is can be initialized at a given path. pub fn can_initialize(package_name: &str, path: &Path) -> bool { // Check that the package name is a valid Aleo program name. - if !Self::is_program_name_valid(package_name) { + if !Self::is_aleo_name_valid(package_name) { return false; } @@ -113,7 +113,7 @@ impl Package { /// Returns `true` if a package is initialized at the given path pub fn is_initialized(package_name: &str, path: &Path) -> bool { // Check that the package name is a valid Aleo program name. - if !Self::is_program_name_valid(package_name) { + if !Self::is_aleo_name_valid(package_name) { return false; } @@ -173,25 +173,25 @@ mod tests { #[test] fn test_is_package_name_valid() { - assert!(Package::::is_program_name_valid("foo")); - assert!(Package::::is_program_name_valid("foo_bar")); - assert!(Package::::is_program_name_valid("foo1")); - assert!(Package::::is_program_name_valid("foo_bar___baz_")); - - assert!(!Package::::is_program_name_valid("foo-bar")); - assert!(!Package::::is_program_name_valid("foo-bar-baz")); - assert!(!Package::::is_program_name_valid("foo-1")); - assert!(!Package::::is_program_name_valid("")); - assert!(!Package::::is_program_name_valid("-")); - assert!(!Package::::is_program_name_valid("-foo")); - assert!(!Package::::is_program_name_valid("-foo-")); - assert!(!Package::::is_program_name_valid("_foo")); - assert!(!Package::::is_program_name_valid("foo--bar")); - assert!(!Package::::is_program_name_valid("foo---bar")); - assert!(!Package::::is_program_name_valid("foo--bar--baz")); - assert!(!Package::::is_program_name_valid("foo---bar---baz")); - assert!(!Package::::is_program_name_valid("foo*bar")); - assert!(!Package::::is_program_name_valid("foo,bar")); - assert!(!Package::::is_program_name_valid("1-foo")); + assert!(Package::::is_aleo_name_valid("foo")); + assert!(Package::::is_aleo_name_valid("foo_bar")); + assert!(Package::::is_aleo_name_valid("foo1")); + assert!(Package::::is_aleo_name_valid("foo_bar___baz_")); + + assert!(!Package::::is_aleo_name_valid("foo-bar")); + assert!(!Package::::is_aleo_name_valid("foo-bar-baz")); + assert!(!Package::::is_aleo_name_valid("foo-1")); + assert!(!Package::::is_aleo_name_valid("")); + assert!(!Package::::is_aleo_name_valid("-")); + assert!(!Package::::is_aleo_name_valid("-foo")); + assert!(!Package::::is_aleo_name_valid("-foo-")); + assert!(!Package::::is_aleo_name_valid("_foo")); + assert!(!Package::::is_aleo_name_valid("foo--bar")); + assert!(!Package::::is_aleo_name_valid("foo---bar")); + assert!(!Package::::is_aleo_name_valid("foo--bar--baz")); + assert!(!Package::::is_aleo_name_valid("foo---bar---baz")); + assert!(!Package::::is_aleo_name_valid("foo*bar")); + assert!(!Package::::is_aleo_name_valid("foo,bar")); + assert!(!Package::::is_aleo_name_valid("1-foo")); } } diff --git a/utils/retriever/src/retriever/mod.rs b/utils/retriever/src/retriever/mod.rs index f67444d1b5..28ecc0639f 100644 --- a/utils/retriever/src/retriever/mod.rs +++ b/utils/retriever/src/retriever/mod.rs @@ -508,7 +508,7 @@ fn fetch_from_network(endpoint: &String, program: &String, network: Network) -> let url = format!("{}/{}/program/{}", endpoint, network.clone(), program); let response = ureq::get(&url.clone()) .call() - .map_err(|err| UtilError::failed_to_retrieve_from_endpoint(url.clone(), err, Default::default()))?; + .map_err(|err| UtilError::failed_to_retrieve_from_endpoint(err, Default::default()))?; if response.status() == 200 { Ok(response.into_string().unwrap()) } else {