Skip to content

Commit

Permalink
add --txid to the wallet txs command (mimblewimble#176)
Browse files Browse the repository at this point in the history
* add --txid to the `wallet txs` command

* add test for `wallet txs` command with `--txid` parameter
  • Loading branch information
hendi authored and yeastplume committed Jul 8, 2019
1 parent acf724a commit 4ae404e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
28 changes: 22 additions & 6 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ pub fn outputs(
/// Txs command args
pub struct TxsArgs {
pub id: Option<u32>,
pub tx_slate_id: Option<Uuid>,
}

pub fn txs(
Expand All @@ -597,8 +598,8 @@ pub fn txs(
) -> Result<(), Error> {
controller::owner_single_use(wallet.clone(), |api| {
let res = api.node_height()?;
let (validated, txs) = api.retrieve_txs(true, args.id, None)?;
let include_status = !args.id.is_some();
let (validated, txs) = api.retrieve_txs(true, args.id, args.tx_slate_id)?;
let include_status = !args.id.is_some() && !args.tx_slate_id.is_some();
display::txs(
&g_args.account,
res.height,
Expand All @@ -607,16 +608,31 @@ pub fn txs(
include_status,
dark_scheme,
)?;
// if given a particular transaction id, also get and display associated

// if given a particular transaction id or uuid, also get and display associated
// inputs/outputs and messages
if args.id.is_some() {
let (_, outputs) = api.retrieve_outputs(true, false, args.id)?;
let id = if args.id.is_some() {
args.id
} else if args.tx_slate_id.is_some() {
if let Some(tx) = txs.iter().find(|t| t.tx_slate_id == args.tx_slate_id) {
Some(tx.id)
} else {
println!("Could not find a transaction matching given txid.\n");
None
}
} else {
None
};

if id.is_some() {
let (_, outputs) = api.retrieve_outputs(true, false, id)?;
display::outputs(&g_args.account, res.height, validated, outputs, dark_scheme)?;
// should only be one here, but just in case
for tx in txs {
display::tx_messages(&tx, dark_scheme)?;
}
};
}

Ok(())
})?;
Ok(())
Expand Down
19 changes: 18 additions & 1 deletion src/bin/cmd/wallet_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,24 @@ pub fn parse_txs_args(args: &ArgMatches) -> Result<command::TxsArgs, ParseError>
None => None,
Some(tx) => Some(parse_u64(tx, "id")? as u32),
};
Ok(command::TxsArgs { id: tx_id })
let tx_slate_id = match args.value_of("txid") {
None => None,
Some(tx) => match tx.parse() {
Ok(t) => Some(t),
Err(e) => {
let msg = format!("Could not parse txid parameter. e={}", e);
return Err(ParseError::ArgumentError(msg));
}
},
};
if tx_id.is_some() && tx_slate_id.is_some() {
let msg = format!("At most one of 'id' (-i) or 'txid' (-t) may be provided.");
return Err(ParseError::ArgumentError(msg));
}
Ok(command::TxsArgs {
id: tx_id,
tx_slate_id: tx_slate_id,
})
}

pub fn parse_repost_args(args: &ArgMatches) -> Result<command::RepostArgs, ParseError> {
Expand Down
13 changes: 13 additions & 0 deletions src/bin/cmd/wallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,19 @@ mod wallet_tests {
let arg_vec = vec!["grin-wallet", "-p", "password", "outputs"];
execute_command(&app, test_dir, "wallet2", &client2, arg_vec)?;

// get tx output via -tx parameter
let mut tx_id = "".to_string();
grin_wallet_controller::controller::owner_single_use(wallet2.clone(), |api| {
api.set_active_account("default")?;
let (_, txs) = api.retrieve_txs(true, None, None)?;
let some_tx_id = txs[0].tx_slate_id.clone();
assert!(some_tx_id.is_some());
tx_id = some_tx_id.unwrap().to_hyphenated().to_string().clone();
Ok(())
})?;
let arg_vec = vec!["grin-wallet", "-p", "password", "txs", "-t", &tx_id[..]];
execute_command(&app, test_dir, "wallet2", &client2, arg_vec)?;

// let logging finish
thread::sleep(Duration::from_millis(200));
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/bin/grin-wallet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ subcommands:
short: i
long: id
takes_value: true
- txid:
help: If specified, display transaction with given TxID UUID and all associated Inputs/Outputs
short: t
long: txid
takes_value: true
- repost:
about: Reposts a stored, completed but unconfirmed transaction to the chain, or dumps it to a file
args:
Expand Down

0 comments on commit 4ae404e

Please sign in to comment.