Skip to content

Commit

Permalink
tx generator: allow args to be specified using a file
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jan 5, 2024
1 parent 1b25005 commit 0ff5fbc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
6 changes: 4 additions & 2 deletions utilities/transaction_generator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ pub struct WriteArgs {
pub overwrite: bool,
#[clap(long, short = 'm')]
pub manifest: Option<PathBuf>,
#[clap(long, short = 'g', alias = "global")]
pub manifest_globals: Vec<String>,
#[clap(long, short = 'a', alias = "arg")]
pub manifest_args: Vec<String>,
#[clap(long, alias = "args-file")]
pub manifest_args_file: Option<PathBuf>,
#[clap(long, short = 'k', alias = "signer")]
pub signer_secret_key: Option<String>,
}
Expand Down
40 changes: 26 additions & 14 deletions utilities/transaction_generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ mod transaction_writer;
use std::{
collections::HashMap,
fs,
io::{stdout, Seek, SeekFrom, Write},
io,
io::{stdout, BufRead, Seek, SeekFrom, Write},
};

use anyhow::anyhow;
Expand Down Expand Up @@ -87,22 +88,33 @@ fn get_transaction_builder(args: &WriteArgs) -> anyhow::Result<BoxedTransactionB
.transpose()
.map_err(|_| anyhow!("Failed to parse secret"))?
.unwrap_or_else(|| RistrettoSecretKey::random(&mut OsRng));
manifest::builder(signer_key, manifest, parse_globals(&args.manifest_globals)?)
let mut manifest_args = parse_args(&args.manifest_args)?;
if let Some(args_file) = &args.manifest_args_file {
let file = io::BufReader::new(fs::File::open(args_file)?);
for ln in file.lines() {
let ln = ln?;
let line = ln.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
manifest_args.extend(parse_arg(line));
}
}
manifest::builder(signer_key, manifest, manifest_args)
},
None => Ok(Box::new(free_coins::builder)),
}
}

fn parse_globals(globals: &[String]) -> Result<HashMap<String, ManifestValue>, anyhow::Error> {
let mut result = HashMap::with_capacity(globals.len());
for global in globals {
let (name, value) = global
.split_once('=')
.ok_or_else(|| anyhow!("Invalid global: {}", global))?;
let value = value
.parse()
.map_err(|err| anyhow!("Failed to parse global '{}': {}", name, err))?;
result.insert(name.to_string(), value);
}
Ok(result)
fn parse_args(globals: &[String]) -> Result<HashMap<String, ManifestValue>, anyhow::Error> {
globals.iter().map(|s| parse_arg(s)).collect()
}

fn parse_arg(arg: &str) -> Result<(String, ManifestValue), anyhow::Error> {
let (name, value) = arg.split_once('=').ok_or_else(|| anyhow!("Invalid arg: {}", arg))?;
let value = value
.trim()
.parse()
.map_err(|err| anyhow!("Failed to parse arg '{}': {}", name, err))?;
Ok((name.trim().to_string(), value))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn builder<P: AsRef<Path>>(
let instructions = tari_transaction_manifest::parse_manifest(&contents, globals)?;
Ok(Box::new(move |_| {
Transaction::builder()
.with_fee_instructions_builder(|builder| builder.with_instructions(instructions.fee_instructions.clone()))
.with_fee_instructions(instructions.fee_instructions.clone())
.with_instructions(instructions.instructions.clone())
.sign(&signer_secret_key)
.build()
Expand Down

0 comments on commit 0ff5fbc

Please sign in to comment.