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,
fs::default_config_path,
monero,
monero::{Amount, CreateWallet, OpenWallet},
monero::{Amount, OpenOrCreate},
protocol::alice::EventLoop,
seed::Seed,
trace::init_tracing,
@ -165,18 +165,7 @@ async fn init_wallets(
);
// Setup the Monero wallet
let open_wallet_response = monero_wallet.open().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);
}
monero_wallet.open_or_create().await?;
let balance = monero_wallet.get_balance().await?;
if balance == Amount::ZERO {

View File

@ -28,7 +28,7 @@ use swap::{
execution_params,
execution_params::GetExecutionParams,
monero,
monero::{CreateWallet, OpenWallet},
monero::OpenOrCreate,
protocol::{
bob,
bob::{cancel::CancelError, Builder, EventLoop},
@ -296,20 +296,7 @@ async fn init_monero_wallet(
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
);
// Setup the temporary Monero wallet necessary for monitoring the blockchain
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
);
}
monero_wallet.open_or_create().await?;
let _test_wallet_connection = monero_wallet
.block_height()

View File

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

View File

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

View File

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

View File

@ -574,7 +574,7 @@ pub struct State5 {
impl State5 {
pub async fn claim_xmr<W>(&self, monero_wallet: &W) -> Result<()>
where
W: monero::CreateWalletForOutput,
W: monero::CreateFromAndLoad,
{
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
// open one.
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?;
Ok(())