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_swap): check for existing maker/taker payment before timeout #2283

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

shamardy
Copy link
Collaborator

@shamardy shamardy commented Nov 27, 2024

This PR moves payment existence check in maker_payment/send_taker_payment before timeout validation and skips timeout if payment is already sent, as the taker swap should proceed to waiting for maker to spend the taker payment.

Should fix #2136 , but we are still not aware why the electrum request doesn't timeout.

Move payment existence check before timeout validation and skip timeout
if payment is already sent, as we should proceed to waiting for maker
to spend the taker payment.
@shamardy shamardy changed the title fix(legacy_taker_swap): check for existing payment before timeout fix(legacy_swap): check for existing maker/taker payment before timeout Nov 27, 2024
Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!
Changes are well-structured and code in general is good to me, I have non mandatory suggestion

mm2src/mm2_main/src/lp_swap/taker_swap.rs Show resolved Hide resolved
@dimxy
Copy link
Collaborator

dimxy commented Nov 28, 2024

So this actually fixes the situation (for taker case):
Taker sent the payment but logged no event for that. Then went offline for a long time. When Taker restarts he finds the timeout has gone and fails the swap in a state in which he does not refund (but he can refund)?

@cipig
Copy link
Member

cipig commented Nov 28, 2024

When Taker restarts he finds the timeout has gone and fails the swap in a state in which he does not refund (but he can refund)?

not sure if i understood the question, but taker can only refund if he removes the TakerPaymentTransactionFailed event from his swap JSON

      {
         "event" : {
            "data" : {
               "error" : "taker_swap:1488] Timeout 1717611160 > 1717610301"
            },
            "type" : "TakerPaymentTransactionFailed"
         },
         "timestamp" : 1717611160346
      },

and replaces it with a TakerPaymentSent event and also adds the correct txid and txhash from that particular swap to the event... like this

      {
         "event" : {
            "data" : {
               "tx_hash" : "84fa6b8d50aff9a727c61b8f1bf33564c8958d120669da33fa385d45f90f6da0",
               "tx_hex" : "0400008085202f8901a33d00490a3d9e3cbccbb394013c91d4f80dc1f2a19191632a29747d2f439301010000006b48304502210094e97cb9999aa77357f29f23a8e0bf778dbbf238523a92875992b7ea8af303e502204e1554dd35d6cbdebe3f5780609163473aa9b918b7213bfbe018dd672a9a12cd0121028a38bafdd6c1aa3ec34c480d6ae9383bee54eef7c115e51a5aeab699734ff055ffffffff03008589dc0000000017a914ea8dcc7b4ea177f7a2c999d7a603aab8d6308b2f870000000000000000166a14718d7a526e8d14f45b5795db8b864924b0cd90c6d2bee2f60d0000001976a914d89058fbcce7402fb342dffadf04bd32dba29abe88ac89a16066000000000000000000000000000000"
            },
            "type" : "TakerPaymentSent"
         },
         "timestamp" : 1717611160346
      },

this was the situation from before this change...

@dimxy
Copy link
Collaborator

dimxy commented Nov 28, 2024

I am trying to work out how taker came to situation that he actually sent taker payment but did not log any event about this (to memorise this, it's not clear from the issue description)

@cipig
Copy link
Member

cipig commented Nov 28, 2024

I am trying to work out how taker came to situation that he actually sent taker payment but did not log any event about this (to memorise this, it's not clear from the issue description)

That's the big question, indeed. I first thought the electrums returned a timeout, but sent the tx anyway. But that's not the case, else we would have an error from electrum there in the event and not simply taker_swap:1488] Timeout 1717611160 > 1717610301. Will show the affected user this, maybe he can tell us more about the "circumstances".

@mariocynicys
Copy link
Collaborator

That's the big question, indeed. I first thought the electrums returned a timeout, but sent the tx anyway. But that's not the case, else we would have an error from electrum there in the event and not simply taker_swap:1488] Timeout 1717611160 > 1717610301. Will show the affected user this, maybe he can tell us more about the "circumstances".

if the process is killed after this line

.send_taker_payment(SendPaymentArgs {

but before the swap event is written, this could happen.

Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

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

tiny notes. LGTM otherwise

mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator Author

shamardy commented Dec 6, 2024

@mariocynicys @laruh please review the refactor next week.

&self.unique_swap_data()[..],
);

let time_lock = match std::env::var("USE_TEST_LOCKTIME") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we not allow this test locktime only if enabled by a cfg feature?
(I guess we tend to make all test code cfg enabled)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed USE_TEST_LOCKTIME occurrences in swaps code completely, and from the whole codebase. Took me some time to make the tests work again but this should make the legacy swap code cleaner. We should look into removing all other env vars in swaps code when we have the time.

@dimxy
Copy link
Collaborator

dimxy commented Dec 11, 2024

I added a note why the swap failed on send taker payment and manual swap file editing was required.
This PR fixes this issue: even if TakerPaymentSent was not saved in the file, the fix checks that the payment tx in fact was sent and the swap continues.

(PS. I guess, there may be similar issues in other swap steps due to the event not saved)

LGTM

laruh
laruh previously approved these changes Dec 11, 2024
Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

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

LGTM!
I have last non blocking suggestion

/// The reward configuration depends on the specific requirements of the coins
/// involved in the swap.
/// Some coins may not support watcher rewards at all.
async fn setup_watcher_reward(&self, taker_payment_lock: u64) -> Result<Option<WatcherReward>, String> {
Copy link
Member

Choose a reason for hiding this comment

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

I suppose its better to name it setup_taker_watcher_reward, to follow watcher reward setup naming on maker_swap.rs

and then rename process_watcher_logic to process_taker_watcher_logic

Or just rename setup_maker_watcher_reward to setup_watcher_reward, its the private function anyway

@dimxy dimxy self-requested a review December 11, 2024 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants