Skip to content

Commit

Permalink
CLI polish
Browse files Browse the repository at this point in the history
This diff:
1. moves account generation related tests to client_proxy instead of grpc_client, which is only suppose to handle grpc related logic.
2. make ClientProxy::wallet private.
3. SignedTransaction::new_for_test -> craft_signed_transaction_for_client
  • Loading branch information
xfyfb authored and calibra-opensource committed Jun 18, 2019
1 parent 291b492 commit 004d472
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 67 deletions.
67 changes: 63 additions & 4 deletions client/src/client_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct ClientProxy {
/// Account used for mint operations.
pub faucet_account: Option<AccountData>,
/// Wallet library managing user accounts.
pub wallet: WalletLibrary,
wallet: WalletLibrary,
}

impl ClientProxy {
Expand Down Expand Up @@ -790,7 +790,7 @@ impl ClientProxy {

fn create_submit_transaction_req(
program: Program,
sender_account: &mut AccountData,
sender_account: &AccountData,
wallet: &WalletLibrary,
gas_unit_price: Option<u64>,
max_gas_amount: Option<u64>,
Expand All @@ -810,7 +810,11 @@ impl ClientProxy {
let hash = RawTransactionBytes(&bytes).hash();
let signature = sign_message(hash, &key_pair.private_key())?;

SignedTransaction::new_for_test(raw_txn, key_pair.public_key(), signature)
SignedTransaction::craft_signed_transaction_for_client(
raw_txn,
key_pair.public_key(),
signature,
)
}
None => wallet.sign_txn(&sender_account.address, raw_txn)?,
};
Expand Down Expand Up @@ -862,8 +866,41 @@ fn format_parse_data_error<T: std::fmt::Debug>(

#[cfg(test)]
mod tests {
use crate::client_proxy::ClientProxy;
use crate::client_proxy::{AddressAndIndex, ClientProxy};
use config::trusted_peers::TrustedPeersConfigHelpers;
use libra_wallet::io_utils;
use proptest::prelude::*;
use tempfile::NamedTempFile;

fn generate_accounts_from_wallet(count: usize) -> (ClientProxy, Vec<AddressAndIndex>) {
let mut accounts = Vec::new();
accounts.reserve(count);
let file = NamedTempFile::new().unwrap();
let mnemonic_path = file.into_temp_path().to_str().unwrap().to_string();
let trust_peer_file = NamedTempFile::new().unwrap();
let (_, trust_peer_config) = TrustedPeersConfigHelpers::get_test_config(1, None);
let trust_peer_path = trust_peer_file.into_temp_path();
trust_peer_config.save_config(&trust_peer_path);

let val_set_file = trust_peer_path.to_str().unwrap().to_string();

// We don't need to specify host/port since the client won't be used to connect, only to
// generate random accounts
let mut client_proxy = ClientProxy::new(
"", /* host */
"", /* port */
&val_set_file,
&"",
None,
Some(mnemonic_path),
)
.unwrap();
for _ in 0..count {
accounts.push(client_proxy.create_next_account(&["c"]).unwrap());
}

(client_proxy, accounts)
}

#[test]
fn test_micro_libra_conversion() {
Expand All @@ -881,6 +918,28 @@ mod tests {
assert!(ClientProxy::convert_to_micro_libras("18446744073709.551616").is_err());
}

#[test]
fn test_generate() {
let num = 1;
let (_, accounts) = generate_accounts_from_wallet(num);
assert_eq!(accounts.len(), num);
}

#[test]
fn test_write_recover() {
let num = 15;
let (client, accounts) = generate_accounts_from_wallet(num);
assert_eq!(accounts.len(), num);

let file = NamedTempFile::new().unwrap();
let path = file.into_temp_path();
io_utils::write_recovery(&client.wallet, &path).expect("failed to write to file");

let wallet = io_utils::recover(&path).expect("failed to load from file");

assert_eq!(client.wallet.mnemonic(), wallet.mnemonic());
}

proptest! {
// Proptest is used to verify that the conversion will not panic with random input.
#[test]
Expand Down
60 changes: 0 additions & 60 deletions client/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,63 +343,3 @@ impl GRPCClient {
.timeout(std::time::Duration::from_millis(5000))
}
}

#[cfg(test)]
mod tests {
use crate::client_proxy::{AddressAndIndex, ClientProxy};
use config::trusted_peers::TrustedPeersConfigHelpers;
use libra_wallet::io_utils;
use tempfile::NamedTempFile;

pub fn generate_accounts_from_wallet(count: usize) -> (ClientProxy, Vec<AddressAndIndex>) {
let mut accounts = Vec::new();
accounts.reserve(count);
let file = NamedTempFile::new().unwrap();
let mnemonic_path = file.into_temp_path().to_str().unwrap().to_string();
let trust_peer_file = NamedTempFile::new().unwrap();
let (_, trust_peer_config) = TrustedPeersConfigHelpers::get_test_config(1, None);
let trust_peer_path = trust_peer_file.into_temp_path();
trust_peer_config.save_config(&trust_peer_path);

let val_set_file = trust_peer_path.to_str().unwrap().to_string();

// We don't need to specify host/port since the client won't be used to connect, only to
// generate random accounts
let mut client_proxy = ClientProxy::new(
"", /* host */
"", /* port */
&val_set_file,
&"",
None,
Some(mnemonic_path),
)
.unwrap();
for _ in 0..count {
accounts.push(client_proxy.create_next_account(&["c"]).unwrap());
}

(client_proxy, accounts)
}

#[test]
fn test_generate() {
let num = 1;
let (_, accounts) = generate_accounts_from_wallet(num);
assert_eq!(accounts.len(), num);
}

#[test]
fn test_write_recover() {
let num = 15;
let (client, accounts) = generate_accounts_from_wallet(num);
assert_eq!(accounts.len(), num);

let file = NamedTempFile::new().unwrap();
let path = file.into_temp_path();
io_utils::write_recovery(&client.wallet, &path).expect("failed to write to file");

let wallet = io_utils::recover(&path).expect("failed to load from file");

assert_eq!(client.wallet.mnemonic(), wallet.mnemonic());
}
}
2 changes: 1 addition & 1 deletion types/src/test_helpers/transaction_test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn get_unverified_test_signed_transaction(
let hash = RawTransactionBytes(&bytes).hash();
let signature = sign_message(hash, &private_key).unwrap();

SignedTransaction::new_for_test(
SignedTransaction::craft_signed_transaction_for_client(
RawTransaction::from_proto(raw_txn).unwrap(),
public_key,
signature,
Expand Down
2 changes: 1 addition & 1 deletion types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl fmt::Debug for SignedTransaction {
}

impl SignedTransaction {
pub fn new_for_test(
pub fn craft_signed_transaction_for_client(
raw_txn: RawTransaction,
public_key: PublicKey,
signature: Signature,
Expand Down
2 changes: 1 addition & 1 deletion types/src/unit_tests/transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use proto_conv::{FromProto, IntoProto};
fn test_signed_transaction_from_proto_invalid_signature() {
let keypair = generate_keypair();
assert!(SignedTransaction::from_proto(
SignedTransaction::new_for_test(
SignedTransaction::craft_signed_transaction_for_client(
RawTransaction::new(
AccountAddress::random(),
0,
Expand Down

0 comments on commit 004d472

Please sign in to comment.