-
Notifications
You must be signed in to change notification settings - Fork 43
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
[WIP] refactor(lnd): use SendPayment instead of SendToRoute #1025
Conversation
a8c50ca
to
dee7c95
Compare
dee7c95
to
5cd6bdd
Compare
Discussion with @offerm if this is really not possible, if not unfortunately close this and @offerm will digg into the Might be the solution for still using |
So, the problem is that:
Looking at the SendPayment API we can specify the following:
As a maker we're currently using |
When maker is creating an invoice for the taker we don't know the total timelock of the route, but we are setting the Based on that we can calculate a shorter timelock for maker's payment and force it with |
Sorry for the length of the below. consider a channel between A and B, each side has 25 BTC. Each side holds a signed commitment transactions that it can use to close the channel by sending to the chain. If we consider A<>B<>C and a payment is done from A to C, so A sends 1 BTC to B and B sends 1 BTC to C, the above will not work any more. If A and B agrees on a new commitment transaction (24-26), B may decide not to send the 1 BTC to C. For this case we have HTLC payment. HTLC is another output in the new commitment transaction between A and B allowing B to claim the amount (1 BTC) if B has the preimage and allowing A to claim the amount after an agreed time (blocks) in case B does not have the preimage or decided not to use it.
The above steps may take some time and B should also consider that he may have connectivity issues himself. B must allow himself time to be sure that he can grab is 1 BTC before A comes and get the 1 BTC back to his wallet (after the agreed time). To protect himself B (the payee) sets the timeout and the payer must respect the value set by the payee. When having a route A<>B<>C<>D, there are actually 3 payments involved - 3 payees - 3 lock time value. Each payee decides his lock time value and each payer must respect it. Since the route and all payments are decided by A, A must know these values for all nodes along the route and create a valid HTLCs. lock time value is expressed in absolute block numbers. So when A makes the payment he should take the current block height and add the timelocks value. When each nodes receive the HTLC, each node subtract the current block height from the HTLC value and check that it was given enough time. Now we can look at the output of queryroutes:
We can see route with 4 nodes. with the following lock time values: 2008, 1968,1928. From where LND gets these numbers? these are shared in the LN network by all nodes. You can see them with When doing SWAP we have 2 payments - BTC and LTC. Say BTC route is A<>B<>C<>D and LTC route is D<>B<>E<>A. Now, if we understand all the above, lets see how this is done in XUD today (before this PR) with the invoice and pay to route:
The log record related to a swap does not show enough information to be sure that we are doing it right. IMHO, if we are doing something wrong, it can explain the unknownexthop error. |
Let's take a look at how the current implementation is doing it:
Judging by the TODOs in the code and the above mentioned steps I feel like the current implementation is broken because of step 5. Maker sets the minimum cltv (final hop) to 144, but does not control how long the total route will be. Loss of funds can occur if the taker pays the invoice with total cltv smaller than The correct step would be: When using |
Putting this one on hold/WIP until #1037 is done. |
Do you see any problem using https://api.lightning.community/#sendpaymentsync with Question: Does |
The general approach of setting I spent a lot of time reading the thoughtful comments here and thinking about this. I can think of 2 more considerations here:
This is pretty complex so let me know if this makes sense. |
Note: |
Definitely an optimization that we could look into post 1.0.
Nice observation. This is corrected in https://github.com/ExchangeUnion/xud/pull/1037/files#diff-207d104426c9b9c110cdd1c78f1fc6ecR487 where we use |
Closing in favor of #1121 |
In this PR we refactor the
LndClient.sendPayment
logic to uselndrpc.SendPaymentSync
instead oflndrpc.SendToRoute
. As a result we can now use the samebuildSendRequest
andexecuteSendRequest
logic for sanity swaps and "regular swaps".In addition we:
LndClient.cltvDelta
to 576 if it's not specified via the config fileLndClient.addInvoice
I also added TODOs for cltvDelta values (to be discussed and addressed in later PR) because I believe the logic as is does not work for Raiden.
Fixes the 2nd part of #984 (comment)