Skip to content

Commit

Permalink
smoke-test: convert external-transaction-signer test to forge
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored and bors-libra committed Jul 14, 2021
1 parent 8517be7 commit 64b879f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 125 deletions.
6 changes: 3 additions & 3 deletions testsuite/smoke-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
mod event_fetcher;
pub use event_fetcher::EventFetcher;

mod transaction;
pub use transaction::ExternalTransactionSigner;

#[cfg(test)]
mod client;

Expand Down Expand Up @@ -46,9 +49,6 @@ mod smoke_test_environment;
#[cfg(test)]
mod test_utils;

#[cfg(test)]
mod transaction;

#[cfg(test)]
mod verifying_client;

Expand Down
228 changes: 108 additions & 120 deletions testsuite/smoke-test/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,126 +1,114 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::test_utils::setup_swarm_and_client_proxy;
use diem_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, SigningKey, Uniform};
use diem_sdk::client::views::TransactionDataView;
use diem_types::{account_config::XUS_NAME, transaction::authenticator::AuthenticationKey};

#[test]
fn test_external_transaction_signer() {
let (_env, mut client) = setup_swarm_and_client_proxy(1, 0);

// generate key pair
let private_key = Ed25519PrivateKey::generate_for_testing();
let public_key = private_key.public_key();

// create transfer parameters
let sender_auth_key = AuthenticationKey::ed25519(&public_key);
let sender_address = sender_auth_key.derived_address();
let (receiver_address, receiver_auth_key) = client
.get_account_address_from_parameter(
"1bfb3b36384dabd29e38b4a0eafd9797b75141bb007cea7943f8a4714d3d784a",
)
.unwrap();
let amount = 1_000_000;
let test_gas_unit_price = 1;
let test_max_gas_amount = 1_000_000;

// mint to the sender address
client
.mint_coins(
&["mintb", &format!("{}", sender_auth_key), "10", "XUS"],
true,
)
.unwrap();
// mint to the recipient address
client
.mint_coins(
&[
"mintb",
&format!("{}", receiver_auth_key.unwrap()),
"1",
"XUS",
],
true,
)
.unwrap();

// prepare transfer transaction
let test_sequence_number = client
.get_sequence_number(&["sequence", &format!("{}", sender_address)])
.unwrap();

let currency_code = XUS_NAME;

let unsigned_txn = client
.prepare_transfer_coins(
sender_address,
test_sequence_number,
receiver_address,
amount,
currency_code.to_owned(),
Some(test_gas_unit_price),
Some(test_max_gas_amount),
Some(currency_code.to_owned()),
)
.unwrap();

assert_eq!(unsigned_txn.sender(), sender_address);

// sign the transaction with the private key
let signature = private_key.sign(&unsigned_txn);

// submit the transaction
let submit_txn_result = client.submit_signed_transaction(unsigned_txn, public_key, signature);

assert!(submit_txn_result.is_ok());

// query the transaction and check it contains the same values as requested
let txn = client
.get_committed_txn_by_acc_seq(&[
"txn_acc_seq",
&format!("{}", sender_address),
&test_sequence_number.to_string(),
"false",
])
.unwrap()
.unwrap();

match txn.transaction {
TransactionDataView::UserTransaction {
sender,
sequence_number,
max_gas_amount,
gas_unit_price,
gas_currency,
script,
..
} => {
assert_eq!(sender, sender_address);
assert_eq!(sequence_number, test_sequence_number);
assert_eq!(gas_unit_price, test_gas_unit_price);
assert_eq!(gas_currency, currency_code.to_string());
assert_eq!(max_gas_amount, test_max_gas_amount);

assert_eq!(script.r#type, "peer_to_peer_with_metadata");
assert_eq!(script.type_arguments.unwrap(), vec!["XUS"]);
assert_eq!(
script.arguments.unwrap(),
vec![
format!("{{ADDRESS: {:?}}}", &receiver_address),
format!("{{U64: {}}}", amount),
"{U8Vector: 0x}".to_string(),
"{U8Vector: 0x}".to_string()
]
);
// legacy fields
assert_eq!(script.receiver.unwrap(), receiver_address);
assert_eq!(script.amount.unwrap(), amount);
assert_eq!(script.currency.unwrap(), currency_code.to_string());
assert!(script.metadata.unwrap().inner().is_empty());
assert!(script.metadata_signature.unwrap().inner().is_empty());
use diem_sdk::{
client::{views::TransactionDataView, SignedTransaction},
crypto::{ed25519::Ed25519PrivateKey, PrivateKey, SigningKey, Uniform},
transaction_builder::Currency,
types::{account_config::XUS_NAME, transaction::authenticator::AuthenticationKey},
};
use forge::{PublicUsageContext, PublicUsageTest, Result, Test};

pub struct ExternalTransactionSigner;

impl Test for ExternalTransactionSigner {
fn name(&self) -> &'static str {
"smoke-test::external-transaction-signer"
}
}

impl PublicUsageTest for ExternalTransactionSigner {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let client = ctx.client();

// generate key pair
let private_key = Ed25519PrivateKey::generate(ctx.rng());
let public_key = private_key.public_key();

// create transfer parameters
let sender_auth_key = AuthenticationKey::ed25519(&public_key);
let sender_address = sender_auth_key.derived_address();
ctx.create_parent_vasp_account(sender_auth_key)?;
ctx.fund(sender_address, 10_000_000)?;

let receiver = ctx.random_account();
ctx.create_parent_vasp_account(receiver.authentication_key())?;
ctx.fund(receiver.address(), 1_000_000)?;

let amount = 1_000_000;
let test_gas_unit_price = 1;
let test_max_gas_amount = 1_000_000;

// prepare transfer transaction
let test_sequence_number = client
.get_account(sender_address)?
.into_inner()
.unwrap()
.sequence_number;

let currency_code = XUS_NAME;

let unsigned_txn = ctx
.transaction_factory()
.peer_to_peer(Currency::XUS, receiver.address(), amount)
.sender(sender_address)
.sequence_number(test_sequence_number)
.max_gas_amount(test_max_gas_amount)
.gas_unit_price(test_gas_unit_price)
.build();

assert_eq!(unsigned_txn.sender(), sender_address);

// sign the transaction with the private key
let signature = private_key.sign(&unsigned_txn);

// submit the transaction
let txn = SignedTransaction::new(unsigned_txn, public_key, signature);
client.submit(&txn)?;
client.wait_for_signed_transaction(&txn, None, None)?;

// query the transaction and check it contains the same values as requested
let txn = client
.get_account_transaction(sender_address, test_sequence_number, false)?
.into_inner()
.unwrap();

match txn.transaction {
TransactionDataView::UserTransaction {
sender,
sequence_number,
max_gas_amount,
gas_unit_price,
gas_currency,
script,
..
} => {
assert_eq!(sender, sender_address);
assert_eq!(sequence_number, test_sequence_number);
assert_eq!(gas_unit_price, test_gas_unit_price);
assert_eq!(gas_currency, currency_code.to_string());
assert_eq!(max_gas_amount, test_max_gas_amount);

assert_eq!(script.r#type, "peer_to_peer_with_metadata");
assert_eq!(script.type_arguments.unwrap(), vec!["XUS"]);
assert_eq!(
script.arguments.unwrap(),
vec![
format!("{{ADDRESS: {:?}}}", receiver.address()),
format!("{{U64: {}}}", amount),
"{U8Vector: 0x}".to_string(),
"{U8Vector: 0x}".to_string()
]
);
// legacy fields
assert_eq!(script.receiver.unwrap(), receiver.address());
assert_eq!(script.amount.unwrap(), amount);
assert_eq!(script.currency.unwrap(), currency_code.to_string());
assert!(script.metadata.unwrap().inner().is_empty());
assert!(script.metadata_signature.unwrap().inner().is_empty());
}
_ => panic!("Query should get user transaction"),
}
_ => panic!("Query should get user transaction"),
Ok(())
}
}
4 changes: 2 additions & 2 deletions testsuite/smoke-test/tests/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use forge::{forge_main, ForgeConfig, LocalFactory, Options, Result};
use smoke_test::EventFetcher;
use smoke_test::{EventFetcher, ExternalTransactionSigner};

fn main() -> Result<()> {
let tests = ForgeConfig {
public_usage_tests: &[&EventFetcher],
public_usage_tests: &[&EventFetcher, &ExternalTransactionSigner],
admin_tests: &[],
network_tests: &[],
};
Expand Down

0 comments on commit 64b879f

Please sign in to comment.