feat: add a new subcommand for tx simulation

This commit is contained in:
rvcas 2022-09-03 13:35:10 -04:00
parent 4615132b05
commit 055ca5c66b
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
4 changed files with 117 additions and 8 deletions

87
Cargo.lock generated
View File

@ -8,6 +8,9 @@ version = "0.0.12"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"hex",
"pallas-primitives 0.13.2",
"pallas-traverse",
"uplc", "uplc",
] ]
@ -46,6 +49,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581" checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581"
[[package]]
name = "bech32"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9ff0bbfd639f15c74af777d81383cf53efb7c93613f6cab67c6c11e05bbf8b"
[[package]] [[package]]
name = "bech32" name = "bech32"
version = "0.9.0" version = "0.9.0"
@ -300,6 +309,20 @@ version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435" checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435"
[[package]]
name = "pallas-addresses"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712ead72b4f40d09f1c8660c47340489862754609cc2d587f24874ce55e51492"
dependencies = [
"base58",
"bech32 0.8.1",
"hex",
"pallas-codec 0.13.2",
"pallas-crypto 0.13.2",
"thiserror",
]
[[package]] [[package]]
name = "pallas-codec" name = "pallas-codec"
version = "0.12.0" version = "0.12.0"
@ -309,6 +332,15 @@ dependencies = [
"minicbor 0.17.1", "minicbor 0.17.1",
] ]
[[package]]
name = "pallas-codec"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1758a68b9b2332a24403cf484ea2dd05dd14cceeb6f7b63cb8d11137865fb4cf"
dependencies = [
"minicbor 0.17.1",
]
[[package]] [[package]]
name = "pallas-crypto" name = "pallas-crypto"
version = "0.12.0" version = "0.12.0"
@ -317,7 +349,20 @@ checksum = "051226367cd851895c73e3115d378b58495ade1ee60c3a154c0d0c30554565fa"
dependencies = [ dependencies = [
"cryptoxide", "cryptoxide",
"hex", "hex",
"pallas-codec", "pallas-codec 0.12.0",
"rand_core",
"thiserror",
]
[[package]]
name = "pallas-crypto"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17a13efbe2609916de03c063d5bbef840616b02c658f3db5222785a18b63362"
dependencies = [
"cryptoxide",
"hex",
"pallas-codec 0.13.2",
"rand_core", "rand_core",
"thiserror", "thiserror",
] ]
@ -329,15 +374,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97a0fcc7d5a7120bc2b2e203ec5e7f8088107c500c0eb665569d0e77a910d3c0" checksum = "97a0fcc7d5a7120bc2b2e203ec5e7f8088107c500c0eb665569d0e77a910d3c0"
dependencies = [ dependencies = [
"base58", "base58",
"bech32", "bech32 0.9.0",
"hex", "hex",
"log", "log",
"pallas-codec", "pallas-codec 0.12.0",
"pallas-crypto", "pallas-crypto 0.12.0",
"serde", "serde",
"serde_json", "serde_json",
] ]
[[package]]
name = "pallas-primitives"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24f8d900bac04b711d2c4b21e906680224efd02d312a87ce25e688595b91d1fc"
dependencies = [
"base58",
"bech32 0.9.0",
"hex",
"log",
"pallas-codec 0.13.2",
"pallas-crypto 0.13.2",
"serde",
"serde_json",
]
[[package]]
name = "pallas-traverse"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0549db2f8bbaf22d13fc35791dd50fb13a50a1290a342a98929c8ce6c422ed86"
dependencies = [
"hex",
"pallas-addresses",
"pallas-codec 0.13.2",
"pallas-crypto 0.13.2",
"pallas-primitives 0.13.2",
"thiserror",
]
[[package]] [[package]]
name = "peg" name = "peg"
version = "0.8.0" version = "0.8.0"
@ -647,8 +722,8 @@ dependencies = [
"flat-rs", "flat-rs",
"hex", "hex",
"minicbor 0.18.0", "minicbor 0.18.0",
"pallas-codec", "pallas-codec 0.12.0",
"pallas-primitives", "pallas-primitives 0.12.0",
"peg", "peg",
"pretty", "pretty",
"proptest", "proptest",

View File

@ -13,4 +13,7 @@ authors = ["Lucas Rosa <x@rvcas.dev>", "Kasey White <kwhitemsg@gmail.com>"]
[dependencies] [dependencies]
anyhow = "1.0.57" anyhow = "1.0.57"
clap = { version = "3.1.14", features = ["derive"] } clap = { version = "3.1.14", features = ["derive"] }
hex = "0.4.3"
pallas-primitives = "0.13.2"
pallas-traverse = "0.13.2"
uplc = { path = '../uplc', version = "0.0.12" } uplc = { path = '../uplc', version = "0.0.12" }

View File

@ -7,11 +7,25 @@ use clap::{Parser, Subcommand};
#[clap(version, about, long_about = None)] #[clap(version, about, long_about = None)]
#[clap(propagate_version = true)] #[clap(propagate_version = true)]
pub enum Args { pub enum Args {
/// A subcommand for working with transactions
#[clap(subcommand)]
Tx(TxCommand),
/// A subcommand for working with Untyped Plutus Core /// A subcommand for working with Untyped Plutus Core
#[clap(subcommand)] #[clap(subcommand)]
Uplc(UplcCommand), Uplc(UplcCommand),
} }
/// Commands for working with transactions
#[derive(Subcommand)]
pub enum TxCommand {
/// Simulate a transaction by evaluating it's script
Simulate {
input: PathBuf,
#[clap(short, long)]
cbor: bool,
},
}
/// Commands for working with Untyped Plutus Core /// Commands for working with Untyped Plutus Core
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum UplcCommand { pub enum UplcCommand {

View File

@ -1,5 +1,6 @@
use std::{fmt::Write as _, fs}; use std::{fmt::Write as _, fs};
use pallas_traverse::{Era, MultiEraTx};
use uplc::{ use uplc::{
ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term}, ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term},
machine::cost_model::ExBudget, machine::cost_model::ExBudget,
@ -8,13 +9,29 @@ use uplc::{
mod args; mod args;
use args::{Args, UplcCommand}; use args::{Args, TxCommand, UplcCommand};
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = Args::default(); let args = Args::default();
match args { match args {
Args::Uplc(uplc) => match uplc { Args::Tx(tx_cmd) => match tx_cmd {
TxCommand::Simulate { input, cbor } => {
let tx_bytes = if cbor {
fs::read(input)?
} else {
let cbor_hex = fs::read_to_string(input)?;
hex::decode(cbor_hex)?
};
let tx = MultiEraTx::decode(Era::Alonzo, &tx_bytes)
.or_else(|_| MultiEraTx::decode(Era::Byron, &tx_bytes))?;
println!("{:?}", tx);
}
},
Args::Uplc(uplc_cmd) => match uplc_cmd {
UplcCommand::Flat { input, print, out } => { UplcCommand::Flat { input, print, out } => {
let code = std::fs::read_to_string(&input)?; let code = std::fs::read_to_string(&input)?;