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 23 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
026cd08
refactor recover_funds for maker & taker
mariocynicys Nov 23, 2024
bb4a9ca
fix recover_funds tests
mariocynicys Nov 24, 2024
8cadb68
make refund_maker_payment more robust
mariocynicys Nov 24, 2024
553d054
make refund_taker_payment more robust
mariocynicys Nov 25, 2024
3aa1811
review(omer): typo - (taker -> maker) payment spend
mariocynicys Nov 30, 2024
df94b09
review(sami): use MmError to track the error origin
mariocynicys Nov 30, 2024
5e0709d
review(alina): restructure match and optimize tx_hex clone
mariocynicys Nov 30, 2024
07f5bed
remove extra line
mariocynicys Nov 30, 2024
ea294e4
can_refund_htlc errors are temporary
mariocynicys Dec 5, 2024
a28c768
wait_for_htlc_refund errors are temporary also
mariocynicys Dec 5, 2024
5c1bb6f
remove todo
mariocynicys Dec 5, 2024
4a44e16
store the swap aborthanle in `running_swaps`
mariocynicys Dec 6, 2024
e5c5f34
add stop/kickstart swap rpcs
mariocynicys Dec 6, 2024
245ea93
fix running_swap memory leak
mariocynicys Dec 16, 2024
c3b3b6b
Revert "remove todo"
mariocynicys Dec 16, 2024
70afcde
remove todo regarding refund finalization
mariocynicys Dec 16, 2024
0ce3c93
Revert "fix running_swap memory leak"
mariocynicys Dec 23, 2024
cc0db2e
review(dimxy): DRY, use a clourse for finding maker and taker coins
mariocynicys Dec 29, 2024
997ad63
review(dimxy): issue an info! log when stop rpc is used
mariocynicys Dec 29, 2024
497c394
Reapply "fix running_swap memory leak"
mariocynicys Jan 24, 2025
caf52dd
merge with origin/dev
mariocynicys Jan 24, 2025
90263d9
review(dimxy): use abortable instead of spawn_abortable and await it …
mariocynicys Jan 24, 2025
ff636f4
cargo fmt
mariocynicys Jan 24, 2025
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