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

[r2r] Lightning Refunds #1592

Merged
merged 30 commits into from
Jan 13, 2023
Merged

[r2r] Lightning Refunds #1592

merged 30 commits into from
Jan 13, 2023

Conversation

shamardy
Copy link
Collaborator

#1045

  • Add 4 new methods to SwapOps traits. These methods are related to auto refunding of swap payments and early release of swap payments by the other side of the swap.
  • Implemented check_if_my_payment_sent, search_for_swap_tx_spend_my, search_for_swap_tx_spend_other SwapOps methods for LightningCoin.
  • Added MakerPaymentInstructionsReceived to MAKER_SUCCESS_EVENTS, TakerPaymentInstructionsReceived to TAKER_SUCCESS_EVENTS.
  • Refactored LightningDB methods to reduce calls to DB.

@shamardy shamardy changed the title [wip] Lightning Refunds [r2r] Lightning Refunds Dec 28, 2022
@shamardy
Copy link
Collaborator Author

@artemii235 @ozkanonur I think the new is_auto_refundable and wait_for_htlc_refund methods
https://github.com/KomodoPlatform/atomicDEX-API/blob/8df164c96106a6c16dc9939f453980b5a3261f64/mm2src/coins/lp_coins.rs#L719-L727
should be implemented for TendermintCoin/TendermintToken to return MakerPaymentRefunded/TakerPaymentRefunded events instead of MakerPaymentRefundFailed/TakerPaymentRefundFailed events and for the early release of maker funds to work in the future for a lightning/tendermint swap
https://github.com/KomodoPlatform/atomicDEX-API/blob/8df164c96106a6c16dc9939f453980b5a3261f64/mm2src/mm2_main/src/lp_swap/taker_swap.rs#L1711-L1720

Copy link
Member

@artemii235 artemii235 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 PR! It's a first review iteration.

mm2src/mm2_main/src/lp_swap/taker_swap.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
}

// Todo: need to also check on-chain spending
Copy link
Member

Choose a reason for hiding this comment

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

Do you plan to do it on next iteration? 🙂

Copy link
Collaborator Author

@shamardy shamardy Jan 10, 2023

Choose a reason for hiding this comment

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

Yes, this should be done in next sprint. This can only happen when the channel the payment is sent/received over is closed before the swap finishes, so this will be done alongside claiming swap or refunding on-chain for closed channels.
I believe it might be easier to implement this if Event::ChannelClosed from rust-lightning side provides info about unsettled HTLCs in the channel closing transaction.https://github.com/KomodoPlatform/atomicDEX-API/blob/ae5daf7c082b3493555057a7c7559e6d5d211893/mm2src/coins/lightning/ln_events.rs#L85 But if this can't be done easy from rust-lightning side I believe it can be done on mm2 straight away. Just need to check the rust-lightning code in details for unsettled HTLCs and make sure I don't miss anything before implementing this, that's why I moved it to next sprint.

mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Good job!
First review iteration

@@ -741,6 +785,74 @@ impl SwapOps for LightningCoin {
}
}

fn can_be_released(&self) -> bool { true }

Choose a reason for hiding this comment

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

In my opinion, can_be_released is not clear enough. Could you please give it another name?
At least, can_release_payment or can_fail_htlc_backwards

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

can_be_released is removed after implementing this suggestion #1592 (comment) by @artemii235

Comment on lines 391 to 393
PaymentInfo::new(payment_hash, PaymentType::InboundPayment, "keysend".into(), amt_msat);
payment_info.preimage = Some(preimage);
payment_info.status = HTLCStatus::Received;

Choose a reason for hiding this comment

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

How about adding PaymentInfo::with_preimage_and_status?

"Swap Payment".into(),
amt_msat,
);
payment_info.status = HTLCStatus::Received;

Choose a reason for hiding this comment

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

How about adding PaymentInfo::with_status?

amount_msat,
fee_paid_msat,
is_outbound,
status,
created_at,
last_updated
) VALUES (
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10

Choose a reason for hiding this comment

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

Comment on lines 879 to 883
let params = [
&preimage as &dyn ToSql,
&last_updated as &dyn ToSql,
&payment_hash as &dyn ToSql,
];

Choose a reason for hiding this comment

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

Please consider using the rusqlite::params or db_common::owned_named_params macros

@shamardy shamardy changed the title [r2r] Lightning Refunds [wip] Lightning Refunds Jan 9, 2023
@shamardy shamardy changed the title [wip] Lightning Refunds [r2r] Lightning Refunds Jan 10, 2023
@shamardy
Copy link
Collaborator Author

@artemii235 @sergeyboyko0791 this is ready for next review iteration

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Next review iteration

mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
@@ -1079,7 +1086,28 @@ impl MakerSwap {
]));
}

