Separate wallet settings and protocol settings

This commit is contained in:
Daniel Karzel 2021-01-27 16:26:22 +11:00
parent 049b7bc329
commit 4cc74bad62
3 changed files with 108 additions and 58 deletions

View File

@ -17,6 +17,7 @@ use anyhow::{Context, Result};
use database::Database;
use prettytable::{row, Table};
use protocol::{alice, bob, bob::Builder, SwapAmounts};
use settings::Settings;
use std::sync::Arc;
use structopt::StructOpt;
use trace::init_tracing;
@ -45,7 +46,6 @@ async fn main() -> Result<()> {
init_tracing(LevelFilter::Info).expect("initialize tracing");
let opt = Options::from_args();
let settings = settings::Protocol::testnet();
info!(
"Database and Seed will be stored in directory: {}",
@ -67,18 +67,15 @@ async fn main() -> Result<()> {
send_monero,
receive_bitcoin,
} => {
let settings =
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
let swap_amounts = SwapAmounts {
xmr: send_monero,
btc: receive_bitcoin,
};
let (bitcoin_wallet, monero_wallet) = setup_wallets(
bitcoind_url,
bitcoin_wallet_name.as_str(),
monero_wallet_rpc_url,
settings,
)
.await?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
let swap_id = Uuid::new_v4();
@ -89,7 +86,7 @@ async fn main() -> Result<()> {
let alice_factory = alice::Builder::new(
seed,
settings,
settings.protocol,
swap_id,
Arc::new(bitcoin_wallet),
Arc::new(monero_wallet),
@ -112,18 +109,15 @@ async fn main() -> Result<()> {
send_bitcoin,
receive_monero,
} => {
let settings =
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
let swap_amounts = SwapAmounts {
btc: send_bitcoin,
xmr: receive_monero,
};
let (bitcoin_wallet, monero_wallet) = setup_wallets(
bitcoind_url,
bitcoin_wallet_name.as_str(),
monero_wallet_rpc_url,
settings,
)
.await?;
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
let swap_id = Uuid::new_v4();
@ -140,7 +134,7 @@ async fn main() -> Result<()> {
Arc::new(monero_wallet),
alice_addr,
alice_peer_id,
settings,
settings.protocol,
);
let (swap, event_loop) = bob_factory.with_init_params(swap_amounts).build().await?;
@ -168,17 +162,14 @@ async fn main() -> Result<()> {
monero_wallet_rpc_url,
listen_addr,
}) => {
let (bitcoin_wallet, monero_wallet) = setup_wallets(
bitcoind_url,
bitcoin_wallet_name.as_str(),
monero_wallet_rpc_url,
settings,
)
.await?;
let settings =
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
let alice_factory = alice::Builder::new(
seed,
settings,
settings.protocol,
swap_id,
Arc::new(bitcoin_wallet),
Arc::new(monero_wallet),
@ -199,13 +190,10 @@ async fn main() -> Result<()> {
alice_peer_id,
alice_addr,
}) => {
let (bitcoin_wallet, monero_wallet) = setup_wallets(
bitcoind_url,
bitcoin_wallet_name.as_str(),
monero_wallet_rpc_url,
settings,
)
.await?;
let settings =
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
let bob_factory = Builder::new(
seed,
@ -215,7 +203,7 @@ async fn main() -> Result<()> {
Arc::new(monero_wallet),
alice_addr,
alice_peer_id,
settings,
settings.protocol,
);
let (swap, event_loop) = bob_factory.build().await?;
@ -227,21 +215,21 @@ async fn main() -> Result<()> {
Ok(())
}
async fn setup_wallets(
bitcoind_url: url::Url,
bitcoin_wallet_name: &str,
monero_wallet_rpc_url: url::Url,
settings: settings::Protocol,
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
let bitcoin_wallet =
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, settings.bitcoin_network).await?;
async fn setup_wallets(settings: settings::Wallets) -> Result<(bitcoin::Wallet, monero::Wallet)> {
let bitcoin_wallet = bitcoin::Wallet::new(
settings.bitcoin.wallet_name.as_str(),
settings.bitcoin.bitcoind_url,
settings.bitcoin.network,
)
.await?;
let bitcoin_balance = bitcoin_wallet.balance().await?;
info!(
"Connection to Bitcoin wallet succeeded, balance: {}",
bitcoin_balance
);
let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url, settings.monero_network);
let monero_wallet =
monero::Wallet::new(settings.monero.wallet_rpc_url, settings.monero.network);
let monero_balance = monero_wallet.get_balance().await?;
info!(
"Connection to Monero wallet succeeded, balance: {}",

View File

@ -1,6 +1,79 @@
use crate::bitcoin::Timelock;
use conquer_once::Lazy;
use std::time::Duration;
use url::Url;
pub struct Settings {
pub wallets: Wallets,
pub protocol: Protocol,
}
impl Settings {
pub fn testnet(
bitcoind_url: Url,
bitcoin_wallet_name: String,
monero_wallet_rpc_url: Url,
) -> Self {
Self {
wallets: Wallets::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url),
protocol: Protocol::testnet(),
}
}
}
pub struct Wallets {
pub bitcoin: Bitcoin,
pub monero: Monero,
}
impl Wallets {
pub fn mainnet(
bitcoind_url: Url,
bitcoin_wallet_name: String,
monero_wallet_rpc_url: Url,
) -> Self {
Self {
bitcoin: Bitcoin {
bitcoind_url,
wallet_name: bitcoin_wallet_name,
network: bitcoin::Network::Bitcoin,
},
monero: Monero {
wallet_rpc_url: monero_wallet_rpc_url,
network: monero::Network::Mainnet,
},
}
}
pub fn testnet(
bitcoind_url: Url,
bitcoin_wallet_name: String,
monero_wallet_rpc_url: Url,
) -> Self {
Self {
bitcoin: Bitcoin {
bitcoind_url,
wallet_name: bitcoin_wallet_name,
network: bitcoin::Network::Testnet,
},
monero: Monero {
wallet_rpc_url: monero_wallet_rpc_url,
network: monero::Network::Stagenet,
},
}
}
}
pub struct Bitcoin {
pub bitcoind_url: Url,
pub wallet_name: String,
pub network: bitcoin::Network,
}
pub struct Monero {
pub wallet_rpc_url: Url,
pub network: monero::Network,
}
#[derive(Debug, Copy, Clone)]
pub struct Protocol {
@ -10,8 +83,6 @@ pub struct Protocol {
pub monero_finality_confirmations: u32,
pub bitcoin_cancel_timelock: Timelock,
pub bitcoin_punish_timelock: Timelock,
pub bitcoin_network: bitcoin::Network,
pub monero_network: monero::Network,
}
impl Protocol {
@ -23,8 +94,6 @@ impl Protocol {
monero_finality_confirmations: mainnet::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: bitcoin::Network::Bitcoin,
monero_network: monero::Network::Mainnet,
}
}
@ -36,8 +105,6 @@ impl Protocol {
monero_finality_confirmations: testnet::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: bitcoin::Network::Testnet,
monero_network: monero::Network::Stagenet,
}
}
@ -49,8 +116,6 @@ impl Protocol {
monero_finality_confirmations: regtest::MONERO_FINALITY_CONFIRMATIONS,
bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK,
bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK,
bitcoin_network: bitcoin::Network::Regtest,
monero_network: monero::Network::default(),
}
}
}

View File

@ -316,12 +316,11 @@ where
.parse()
.expect("failed to parse Alice's address");
let (alice_bitcoin_wallet, alice_monero_wallet) = init_wallets(
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
"alice",
&containers.bitcoind,
&monero,
alice_starting_balances.clone(),
settings,
)
.await;
@ -340,12 +339,11 @@ where
btc: swap_amounts.btc * 10,
};
let (bob_bitcoin_wallet, bob_monero_wallet) = init_wallets(
let (bob_bitcoin_wallet, bob_monero_wallet) = init_test_wallets(
"bob",
&containers.bitcoind,
&monero,
bob_starting_balances.clone(),
settings,
)
.await;
@ -385,12 +383,11 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
(monero, Containers { bitcoind, monerods })
}
async fn init_wallets(
async fn init_test_wallets(
name: &str,
bitcoind: &Bitcoind<'_>,
monero: &Monero,
starting_balances: StartingBalances,
settings: settings::Protocol,
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
monero
.init(vec![(name, starting_balances.xmr.as_piconero())])
@ -399,11 +396,11 @@ async fn init_wallets(
let xmr_wallet = Arc::new(swap::monero::Wallet {
inner: monero.wallet(name).unwrap().client(),
network: settings.monero_network,
network: monero::Network::default(),
});
let btc_wallet = Arc::new(
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), settings.bitcoin_network)
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), bitcoin::Network::Regtest)
.await
.unwrap(),
);