mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Simplify xmr-btc/tests Monero wallet
- Make it the same for Alice and Bob. - Make it contain a wallet client instead of the `Monero` struct. Also: Remove `Container` from inside `Monero` struct. The caller of `new` can simply ensure that `Container` is not dropped to keep the container alive. This makes the `Monero` struct easier to work with, as it just holds the data necessary to create the different clients created during `init`, and does not have any lifetime restrictions.
This commit is contained in:
parent
55629838f4
commit
50ed74319f
@ -48,18 +48,17 @@ const WAIT_WALLET_SYNC_MILLIS: u64 = 1000;
|
|||||||
/// Wallet sub-account indices.
|
/// Wallet sub-account indices.
|
||||||
const ACCOUNT_INDEX_PRIMARY: u32 = 0;
|
const ACCOUNT_INDEX_PRIMARY: u32 = 0;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct Monero<'c> {
|
pub struct Monero {
|
||||||
pub docker: Container<'c, Cli, image::Monero>,
|
monerod_rpc_port: u16,
|
||||||
pub monerod_rpc_port: u16,
|
miner_wallet_rpc_port: u16,
|
||||||
pub miner_wallet_rpc_port: u16,
|
alice_wallet_rpc_port: u16,
|
||||||
pub alice_wallet_rpc_port: u16,
|
bob_wallet_rpc_port: u16,
|
||||||
pub bob_wallet_rpc_port: u16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'c> Monero<'c> {
|
impl<'c> Monero {
|
||||||
/// Starts a new regtest monero container.
|
/// Starts a new regtest monero container.
|
||||||
pub fn new(cli: &'c Cli) -> Self {
|
pub fn new(cli: &'c Cli) -> (Self, Container<'c, Cli, image::Monero>) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let monerod_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
|
let monerod_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
|
||||||
let miner_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
|
let miner_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX);
|
||||||
@ -91,13 +90,15 @@ impl<'c> Monero<'c> {
|
|||||||
let docker = cli.run(image);
|
let docker = cli.run(image);
|
||||||
println!("image ran");
|
println!("image ran");
|
||||||
|
|
||||||
Self {
|
(
|
||||||
|
Self {
|
||||||
|
monerod_rpc_port,
|
||||||
|
miner_wallet_rpc_port,
|
||||||
|
alice_wallet_rpc_port,
|
||||||
|
bob_wallet_rpc_port,
|
||||||
|
},
|
||||||
docker,
|
docker,
|
||||||
monerod_rpc_port,
|
)
|
||||||
miner_wallet_rpc_port,
|
|
||||||
alice_wallet_rpc_port,
|
|
||||||
bob_wallet_rpc_port,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn miner_wallet_rpc_client(&self) -> wallet::Client {
|
pub fn miner_wallet_rpc_client(&self) -> wallet::Client {
|
||||||
@ -156,21 +157,6 @@ impl<'c> Monero<'c> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Just create a wallet and start mining (you probably want `init()`).
|
|
||||||
pub async fn init_just_miner(&self, blocks: u32) -> Result<()> {
|
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
|
||||||
let monerod = self.monerod_rpc_client();
|
|
||||||
|
|
||||||
wallet.create_wallet("miner_wallet").await?;
|
|
||||||
let miner = self.get_address_miner().await?.address;
|
|
||||||
|
|
||||||
let _ = monerod.generate_blocks(blocks, &miner).await?;
|
|
||||||
|
|
||||||
let _ = tokio::spawn(mine(monerod.clone(), miner));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fund_account(&self, address: &str, miner: &str, funding: u64) -> Result<()> {
|
async fn fund_account(&self, address: &str, miner: &str, funding: u64) -> Result<()> {
|
||||||
let monerod = self.monerod_rpc_client();
|
let monerod = self.monerod_rpc_client();
|
||||||
|
|
||||||
@ -208,64 +194,42 @@ impl<'c> Monero<'c> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get addresses for the primary account.
|
/// Get addresses for the primary account.
|
||||||
pub async fn get_address_miner(&self) -> Result<GetAddress> {
|
async fn get_address_miner(&self) -> Result<GetAddress> {
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
let wallet = self.miner_wallet_rpc_client();
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get addresses for the Alice's account.
|
/// Get addresses for the Alice's account.
|
||||||
pub async fn get_address_alice(&self) -> Result<GetAddress> {
|
async fn get_address_alice(&self) -> Result<GetAddress> {
|
||||||
let wallet = self.alice_wallet_rpc_client();
|
let wallet = self.alice_wallet_rpc_client();
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get addresses for the Bob's account.
|
/// Get addresses for the Bob's account.
|
||||||
pub async fn get_address_bob(&self) -> Result<GetAddress> {
|
async fn get_address_bob(&self) -> Result<GetAddress> {
|
||||||
let wallet = self.bob_wallet_rpc_client();
|
let wallet = self.bob_wallet_rpc_client();
|
||||||
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
wallet.get_address(ACCOUNT_INDEX_PRIMARY).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the balance of the wallet primary account.
|
|
||||||
pub async fn get_balance_primary(&self) -> Result<u64> {
|
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
|
||||||
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the balance of Alice's account.
|
/// Gets the balance of Alice's account.
|
||||||
pub async fn get_balance_alice(&self) -> Result<u64> {
|
async fn get_balance_alice(&self) -> Result<u64> {
|
||||||
let wallet = self.alice_wallet_rpc_client();
|
let wallet = self.alice_wallet_rpc_client();
|
||||||
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the balance of Bob's account.
|
/// Gets the balance of Bob's account.
|
||||||
pub async fn get_balance_bob(&self) -> Result<u64> {
|
async fn get_balance_bob(&self) -> Result<u64> {
|
||||||
let wallet = self.bob_wallet_rpc_client();
|
let wallet = self.bob_wallet_rpc_client();
|
||||||
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
wallet.get_balance(ACCOUNT_INDEX_PRIMARY).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transfers moneroj from the primary account.
|
/// Transfers moneroj from the primary account.
|
||||||
pub async fn transfer_from_primary(&self, amount: u64, address: &str) -> Result<Transfer> {
|
async fn transfer_from_primary(&self, amount: u64, address: &str) -> Result<Transfer> {
|
||||||
let wallet = self.miner_wallet_rpc_client();
|
let wallet = self.miner_wallet_rpc_client();
|
||||||
wallet
|
wallet
|
||||||
.transfer(ACCOUNT_INDEX_PRIMARY, amount, address)
|
.transfer(ACCOUNT_INDEX_PRIMARY, amount, address)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transfers moneroj from Alice's account.
|
|
||||||
pub async fn transfer_from_alice(&self, amount: u64, address: &str) -> Result<Transfer> {
|
|
||||||
let wallet = self.alice_wallet_rpc_client();
|
|
||||||
wallet
|
|
||||||
.transfer(ACCOUNT_INDEX_PRIMARY, amount, address)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Transfers moneroj from Bob's account.
|
|
||||||
pub async fn transfer_from_bob(&self, amount: u64, address: &str) -> Result<Transfer> {
|
|
||||||
let wallet = self.bob_wallet_rpc_client();
|
|
||||||
wallet
|
|
||||||
.transfer(ACCOUNT_INDEX_PRIMARY, amount, address)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
/// Mine a block ever BLOCK_TIME_SECS seconds.
|
||||||
|
@ -5,29 +5,24 @@ use testcontainers::clients::Cli;
|
|||||||
const ALICE_FUND_AMOUNT: u64 = 1_000_000_000_000;
|
const ALICE_FUND_AMOUNT: u64 = 1_000_000_000_000;
|
||||||
const BOB_FUND_AMOUNT: u64 = 0;
|
const BOB_FUND_AMOUNT: u64 = 0;
|
||||||
|
|
||||||
fn init_cli() -> Cli {
|
|
||||||
Cli::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn init_monero(tc: &'_ Cli) -> Monero<'_> {
|
|
||||||
let monero = Monero::new(tc);
|
|
||||||
let _ = monero.init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT).await;
|
|
||||||
|
|
||||||
monero
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn init_accounts_for_alice_and_bob() {
|
async fn init_accounts_for_alice_and_bob() {
|
||||||
let cli = init_cli();
|
let tc = Cli::default();
|
||||||
let monero = init_monero(&cli).await;
|
let (monero, _container) = Monero::new(&tc);
|
||||||
|
monero
|
||||||
|
.init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let got_balance_alice = monero
|
let got_balance_alice = monero
|
||||||
.get_balance_alice()
|
.alice_wallet_rpc_client()
|
||||||
|
.get_balance(0)
|
||||||
.await
|
.await
|
||||||
.expect("failed to get alice's balance");
|
.expect("failed to get alice's balance");
|
||||||
|
|
||||||
let got_balance_bob = monero
|
let got_balance_bob = monero
|
||||||
.get_balance_bob()
|
.bob_wallet_rpc_client()
|
||||||
|
.get_balance(0)
|
||||||
.await
|
.await
|
||||||
.expect("failed to get bob's balance");
|
.expect("failed to get bob's balance");
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use monero_harness::{rpc::monerod::Client, Monero};
|
use monero_harness::Monero;
|
||||||
use spectral::prelude::*;
|
use spectral::prelude::*;
|
||||||
use std::time::Duration;
|
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
use tokio::time;
|
|
||||||
|
|
||||||
fn init_cli() -> Cli {
|
fn init_cli() -> Cli {
|
||||||
Cli::default()
|
Cli::default()
|
||||||
@ -11,8 +9,8 @@ fn init_cli() -> Cli {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn connect_to_monerod() {
|
async fn connect_to_monerod() {
|
||||||
let tc = init_cli();
|
let tc = init_cli();
|
||||||
let monero = Monero::new(&tc);
|
let (monero, _container) = Monero::new(&tc);
|
||||||
let cli = Client::localhost(monero.monerod_rpc_port);
|
let cli = monero.monerod_rpc_client();
|
||||||
|
|
||||||
let header = cli
|
let header = cli
|
||||||
.get_block_header_by_height(0)
|
.get_block_header_by_height(0)
|
||||||
@ -21,27 +19,3 @@ async fn connect_to_monerod() {
|
|||||||
|
|
||||||
assert_that!(header.height).is_equal_to(0);
|
assert_that!(header.height).is_equal_to(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn miner_is_running_and_producing_blocks() {
|
|
||||||
let tc = init_cli();
|
|
||||||
let monero = Monero::new(&tc);
|
|
||||||
let cli = Client::localhost(monero.monerod_rpc_port);
|
|
||||||
|
|
||||||
monero
|
|
||||||
.init_just_miner(2)
|
|
||||||
.await
|
|
||||||
.expect("Failed to initialize");
|
|
||||||
|
|
||||||
// Only need 3 seconds since we mine a block every second but
|
|
||||||
// give it 5 just for good measure.
|
|
||||||
time::delay_for(Duration::from_secs(5)).await;
|
|
||||||
|
|
||||||
// We should have at least 5 blocks by now.
|
|
||||||
let header = cli
|
|
||||||
.get_block_header_by_height(5)
|
|
||||||
.await
|
|
||||||
.expect("failed to get block");
|
|
||||||
|
|
||||||
assert_that!(header.height).is_equal_to(5);
|
|
||||||
}
|
|
||||||
|
@ -1,24 +1,21 @@
|
|||||||
use monero_harness::{rpc::wallet::Client, Monero};
|
use monero_harness::Monero;
|
||||||
use spectral::prelude::*;
|
use spectral::prelude::*;
|
||||||
use testcontainers::clients::Cli;
|
use testcontainers::clients::Cli;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn wallet_and_accounts() {
|
async fn wallet_and_accounts() {
|
||||||
let tc = Cli::default();
|
let tc = Cli::default();
|
||||||
let monero = Monero::new(&tc);
|
let (monero, _container) = Monero::new(&tc);
|
||||||
let miner_wallet = Client::localhost(monero.miner_wallet_rpc_port);
|
let cli = monero.miner_wallet_rpc_client();
|
||||||
|
|
||||||
println!("creating wallet ...");
|
println!("creating wallet ...");
|
||||||
|
|
||||||
let _ = miner_wallet
|
let _ = cli
|
||||||
.create_wallet("wallet")
|
.create_wallet("wallet")
|
||||||
.await
|
.await
|
||||||
.expect("failed to create wallet");
|
.expect("failed to create wallet");
|
||||||
|
|
||||||
let got = miner_wallet
|
let got = cli.get_balance(0).await.expect("failed to get balance");
|
||||||
.get_balance(0)
|
|
||||||
.await
|
|
||||||
.expect("failed to get balance");
|
|
||||||
let want = 0;
|
let want = 0;
|
||||||
|
|
||||||
assert_that!(got).is_equal_to(want);
|
assert_that!(got).is_equal_to(want);
|
||||||
@ -27,8 +24,8 @@ async fn wallet_and_accounts() {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn create_account_and_retrieve_it() {
|
async fn create_account_and_retrieve_it() {
|
||||||
let tc = Cli::default();
|
let tc = Cli::default();
|
||||||
let monero = Monero::new(&tc);
|
let (monero, _container) = Monero::new(&tc);
|
||||||
let cli = Client::localhost(monero.miner_wallet_rpc_port);
|
let cli = monero.miner_wallet_rpc_client();
|
||||||
|
|
||||||
let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
|
let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
|
||||||
|
|
||||||
@ -61,18 +58,20 @@ async fn transfer_and_check_tx_key() {
|
|||||||
let fund_bob = 0;
|
let fund_bob = 0;
|
||||||
|
|
||||||
let tc = Cli::default();
|
let tc = Cli::default();
|
||||||
let monero = Monero::new(&tc);
|
let (monero, _container) = Monero::new(&tc);
|
||||||
let _ = monero.init(fund_alice, fund_bob).await;
|
let _ = monero.init(fund_alice, fund_bob).await;
|
||||||
|
|
||||||
let address_bob = monero
|
let address_bob = monero
|
||||||
.get_address_bob()
|
.bob_wallet_rpc_client()
|
||||||
|
.get_address(0)
|
||||||
.await
|
.await
|
||||||
.expect("failed to get Bob's address")
|
.expect("failed to get Bob's address")
|
||||||
.address;
|
.address;
|
||||||
|
|
||||||
let transfer_amount = 100;
|
let transfer_amount = 100;
|
||||||
let transfer = monero
|
let transfer = monero
|
||||||
.transfer_from_alice(transfer_amount, &address_bob)
|
.alice_wallet_rpc_client()
|
||||||
|
.transfer(0, transfer_amount, &address_bob)
|
||||||
.await
|
.await
|
||||||
.expect("transfer failed");
|
.expect("transfer failed");
|
||||||
|
|
||||||
|
@ -59,14 +59,14 @@ pub fn init_alice_and_bob_transports() -> (
|
|||||||
(a_transport, b_transport)
|
(a_transport, b_transport)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init_test<'a>(
|
pub async fn init_test(
|
||||||
monero: &'a Monero<'a>,
|
monero: &Monero,
|
||||||
bitcoind: &Bitcoind<'_>,
|
bitcoind: &Bitcoind<'_>,
|
||||||
) -> (
|
) -> (
|
||||||
alice::State0,
|
alice::State0,
|
||||||
bob::State0,
|
bob::State0,
|
||||||
AliceNode<'a>,
|
AliceNode,
|
||||||
BobNode<'a>,
|
BobNode,
|
||||||
InitialBalances,
|
InitialBalances,
|
||||||
SwapAmounts,
|
SwapAmounts,
|
||||||
) {
|
) {
|
||||||
@ -83,8 +83,8 @@ pub async fn init_test<'a>(
|
|||||||
let fund_bob = 0;
|
let fund_bob = 0;
|
||||||
monero.init(fund_alice, fund_bob).await.unwrap();
|
monero.init(fund_alice, fund_bob).await.unwrap();
|
||||||
|
|
||||||
let alice_monero_wallet = wallet::monero::AliceWallet(&monero);
|
let alice_monero_wallet = wallet::monero::Wallet(monero.alice_wallet_rpc_client());
|
||||||
let bob_monero_wallet = wallet::monero::BobWallet(&monero);
|
let bob_monero_wallet = wallet::monero::Wallet(monero.bob_wallet_rpc_client());
|
||||||
|
|
||||||
let alice_btc_wallet = wallet::bitcoin::Wallet::new("alice", &bitcoind.node_url)
|
let alice_btc_wallet = wallet::bitcoin::Wallet::new("alice", &bitcoind.node_url)
|
||||||
.await
|
.await
|
||||||
@ -101,8 +101,8 @@ pub async fn init_test<'a>(
|
|||||||
let alice_initial_btc_balance = alice.bitcoin_wallet.balance().await.unwrap();
|
let alice_initial_btc_balance = alice.bitcoin_wallet.balance().await.unwrap();
|
||||||
let bob_initial_btc_balance = bob.bitcoin_wallet.balance().await.unwrap();
|
let bob_initial_btc_balance = bob.bitcoin_wallet.balance().await.unwrap();
|
||||||
|
|
||||||
let alice_initial_xmr_balance = alice.monero_wallet.0.get_balance_alice().await.unwrap();
|
let alice_initial_xmr_balance = alice.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
let bob_initial_xmr_balance = bob.monero_wallet.0.get_balance_bob().await.unwrap();
|
let bob_initial_xmr_balance = bob.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
|
|
||||||
let redeem_address = alice.bitcoin_wallet.new_address().await.unwrap();
|
let redeem_address = alice.bitcoin_wallet.new_address().await.unwrap();
|
||||||
let punish_address = redeem_address.clone();
|
let punish_address = redeem_address.clone();
|
||||||
@ -166,7 +166,7 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let monero = Monero::new(&cli);
|
let (monero, _container) = Monero::new(&cli);
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
@ -207,21 +207,11 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let alice_final_xmr_balance = alice_node
|
let alice_final_xmr_balance = alice_node.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
.monero_wallet
|
|
||||||
.0
|
|
||||||
.get_balance_alice()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
bob_node
|
monero.wait_for_bob_wallet_block_height().await.unwrap();
|
||||||
.monero_wallet
|
|
||||||
.0
|
|
||||||
.wait_for_bob_wallet_block_height()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance_bob().await.unwrap();
|
let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
alice_final_btc_balance,
|
alice_final_btc_balance,
|
||||||
@ -252,7 +242,7 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let monero = Monero::new(&cli);
|
let (monero, _container) = Monero::new(&cli);
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
@ -304,19 +294,9 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
alice_node
|
monero.wait_for_alice_wallet_block_height().await.unwrap();
|
||||||
.monero_wallet
|
let alice_final_xmr_balance = alice_node.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
.0
|
let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance(0).await.unwrap();
|
||||||
.wait_for_alice_wallet_block_height()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let alice_final_xmr_balance = alice_node
|
|
||||||
.monero_wallet
|
|
||||||
.0
|
|
||||||
.get_balance_alice()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let bob_final_xmr_balance = bob_node.monero_wallet.0.get_balance_bob().await.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(alice_final_btc_balance, initial_balances.alice_btc);
|
assert_eq!(alice_final_btc_balance, initial_balances.alice_btc);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -338,7 +318,7 @@ mod tests {
|
|||||||
.set_default();
|
.set_default();
|
||||||
|
|
||||||
let cli = Cli::default();
|
let cli = Cli::default();
|
||||||
let monero = Monero::new(&cli);
|
let (monero, _container) = Monero::new(&cli);
|
||||||
let bitcoind = init_bitcoind(&cli).await;
|
let bitcoind = init_bitcoind(&cli).await;
|
||||||
|
|
||||||
let (
|
let (
|
||||||
|
@ -5,18 +5,18 @@ use xmr_btc::{alice, bob};
|
|||||||
|
|
||||||
// TODO: merge this with bob node
|
// TODO: merge this with bob node
|
||||||
// This struct is responsible for I/O
|
// This struct is responsible for I/O
|
||||||
pub struct AliceNode<'a> {
|
pub struct AliceNode {
|
||||||
transport: Transport<alice::Message, bob::Message>,
|
transport: Transport<alice::Message, bob::Message>,
|
||||||
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
||||||
pub monero_wallet: wallet::monero::AliceWallet<'a>,
|
pub monero_wallet: wallet::monero::Wallet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AliceNode<'a> {
|
impl AliceNode {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
transport: Transport<alice::Message, bob::Message>,
|
transport: Transport<alice::Message, bob::Message>,
|
||||||
bitcoin_wallet: wallet::bitcoin::Wallet,
|
bitcoin_wallet: wallet::bitcoin::Wallet,
|
||||||
monero_wallet: wallet::monero::AliceWallet<'a>,
|
monero_wallet: wallet::monero::Wallet,
|
||||||
) -> AliceNode<'a> {
|
) -> AliceNode {
|
||||||
Self {
|
Self {
|
||||||
transport,
|
transport,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
@ -25,8 +25,8 @@ impl<'a> AliceNode<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_alice_until<'a, R: RngCore + CryptoRng>(
|
pub async fn run_alice_until<R: RngCore + CryptoRng>(
|
||||||
alice: &mut AliceNode<'a>,
|
alice: &mut AliceNode,
|
||||||
initial_state: alice::State,
|
initial_state: alice::State,
|
||||||
is_state: fn(&alice::State) -> bool,
|
is_state: fn(&alice::State) -> bool,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
@ -49,18 +49,18 @@ pub async fn run_alice_until<'a, R: RngCore + CryptoRng>(
|
|||||||
|
|
||||||
// TODO: merge this with alice node
|
// TODO: merge this with alice node
|
||||||
// This struct is responsible for I/O
|
// This struct is responsible for I/O
|
||||||
pub struct BobNode<'a> {
|
pub struct BobNode {
|
||||||
transport: Transport<bob::Message, alice::Message>,
|
transport: Transport<bob::Message, alice::Message>,
|
||||||
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
pub bitcoin_wallet: wallet::bitcoin::Wallet,
|
||||||
pub monero_wallet: wallet::monero::BobWallet<'a>,
|
pub monero_wallet: wallet::monero::Wallet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BobNode<'a> {
|
impl BobNode {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
transport: Transport<bob::Message, alice::Message>,
|
transport: Transport<bob::Message, alice::Message>,
|
||||||
bitcoin_wallet: wallet::bitcoin::Wallet,
|
bitcoin_wallet: wallet::bitcoin::Wallet,
|
||||||
monero_wallet: wallet::monero::BobWallet<'a>,
|
monero_wallet: wallet::monero::Wallet,
|
||||||
) -> BobNode<'a> {
|
) -> BobNode {
|
||||||
Self {
|
Self {
|
||||||
transport,
|
transport,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
@ -69,8 +69,8 @@ impl<'a> BobNode<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_bob_until<'a, R: RngCore + CryptoRng>(
|
pub async fn run_bob_until<R: RngCore + CryptoRng>(
|
||||||
bob: &mut BobNode<'a>,
|
bob: &mut BobNode,
|
||||||
initial_state: bob::State,
|
initial_state: bob::State,
|
||||||
is_state: fn(&bob::State) -> bool,
|
is_state: fn(&bob::State) -> bool,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
|
@ -2,18 +2,17 @@ use anyhow::Result;
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use backoff::{future::FutureOperation as _, ExponentialBackoff};
|
use backoff::{future::FutureOperation as _, ExponentialBackoff};
|
||||||
use monero::{Address, Network, PrivateKey};
|
use monero::{Address, Network, PrivateKey};
|
||||||
use monero_harness::Monero;
|
use monero_harness::rpc::wallet;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use xmr_btc::monero::{
|
use xmr_btc::monero::{
|
||||||
Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicKey, PublicViewKey,
|
Amount, CreateWalletForOutput, InsufficientFunds, PrivateViewKey, PublicKey, PublicViewKey,
|
||||||
Transfer, TransferProof, TxHash, WatchForTransfer,
|
Transfer, TransferProof, TxHash, WatchForTransfer,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub struct Wallet(pub wallet::Client);
|
||||||
pub struct AliceWallet<'c>(pub &'c Monero<'c>);
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Transfer for AliceWallet<'_> {
|
impl Transfer for Wallet {
|
||||||
async fn transfer(
|
async fn transfer(
|
||||||
&self,
|
&self,
|
||||||
public_spend_key: PublicKey,
|
public_spend_key: PublicKey,
|
||||||
@ -25,7 +24,7 @@ impl Transfer for AliceWallet<'_> {
|
|||||||
|
|
||||||
let res = self
|
let res = self
|
||||||
.0
|
.0
|
||||||
.transfer_from_alice(amount.as_piconero(), &destination_address.to_string())
|
.transfer(0, amount.as_piconero(), &destination_address.to_string())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let tx_hash = TxHash(res.tx_hash);
|
let tx_hash = TxHash(res.tx_hash);
|
||||||
@ -38,7 +37,7 @@ impl Transfer for AliceWallet<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl CreateWalletForOutput for AliceWallet<'_> {
|
impl CreateWalletForOutput for Wallet {
|
||||||
async fn create_and_load_wallet_for_output(
|
async fn create_and_load_wallet_for_output(
|
||||||
&self,
|
&self,
|
||||||
private_spend_key: PrivateKey,
|
private_spend_key: PrivateKey,
|
||||||
@ -51,7 +50,6 @@ impl CreateWalletForOutput for AliceWallet<'_> {
|
|||||||
|
|
||||||
let _ = self
|
let _ = self
|
||||||
.0
|
.0
|
||||||
.alice_wallet_rpc_client()
|
|
||||||
.generate_from_keys(
|
.generate_from_keys(
|
||||||
&address.to_string(),
|
&address.to_string(),
|
||||||
&private_spend_key.to_string(),
|
&private_spend_key.to_string(),
|
||||||
@ -63,11 +61,8 @@ impl CreateWalletForOutput for AliceWallet<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct BobWallet<'c>(pub &'c Monero<'c>);
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl WatchForTransfer for BobWallet<'_> {
|
impl WatchForTransfer for Wallet {
|
||||||
async fn watch_for_transfer(
|
async fn watch_for_transfer(
|
||||||
&self,
|
&self,
|
||||||
public_spend_key: PublicKey,
|
public_spend_key: PublicKey,
|
||||||
@ -82,14 +77,14 @@ impl WatchForTransfer for BobWallet<'_> {
|
|||||||
InsufficientFunds { expected: Amount, actual: Amount },
|
InsufficientFunds { expected: Amount, actual: Amount },
|
||||||
}
|
}
|
||||||
|
|
||||||
let wallet = self.0.bob_wallet_rpc_client();
|
|
||||||
let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key.into());
|
let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key.into());
|
||||||
|
|
||||||
let res = (|| async {
|
let res = (|| async {
|
||||||
// NOTE: Currently, this is conflating IO errors with the transaction not being
|
// NOTE: Currently, this is conflating IO errors with the transaction not being
|
||||||
// in the blockchain yet, or not having enough confirmations on it. All these
|
// in the blockchain yet, or not having enough confirmations on it. All these
|
||||||
// errors warrant a retry, but the strategy should probably differ per case
|
// errors warrant a retry, but the strategy should probably differ per case
|
||||||
let proof = wallet
|
let proof = self
|
||||||
|
.0
|
||||||
.check_tx_key(
|
.check_tx_key(
|
||||||
&String::from(transfer_proof.tx_hash()),
|
&String::from(transfer_proof.tx_hash()),
|
||||||
&transfer_proof.tx_key().to_string(),
|
&transfer_proof.tx_key().to_string(),
|
||||||
@ -124,29 +119,3 @@ impl WatchForTransfer for BobWallet<'_> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl CreateWalletForOutput for BobWallet<'_> {
|
|
||||||
async fn create_and_load_wallet_for_output(
|
|
||||||
&self,
|
|
||||||
private_spend_key: PrivateKey,
|
|
||||||
private_view_key: PrivateViewKey,
|
|
||||||
) -> Result<()> {
|
|
||||||
let public_spend_key = PublicKey::from_private_key(&private_spend_key);
|
|
||||||
let public_view_key = PublicKey::from_private_key(&private_view_key.into());
|
|
||||||
|
|
||||||
let address = Address::standard(Network::Mainnet, public_spend_key, public_view_key);
|
|
||||||
|
|
||||||
let _ = self
|
|
||||||
.0
|
|
||||||
.bob_wallet_rpc_client()
|
|
||||||
.generate_from_keys(
|
|
||||||
&address.to_string(),
|
|
||||||
&private_spend_key.to_string(),
|
|
||||||
&PrivateKey::from(private_view_key).to_string(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -45,7 +45,7 @@ async fn swap_as_alice(
|
|||||||
// FIXME: It would be more intuitive to have a single network/transport struct instead of
|
// FIXME: It would be more intuitive to have a single network/transport struct instead of
|
||||||
// splitting into two, but Rust ownership rules make this tedious
|
// splitting into two, but Rust ownership rules make this tedious
|
||||||
mut sender: Sender<TransferProof>,
|
mut sender: Sender<TransferProof>,
|
||||||
monero_wallet: &'static monero::AliceWallet<'static>,
|
monero_wallet: &'static monero::Wallet,
|
||||||
bitcoin_wallet: &'static bitcoin::Wallet,
|
bitcoin_wallet: &'static bitcoin::Wallet,
|
||||||
state: alice::State3,
|
state: alice::State3,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
@ -86,7 +86,7 @@ async fn swap_as_alice(
|
|||||||
async fn swap_as_bob(
|
async fn swap_as_bob(
|
||||||
network: &'static mut BobNetwork,
|
network: &'static mut BobNetwork,
|
||||||
mut sender: Sender<EncryptedSignature>,
|
mut sender: Sender<EncryptedSignature>,
|
||||||
monero_wallet: &'static monero::BobWallet<'static>,
|
monero_wallet: &'static monero::Wallet,
|
||||||
bitcoin_wallet: &'static bitcoin::Wallet,
|
bitcoin_wallet: &'static bitcoin::Wallet,
|
||||||
state: bob::State2,
|
state: bob::State2,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user