Simple fund and send in test

This commit is contained in:
Philipp Hoenisch 2020-11-02 14:42:08 +11:00
parent f5643a4ea4
commit 738c67a421
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
5 changed files with 110 additions and 95 deletions

View File

@ -84,9 +84,7 @@ impl Default for Monero {
args: Args::default(),
ports: None,
entrypoint: Some("".into()),
wait_for_message:
"The daemon is running offline and will not attempt to sync to the Monero network"
.to_string(),
wait_for_message: "core RPC server started ok".to_string(),
}
}
}
@ -167,7 +165,7 @@ pub struct MonerodArgs {
pub struct WalletArgs {
pub disable_rpc_login: bool,
pub confirm_external_bind: bool,
pub wallet_file: String,
pub wallet_dir: String,
pub rpc_bind_ip: String,
pub rpc_bind_port: u16,
pub daemon_address: String,
@ -259,7 +257,7 @@ impl WalletArgs {
WalletArgs {
disable_rpc_login: true,
confirm_external_bind: true,
wallet_file: wallet_name.into(),
wallet_dir: wallet_name.into(),
rpc_bind_ip: "0.0.0.0".into(),
rpc_bind_port: rpc_port,
daemon_address,
@ -279,10 +277,8 @@ impl WalletArgs {
args.push("--confirm-external-bind".to_string())
}
if !self.wallet_file.is_empty() {
args.push(format!("--wallet-dir /monero"));
// args.push(format!("--wallet-file {}", self.wallet_file));
// args.push(format!("--password {}", self.wallet_file));
if !self.wallet_dir.is_empty() {
args.push(format!("--wallet-dir {}", self.wallet_dir));
}
if !self.rpc_bind_ip.is_empty() {
@ -300,11 +296,6 @@ impl WalletArgs {
if self.log_level != 0 {
args.push(format!("--log-level {}", self.log_level));
}
// args.push(format!("--daemon-login username:password"));
// docker run --rm -d --net host -e DAEMON_HOST=node.xmr.to -e DAEMON_PORT=18081
// -e RPC_BIND_PORT=18083 -e RPC_USER=user -e RPC_PASSWD=passwd -v
// <path/to/and/including/wallet_folder>:/monero xmrto/monero monero-wallet-rpc
// --wallet-file wallet --password-file wallet.passwd
args.join(" ")
}

View File

@ -36,7 +36,7 @@ use crate::{
},
rpc::{
monerod,
wallet::{self, GetAddress, Transfer},
wallet::{self, Transfer},
},
};
@ -77,6 +77,8 @@ impl<'c> Monero {
))
}
/// Starts a new wallet container which is attached to
/// MONEROD_DEFAULT_NETWORK and MONEROD_DAEMON_CONTAINER_NAME
pub async fn new_wallet(
cli: &'c Cli,
name: &str,
@ -141,6 +143,15 @@ impl<'c> Monero {
}
Ok(())
}
/// Sends amount to address
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
let miner_wallet = self.wallet_rpc_client();
let transfer = miner_wallet.transfer(0, amount, address).await?;
Ok(transfer)
}
}
/// Mine a block ever BLOCK_TIME_SECS seconds.

View File

@ -4,7 +4,6 @@ use crate::{
};
use anyhow::Result;
use digest_auth::AuthContext;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use tracing::debug;

View File

@ -264,6 +264,24 @@ impl Client {
let r: Response<GenerateFromKeys> = serde_json::from_str(&response)?;
Ok(r.result)
}
pub async fn refresh(&self) -> Result<Refreshed> {
let request = Request::new("refresh", "");
let response = self
.inner
.post(self.url.clone())
.json(&request)
.send()
.await?
.text()
.await?;
debug!("refresh RPC response: {}", response);
let r: Response<Refreshed> = serde_json::from_str(&response)?;
Ok(r.result)
}
}
#[derive(Serialize, Debug, Clone)]
@ -393,3 +411,9 @@ pub struct GenerateFromKeys {
pub address: String,
pub info: String,
}
#[derive(Clone, Copy, Debug, Deserialize)]
pub struct Refreshed {
pub blocks_fetched: u32,
pub received_money: bool,
}

View File

@ -3,87 +3,77 @@ use spectral::prelude::*;
use testcontainers::clients::Cli;
#[tokio::test]
async fn wallet_and_accounts() {
let tc = Cli::default();
let (monero, _monerod_container) = Monero::new_monerod(&tc).unwrap();
let (wallet, _wallet_container) = Monero::new_wallet(&tc, "wallet").unwrap();
// let cli = monero.miner_wallet_rpc_client();
//
// println!("creating wallet ...");
//
// let _ = cli
// .create_wallet("wallet")
// .await
// .expect("failed to create wallet");
//
// let got = cli.get_balance(0).await.expect("failed to get balance");
// let want = 0;
//
// assert_that!(got).is_equal_to(want);
}
#[tokio::test]
async fn create_account_and_retrieve_it() {
let tc = Cli::default();
let (monero, _container) = Monero::new_monerod(&tc).unwrap();
// let cli = monero.miner_wallet_rpc_client();
//
// let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
//
// let _ = cli
// .create_wallet("wallet")
// .await
// .expect("failed to create wallet");
//
// let _ = cli
// .create_account(label)
// .await
// .expect("failed to create account");
//
// let mut found: bool = false;
// let accounts = cli
// .get_accounts("") // Empty filter.
// .await
// .expect("failed to get accounts");
// for account in accounts.subaddress_accounts {
// if account.label == label {
// found = true;
// }
// }
// assert!(found);
}
#[tokio::test]
async fn transfer_and_check_tx_key() {
async fn fund_transfer_and_check_tx_key() {
let fund_alice: u64 = 1_000_000_000_000;
let fund_bob = 0;
let tc = Cli::default();
let (monero, _container) = Monero::new_monerod(&tc).unwrap();
// let _ = monero.init(fund_alice, fund_bob).await;
//
// let address_bob = monero
// .bob_wallet_rpc_client()
// .get_address(0)
// .await
// .expect("failed to get Bob's address")
// .address;
//
// let transfer_amount = 100;
// let transfer = monero
// .alice_wallet_rpc_client()
// .transfer(0, transfer_amount, &address_bob)
// .await
// .expect("transfer failed");
//
// let tx_id = transfer.tx_hash;
// let tx_key = transfer.tx_key;
//
// let cli = monero.miner_wallet_rpc_client();
// let res = cli
// .check_tx_key(&tx_id, &tx_key, &address_bob)
// .await
// .expect("failed to check tx by key");
//
// assert_that!(res.received).is_equal_to(transfer_amount);
let (monerod, _monerod_container) = Monero::new_monerod(&tc).unwrap();
let (miner_wallet, _wallet_container) = Monero::new_wallet(&tc, "miner").await.unwrap();
let (alice_wallet, _alice_wallet_container) = Monero::new_wallet(&tc, "alice").await.unwrap();
let (bob_wallet, _bob_wallet_container) = Monero::new_wallet(&tc, "bob").await.unwrap();
let address = miner_wallet
.wallet_rpc_client()
.get_address(0)
.await
.unwrap()
.address;
monerod.start_miner(&address).await.unwrap();
let block_height = monerod
.monerod_rpc_client()
.get_block_count()
.await
.unwrap();
miner_wallet
.wait_for_wallet_height(block_height)
.await
.unwrap();
let alice_address = alice_wallet
.wallet_rpc_client()
.get_address(0)
.await
.unwrap()
.address;
let transfer = miner_wallet
.transfer(&alice_address, fund_alice)
.await
.unwrap();
monerod
.monerod_rpc_client()
.generate_blocks(10, &address)
.await
.unwrap();
let refreshed = alice_wallet.wallet_rpc_client().refresh().await.unwrap();
assert_that(&refreshed.received_money).is_true();
let got_alice_balance = alice_wallet
.wallet_rpc_client()
.get_balance(0)
.await
.unwrap();
let got_bob_balance = bob_wallet.wallet_rpc_client().get_balance(0).await.unwrap();
assert_that(&got_alice_balance).is_equal_to(fund_alice);
assert_that(&got_bob_balance).is_equal_to(fund_bob);
let tx_id = transfer.tx_hash;
let tx_key = transfer.tx_key;
let res = alice_wallet
.wallet_rpc_client()
.check_tx_key(&tx_id, &tx_key, &alice_address)
.await
.expect("failed to check tx by key");
assert_that!(res.received).is_equal_to(fund_alice);
}