feat: output total budget spent from cli
This commit is contained in:
parent
4166e27fd7
commit
08596588a7
|
@ -1,4 +0,0 @@
|
|||
(program
|
||||
1.0.0
|
||||
(lam y (lam x [ [ (builtin addInteger) x ] y ]))
|
||||
)
|
|
@ -20,21 +20,29 @@ pub enum Args {
|
|||
pub enum TxCommand {
|
||||
/// Simulate a transaction by evaluating it's script
|
||||
Simulate {
|
||||
/// A file containing cbor hex
|
||||
/// A file containing cbor hex for a transaction
|
||||
input: PathBuf,
|
||||
|
||||
/// Toggle whether input is raw cbor or a hex string
|
||||
#[clap(short, long)]
|
||||
cbor: bool,
|
||||
|
||||
/// Json file containing resolved inputs
|
||||
#[clap(short, long)]
|
||||
resolved_inputs: PathBuf,
|
||||
#[clap(short, long)]
|
||||
/// A file containing cbor hex for the raw inputs
|
||||
raw_inputs: PathBuf,
|
||||
|
||||
/// A file containing cbor hex for the raw outputs
|
||||
raw_outputs: PathBuf,
|
||||
|
||||
/// Time between each slot
|
||||
#[clap(short, long, default_value_t = 1000)]
|
||||
slot_length: u64,
|
||||
#[clap(long)]
|
||||
|
||||
/// Time of shelley hardfork
|
||||
#[clap(long, default_value_t = 1596059091000)]
|
||||
zero_time: u64,
|
||||
#[clap(long)]
|
||||
|
||||
/// Slot number at the start of the shelley hardfork
|
||||
#[clap(long, default_value_t = 4492800)]
|
||||
zero_slot: u64,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
use std::{fmt::Write as _, fs};
|
||||
|
||||
use pallas_primitives::{
|
||||
babbage::{TransactionInput, TransactionOutput},
|
||||
Fragment,
|
||||
};
|
||||
use pallas_traverse::{Era, MultiEraTx};
|
||||
use uplc::{
|
||||
ast::{DeBruijn, FakeNamedDeBruijn, Name, NamedDeBruijn, Program, Term},
|
||||
|
@ -23,27 +27,48 @@ fn main() -> anyhow::Result<()> {
|
|||
TxCommand::Simulate {
|
||||
input,
|
||||
cbor,
|
||||
resolved_inputs,
|
||||
raw_inputs,
|
||||
raw_outputs,
|
||||
slot_length,
|
||||
zero_time,
|
||||
zero_slot,
|
||||
} => {
|
||||
let tx_bytes = if cbor {
|
||||
fs::read(input)?
|
||||
let (tx_bytes, inputs_bytes, outputs_bytes) = if cbor {
|
||||
(
|
||||
fs::read(input)?,
|
||||
fs::read(raw_inputs)?,
|
||||
fs::read(raw_outputs)?,
|
||||
)
|
||||
} else {
|
||||
let cbor_hex = fs::read_to_string(input)?;
|
||||
let inputs_hex = fs::read_to_string(raw_inputs)?;
|
||||
let outputs_hex = fs::read_to_string(raw_outputs)?;
|
||||
|
||||
hex::decode(cbor_hex.trim())?
|
||||
(
|
||||
hex::decode(cbor_hex.trim())?,
|
||||
hex::decode(inputs_hex.trim())?,
|
||||
hex::decode(outputs_hex.trim())?,
|
||||
)
|
||||
};
|
||||
|
||||
let tx = MultiEraTx::decode(Era::Babbage, &tx_bytes)
|
||||
.or_else(|_| MultiEraTx::decode(Era::Alonzo, &tx_bytes))?;
|
||||
|
||||
let inputs = Vec::<TransactionInput>::decode_fragment(&inputs_bytes).unwrap();
|
||||
let outputs = Vec::<TransactionOutput>::decode_fragment(&outputs_bytes).unwrap();
|
||||
|
||||
let resolved_inputs: Vec<ResolvedInput> = inputs
|
||||
.iter()
|
||||
.zip(outputs.iter())
|
||||
.map(|(input, output)| ResolvedInput {
|
||||
input: input.clone(),
|
||||
output: output.clone(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
println!("Simulating: {}", tx.hash());
|
||||
|
||||
if let Some(tx_babbage) = tx.as_babbage() {
|
||||
let resolved_inputs = ResolvedInput::from_json(&resolved_inputs)?;
|
||||
|
||||
let slot_config = SlotConfig {
|
||||
zero_time,
|
||||
zero_slot,
|
||||
|
@ -61,11 +86,18 @@ fn main() -> anyhow::Result<()> {
|
|||
|
||||
match result {
|
||||
Ok(redeemers) => {
|
||||
println!("\nResult\n------\n\n");
|
||||
println!("\nTotal Budget Used\n-----------------\n");
|
||||
|
||||
for redeemer in redeemers {
|
||||
println!("{:#?}", redeemer)
|
||||
}
|
||||
let total_budget_used = redeemers.iter().fold(
|
||||
ExBudget { mem: 0, cpu: 0 },
|
||||
|accum, curr| ExBudget {
|
||||
mem: accum.mem + curr.ex_units.mem as i64,
|
||||
cpu: accum.cpu + curr.ex_units.steps as i64,
|
||||
},
|
||||
);
|
||||
|
||||
println!("mem: {}", total_budget_used.mem);
|
||||
println!("cpu: {}", total_budget_used.cpu);
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("\nError\n-----\n\n{}\n", err);
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::{fs::File, io::BufReader, path::PathBuf};
|
||||
|
||||
use pallas_codec::utils::KeyValuePairs;
|
||||
use pallas_crypto::hash::Hash;
|
||||
use pallas_primitives::babbage::{
|
||||
|
@ -16,26 +14,6 @@ pub struct ResolvedInput {
|
|||
pub output: TransactionOutput,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("{0}")]
|
||||
File(#[from] std::io::Error),
|
||||
#[error("{0}")]
|
||||
Serde(#[from] serde_json::error::Error),
|
||||
}
|
||||
|
||||
impl ResolvedInput {
|
||||
pub fn from_json(file: &PathBuf) -> Result<Vec<Self>, Error> {
|
||||
let file = File::open(file)?;
|
||||
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let resolved_inputs: Vec<ResolvedInput> = serde_json::from_reader(reader)?;
|
||||
|
||||
Ok(resolved_inputs)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct TxInInfo {
|
||||
pub out_ref: TransactionInput,
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
[
|
||||
{"input":{"tx_hash":"71b02d2309057ca589878c02ef9f89ca2a911f4282bef459a44b035deee292f0","index":0},"output":{"address":"addr1zxj47sy4qxlktqzmkrw8dahe46gtv8seakrshsqz26qnvzypw288a4x0xf8pxgcntelxmyclq83s0ykeehchz2wtspksr3q9nx","value":[1724100,{"deebf749dd081b3aea1c59ef2a1be1038d61a0c7398de15c310244be": {"54455354544f4b454e313631": 1}}],"datum":{"datum_hash": "d908988cd6197fb46e9711a8e84eda57e1b134b0e0fe11ea7e9cdc6e3d484189"}}},
|
||||
{"input":{"tx_hash":"ba68b9076c6c34666d5a554d7cd0fdffacc1c38d86cd37ff5c565d77a1ce2fd0","index":0},"output":{"address":"addr1qxpct7l5e9gmtlt73uz9000y08yelyg0tdcqdc9ykle7w98vqctk0alpkvrf7l226m8djg05lusu74uvx69hp6ve808qxc39wj","value":[5000000,{}]}}
|
||||
]
|
Loading…
Reference in New Issue