LNSwap / lnstxbridge

GNU Affero General Public License v3.0
17 stars 12 forks source link

Refund #10

Closed gourgueg closed 2 years ago

gourgueg commented 2 years ago

Hi! In my last transaction the STX transfer went through, but I also received a BTC refund.

pseudozach commented 2 years ago

Thanks for using and reporting!

can you please provide relevant transaction ids? I'm already investigating a timing mismatch on the backend where swap is canceled before timeout which would lead to this situation. I'll share the findings shortly and then apply the fix.

gourgueg commented 2 years ago

Hi!

My pleasure. LNSwap is very useful. I love it!

Here's the transaction id on the stacks part : 0x363798511a3498342af75c8f80c6a62e477e8782793d9b391c726be6f3b613b0

For the BTC part, I'm not sure where to find the transaction id. Here's what I have:

Lightning invoice: lnbc3175440n1pshltj2pp5w9m2kjvzpjnfvvxyunmj3at35vayrv94larc48aqspckc605jgxsdql2djkuepqw3hjq565tqsxzerywfjhxuccqzylxqrrsssp58vefdq5exqz3n3462jrk6s3xnlg0f3058euaymvd6ss6pmk0gz0q9qyyssqe347dwrr3zt4qe7llhpktev0af0g8h3arfx8gjywjvlky4kul4n4dhde0dpnr4zp4v7rfpxdx9eqasx5ceq32qdgauwft2qm9vgk7wcq7ws7rf

Receiving node: 0214d3cf7140506ed6c0b459892f5734272fa0b19e0ffc7e99ef120d40eca675fb

Is that ok?

pseudozach commented 2 years ago

wow yes thanks for the report, that was most of the funds on the node so I hope you're open to returning the funds after the investigation is complete :)

so what happened and the timing is as below:

swap id: Uo9d8M is created at this time. 01/11/2021 09:07:24:397 debug: Swap Uo9d8M update: { "status": "swap.created" }

lockup: 01/11/2021 09:10:30:040 verbose: Locked up 9948371835 Stx for Reverse Swap Uo9d8M: txid: 0xac818078e7c65c5175a21a59ea708d2ddf5e28a0a337858ba86252b33e54c246 lockup tx confirmed at 01/11/2021 09:47:18:704

claim: success at this time 01/11/2021 19:06:31:220 https://explorer.stacks.co/txid/0x363798511a3498342af75c8f80c6a62e477e8782793d9b391c726be6f3b613b0?chain=mainnet

01/11/2021 19:06:31:276 error: Unhandled rejection: 2 UNKNOWN: invoice already canceled at which point the backend was trying to claim the HODL HTLC but failed.

-> this is my own fault because I canceled this invoice manually to avoid LN channel force closure Obviously this was wrong because it means I lost the cryptographic proof that I had a claim on that HTLC. I mistakenly assumed this swap was canceled without checking...

I'm working on improving the stability and timing of these claims. My goal is to ensure I avoid force closures while making sure there are no gaps in the lock/claim mechanism.

Again thanks for report and if you're open to returning the funds you can send to SP13R6D5P5TYE71D81GZQWSD9PGQMQQN54A2YT3BY which is the lnswap stacks address.

gourgueg commented 2 years ago

On my side, I started that swap then fell asleep. When I woke up I saw the claim button so I pressed it, used the stacks, and later during the day I saw the BTC was returned to my wallet.

I don't understand the technical part very well. I was trying to make another swap and the locking of the STX for the transfer failed. I assume I'll receive the BTC refund for that transaction soon. Here's that transaction info:

Lighting invoice: lnbc3258u1pscqhjupp59ex9cgnjstxgtgq2rkf0yydkcfxfxduj5detwzuc36ate57afgzsdql2djkuepqw3hjq565tqsxzerywfjhxuccqzylxqrrsssp5w0z44fcsuaz4dh5l803pqh3huk3vxtzw694r2fkd2x3lexx0hk3s9qyyssqrxr86ch7qlc0guvwax45k2zx6nx594xzg78g723gu33xzn4s50mpmvul0p55kj9ncmalzdw2x3hm3lw820xw6p8ke20km44s5lk9n3sqh852v2

Receiving node: 0214d3cf7140506ed6c0b459892f5734272fa0b19e0ffc7e99ef120d40eca675fb

Right now I don't have much unlocked STX. Would you be open to receiving the amount in BTC instead?

Thank you and keep up the good work!

pseudozach commented 2 years ago

I've also received a report over email and I'll note the investigation here for public record/tracking purposes. this one is a little different, there was no manual error on my part but an interesting race condition:

