mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Refactor out some helper functions to generate blocks after funding
This commit is contained in:
parent
3a34800311
commit
3cc32002b0
@ -25,7 +25,6 @@ pub mod image;
|
|||||||
pub mod rpc;
|
pub mod rpc;
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, Result};
|
use anyhow::{anyhow, bail, Result};
|
||||||
use serde::Deserialize;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs};
|
use testcontainers::{clients::Cli, core::Port, Container, Docker, RunArgs};
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
@ -51,6 +50,7 @@ pub struct Monero {
|
|||||||
monerod: Monerod,
|
monerod: Monerod,
|
||||||
wallets: Vec<MoneroWalletRpc>,
|
wallets: Vec<MoneroWalletRpc>,
|
||||||
miner_address: String,
|
miner_address: String,
|
||||||
|
container_prefix: String,
|
||||||
}
|
}
|
||||||
impl<'c> Monero {
|
impl<'c> Monero {
|
||||||
/// Starts a new regtest monero container setup consisting out of 1 monerod
|
/// 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
|
/// `container_prefix` if provided. There will be 1 miner wallet started
|
||||||
/// automatically. Default monerod container name will be: `monerod`
|
/// automatically. Default monerod container name will be: `monerod`
|
||||||
/// Default miner wallet container name will be: `miner`
|
/// Default miner wallet container name will be: `miner`
|
||||||
/// Default network `monero`
|
/// Default network will be: `monero`
|
||||||
pub async fn new(
|
pub async fn new(
|
||||||
cli: &'c Cli,
|
cli: &'c Cli,
|
||||||
container_prefix: Option<String>,
|
container_prefix: Option<String>,
|
||||||
network_prefix: Option<String>,
|
network_prefix: Option<String>,
|
||||||
additional_wallets: Vec<String>,
|
additional_wallets: Vec<String>,
|
||||||
) -> Result<(Self, Vec<Container<'c, Cli, image::Monero>>)> {
|
) -> Result<(Self, Vec<Container<'c, Cli, image::Monero>>)> {
|
||||||
let monerod_name = format!(
|
let container_prefix = container_prefix.unwrap_or_else(|| "".to_string());
|
||||||
"{}{}",
|
|
||||||
container_prefix.unwrap_or_else(|| "".to_string()),
|
let monerod_name = format!("{}{}", container_prefix, MONEROD_DAEMON_CONTAINER_NAME);
|
||||||
MONEROD_DAEMON_CONTAINER_NAME
|
|
||||||
);
|
|
||||||
let network = format!(
|
let network = format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
network_prefix.unwrap_or_else(|| "".to_string()),
|
network_prefix.unwrap_or_else(|| "".to_string()),
|
||||||
@ -82,7 +80,8 @@ impl<'c> Monero {
|
|||||||
let mut wallets = vec![];
|
let mut wallets = vec![];
|
||||||
|
|
||||||
tracing::info!("Starting miner...");
|
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;
|
let miner_address = miner_wallet.address().await?.address;
|
||||||
|
|
||||||
monerod.start_miner(&miner_address).await?;
|
monerod.start_miner(&miner_address).await?;
|
||||||
@ -98,7 +97,8 @@ impl<'c> Monero {
|
|||||||
containers.push(miner_container);
|
containers.push(miner_container);
|
||||||
for wallet in additional_wallets.iter() {
|
for wallet in additional_wallets.iter() {
|
||||||
tracing::info!("Starting wallet: {}...", wallet);
|
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);
|
wallets.push(wallet);
|
||||||
containers.push(container);
|
containers.push(container);
|
||||||
}
|
}
|
||||||
@ -108,6 +108,7 @@ impl<'c> Monero {
|
|||||||
monerod,
|
monerod,
|
||||||
wallets,
|
wallets,
|
||||||
miner_address,
|
miner_address,
|
||||||
|
container_prefix,
|
||||||
},
|
},
|
||||||
containers,
|
containers,
|
||||||
))
|
))
|
||||||
@ -118,22 +119,36 @@ impl<'c> Monero {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
|
pub fn wallet(&self, name: &str) -> Result<&MoneroWalletRpc> {
|
||||||
|
let name = format!("{}{}", self.container_prefix, name);
|
||||||
let wallet = self
|
let wallet = self
|
||||||
.wallets
|
.wallets
|
||||||
.iter()
|
.iter()
|
||||||
.find(|wallet| wallet.name.eq(name))
|
.find(|wallet| wallet.name.eq(&name))
|
||||||
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
|
.ok_or_else(|| anyhow!("Could not find wallet container."))?;
|
||||||
|
|
||||||
Ok(wallet)
|
Ok(wallet)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fund(&self, address: &str, amount: u64) -> Result<Transfer> {
|
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
|
self.monerod
|
||||||
.inner()
|
.inner()
|
||||||
.generate_blocks(10, &self.miner_address)
|
.generate_blocks(10, &self.miner_address)
|
||||||
.await?;
|
.await?;
|
||||||
|
from.inner().refresh().await?;
|
||||||
Ok(transfer)
|
Ok(transfer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,10 +271,8 @@ impl<'c> MoneroWalletRpc {
|
|||||||
|
|
||||||
/// Sends amount to address
|
/// Sends amount to address
|
||||||
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
|
pub async fn transfer(&self, address: &str, amount: u64) -> Result<Transfer> {
|
||||||
let miner_wallet = self.inner();
|
let transfer = self.inner().transfer(0, amount, address).await?;
|
||||||
|
self.inner().refresh().await?;
|
||||||
let transfer = miner_wallet.transfer(0, amount, address).await?;
|
|
||||||
|
|
||||||
Ok(transfer)
|
Ok(transfer)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,6 +281,7 @@ impl<'c> MoneroWalletRpc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn balance(&self) -> Result<u64> {
|
pub async fn balance(&self) -> Result<u64> {
|
||||||
|
self.inner().refresh().await?;
|
||||||
self.inner().get_balance(0).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?;
|
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,
|
|
||||||
}
|
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
use crate::{
|
use crate::rpc::{Request, Response};
|
||||||
rpc::{Request, Response},
|
|
||||||
BlockHeader,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
@ -137,3 +134,22 @@ struct BlockCount {
|
|||||||
count: u32,
|
count: u32,
|
||||||
status: String,
|
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,
|
||||||
|
}
|
||||||
|
@ -5,10 +5,10 @@ use testcontainers::clients::Cli;
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn fund_transfer_and_check_tx_key() {
|
async fn fund_transfer_and_check_tx_key() {
|
||||||
let fund_alice: u64 = 1_000_000_000_000;
|
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 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(),
|
"alice".to_string(),
|
||||||
"bob".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 bob_wallet = monero.wallet("bob").unwrap();
|
||||||
|
|
||||||
let alice_address = alice_wallet.address().await.unwrap().address;
|
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();
|
let refreshed = alice_wallet.inner().refresh().await.unwrap();
|
||||||
assert_that(&refreshed.received_money).is_true();
|
assert_that(&refreshed.received_money).is_true();
|
||||||
|
|
||||||
let got_alice_balance = alice_wallet.balance().await.unwrap();
|
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);
|
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);
|
assert_that(&got_bob_balance).is_equal_to(fund_bob);
|
||||||
|
|
||||||
|
// check if tx was actually seen
|
||||||
let tx_id = transfer.tx_hash;
|
let tx_id = transfer.tx_hash;
|
||||||
let tx_key = transfer.tx_key;
|
let tx_key = transfer.tx_key;
|
||||||
|
let res = bob_wallet
|
||||||
let res = alice_wallet
|
|
||||||
.inner()
|
.inner()
|
||||||
.check_tx_key(&tx_id, &tx_key, &alice_address)
|
.check_tx_key(&tx_id, &tx_key, &bob_address)
|
||||||
.await
|
.await
|
||||||
.expect("failed to check tx by key");
|
.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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user