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(legacy-refunds): support going the spending path on refund #2280

Open
wants to merge 19 commits into
base: dev
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
28 changes: 22 additions & 6 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ pub struct RecoveredSwap {
transaction: TransactionEnum,
}

#[derive(Display, Debug, PartialEq)]
pub enum RecoverSwapError {
// We might not find the original payment tx on chain (e.g. re-orged). This doesn't mean though that nobody has it.
// TODO: These coins should be spent ASAP to avoid them getting locked (or stolen).
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
#[display(fmt = "The payment tx is not on-chain. Nothing to recover.")]
PaymentTxNotFound,
#[display(fmt = "An unknown error occurred. Retrying might fix it: {}", _0)]
Temporary(String),
#[display(fmt = "The swap is not recoverable: {}", _0)]
Irrecoverable(String),
#[display(fmt = "Wait {}s and try to recover again.", _0)]
WaitAndRetry(u64),
#[display(fmt = "The funds will be automatically recovered after lock-time: {}", _0)]
AutoRecoverableAfter(u64),
}

/// Represents the amount of a coin locked by ongoing swap
#[derive(Debug)]
pub struct LockedAmount {
Expand Down Expand Up @@ -516,7 +532,7 @@ struct LockedAmountInfo {
}

struct SwapsContext {
running_swaps: Mutex<Vec<Weak<dyn AtomicSwap>>>,
running_swaps: Mutex<Vec<(Weak<dyn AtomicSwap>, AbortOnDropHandle)>>,
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell why do we need this change?

Is it related to some review note? may be I missed smth

Copy link
Collaborator Author

@mariocynicys mariocynicys Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah actually, not discussed in review. @shamardy just told me we need the swaps to be stoppable via rpc (since we now do a run forever recovery), that's why we record their abort handles to be able to stop them mid-recover (or even mid-swap).

active_swaps_v2_infos: Mutex<HashMap<Uuid, ActiveSwapV2Info>>,
banned_pubkeys: Mutex<HashMap<H256Json, BanReason>>,
swap_msgs: Mutex<HashMap<Uuid, SwapMsgStore>>,
Expand Down Expand Up @@ -619,7 +635,7 @@ pub fn get_locked_amount(ctx: &MmArc, coin: &str) -> MmNumber {

let mut locked = swap_lock
.iter()
.filter_map(|swap| swap.upgrade())
.filter_map(|(swap, _)| swap.upgrade())
.flat_map(|swap| swap.locked_amount())
.fold(MmNumber::from(0), |mut total_amount, locked| {
if locked.coin == coin {
Expand Down Expand Up @@ -656,7 +672,7 @@ pub fn get_locked_amount(ctx: &MmArc, coin: &str) -> MmNumber {
pub fn running_swaps_num(ctx: &MmArc) -> u64 {
let swap_ctx = SwapsContext::from_ctx(ctx).unwrap();
let swaps = swap_ctx.running_swaps.lock().unwrap();
swaps.iter().fold(0, |total, swap| match swap.upgrade() {
swaps.iter().fold(0, |total, (swap, _)| match swap.upgrade() {
Some(_) => total + 1,
None => total,
})
Expand All @@ -669,7 +685,7 @@ fn get_locked_amount_by_other_swaps(ctx: &MmArc, except_uuid: &Uuid, coin: &str)

swap_lock
.iter()
.filter_map(|swap| swap.upgrade())
.filter_map(|(swap, _)| swap.upgrade())
.filter(|swap| swap.uuid() != except_uuid)
.flat_map(|swap| swap.locked_amount())
.fold(MmNumber::from(0), |mut total_amount, locked| {
Expand All @@ -689,7 +705,7 @@ pub fn active_swaps_using_coins(ctx: &MmArc, coins: &HashSet<String>) -> Result<
let swap_ctx = try_s!(SwapsContext::from_ctx(ctx));
let swaps = try_s!(swap_ctx.running_swaps.lock());
let mut uuids = vec![];
for swap in swaps.iter() {
for (swap, _) in swaps.iter() {
if let Some(swap) = swap.upgrade() {
if coins.contains(&swap.maker_coin().to_string()) || coins.contains(&swap.taker_coin().to_string()) {
uuids.push(*swap.uuid())
Expand All @@ -711,7 +727,7 @@ pub fn active_swaps(ctx: &MmArc) -> Result<Vec<(Uuid, u8)>, String> {
let swap_ctx = try_s!(SwapsContext::from_ctx(ctx));
let swaps = swap_ctx.running_swaps.lock().unwrap();
let mut uuids = vec![];
for swap in swaps.iter() {
for (swap, _) in swaps.iter() {
if let Some(swap) = swap.upgrade() {
uuids.push((*swap.uuid(), LEGACY_SWAP_TYPE))
}
Expand Down
Loading
Loading