Update xmr-btc lib to use new monero-harness

This commit is contained in:
Philipp Hoenisch 2020-11-02 21:12:38 +11:00
parent 2d064f305f
commit 0b9e8c145e
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
7 changed files with 113 additions and 46 deletions

View File

@ -35,7 +35,7 @@ use crate::{
}, },
rpc::{ rpc::{
monerod, monerod,
wallet::{self, GetAddress, Transfer}, wallet::{self, GetAddress, Refreshed, Transfer},
}, },
}; };
@ -49,7 +49,6 @@ const WAIT_WALLET_SYNC_MILLIS: u64 = 1000;
pub struct Monero { pub struct Monero {
monerod: Monerod, monerod: Monerod,
wallets: Vec<MoneroWalletRpc>, wallets: Vec<MoneroWalletRpc>,
miner_address: String,
container_prefix: String, container_prefix: String,
} }
impl<'c> Monero { impl<'c> Monero {
@ -79,19 +78,9 @@ impl<'c> Monero {
let mut containers = vec![monerod_container]; let mut containers = vec![monerod_container];
let mut wallets = vec![]; let mut wallets = vec![];
tracing::info!("Starting miner..."); tracing::info!("Starting miner wallet...");
let miner = format!("{}{}", container_prefix, "miner"); let miner = format!("{}{}", container_prefix, "miner");
let (miner_wallet, miner_container) = MoneroWalletRpc::new(cli, &miner, &monerod).await?; 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?;
tracing::info!("Waiting for miner wallet to catch up...");
let block_height = monerod.inner().get_block_count().await?;
miner_wallet
.wait_for_wallet_height(block_height)
.await
.unwrap();
wallets.push(miner_wallet); wallets.push(miner_wallet);
containers.push(miner_container); containers.push(miner_container);
@ -107,7 +96,6 @@ impl<'c> Monero {
Self { Self {
monerod, monerod,
wallets, wallets,
miner_address,
container_prefix, container_prefix,
}, },
containers, containers,
@ -129,6 +117,46 @@ impl<'c> Monero {
Ok(wallet) Ok(wallet)
} }
pub async fn init(&self, alice_amount: u64, bob_amount: u64) -> Result<()> {
let miner_wallet = self.wallet("miner")?;
let miner_address = miner_wallet.address().await?.address;
let alice_wallet = self.wallet("alice")?;
let alice_address = alice_wallet.address().await?.address;
let bob_wallet = self.wallet("bob")?;
let bob_address = bob_wallet.address().await?.address;
// generate the first 70 as bulk
let monerod = &self.monerod;
let block = monerod.inner().generate_blocks(70, &miner_address).await?;
tracing::info!("Generated {:?} blocks", block);
miner_wallet.refresh().await?;
if alice_amount > 0 {
miner_wallet.transfer(&alice_address, alice_amount).await?;
tracing::info!("Funded alice wallet with {}", alice_amount);
}
if bob_amount > 0 {
miner_wallet.transfer(&bob_address, bob_amount).await?;
tracing::info!("Funded bob wallet with {}", bob_amount);
}
monerod.inner().generate_blocks(10, &miner_address).await?;
alice_wallet.refresh().await?;
bob_wallet.refresh().await?;
monerod.start_miner(&miner_address).await?;
tracing::info!("Waiting for miner wallet to catch up...");
let block_height = monerod.inner().get_block_count().await?;
miner_wallet
.wait_for_wallet_height(block_height)
.await
.unwrap();
Ok(())
}
pub async fn fund(&self, address: &str, amount: u64) -> Result<Transfer> { pub async fn fund(&self, address: &str, amount: u64) -> Result<Transfer> {
self.transfer("miner", address, amount).await self.transfer("miner", address, amount).await
} }
@ -144,9 +172,10 @@ impl<'c> Monero {
async fn transfer(&self, from_wallet: &str, address: &str, amount: u64) -> Result<Transfer> { async fn transfer(&self, from_wallet: &str, address: &str, amount: u64) -> Result<Transfer> {
let from = self.wallet(from_wallet)?; let from = self.wallet(from_wallet)?;
let transfer = from.transfer(address, amount).await?; let transfer = from.transfer(address, amount).await?;
let miner_address = self.wallet("miner")?.address().await?.address;
self.monerod self.monerod
.inner() .inner()
.generate_blocks(10, &self.miner_address) .generate_blocks(10, &miner_address)
.await?; .await?;
from.inner().refresh().await?; from.inner().refresh().await?;
Ok(transfer) Ok(transfer)
@ -204,10 +233,7 @@ impl<'c> Monerod {
/// address /// address
pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> { pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> {
let monerod = self.inner(); let monerod = self.inner();
// generate the first 70 as bulk let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string()));
let block = monerod.generate_blocks(70, &miner_wallet_address).await?;
println!("Generated {:?} blocks", block);
let _ = tokio::spawn(mine(monerod.clone(), miner_wallet_address.to_string()));
Ok(()) Ok(())
} }
} }
@ -284,6 +310,10 @@ impl<'c> MoneroWalletRpc {
self.inner().refresh().await?; self.inner().refresh().await?;
self.inner().get_balance(0).await self.inner().get_balance(0).await
} }
pub async fn refresh(&self) -> Result<Refreshed> {
self.inner().refresh().await
}
} }
/// Mine a block ever BLOCK_TIME_SECS seconds. /// Mine a block ever BLOCK_TIME_SECS seconds.
async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> { async fn mine(monerod: monerod::Client, reward_address: String) -> Result<()> {

View File

@ -5,7 +5,8 @@ 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 = 5_000_000_000; let fund_bob = 0;
let send_to_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![
@ -17,28 +18,24 @@ async fn fund_transfer_and_check_tx_key() {
let alice_wallet = monero.wallet("alice").unwrap(); let alice_wallet = monero.wallet("alice").unwrap();
let bob_wallet = monero.wallet("bob").unwrap(); 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;
// fund alice // fund alice
monero.fund(&alice_address, fund_alice).await.unwrap(); monero.init(fund_alice, fund_bob).await.unwrap();
// check alice balance // check alice balance
let refreshed = alice_wallet.inner().refresh().await.unwrap(); alice_wallet.inner().refresh().await.unwrap();
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();
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 // transfer from alice to bob
let bob_address = bob_wallet.address().await.unwrap().address;
let transfer = monero let transfer = monero
.transfer_from_alice(&bob_address, fund_bob) .transfer_from_alice(&bob_address, send_to_bob)
.await .await
.unwrap(); .unwrap();
let refreshed = bob_wallet.inner().refresh().await.unwrap(); bob_wallet.inner().refresh().await.unwrap();
assert_that(&refreshed.received_money).is_true();
let got_bob_balance = bob_wallet.balance().await.unwrap(); 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(send_to_bob);
// check if tx was actually seen // check if tx was actually seen
let tx_id = transfer.tx_hash; let tx_id = transfer.tx_hash;
@ -49,5 +46,5 @@ async fn fund_transfer_and_check_tx_key() {
.await .await
.expect("failed to check tx by key"); .expect("failed to check tx by key");
assert_that!(res.received).is_equal_to(fund_bob); assert_that!(res.received).is_equal_to(send_to_bob);
} }

View File

@ -12,7 +12,7 @@ atty = "0.2"
backoff = { version = "0.2", features = ["tokio"] } backoff = { version = "0.2", features = ["tokio"] }
base64 = "0.12" base64 = "0.12"
bitcoin = { version = "0.23", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version. bitcoin = { version = "0.23", features = ["rand", "use-serde"] } # TODO: Upgrade other crates in this repo to use this version.
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "f1bbe6a4540d0741f1f4f22577cfeeadbfd7aaaf" } bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "3be644cd9512c157d3337a189298b8257ed54d04" }
derivative = "2" derivative = "2"
futures = { version = "0.3", default-features = false } futures = { version = "0.3", default-features = false }
genawaiter = "0.99.1" genawaiter = "0.99.1"

View File

@ -28,12 +28,12 @@ tracing = "0.1"
[dev-dependencies] [dev-dependencies]
backoff = { version = "0.2", features = ["tokio"] } backoff = { version = "0.2", features = ["tokio"] }
base64 = "0.12" base64 = "0.12"
bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "f1bbe6a4540d0741f1f4f22577cfeeadbfd7aaaf" } bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "3be644cd9512c157d3337a189298b8257ed54d04" }
futures = "0.3" futures = "0.3"
monero-harness = { path = "../monero-harness" } monero-harness = { path = "../monero-harness" }
reqwest = { version = "0.10", default-features = false } reqwest = { version = "0.10", default-features = false }
serde_cbor = "0.11" serde_cbor = "0.11"
tempfile = "3" tempfile = "3"
testcontainers = "0.10" testcontainers = "0.11"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.2" tracing-subscriber = "0.2"

View File

@ -32,7 +32,13 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _container) =
Monero::new(&cli, Some("hp".to_string()), Some("hp".to_string()), vec![
"alice".to_string(),
"bob".to_string(),
])
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (
@ -75,7 +81,7 @@ mod tests {
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap(); let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
monero.wait_for_bob_wallet_block_height().await.unwrap(); monero.wallet("bob").unwrap().refresh().await.unwrap();
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap(); let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
@ -106,7 +112,13 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _container) =
Monero::new(&cli, Some("br".to_string()), Some("br".to_string()), vec![
"alice".to_string(),
"bob".to_string(),
])
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (
@ -158,7 +170,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
monero.wait_for_alice_wallet_block_height().await.unwrap(); monero.wallet("alice").unwrap().refresh().await.unwrap();
let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap(); let alice_final_xmr_balance = alice_node.monero_wallet.get_balance().await.unwrap();
let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap(); let bob_final_xmr_balance = bob_node.monero_wallet.get_balance().await.unwrap();
@ -182,7 +194,14 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _containers) =
Monero::new(&cli, Some("ap".to_string()), Some("ap".to_string()), vec![
"alice".to_string(),
"bob".to_string(),
])
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (

View File

@ -132,8 +132,8 @@ pub async fn init_test(
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::Wallet(monero.alice_wallet_rpc_client()); let alice_monero_wallet = wallet::monero::Wallet(monero.wallet("alice").unwrap().inner());
let bob_monero_wallet = wallet::monero::Wallet(monero.bob_wallet_rpc_client()); let bob_monero_wallet = wallet::monero::Wallet(monero.wallet("bob").unwrap().inner());
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

View File

@ -238,7 +238,14 @@ async fn swap_as_bob(
#[tokio::test] #[tokio::test]
async fn on_chain_happy_path() { async fn on_chain_happy_path() {
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _container) = Monero::new(
&cli,
Some("ochp".to_string()),
Some("ochp".to_string()),
vec!["alice".to_string(), "bob".to_string()],
)
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) = let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
@ -304,7 +311,7 @@ async fn on_chain_happy_path() {
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap(); let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
monero.wait_for_bob_wallet_block_height().await.unwrap(); monero.wallet("bob").unwrap().refresh().await.unwrap();
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap(); let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
assert_eq!( assert_eq!(
@ -329,7 +336,14 @@ async fn on_chain_happy_path() {
#[tokio::test] #[tokio::test]
async fn on_chain_both_refund_if_alice_never_redeems() { async fn on_chain_both_refund_if_alice_never_redeems() {
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _container) = Monero::new(
&cli,
Some("ocbr".to_string()),
Some("ocbr".to_string()),
vec!["alice".to_string(), "bob".to_string()],
)
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) = let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =
@ -396,7 +410,7 @@ async fn on_chain_both_refund_if_alice_never_redeems() {
.await .await
.unwrap(); .unwrap();
monero.wait_for_alice_wallet_block_height().await.unwrap(); monero.wallet("alice").unwrap().refresh().await.unwrap();
let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap(); let alice_final_xmr_balance = alice_monero_wallet.get_balance().await.unwrap();
let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap(); let bob_final_xmr_balance = bob_monero_wallet.get_balance().await.unwrap();
@ -419,7 +433,14 @@ async fn on_chain_both_refund_if_alice_never_redeems() {
#[tokio::test] #[tokio::test]
async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() { async fn on_chain_alice_punishes_if_bob_never_acts_after_fund() {
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli).unwrap(); let (monero, _container) = Monero::new(
&cli,
Some("ocap".to_string()),
Some("ocap".to_string()),
vec!["alice".to_string(), "bob".to_string()],
)
.await
.unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) = let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =