Refactor out some helper functions to generate blocks after funding

This commit is contained in:
Philipp Hoenisch 2020-11-02 18:58:52 +11:00
parent 3a34800311
commit 3cc32002b0
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
3 changed files with 69 additions and 48 deletions

View File

@ -25,7 +25,6 @@ pub mod image;
pub mod rpc;
use anyhow::{anyhow, bail, Result};
use serde::Deserialize;
use std::time::Duration;
use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs};
use tokio::time;
@ -51,6 +50,7 @@ pub struct Monero {
monerod: Monerod,
wallets: Vec<MoneroWalletRpc>,
miner_address: String,
container_prefix: String,
}
impl<'c> Monero {
/// Starts a new regtest monero container setup consisting out of 1 monerod
@ -58,18 +58,16 @@ impl<'c> Monero {
/// `container_prefix` if provided. There will be 1 miner wallet started
/// automatically. Default monerod container name will be: `monerod`
/// Default miner wallet container name will be: `miner`
/// Default network `monero`
/// Default network will be: `monero`
pub async fn new(
cli: &'c Cli,
container_prefix: Option<String>,
network_prefix: Option<String>,
additional_wallets: Vec<String>,
) -> Result<(Self, Vec<Container<'c, Cli, image::Monero>>)> {
let monerod_name = format!(
"{}{}",
container_prefix.unwrap_or_else(|| "".to_string()),
MONEROD_DAEMON_CONTAINER_NAME
);
let container_prefix = container_prefix.unwrap_or_else(|| "".to_string());
let monerod_name = format!("{}{}", container_prefix, MONEROD_DAEMON_CONTAINER_NAME);
let network = format!(
"{}{}",
network_prefix.unwrap_or_else(|| "".to_string()),
@ -82,7 +80,8 @@ impl<'c> Monero {
let mut wallets = vec![];
tracing::info!("Starting miner...");
let (miner_wallet, miner_container) = MoneroWalletRpc::new(cli, "miner", &monerod).await?;
let miner = format!("{}{}", container_prefix, "miner");
let (miner_wallet, miner_container) = MoneroWalletRpc::new(cli, &miner, &monerod).await?;
let miner_address = miner_wallet.address().await?.address;
monerod.start_miner(&miner_address).await?;
@ -98,7 +97,8 @@ impl<'c> Monero {
containers.push(miner_container);
for wallet in additional_wallets.iter() {
tracing::info!("Starting wallet: {}...", wallet);
let (wallet, container) = MoneroWalletRpc::new(cli, wallet, &monerod).await?;
let wallet = format!("{}{}", container_prefix, wallet);
let (wallet, container) = MoneroWalletRpc::new(cli, &wallet, &monerod).await?;
wallets.push(wallet);
containers.push(container);
}
@ -108,6 +108,7 @@ impl<'c> Monero {
monerod,
wallets,
miner_address,
container_prefix,
},
containers,
))
@ -118,22 +119,36 @@ impl<'c> Monero {
}
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
let name = format!("{}{}", self.container_prefix, name);
let wallet = self
.wallets
.iter()
.find(|wallet| wallet.name.eq(name))
.find(|wallet| wallet.name.eq(&name))
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
Ok(wallet)
}
pub async fn fund(&self, address: &str, amount: u64) -> Result<Transfer> {
let transfer = self.wallet("miner")?.transfer(address, amount).await?;
self.transfer("miner", address, amount).await
}
pub async fn transfer_from_alice(&self, address: &str, amount: u64) -> Result<Transfer> {
self.transfer("alice", address, amount).await
}
pub async fn transfer_from_bob(&self, address: &str, amount: u64) -> Result<Transfer> {
self.transfer("bob", address, amount).await
}
async fn transfer(&self, from_wallet: &str, address: &str, amount: u64) -> Result<Transfer> {
let from = self.wallet(from_wallet)?;
let transfer = from.transfer(address, amount).await?;
self.monerod
.inner()
.generate_blocks(10, &self.miner_address)
.await?;
from.inner().refresh().await?;
Ok(transfer)
}
}
@ -256,10 +271,8 @@ impl<'c> MoneroWalletRpc {
/// Sends amount to address
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
let miner_wallet = self.inner();
let transfer = miner_wallet.transfer(0, amount, address).await?;
let transfer = self.inner().transfer(0, amount, address).await?;
self.inner().refresh().await?;
Ok(transfer)
}
@ -268,6 +281,7 @@ impl<'c> MoneroWalletRpc {
}
pub async fn balance(&self) -> Result<u64> {
self.inner().refresh().await?;
self.inner().get_balance(0).await
}
}
@ -278,22 +292,3 @@ async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {
monerod.generate_blocks(1, &reward_address).await?;
}
}
// We should be able to use monero-rs for this but it does not include all
// the fields.
#[derive(Clone, Debug, Deserialize)]
pub struct BlockHeader {
pub block_size: u32,
pub depth: u32,
pub difficulty: u32,
pub hash: String,
pub height: u32,
pub major_version: u32,
pub minor_version: u32,
pub nonce: u32,
pub num_txes: u32,
pub orphan_status: bool,
pub prev_hash: String,
pub reward: u64,
pub timestamp: u32,
}

