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 6 commits into
base: fix-tpu-v2-wait-for-payment-spend
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
127 changes: 95 additions & 32 deletions mm2src/mm2_main/src/lp_swap/swap_v2_common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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::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 +293,18 @@ 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.
/// If an unexpected issue occurs, function logs the error and returns `None`.
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 +315,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 +334,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 +372,65 @@ 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,
) {
if let Some((maker_coin, taker_coin)) = swap_kickstart_coins(&ctx, &swap_repr, &uuid).await {
match (maker_coin, taker_coin) {
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<MakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
_ => {
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,
) {
if let Some((maker_coin, taker_coin)) = swap_kickstart_coins(&ctx, &swap_repr, &uuid).await {
match (maker_coin, taker_coin) {
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::UtxoCoin(m), MmCoinEnum::EthCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
(MmCoinEnum::EthCoin(m), MmCoinEnum::UtxoCoin(t)) => {
swap_kickstart_handler::<TakerSwapStateMachine<_, _>, _, _>(swap_repr, storage, uuid, m, t).await
},
_ => {
error!(
"V2 swaps are not currently supported for {}/{} pair",
swap_repr.maker_coin(),
swap_repr.taker_coin()
);
},
}
}
}
Loading