Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Parse ProgramID as Literal. #2184

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion console/program/src/data/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod to_bits;
mod to_type;
mod variant;

use crate::LiteralType;
use crate::{LiteralType, ProgramID};
use snarkvm_console_account::{ComputeKey, PrivateKey, Signature};
use snarkvm_console_network::Network;
use snarkvm_console_types::{prelude::*, Boolean};
Expand Down
26 changes: 26 additions & 0 deletions console/program/src/data/literal/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ impl<N: Network> Parser for Literal<N> {
map(Scalar::<N>::parse, |literal| Self::Scalar(literal)),
map(Signature::<N>::parse, |literal| Self::Signature(Box::new(literal))),
map(StringType::<N>::parse, |literal| Self::String(literal)),
// This allows users to implicitly declare program IDs as literals.
map_res(ProgramID::<N>::parse, |program_id| Ok::<Self, Error>(Self::Address(program_id.to_address()?))),
))(string)
}
}
Expand Down Expand Up @@ -87,3 +89,27 @@ impl<N: Network> Display for Literal<N> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_parse_program_id() -> Result<()> {
let (remainder, candidate) = Literal::<CurrentNetwork>::parse("credits.aleo")?;
assert!(matches!(candidate, Literal::Address(_)));
assert_eq!(candidate.to_string(), "aleo1lqmly7ez2k48ajf5hs92ulphaqr05qm4n8qwzj8v0yprmasgpqgsez59gg");
assert_eq!("", remainder);

let result = Literal::<CurrentNetwork>::parse("credits.ale");
assert!(result.is_err());

let result = Literal::<CurrentNetwork>::parse("credits.aleo1");
assert!(result.is_err());

Ok(())
}
}
4 changes: 3 additions & 1 deletion synthesizer/program/src/logic/instruction/operand/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ impl<N: Network> Parser for Operand<N> {
map(tag("self.signer"), |_| Self::Signer),
map(tag("self.caller"), |_| Self::Caller),
map(tag("block.height"), |_| Self::BlockHeight),
// Note that `Operand::ProgramID`s must be parsed before `Operand::Literal`s, since a program ID can be implicitly parsed as a literal address.
// This ensures that the string representation of a program uses the `Operand::ProgramID` variant.
map(ProgramID::parse, |program_id| Self::ProgramID(program_id)),
map(Literal::parse, |literal| Self::Literal(literal)),
map(Register::parse, |register| Self::Register(register)),
map(ProgramID::parse, |program_id| Self::ProgramID(program_id)),
))(string)
}
}
Expand Down
8 changes: 6 additions & 2 deletions vm/cli/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ mod tests {

#[test]
fn clap_snarkvm_run() {
let arg_vec = vec!["snarkvm", "run", "hello", "1u32", "2u32"];
let arg_vec = vec!["snarkvm", "run", "hello", "1u32", "2u32", "foo.aleo"];
let cli = CLI::parse_from(&arg_vec);

if let Command::Run(run) = cli.command {
assert_eq!(run.function, Identifier::try_from(arg_vec[2]).unwrap());
assert_eq!(run.inputs, vec![Value::try_from(arg_vec[3]).unwrap(), Value::try_from(arg_vec[4]).unwrap()]);
assert_eq!(run.inputs, vec![
Value::try_from(arg_vec[3]).unwrap(),
Value::try_from(arg_vec[4]).unwrap(),
Value::try_from(arg_vec[5]).unwrap()
]);
} else {
panic!("Unexpected result of clap parsing!");
}
Expand Down