View File

@ -1,7 +1,4 @@
use crate::{
rpc::{Request, Response},
BlockHeader,
};
use crate::rpc::{Request, Response};
use anyhow::Result;
use reqwest::Url;
@ -137,3 +134,22 @@ struct BlockCount {
count: u32,
status: String,
}
// We should be able to use monero-rs for this but it does not include all
// the fields.
#[derive(Clone, Debug, Deserialize)]
pub struct BlockHeader {
pub block_size: u32,
pub depth: u32,
pub difficulty: u32,
pub hash: String,
pub height: u32,
pub major_version: u32,
pub minor_version: u32,
pub nonce: u32,
pub num_txes: u32,
pub orphan_status: bool,
pub prev_hash: String,
pub reward: u64,
pub timestamp: u32,
}

View File

@ -5,10 +5,10 @@ use testcontainers::clients::Cli;
#[tokio::test]
async fn fund_transfer_and_check_tx_key() {
let fund_alice: u64 = 1_000_000_000_000;
let fund_bob = 0;
let fund_bob = 5_000_000_000;
let tc = Cli::default();
let (monero, _containers) = Monero::new(&tc, Some("test".to_string()), None, vec![
let (monero, _containers) = Monero::new(&tc, Some("test_".to_string()), None, vec![
"alice".to_string(),
"bob".to_string(),
])
@ -18,26 +18,36 @@ async fn fund_transfer_and_check_tx_key() {
let bob_wallet = monero.wallet("bob").unwrap();
let alice_address = alice_wallet.address().await.unwrap().address;
let bob_address = bob_wallet.address().await.unwrap().address;
let transfer = monero.fund(&alice_address, fund_alice).await.unwrap();
// fund alice
monero.fund(&alice_address, fund_alice).await.unwrap();
// check alice balance
let refreshed = alice_wallet.inner().refresh().await.unwrap();
assert_that(&refreshed.received_money).is_true();
let got_alice_balance = alice_wallet.balance().await.unwrap();
let got_bob_balance = bob_wallet.balance().await.unwrap();
assert_that(&got_alice_balance).is_equal_to(fund_alice);
// transfer from alice to bob
let transfer = monero
.transfer_from_alice(&bob_address, fund_bob)
.await
.unwrap();
let refreshed = bob_wallet.inner().refresh().await.unwrap();
assert_that(&refreshed.received_money).is_true();
let got_bob_balance = bob_wallet.balance().await.unwrap();
assert_that(&got_bob_balance).is_equal_to(fund_bob);
// check if tx was actually seen
let tx_id = transfer.tx_hash;
let tx_key = transfer.tx_key;
let res = alice_wallet
let res = bob_wallet
.inner()
.check_tx_key(&tx_id, &tx_key, &alice_address)
.check_tx_key(&tx_id, &tx_key, &bob_address)
.await
.expect("failed to check tx by key");
assert_that!(res.received).is_equal_to(fund_alice);
assert_that!(res.received).is_equal_to(fund_bob);
}