#[macro_use]
extern crate log;
extern crate mwc_wallet_controller as wallet;
extern crate mwc_wallet_impls as impls;
use mwc_wallet_util::mwc_core as core;
use mwc_wallet_util::mwc_core::global;
use self::libwallet::OutputStatus;
use impls::test_framework::{self, LocalWalletClient};
use mwc_wallet_libwallet as libwallet;
use std::thread;
use std::time::Duration;
#[macro_use]
mod common;
use common::{clean_output_dir, create_wallet_proxy, setup};
fn self_spend_impl(test_dir: &'static str) -> Result<(), wallet::Error> {
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
let mut wallet_proxy = create_wallet_proxy(test_dir);
let chain = wallet_proxy.chain.clone();
create_wallet_and_add!(
client1,
wallet1,
mask1_i,
test_dir,
"wallet1",
None,
&mut wallet_proxy,
true
);
let mask1 = (&mask1_i).as_ref();
thread::spawn(move || {
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
if let Err(e) = wallet_proxy.run() {
error!("Wallet Proxy error: {}", e);
}
});
let reward = core::consensus::MWC_FIRST_GROUP_REWARD;
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
api.create_account_path(m, "mining1")?;
Ok(())
})?;
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining1")?;
}
let mut bh = 4u64;
let _ =
test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, bh as usize, false);
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
let (_wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(m, true, 1)?;
debug!(
"Wallet 1 Info Pre-Transaction, after {} blocks: {:?}",
wallet1_info.last_confirmed_height,
wallet1_info );
assert_eq!(wallet1_info.total, bh * reward);
Ok(())
})?;
let (_, output_mappings) =
libwallet::owner::retrieve_outputs(wallet1.clone(), mask1, &None, false, false, None)?;
let mut output_list = Vec::new();
for m in output_mappings.clone() {
debug!("===========the outputs are {:?}", m);
if m.output.status != OutputStatus::Unconfirmed && m.output.status != OutputStatus::Locked {
output_list.push(m.output);
}
}
debug!("===========the outputs list are {:?}", output_list);
assert!(output_list.len() == 4);
libwallet::owner::self_spend_particular_output(
wallet1.clone(),
mask1,
output_list[0].clone(),
Some("mining1".to_string()),
1,
1,
true,
)?;
libwallet::owner::self_spend_particular_output(
wallet1.clone(),
mask1,
output_list[1].clone(),
Some("mining1".to_string()),
1,
1,
true,
)?;
let _fee = core::libtx::tx_fee(1, 1, 1);
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
let (_wallet1_refreshed, wallet1_info) = api.retrieve_summary_info(m, true, 1)?;
debug!(
"Wallet 1 Info Pre-Transaction, after {} blocks: {:?}",
wallet1_info.last_confirmed_height, wallet1_info
);
bh += 2;
assert_eq!(wallet1_info.total, bh * reward); Ok(())
})?;
let (_, output_mappings_after_spend) =
libwallet::owner::retrieve_outputs(wallet1.clone(), mask1, &None, false, false, None)?;
let mut output_list_after_spend = Vec::new();
for m in output_mappings_after_spend.clone() {
debug!("===========afterwards the outputs are {:?}", m);
if m.output.status != OutputStatus::Unconfirmed && m.output.status != OutputStatus::Locked {
output_list_after_spend.push(m.output);
} else {
println!("{:?}", m);
}
}
assert!(output_list_after_spend.len() == 6);
thread::sleep(Duration::from_millis(200));
Ok(())
}
#[test]
fn wallet_self_spend() {
let test_dir = "test_output/self_spend";
setup(test_dir);
if let Err(e) = self_spend_impl(test_dir) {
panic!("Libwallet Error: {}", e);
}
clean_output_dir(test_dir);
}