Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tpu-v2): provide ethcoin support in tpu kickstart process #2300

Open
wants to merge 10 commits into
base: fix-tpu-v2-wait-for-payment-spend
Choose a base branch
from
24 changes: 8 additions & 16 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@

use super::lp_network::P2PRequestResult;
use crate::lp_network::{broadcast_p2p_msg, Libp2pPeerId, P2PProcessError, P2PProcessResult, P2PRequestError};
use crate::lp_swap::maker_swap_v2::{MakerSwapStateMachine, MakerSwapStorage};
use crate::lp_swap::taker_swap_v2::{TakerSwapStateMachine, TakerSwapStorage};
use crate::lp_swap::maker_swap_v2::MakerSwapStorage;
use crate::lp_swap::taker_swap_v2::TakerSwapStorage;
use bitcrypto::sha256;
use coins::{lp_coinfind, lp_coinfind_or_err, CoinFindError, DexFee, MmCoin, MmCoinEnum, TradeFee, TransactionEnum};
use common::log::{debug, warn};
Expand Down Expand Up @@ -116,7 +116,6 @@ mod trade_preimage;
#[cfg(target_arch = "wasm32")] mod swap_wasm_db;

pub use check_balance::{check_other_coin_balance_for_swap, CheckBalanceError, CheckBalanceResult};
use coins::utxo::utxo_standard::UtxoStandardCoin;
use crypto::secret_hash_algo::SecretHashAlgo;
use crypto::CryptoCtx;
use keys::{KeyPair, SECP_SIGN, SECP_VERIFY};
Expand All @@ -130,7 +129,8 @@ use pubkey_banning::BanReason;
pub use pubkey_banning::{ban_pubkey_rpc, is_pubkey_banned, list_banned_pubkeys_rpc, unban_pubkeys_rpc};
pub use recreate_swap_data::recreate_swap_data;
pub use saved_swap::{SavedSwap, SavedSwapError, SavedSwapIo, SavedSwapResult};
use swap_v2_common::{get_unfinished_swaps_uuids, swap_kickstart_handler, ActiveSwapV2Info};
use swap_v2_common::{get_unfinished_swaps_uuids, swap_kickstart_handler_for_maker, swap_kickstart_handler_for_taker,
ActiveSwapV2Info};
use swap_v2_pb::*;
use swap_v2_rpcs::{get_maker_swap_data_for_rpc, get_swap_type, get_taker_swap_data_for_rpc};
pub use swap_watcher::{process_watcher_msg, watcher_topic, TakerSwapWatcherData, MAKER_PAYMENT_SPEND_FOUND_LOG,
Expand Down Expand Up @@ -1409,12 +1409,8 @@ pub async fn swap_kick_starts(ctx: MmArc) -> Result<HashSet<String>, String> {
coins.insert(maker_swap_repr.maker_coin.clone());
coins.insert(maker_swap_repr.taker_coin.clone());

let fut = swap_kickstart_handler::<MakerSwapStateMachine<UtxoStandardCoin, UtxoStandardCoin>>(
ctx.clone(),
maker_swap_repr,
maker_swap_storage.clone(),
maker_uuid,
);
let fut =
swap_kickstart_handler_for_maker(ctx.clone(), maker_swap_repr, maker_swap_storage.clone(), maker_uuid);
ctx.spawner().spawn(fut);
}

Expand All @@ -1434,12 +1430,8 @@ pub async fn swap_kick_starts(ctx: MmArc) -> Result<HashSet<String>, String> {
coins.insert(taker_swap_repr.maker_coin.clone());
coins.insert(taker_swap_repr.taker_coin.clone());

let fut = swap_kickstart_handler::<TakerSwapStateMachine<UtxoStandardCoin, UtxoStandardCoin>>(
ctx.clone(),
taker_swap_repr,
taker_swap_storage.clone(),
taker_uuid,
);
let fut =
swap_kickstart_handler_for_taker(ctx.clone(), taker_swap_repr, taker_swap_storage.clone(), taker_uuid);
ctx.spawner().spawn(fut);
}
Ok(coins)
Expand Down
160 changes: 129 additions & 31 deletions mm2src/mm2_main/src/lp_swap/swap_v2_common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::lp_network::{subscribe_to_topic, unsubscribe_from_topic};
use crate::lp_swap::maker_swap_v2::{MakerSwapDbRepr, MakerSwapStateMachine, MakerSwapStorage};
use crate::lp_swap::swap_lock::{SwapLock, SwapLockError, SwapLockOps};
use crate::lp_swap::taker_swap_v2::{TakerSwapDbRepr, TakerSwapStateMachine, TakerSwapStorage};
use crate::lp_swap::{swap_v2_topic, SwapsContext};
use coins::eth::EthCoin;
use coins::utxo::utxo_standard::UtxoStandardCoin;
use coins::{lp_coinfind, MmCoinEnum};
use coins::{lp_coinfind, MakerCoinSwapOpsV2, MmCoin, MmCoinEnum, TakerCoinSwapOpsV2};
use common::executor::abortable_queue::AbortableQueue;
use common::executor::{SpawnFuture, Timer};
use common::log::{error, info, warn};
Expand Down Expand Up @@ -292,25 +295,17 @@ pub(super) trait GetSwapCoins {
fn taker_coin(&self) -> &str;
}

/// Generic function for upgraded swaps kickstart handling.
/// It is implemented only for UtxoStandardCoin/UtxoStandardCoin case temporary.
pub(super) async fn swap_kickstart_handler<
T: StorableStateMachine<RecreateCtx = SwapRecreateCtx<UtxoStandardCoin, UtxoStandardCoin>>,
>(
ctx: MmArc,
swap_repr: <T::Storage as StateMachineStorage>::DbRepr,
storage: T::Storage,
uuid: <T::Storage as StateMachineStorage>::MachineId,
) where
<T::Storage as StateMachineStorage>::MachineId: Copy + std::fmt::Display,
<T::Storage as StateMachineStorage>::DbRepr: GetSwapCoins,
T::Error: std::fmt::Display,
T::RecreateError: std::fmt::Display,
{
/// Attempts to find and return the maker and taker coins required for the swap to proceed.
/// If a coin is not activated, it logs the information and retries until the coin is found or an error occurs.
onur-ozkan marked this conversation as resolved.
Show resolved Hide resolved
pub(super) async fn swap_kickstart_coins<T: GetSwapCoins>(
ctx: &MmArc,
swap_repr: &T,
uuid: &Uuid,
) -> Option<(MmCoinEnum, MmCoinEnum)> {
let taker_coin_ticker = swap_repr.taker_coin();

let taker_coin = loop {
match lp_coinfind(&ctx, taker_coin_ticker).await {
match lp_coinfind(ctx, taker_coin_ticker).await {
Ok(Some(c)) => break c,
Ok(None) => {
info!(
Expand All @@ -321,15 +316,15 @@ pub(super) async fn swap_kickstart_handler<
},
Err(e) => {
error!("Error {} on {} find attempt", e, taker_coin_ticker);
return;
return None;
},
};
};

let maker_coin_ticker = swap_repr.maker_coin();

let maker_coin = loop {
match lp_coinfind(&ctx, maker_coin_ticker).await {
match lp_coinfind(ctx, maker_coin_ticker).await {
Ok(Some(c)) => break c,
Ok(None) => {
info!(
Expand All @@ -340,23 +335,30 @@ pub(super) async fn swap_kickstart_handler<
},
Err(e) => {
error!("Error {} on {} find attempt", e, maker_coin_ticker);
return;
return None;
},
};
};

// TODO add ETH support
let (maker_coin, taker_coin) = match (maker_coin, taker_coin) {
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::UtxoCoin(t)) => (m, t),
_ => {
error!(
"V2 swaps are not currently supported for {}/{} pair",
maker_coin_ticker, taker_coin_ticker
);
return;
},
};
Some((maker_coin, taker_coin))
}

/// Handles the recreation and kickstart of a swap state machine.
pub(super) async fn swap_kickstart_handler<
T: StorableStateMachine<RecreateCtx = SwapRecreateCtx<MakerCoin, TakerCoin>>,
MakerCoin: MmCoin + MakerCoinSwapOpsV2,
TakerCoin: MmCoin + TakerCoinSwapOpsV2,
>(
swap_repr: <T::Storage as StateMachineStorage>::DbRepr,
storage: T::Storage,
uuid: <T::Storage as StateMachineStorage>::MachineId,
maker_coin: MakerCoin,
taker_coin: TakerCoin,
) where
<T::Storage as StateMachineStorage>::MachineId: Copy + std::fmt::Display,
T::Error: std::fmt::Display,
T::RecreateError: std::fmt::Display,
{
let recreate_context = SwapRecreateCtx { maker_coin, taker_coin };

let (mut state_machine, state) = match T::recreate_machine(uuid, storage, swap_repr, recreate_context).await {
Expand All @@ -371,3 +373,99 @@ pub(super) async fn swap_kickstart_handler<
error!("Error {} on trying to run the swap {}", e, uuid);
}
}

pub(super) async fn swap_kickstart_handler_for_maker(
ctx: MmArc,
swap_repr: MakerSwapDbRepr,
storage: MakerSwapStorage,
uuid: Uuid,
) {
let (maker_coin, taker_coin) = match swap_kickstart_coins(&ctx, &swap_repr, &uuid).await {
Some(coins) => coins,
None => return,
};
onur-ozkan marked this conversation as resolved.
Show resolved Hide resolved

match (maker_coin, taker_coin) {
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<
MakerSwapStateMachine<UtxoStandardCoin, UtxoStandardCoin>,
UtxoStandardCoin,
UtxoStandardCoin,
>(swap_repr, storage, uuid, m, t)
.await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<EthCoin, EthCoin>, EthCoin, EthCoin>(
swap_repr, storage, uuid, m, t,
)
.await
},
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<UtxoStandardCoin, EthCoin>, UtxoStandardCoin, EthCoin>(
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
swap_repr, storage, uuid, m, t,
)
.await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<EthCoin, UtxoStandardCoin>, EthCoin, UtxoStandardCoin>(
swap_repr, storage, uuid, m, t,
)
.await
},
borngraced marked this conversation as resolved.
Show resolved Hide resolved
_ => {
error!(
"V2 swaps are not currently supported for {}/{} pair",
swap_repr.maker_coin(),
swap_repr.taker_coin()
);
},
};
}

pub(super) async fn swap_kickstart_handler_for_taker(
ctx: MmArc,
swap_repr: TakerSwapDbRepr,
storage: TakerSwapStorage,
uuid: Uuid,
) {
let (maker_coin, taker_coin) = match swap_kickstart_coins(&ctx, &swap_repr, &uuid).await {
Some(coins) => coins,
None => return,
};
borngraced marked this conversation as resolved.
Show resolved Hide resolved

match (maker_coin, taker_coin) {
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<
TakerSwapStateMachine<UtxoStandardCoin, UtxoStandardCoin>,
UtxoStandardCoin,
UtxoStandardCoin,
>(swap_repr, storage, uuid, m, t)
.await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<EthCoin, EthCoin>, EthCoin, EthCoin>(
swap_repr, storage, uuid, m, t,
)
.await
},
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<UtxoStandardCoin, EthCoin>, UtxoStandardCoin, EthCoin>(
swap_repr, storage, uuid, m, t,
)
.await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<EthCoin, UtxoStandardCoin>, EthCoin, UtxoStandardCoin>(
swap_repr, storage, uuid, m, t,
)
.await
},
_ => {
error!(
"V2 swaps are not currently supported for {}/{} pair",
swap_repr.maker_coin(),
swap_repr.taker_coin()
);
},
};
borngraced marked this conversation as resolved.
Show resolved Hide resolved
}
Loading