From d63790c2a6fe3fe203b3a24f44cfa62794ea608d Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 3 Mar 2021 15:42:26 +1100 Subject: [PATCH] Remove unnecessary monero wallet trait abstractions --- swap/src/bin/asb.rs | 2 +- swap/src/bin/swap_cli.rs | 1 - swap/src/monero.rs | 69 -------------------------------- swap/src/monero/wallet.rs | 36 ++++------------- swap/src/protocol/alice/steps.rs | 10 ++--- swap/src/protocol/alice/swap.rs | 1 - swap/src/protocol/bob/state.rs | 14 ++----- swap/tests/testutils/mod.rs | 1 - 8 files changed, 15 insertions(+), 119 deletions(-) diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 5faf0029..4512db99 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -32,7 +32,7 @@ use swap::{ execution_params::GetExecutionParams, fs::default_config_path, monero, - monero::{Amount, OpenOrCreate}, + monero::Amount, protocol::alice::EventLoop, seed::Seed, trace::init_tracing, diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index 9e4ba0bc..36f1326a 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -28,7 +28,6 @@ use swap::{ execution_params, execution_params::GetExecutionParams, monero, - monero::OpenOrCreate, protocol::{ bob, bob::{cancel::CancelError, Builder, EventLoop}, diff --git a/swap/src/monero.rs b/swap/src/monero.rs index e1da64e2..613e2095 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -9,8 +9,6 @@ pub use wallet_rpc::{WalletRpc, WalletRpcProcess}; use crate::bitcoin; use ::bitcoin::hashes::core::fmt::Formatter; use anyhow::Result; -use async_trait::async_trait; -use monero_rpc::wallet::{BlockHeight, Refreshed}; use rand::{CryptoRng, RngCore}; use rust_decimal::{ prelude::{FromPrimitive, ToPrimitive}, @@ -182,28 +180,6 @@ impl From for String { } } -#[async_trait] -pub trait Transfer { - async fn transfer( - &self, - public_spend_key: PublicKey, - public_view_key: PublicViewKey, - amount: Amount, - ) -> Result; -} - -#[async_trait] -pub trait WatchForTransfer { - async fn watch_for_transfer( - &self, - public_spend_key: PublicKey, - public_view_key: PublicViewKey, - transfer_proof: TransferProof, - amount: Amount, - expected_confirmations: u32, - ) -> Result<(), InsufficientFunds>; -} - #[derive(Debug, Clone, Copy, thiserror::Error)] #[error("transaction does not pay enough: expected {expected}, got {actual}")] pub struct InsufficientFunds { @@ -217,51 +193,6 @@ pub struct BalanceTooLow { pub balance: Amount, } -#[async_trait] -pub trait CreateFromAndLoad { - async fn create_from_and_load( - &self, - private_spend_key: PrivateKey, - private_view_key: PrivateViewKey, - restore_height: BlockHeight, - ) -> Result<()>; -} - -#[async_trait] -pub trait CreateFrom { - async fn create_from( - &self, - private_spend_key: PrivateKey, - private_view_key: PrivateViewKey, - restore_height: BlockHeight, - ) -> Result<()>; -} - -#[async_trait] -pub trait OpenWallet { - async fn open(&self) -> Result<()>; -} - -#[async_trait] -pub trait OpenOrCreate { - async fn open_or_create(&self) -> Result<()>; -} - -#[async_trait] -pub trait WalletBlockHeight { - async fn block_height(&self) -> Result; -} - -#[async_trait] -pub trait GetAddress { - async fn get_main_address(&self) -> Result
; -} - -#[async_trait] -pub trait Refresh { - async fn refresh(&self) -> Result; -} - #[derive(thiserror::Error, Debug, Clone, PartialEq)] #[error("Overflow, cannot convert {0} to u64")] pub struct OverflowError(pub String); diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 38b88c1f..09743f6c 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -1,10 +1,8 @@ use crate::monero::{ - Amount, CreateFrom, CreateFromAndLoad, InsufficientFunds, OpenOrCreate, OpenWallet, - PrivateViewKey, PublicViewKey, Transfer, TransferProof, TxHash, WatchForTransfer, + Amount, InsufficientFunds, PrivateViewKey, PublicViewKey, TransferProof, TxHash, }; use ::monero::{Address, Network, PrivateKey, PublicKey}; use anyhow::{Context, Result}; -use async_trait::async_trait; use backoff::{backoff::Constant as ConstantBackoff, future::retry}; use bitcoin::hashes::core::sync::atomic::AtomicU32; use monero_rpc::{ @@ -76,11 +74,8 @@ impl Wallet { // Median tx fees on Monero as found here: https://www.monero.how/monero-transaction-fees, 0.000_015 * 2 (to be on the safe side) Amount::from_monero(0.000_03f64).expect("static fee to be convertible without problems") } -} -#[async_trait] -impl Transfer for Wallet { - async fn transfer( + pub async fn transfer( &self, public_spend_key: PublicKey, public_view_key: PublicViewKey, @@ -108,11 +103,8 @@ impl Transfer for Wallet { PrivateKey::from_str(&res.tx_key)?, )) } -} -#[async_trait] -impl CreateFromAndLoad for Wallet { - async fn create_from_and_load( + pub async fn create_from_and_load( &self, private_spend_key: PrivateKey, private_view_key: PrivateViewKey, @@ -140,11 +132,8 @@ impl CreateFromAndLoad for Wallet { Ok(()) } -} -#[async_trait] -impl CreateFrom for Wallet { - async fn create_from( + pub async fn create_from( &self, private_spend_key: PrivateKey, private_view_key: PrivateViewKey, @@ -174,11 +163,8 @@ impl CreateFrom for Wallet { Ok(()) } -} -#[async_trait] -impl OpenWallet for Wallet { - async fn open(&self) -> Result<()> { + pub async fn open(&self) -> Result<()> { self.inner .lock() .await @@ -186,11 +172,8 @@ impl OpenWallet for Wallet { .await?; Ok(()) } -} -#[async_trait] -impl OpenOrCreate for Wallet { - async fn open_or_create(&self) -> Result<()> { + pub 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( @@ -204,13 +187,8 @@ impl OpenOrCreate for Wallet { Ok(()) } -} -// TODO: For retry, use `backoff::ExponentialBackoff` in production as opposed -// to `ConstantBackoff`. -#[async_trait] -impl WatchForTransfer for Wallet { - async fn watch_for_transfer( + pub async fn watch_for_transfer( &self, public_spend_key: PublicKey, public_view_key: PublicViewKey, diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index e65567d1..7fb0e57d 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -6,7 +6,6 @@ use crate::{ }, execution_params::ExecutionParams, monero, - monero::Transfer, protocol::{ alice, alice::{event_loop::EventLoopHandle, TransferProof}, @@ -49,15 +48,12 @@ pub async fn wait_for_locked_bitcoin( Ok(()) } -pub async fn lock_xmr( +pub async fn lock_xmr( bob_peer_id: PeerId, state3: alice::State3, event_loop_handle: &mut EventLoopHandle, - monero_wallet: Arc, -) -> Result<()> -where - W: Transfer, -{ + monero_wallet: Arc, +) -> Result<()> { let S_a = monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: state3.s_a }); let public_spend_key = S_a + state3.S_b_monero; diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 3cd2f45b..f5182227 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -7,7 +7,6 @@ use crate::{ database::Database, execution_params::ExecutionParams, monero, - monero::CreateFrom, monero_ext::ScalarExt, protocol::{ alice, diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index 766c1c78..3f407f0f 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -316,15 +316,12 @@ pub struct State3 { } impl State3 { - pub async fn watch_for_lock_xmr( + pub async fn watch_for_lock_xmr( self, - xmr_wallet: &W, + xmr_wallet: &monero::Wallet, transfer_proof: TransferProof, monero_wallet_restore_blockheight: BlockHeight, - ) -> Result> - where - W: monero::WatchForTransfer, - { + ) -> Result> { let S_b_monero = monero::PublicKey::from_private_key(&monero::PrivateKey::from_scalar(self.s_b)); let S = self.S_a_monero + S_b_monero; @@ -572,10 +569,7 @@ pub struct State5 { } impl State5 { - pub async fn claim_xmr(&self, monero_wallet: &W) -> Result<()> - where - W: monero::CreateFromAndLoad, - { + pub async fn claim_xmr(&self, monero_wallet: &monero::Wallet) -> Result<()> { let s_b = monero::PrivateKey { scalar: self.s_b }; let s = self.s_a + s_b; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index e41e124b..509123f7 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -22,7 +22,6 @@ use swap::{ execution_params, execution_params::{ExecutionParams, GetExecutionParams}, monero, - monero::OpenWallet, protocol::{alice, alice::AliceState, bob, bob::BobState}, seed::Seed, };