From 92e37e172d0b84ba52bc8d08fefe55cad98b8076 Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Sat, 4 Nov 2023 09:00:58 +0200 Subject: [PATCH] add tests --- .../output_manager_service_tests/service.rs | 24 +++++- .../transaction_service_tests/service.rs | 81 +++++++++++++++++++ .../tests/features/WalletCli.feature | 15 ++++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/base_layer/wallet/tests/output_manager_service_tests/service.rs b/base_layer/wallet/tests/output_manager_service_tests/service.rs index b8e4da968c..a25bdcb42c 100644 --- a/base_layer/wallet/tests/output_manager_service_tests/service.rs +++ b/base_layer/wallet/tests/output_manager_service_tests/service.rs @@ -62,7 +62,7 @@ use tari_core::{ transactions::{ fee::Fee, key_manager::{TransactionKeyManagerBranch, TransactionKeyManagerInterface}, - tari_amount::{uT, MicroMinotari}, + tari_amount::{uT, MicroMinotari, T}, test_helpers::{ create_test_core_key_manager_with_memory_db, create_wallet_output_with_data, @@ -1235,6 +1235,28 @@ async fn coin_split_no_change() { assert_eq!(amount, val1 + val2 + val3); } +#[tokio::test] +async fn it_handles_large_coin_splits() { + let (connection, _tempdir) = get_temp_sqlite_database_connection(); + let backend = OutputManagerSqliteDatabase::new(connection.clone()); + let mut oms = setup_output_manager_service(backend, true).await; + + let val = 20 * T; + let uo = make_input(&mut OsRng, val, &OutputFeatures::default(), &oms.key_manager_handle).await; + assert!(oms.output_manager_handle.add_output(uo, None).await.is_ok()); + + let fee_per_gram = MicroMinotari::from(1); + let split_count = 499; + + let (_tx_id, coin_split_tx, _amount) = oms + .output_manager_handle + .create_coin_split(vec![], 10000.into(), split_count, fee_per_gram) + .await + .unwrap(); + assert_eq!(coin_split_tx.body.inputs().len(), 1); + assert_eq!(coin_split_tx.body.outputs().len(), split_count + 1); +} + #[tokio::test] async fn handle_coinbase_with_bulletproofs_rewinding() { let (connection, _tempdir) = get_temp_sqlite_database_connection(); diff --git a/base_layer/wallet/tests/transaction_service_tests/service.rs b/base_layer/wallet/tests/transaction_service_tests/service.rs index 3e0bdf7ee7..5f1bd51ae2 100644 --- a/base_layer/wallet/tests/transaction_service_tests/service.rs +++ b/base_layer/wallet/tests/transaction_service_tests/service.rs @@ -741,6 +741,87 @@ async fn single_transaction_to_self() { ); } +#[tokio::test] +async fn large_coin_split_transaction() { + let network = Network::LocalNet; + let consensus_manager = ConsensusManager::builder(network).build().unwrap(); + let factories = CryptoFactories::default(); + // Alice's parameters + let alice_node_identity = Arc::new(NodeIdentity::random( + &mut OsRng, + get_next_memory_address(), + PeerFeatures::COMMUNICATION_NODE, + )); + + let base_node_identity = Arc::new(NodeIdentity::random( + &mut OsRng, + get_next_memory_address(), + PeerFeatures::COMMUNICATION_NODE, + )); + + log::info!( + "large_coin_split_transaction: Alice: '{}', Base: '{}'", + alice_node_identity.node_id().short_str(), + base_node_identity.node_id().short_str() + ); + + let temp_dir = tempdir().unwrap(); + let database_path = temp_dir.path().to_str().unwrap().to_string(); + + let (db_connection, _tempdir) = make_wallet_database_connection(Some(database_path.clone())); + + let shutdown = Shutdown::new(); + let (mut alice_ts, mut alice_oms, _alice_comms, _alice_connectivity, key_manager_handle) = + setup_transaction_service( + alice_node_identity.clone(), + vec![], + consensus_manager, + factories.clone(), + db_connection, + database_path, + Duration::from_secs(0), + shutdown.to_signal(), + ) + .await; + + let initial_wallet_value = 20 * T; + let uo1 = make_input( + &mut OsRng, + initial_wallet_value, + &OutputFeatures::default(), + &key_manager_handle, + ) + .await; + + alice_oms.add_output(uo1, None).await.unwrap(); + + let fee_per_gram = MicroMinotari::from(1); + let split_count = 499; + let (tx_id, coin_split_tx, amount) = alice_oms + .create_coin_split(vec![], 10000.into(), split_count, fee_per_gram) + .await + .unwrap(); + assert_eq!(coin_split_tx.body.inputs().len(), 1); + assert_eq!(coin_split_tx.body.outputs().len(), split_count + 1); + + alice_ts + .submit_transaction(tx_id, coin_split_tx, amount, "large coin-split".to_string()) + .await + .expect("Alice sending coin-split tx"); + + let completed_tx = alice_ts + .get_completed_transaction(tx_id) + .await + .expect("Could not find tx"); + + let fees = completed_tx.fee; + + assert_eq!( + alice_oms.get_balance().await.unwrap().pending_incoming_balance, + initial_wallet_value - fees + ); +} + #[tokio::test] async fn single_transaction_burn_tari() { // let _ = env_logger::builder().is_test(true).try_init(); // Need `$env:RUST_LOG = "trace"` for this to work diff --git a/integration_tests/tests/features/WalletCli.feature b/integration_tests/tests/features/WalletCli.feature index 197864766c..61d8cc682a 100644 --- a/integration_tests/tests/features/WalletCli.feature +++ b/integration_tests/tests/features/WalletCli.feature @@ -105,6 +105,21 @@ Feature: Wallet CLI Then wallet WALLET has at least 1 transactions that are all TRANSACTION_STATUS_MINED_CONFIRMED and not cancelled Then I get count of utxos of wallet WALLET and it's at least 10 via command line + @long-running + Scenario: As a user I want to large coin-split via command line + Given I have a seed node SEED + When I have a base node BASE connected to seed SEED + When I have wallet WALLET connected to base node BASE + When I have mining node MINE connected to base node BASE and wallet WALLET + When mining node MINE mines 4 blocks + Then I wait for wallet WALLET to have at least 1100000 uT + When I wait 30 seconds + When I do coin split on wallet WALLET to 10000 uT 499 coins via command line + Then wallet WALLET has at least 1 transactions that are all TRANSACTION_STATUS_BROADCAST and not cancelled + When mining node MINE mines 5 blocks + Then wallet WALLET has at least 1 transactions that are all TRANSACTION_STATUS_MINED_CONFIRMED and not cancelled + Then I get count of utxos of wallet WALLET and it's at least 499 via command line + Scenario: As a user I want to count utxos via command line Given I have a base node BASE When I have wallet WALLET connected to base node BASE