let taker_payment = self.r().taker_payment.clone();
if let Some(payment) = &taker_payment {
if let Err(e) = self.taker_coin.on_maker_payment_refund_start(&payment.tx_hex).await {

Choose a reason for hiding this comment

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

This is really confusing because TakerSwapOps::on_maker_payment_refund_start is called on the Maker side if only the taker payment has been sent.
Shouldn't TakerSwapOps be used on the Taker side only?

Also I'm really not sure if MakerSwapOps and TakerSwapOps names are suitable as the coins traits.
What if we have MakerSwapMakerCoin, MakerSwapTakerCoin, TakerSwapMakerCoin, TakerSwapTakerCoin.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What if we have MakerSwapMakerCoin, MakerSwapTakerCoin, TakerSwapMakerCoin, TakerSwapTakerCoin

I like the idea, it will remove a lot of the confusion. We need @artemii235 opinion on this too before proceeding since he suggested this #1592 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this looks more clear than Maker/TakerSwapOps, so I agree 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only MakerSwapTakerCoin, TakerSwapMakerCoin are needed in this PR. Will open an issue for the refactor/spliting of SwapOps #1592 (comment) to the 4 above traits.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will open an issue for the refactor/spliting of SwapOps #1592 (comment) to the 4 above traits.

#1610

Choose a reason for hiding this comment

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

Consider this as solved!

mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/taker_swap.rs Show resolved Hide resolved
Comment on lines 1725 to 1729
// Since taker payment is refunded successfully, maker payment can be released for lightning and similar protocols.
// # Important: New code that leads to refund failure shouldn't be added below this code block.
if let Err(e) = self.maker_coin.on_taker_payment_refund_success(&maker_payment).await {
error!("Error {} on calling on_taker_payment_refund!", e)
}

Choose a reason for hiding this comment

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

Should we do the same if self.taker_coin.is_auto_refundable()?

Copy link
Collaborator Author

@shamardy shamardy Jan 11, 2023

Choose a reason for hiding this comment

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

Yes, @sergeyboyko0791 @artemii235 do you think it's worth adding 2 new swap commands TakerSwapCommand::OnRefundStart , TakerSwapCommand::OnRefundSuccess and the same for maker_swap? This will help us avoid a lot of confusion in the future.

Copy link
Member

@artemii235 artemii235 Jan 12, 2023

Choose a reason for hiding this comment

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

Yeah, looks like it's worth doing it. As these are commands, I would name them PrepareForRefund and FinalizeRefund or something similar 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PrepareForRefund and FinalizeRefund are good enough names, couldn't come up with better names myself :)

Choose a reason for hiding this comment

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

I really like this idea.

do you think it's worth adding 2 new swap commands TakerSwapCommand::OnRefundStart , TakerSwapCommand::OnRefundSuccess and the same for maker_swap? This will help us avoid a lot of confusion in the future.

Consider this as solved, but will not resolve so Artem can see this conversation at the next review iteration

mm2src/mm2_main/src/lp_swap/taker_swap.rs Outdated Show resolved Hide resolved
mm2src/mm2_main/src/lp_swap/maker_swap.rs Outdated Show resolved Hide resolved
@shamardy shamardy changed the title [r2r] Lightning Refunds [wip] Lightning Refunds Jan 11, 2023
@shamardy shamardy changed the title [wip] Lightning Refunds [r2r] Lightning Refunds Jan 11, 2023
…Refund, TakerSwapCommand::FinalizeTakerPaymentRefund
…nd, MakerSwapCommand::FinalizeMakerPaymentRefund
@shamardy
Copy link
Collaborator Author

@artemii235 @sergeyboyko0791 this is ready for another review!

mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lp_coins.rs Outdated Show resolved Hide resolved
Comment on lines 1725 to 1729
// Since taker payment is refunded successfully, maker payment can be released for lightning and similar protocols.
// # Important: New code that leads to refund failure shouldn't be added below this code block.
if let Err(e) = self.maker_coin.on_taker_payment_refund_success(&maker_payment).await {
error!("Error {} on calling on_taker_payment_refund!", e)
}

Choose a reason for hiding this comment

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

I really like this idea.

do you think it's worth adding 2 new swap commands TakerSwapCommand::OnRefundStart , TakerSwapCommand::OnRefundSuccess and the same for maker_swap? This will help us avoid a lot of confusion in the future.

Consider this as solved, but will not resolve so Artem can see this conversation at the next review iteration

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Thank you for the fixes! Just a few questions

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

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

Awesome!

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

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

🔥

@artemii235 artemii235 merged commit e2f9046 into dev Jan 13, 2023
@artemii235 artemii235 deleted the ln-refunds branch January 13, 2023 15:13
@shamardy shamardy mentioned this pull request Jan 31, 2023
24 tasks
@shamardy shamardy mentioned this pull request Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants