Work in review comments

This commit is contained in:
Daniel Karzel 2021-03-03 15:30:58 +11:00
parent 66c8401c95
commit 1041212a60
6 changed files with 41 additions and 61 deletions

View File

@ -32,7 +32,7 @@ use swap::{
execution_params::GetExecutionParams, execution_params::GetExecutionParams,
fs::default_config_path, fs::default_config_path,
monero, monero,
monero::{Amount, CreateWallet, OpenWallet}, monero::{Amount, OpenOrCreate},
protocol::alice::EventLoop, protocol::alice::EventLoop,
seed::Seed, seed::Seed,
trace::init_tracing, trace::init_tracing,
@ -165,18 +165,7 @@ async fn init_wallets(
); );
// Setup the Monero wallet // Setup the Monero wallet
let open_wallet_response = monero_wallet.open().await; monero_wallet.open_or_create().await?;
if open_wallet_response.is_err() {
monero_wallet.create().await.context(format!(
"Unable to create Monero wallet.\
Please ensure that the monero-wallet-rpc is available at {}",
config.monero.wallet_rpc_url
))?;
info!("Created Monero wallet {}", DEFAULT_WALLET_NAME);
} else {
info!("Opened Monero wallet {}", DEFAULT_WALLET_NAME);
}
let balance = monero_wallet.get_balance().await?; let balance = monero_wallet.get_balance().await?;
if balance == Amount::ZERO { if balance == Amount::ZERO {

View File

@ -28,7 +28,7 @@ use swap::{
execution_params, execution_params,
execution_params::GetExecutionParams, execution_params::GetExecutionParams,
monero, monero,
monero::{CreateWallet, OpenWallet}, monero::OpenOrCreate,
protocol::{ protocol::{
bob, bob,
bob::{cancel::CancelError, Builder, EventLoop}, bob::{cancel::CancelError, Builder, EventLoop},
@ -296,20 +296,7 @@ async fn init_monero_wallet(
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(), MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
); );
// Setup the temporary Monero wallet necessary for monitoring the blockchain monero_wallet.open_or_create().await?;
let open_monitoring_wallet_response = monero_wallet.open().await;
if open_monitoring_wallet_response.is_err() {
monero_wallet.create().await.context(format!(
"Unable to create Monero wallet for blockchain monitoring.\
Please ensure that the monero-wallet-rpc is available at {}",
monero_wallet_rpc_url
))?;
debug!(
"Created Monero wallet for blockchain monitoring with name {}",
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME
);
}
let _test_wallet_connection = monero_wallet let _test_wallet_connection = monero_wallet
.block_height() .block_height()

View File

@ -218,8 +218,8 @@ pub struct BalanceTooLow {
} }
#[async_trait] #[async_trait]
pub trait CreateWalletForOutput { pub trait CreateFromAndLoad {
async fn create_and_load_wallet_for_output( async fn create_from_and_load(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -228,8 +228,8 @@ pub trait CreateWalletForOutput {
} }
#[async_trait] #[async_trait]
pub trait CreateWalletForOutputThenReloadWallet { pub trait CreateFrom {
async fn create_and_load_wallet_for_output_then_reload_wallet( async fn create_from(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -243,8 +243,8 @@ pub trait OpenWallet {
} }
#[async_trait] #[async_trait]
pub trait CreateWallet { pub trait OpenOrCreate {
async fn create(&self) -> Result<()>; async fn open_or_create(&self) -> Result<()>;
} }
#[async_trait] #[async_trait]

View File

@ -1,10 +1,9 @@
use crate::monero::{ use crate::monero::{
Amount, CreateWallet, CreateWalletForOutput, CreateWalletForOutputThenReloadWallet, Amount, CreateFrom, CreateFromAndLoad, InsufficientFunds, OpenOrCreate, OpenWallet,
InsufficientFunds, OpenWallet, PrivateViewKey, PublicViewKey, Transfer, TransferProof, TxHash, PrivateViewKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer,
WatchForTransfer,
}; };
use ::monero::{Address, Network, PrivateKey, PublicKey}; use ::monero::{Address, Network, PrivateKey, PublicKey};
use anyhow::Result; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use backoff::{backoff::Constant as ConstantBackoff, future::retry}; use backoff::{backoff::Constant as ConstantBackoff, future::retry};
use bitcoin::hashes::core::sync::atomic::AtomicU32; use bitcoin::hashes::core::sync::atomic::AtomicU32;
@ -14,7 +13,7 @@ use monero_rpc::{
}; };
use std::{str::FromStr, sync::atomic::Ordering, time::Duration}; use std::{str::FromStr, sync::atomic::Ordering, time::Duration};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::info; use tracing::{debug, info};
use url::Url; use url::Url;
#[derive(Debug)] #[derive(Debug)]
@ -62,12 +61,15 @@ impl Wallet {
} }
pub async fn sweep_all(&self, address: Address) -> Result<Vec<TxHash>> { pub async fn sweep_all(&self, address: Address) -> Result<Vec<TxHash>> {
self.inner let sweep_all = self
.inner
.lock() .lock()
.await .await
.sweep_all(address.to_string().as_str()) .sweep_all(address.to_string().as_str())
.await .await?;
.map(|sweep_all| sweep_all.tx_hash_list.into_iter().map(TxHash).collect())
let tx_hashes = sweep_all.tx_hash_list.into_iter().map(TxHash).collect();
Ok(tx_hashes)
} }
pub fn static_tx_fee_estimate(&self) -> Amount { pub fn static_tx_fee_estimate(&self) -> Amount {
@ -109,8 +111,8 @@ impl Transfer for Wallet {
} }
#[async_trait] #[async_trait]
impl CreateWalletForOutput for Wallet { impl CreateFromAndLoad for Wallet {
async fn create_and_load_wallet_for_output( async fn create_from_and_load(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -141,8 +143,8 @@ impl CreateWalletForOutput for Wallet {
} }
#[async_trait] #[async_trait]
impl CreateWalletForOutputThenReloadWallet for Wallet { impl CreateFrom for Wallet {
async fn create_and_load_wallet_for_output_then_reload_wallet( async fn create_from(
&self, &self,
private_spend_key: PrivateKey, private_spend_key: PrivateKey,
private_view_key: PrivateViewKey, private_view_key: PrivateViewKey,
@ -187,13 +189,19 @@ impl OpenWallet for Wallet {
} }
#[async_trait] #[async_trait]
impl CreateWallet for Wallet { impl OpenOrCreate for Wallet {
async fn create(&self) -> Result<()> { async fn open_or_create(&self) -> Result<()> {
self.inner let open_wallet_response = self.open().await;
.lock() if open_wallet_response.is_err() {
.await self.inner.lock().await.create_wallet(self.name.as_str()).await.context(
.create_wallet(self.name.as_str()) "Unable to create Monero wallet, please ensure that the monero-wallet-rpc is available",
.await?; )?;
debug!("Created Monero wallet {}", self.name);
} else {
debug!("Opened Monero wallet {}", self.name);
}
Ok(()) Ok(())
} }
} }

View File

@ -7,7 +7,7 @@ use crate::{
database::Database, database::Database,
execution_params::ExecutionParams, execution_params::ExecutionParams,
monero, monero,
monero::CreateWalletForOutputThenReloadWallet, monero::CreateFrom,
monero_ext::ScalarExt, monero_ext::ScalarExt,
protocol::{ protocol::{
alice, alice,
@ -392,11 +392,7 @@ async fn run_until_internal(
let view_key = state3.v; let view_key = state3.v;
monero_wallet monero_wallet
.create_and_load_wallet_for_output_then_reload_wallet( .create_from(spend_key, view_key, monero_wallet_restore_blockheight)
spend_key,
view_key,
monero_wallet_restore_blockheight,
)
.await?; .await?;
let state = AliceState::XmrRefunded; let state = AliceState::XmrRefunded;

View File

@ -574,7 +574,7 @@ pub struct State5 {
impl State5 { impl State5 {
pub async fn claim_xmr<W>(&self, monero_wallet: &W) -> Result<()> pub async fn claim_xmr<W>(&self, monero_wallet: &W) -> Result<()>
where where
W: monero::CreateWalletForOutput, W: monero::CreateFromAndLoad,
{ {
let s_b = monero::PrivateKey { scalar: self.s_b }; let s_b = monero::PrivateKey { scalar: self.s_b };
@ -583,7 +583,7 @@ impl State5 {
// NOTE: This actually generates and opens a new wallet, closing the currently // NOTE: This actually generates and opens a new wallet, closing the currently
// open one. // open one.
monero_wallet monero_wallet
.create_and_load_wallet_for_output(s, self.v, self.monero_wallet_restore_blockheight) .create_from_and_load(s, self.v, self.monero_wallet_restore_blockheight)
.await?; .await?;
Ok(()) Ok(())