Skip to content

Commit

Permalink
feat: add one-sided txns to make-it-rain (#3084)
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Jul 14, 2021
2 parents a702188 + 97f38eb commit 043f27d
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 82 deletions.
25 changes: 22 additions & 3 deletions applications/tari_console_wallet/README.md
Expand Up @@ -47,18 +47,37 @@ Monitoring 1 sent transactions to Broadcast stage...
Done! All transactions monitored to Broadcast stage.
```

- **send-one-sided**

Send an amount of Tari to a public key or emoji id in a one-sided transaction.

`tari_console_wallet --command send-one-sided <amount> <pubkey> <optional message>"`

example:

```
$ tari_console_wallet --command "send-one-sided 1T c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 coffee"
1. send-tari 1.000000 T c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 coffee
Monitoring 1 sent transactions to Broadcast stage...
Done! All transactions monitored to Broadcast stage.
```

- **make-it-rain**

Make it rain! Send many transactions to a public key or emoji id.

`tari_console_wallet --command "make-it-rain <tx/sec> <duration> <amount> <increment> <start time or now> <pubkey> <optional message>"`
`tari_console_wallet --command "make-it-rain <tx/sec> <duration> <amount> <increment> <start time or now> <pubkey> <transaction type> <optional message>"`

`<type>` can be `negotiated` or `one_sided`

example:

```
$ tari_console_wallet --command "make-it-rain 1 10 8000 100 now c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 makin it rain yo"
$ tari_console_wallet --command "make-it-rain 1 10 8000 100 now c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 negotiated makin it rain yo"
1. make-it-rain 1 10 8000 µT 100 µT 2021-03-26 10:03:30.459157 UTC c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 makin it rain yo
1. make-it-rain 1 10 8000 µT 100 µT 2021-03-26 10:03:30.459157 UTC c69fbe5f05a304eaec65d5f234a6aa258a90b8bb5b9ceffea779653667ef2108 negotiated makin it rain yo
Monitoring 10 sent transactions to Broadcast stage...
Done! All transactions monitored to Broadcast stage.
Expand Down
82 changes: 79 additions & 3 deletions applications/tari_console_wallet/src/automation/command_parser.rs
Expand Up @@ -81,6 +81,7 @@ pub enum ParsedArgument {
OutputToCSVFile(String),
CSVFileName(String),
Address(Multiaddr),
Negotiated(bool),
}

impl Display for ParsedArgument {
Expand All @@ -96,6 +97,7 @@ impl Display for ParsedArgument {
OutputToCSVFile(v) => write!(f, "{}", v.to_string()),
CSVFileName(v) => write!(f, "{}", v.to_string()),
Address(v) => write!(f, "{}", v.to_string()),
Negotiated(v) => write!(f, "{}", v.to_string()),
}
}
}
Expand Down Expand Up @@ -181,7 +183,7 @@ fn parse_make_it_rain(mut args: SplitWhitespace) -> Result<Vec<ParsedArgument>,
let txps = txps.parse::<f64>().map_err(ParseError::Float)?;
if txps > 25.0 {
println!("Maximum transaction rate is 25/sec");
return Err(ParseError::Invalid);
return Err(ParseError::Invalid("Maximum transaction rate is 25/sec".to_string()));
}
parsed_args.push(ParsedArgument::Float(txps));

Expand All @@ -192,7 +194,9 @@ fn parse_make_it_rain(mut args: SplitWhitespace) -> Result<Vec<ParsedArgument>,

if (txps * duration as f64) < 1.0 {
println!("Invalid data provided for [number of Txs/s] * [test duration (s)], must be >= 1\n");
return Err(ParseError::Invalid);
return Err(ParseError::Invalid(
"Invalid data provided for [number of Txs/s] * [test duration (s)], must be >= 1".to_string(),
));
}

// start amount
Expand Down Expand Up @@ -226,6 +230,22 @@ fn parse_make_it_rain(mut args: SplitWhitespace) -> Result<Vec<ParsedArgument>,
let pubkey = parse_emoji_id_or_public_key(pubkey).ok_or(ParseError::PublicKey)?;
parsed_args.push(ParsedArgument::PublicKey(pubkey));

// transaction type
let txn_type = args
.next()
.ok_or_else(|| ParseError::Empty("transaction type".to_string()))?;
let negotiated = match txn_type {
"negotiated" => true,
"one_sided" => false,
_ => {
println!("Invalid data provided for <transaction type>, must be 'negotiated' or 'one_sided'\n");
return Err(ParseError::Invalid(
"Invalid data provided for <transaction type>, must be 'negotiated' or 'one_sided'".to_string(),
));
},
};
parsed_args.push(ParsedArgument::Negotiated(negotiated));

// message
let message = args.collect::<Vec<&str>>().join(" ");
parsed_args.push(ParsedArgument::Text(message));
Expand Down Expand Up @@ -322,7 +342,10 @@ fn parse_coin_split(mut args: SplitWhitespace) -> Result<Vec<ParsedArgument>, Pa

#[cfg(test)]
mod test {
use crate::automation::command_parser::{parse_command, ParsedArgument};
use crate::automation::{
command_parser::{parse_command, ParsedArgument},
error::ParseError,
};
use rand::rngs::OsRng;
use std::str::FromStr;
use tari_core::transactions::{tari_amount::MicroTari, types::PublicKey};
Expand Down Expand Up @@ -402,5 +425,58 @@ mod test {
} else {
panic!("Parsed csv file name is not the same as provided.");
}

let transaction_type = "negotiated";
let message = "Testing the network!";
let command_str = format!(
"make-it-rain 20 225 9000 0 now {} {} {}",
public_key, transaction_type, message
);
let parsed = parse_command(&command_str).unwrap();

if let ParsedArgument::PublicKey(pk) = parsed.args[5].clone() {
assert_eq!(pk, public_key);
} else {
panic!("Parsed public key is not the same as provided.");
}
if let ParsedArgument::Negotiated(negotiated) = parsed.args[6].clone() {
assert!(negotiated);
} else {
panic!("Parsed <transaction type> is not the same as provided.");
}
if let ParsedArgument::Text(msg) = parsed.args[7].clone() {
assert_eq!(message, msg);
} else {
panic!("Parsed message is not the same as provided.");
}

let transaction_type = "one_sided";
let command_str = format!(
"make-it-rain 20 225 9000 0 now {} {} {}",
public_key, transaction_type, message
);
let parsed = parse_command(&command_str).unwrap();

if let ParsedArgument::Negotiated(negotiated) = parsed.args[6].clone() {
assert!(!negotiated);
} else {
panic!("Parsed <transaction type> is not the same as provided.");
}

let transaction_type = "what_ever";
let command_str = format!(
"make-it-rain 20 225 9000 0 now {} {} {}",
public_key, transaction_type, message
);
match parse_command(&command_str) {
Ok(_) => panic!("<transaction type> argument '{}' not allowed", transaction_type),
Err(e) => match e {
ParseError::Invalid(e) => assert_eq!(
e,
"Invalid data provided for <transaction type>, must be 'negotiated' or 'one_sided'".to_string()
),
_ => panic!("Expected parsing <transaction type> to return an error here"),
},
}
}
}

0 comments on commit 043f27d

Please sign in to comment.