TL;DR: Refund and claim contract calls went into the same block, while backend thought refundstx succeeded so it canceled the hodl invoice, actually claim succeeded in the same block which means user got paid twice.

On 2nd thought, it could be that there was no race condition, refundstx was sitting in mempool but once claimstx came in, it became just cheap enough for the miner to include the refundstx with an error condition instead of all the read/write operations that would be required had the refundstx succeeded.

swapId: w5DAnB LOCKUP tx: 31/10/2021 00:57:12:376 debug: LND BTC accepted 1 HTLC for invoice: lnbc160u1 LOCKED: 31/10/2021 01:13:19:727 debug: Swap w5DAnB update: { "status": "transaction.confirmed" success lock https://explorer.stacks.co/txid/0x6ead494fbc8d67a1a6bfba4aab8138b5fdba3cfe15f30e134f531ab7707c9e44?chain=mainnet at block #35907 ... timelock 0x00000000000000000000000000008cd1 -> 36049 is the timeout blockheight! 31/10/2021 01:13:19:727 debug: Swap w5DAnB update: { "status": "transaction.confirmed", "transaction": { "id": "0x6ead494fbc8d67a1a6bfba4aab8138b5fdba3cfe15f30e134f531ab7707c9e44" } }

REFUND fail 01/11/2021 04:17:40:192 info: Refunded Reverse Swap w5DAnB 09:14:18 10/31 PM -> 04:14:18 AM 1/11 - at block height #36053 failed refundstx (err u1000) -> BECAUSE claimStx went into the same block and got in before this tx which means refund failed because claim succeeded and the swap was deleted from onchain map db https://explorer.stacks.co/txid/0x12acabde89e43b6f4d87494ebfddb09854174d133be143f6c45f1df98fae1544?chain=mainnet

CLAIM: success https://stacks-node-api.mainnet.stacks.co/extended/v1/tx/0x6c9ca9aebaffb0a8bc17320e1441184d08ac6d35fd1cfc062cc9bbceaef43c71

36053

CLAIMED: 01/11/2021 04:31:08:626 debug: Found claim in contract for Reverse Swap w5DAnB: 01/11/2021 04:31:08:657 error: Unhandled rejection: 2 UNKNOWN: invoice already canceled

I'm considering these 2 potential fixes:

pseudozach commented 2 years ago

On my side, I started that swap then fell asleep. When I woke up I saw the claim button so I pressed it, used the stacks, and later during the day I saw the BTC was returned to my wallet.

I don't understand the technical part very well. I was trying to make another swap and the locking of the STX for the transfer failed. I assume I'll receive the BTC refund for that transaction soon. Here's that transaction info:

Lighting invoice: lnbc3258u1pscqhjupp59ex9cgnjstxgtgq2rkf0yydkcfxfxduj5detwzuc36ate57afgzsdql2djkuepqw3hjq565tqsxzerywfjhxuccqzylxqrrsssp5w0z44fcsuaz4dh5l803pqh3huk3vxtzw694r2fkd2x3lexx0hk3s9qyyssqrxr86ch7qlc0guvwax45k2zx6nx594xzg78g723gu33xzn4s50mpmvul0p55kj9ncmalzdw2x3hm3lw820xw6p8ke20km44s5lk9n3sqh852v2

Receiving node: 0214d3cf7140506ed6c0b459892f5734272fa0b19e0ffc7e99ef120d40eca675fb

Right now I don't have much unlocked STX. Would you be open to receiving the amount in BTC instead?

Thank you and keep up the good work!

You've been very helpful indeed, I'll update the contract and push a new version that protects against this type of issue, this will make lnswap more robust and useful for future users!

you can return the funds (should be 317544 sats) to this LN invoice: lnbc3175440n1pscqljepp56mk004ejs8srkr7jq35dc34c06vrkn3hz4ngahy2pdamle6rj6hqdqqcqzpgsp54c6hhg3fl26scthj9vu7a0ksefu8z5ua5v492jfgzx5etk4vrp5q9qyyssqfmq4qjmkp9esn6uf8a7lreltjf07uxjxxw66z08f0qaledrrjpcsfx25qyzgqxjafead9r9z02tx5uvn850fpv352mnsfmsqzn70gmsqs37zkr

or to this btc address: bc1qyrgecxgsuucue36vlpsq4hnl8jma7ylqk3365p

thanks again,

Update -> new contract deployed: https://explorer.stacks.co/txid/0x0ba2bbd9b965fbb368b14e7b85e653f9d18d9f00668a580ae1ccf03c0c12355e?chain=mainnet

gourgueg commented 2 years ago

Hi!

That's great!

My pleasure. I've sent the funds to the btc address.

Thank You. I'm looking forward to using lnswap again!