Research/Bitcoin Dev Archive / lightning-dev list
Return to the Layered Commit Transactions?
7 messages, Nov 24 to 30, 2015. Started by Mats Jerratsch.
Anthony Towns (4), Rusty Russell (2), Mats Jerratsch
While working on the payment layer and messing around with timeouts / timeframes for CSV and CLTV, I noticed a few things...
(1) It is indeed not possible for HTLCs to have a long revocation time and a short refund time. And it makes sense the way we designed the scripts and the commit transaction. As it's a single output, we either can prevent the other side from doing ANYTHING for the revocation timeout T1, or the other party is able to claim the complete output without any encumbering... In long chains of multiple hops, I might need R to settle the payment normally, while you still wait out the revocation timeout before you can reveal R...
I described that some time ago, but was unsure if I was right https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2015/timeframes-for-refunds-and-revocation#m-b912d5b4
(2) As revocation-time and refund time are equal, we have some problematic race conditions, where both parties can technically claim the payment, depending on propagation time of the transaction. Further, if you claim the payment that I was actually entitled to per refund, I may be out of luck getting my money back on the next hop...
(3) As the absolute CLTV timeout has to increase with each hop, it can give us insight about our position within the route, even with onion routing. It is possible to have the sender add some delay, but this just increases the (already long) total refund time.
I played with the numbers back and forth, but I don't really see any way out of this problem. Anything below one day might be dangerous already for DDoS attacks, with 20 hops we might end up with 30-40 days for a refund though...
If we go back to the original design, we are able to separate 'claiming-a-payment-using-R' and 'revoke-an-output' into two layers and such, we can enforce revealing R within a separate timeframe than the revocation timeframe. To do this, we need either SIGHASH_NOINPUT or SW, as we can't construct the transaction on top of the 'reveal-R' transaction without knowing R currently. Short refund timeouts would also mitigate the problems of (3)
I would really love to stick with the current design, as not having to keep another set of signatures makes everything easier ...
Cheers Mats
Mats Jerratsch <matsjj at gmail.com> writes:
Quoted text (12 lines)
> While working on the payment layer and messing around with timeouts / > timeframes for CSV and CLTV, I noticed a few things... > > (1) > It is indeed not possible for HTLCs to have a long revocation time and > a short refund time. And it makes sense the way we designed the > scripts and the commit transaction. As it's a single output, we either > can prevent the other side from doing ANYTHING for the revocation > timeout T1, or the other party is able to claim the complete output > without any encumbering... In long chains of multiple hops, I might > need R to settle the payment normally, while you still wait out the > revocation timeout before you can reveal R...
Right.
This isn't immediately obvious, so let's unpack this a bit:
Because any to-self output needs to have an additional relative revocation delay, A's HTLC output would look like:
For A if HTLC times out + revocation relative timeout, OR
For B with HTLC R value, OR
For B with revocation hash.
A needs to handle the case where B spends after the HTLC timeout, but before the revocation relative timeout, so its inter-hop HTLC timeout delta has to be > revocation timeout.
(Aside: can we reduce the HTLC timeout there so that it takes into account the relative timeout? I'd have to think harder about all the other scripts, too, but it seems reasonable on the surface?).
Quoted text (6 lines)
> (2) > As revocation-time and refund time are equal, we have some problematic > race conditions, where both parties can technically claim the payment, > depending on propagation time of the transaction. Further, if you > claim the payment that I was actually entitled to per refund, I may be > out of luck getting my money back on the next hop...
Yeah, don't do this.
Quoted text (5 lines)
> (3) > As the absolute CLTV timeout has to increase with each hop, it can > give us insight about our position within the route, even with onion > routing. It is possible to have the sender add some delay, but this > just increases the (already long) total refund time.
This was my plan.
Quoted text (4 lines)
> I played with the numbers back and forth, but I don't really see any > way out of this problem. Anything below one day might be dangerous > already for DDoS attacks, with 20 hops we might end up with 30-40 days > for a refund though...
It's not about DoS, really. The revocation time has to be long enough so you're certain to spot someone cheating. And we've established that the HTLC inter-hop-delta should be >> revocation time.
If your node is online and robust, 3 hours should be sufficient for revocation time (remember, you can jack up fees if you need to). 12 hours will give you plenty of time to take your claim transactions and inject them into the bitcoin network via carrier pidgeon or whatever you need to get around any DDoS.
Still, let's assume everyone uses 1 day for revocation time, and 2 days for HTLC timeout delta, and we have 20 hops.
The original HTLC (NODE 0 -> NODE 1) times out in 40 days. The last HTLC (NODE 19 -> NODE 20) times out in 2 days, and that one falls to the blockchain. NODE 19 takes 3 days to get a refund. Then it can immediately close the incoming HTLC from NODE18 (why wait for a timeout? It no longer has outstanding liabilities or any reason to believe it will get the R value), etc.
So, worst case is 3 days unless there are multiple hop failures?
Quoted text (10 lines)
> If we go back to the original design, we are able to separate > 'claiming-a-payment-using-R' and 'revoke-an-output' into two layers > and such, we can enforce revealing R within a separate timeframe than > the revocation timeframe. To do this, we need either SIGHASH_NOINPUT > or SW, as we can't construct the transaction on top of the 'reveal-R' > transaction without knowing R currently. Short refund timeouts would > also mitigate the problems of (3) > > I would really love to stick with the current design, as not having to > keep another set of signatures makes everything easier ...
Agreed.
What do you think about reducing the OP_CHECKLOCKTIMEVERIFY argument if it's followed by the revocation delay?
Thanks! Rusty.
On Wed, Nov 25, 2015 at 10:32:54AM +1030, Rusty Russell wrote:
Quoted text (4 lines)
> For A if HTLC times out + revocation relative timeout, OR > (Aside: can we reduce the HTLC timeout there so that it takes into account > the relative timeout? I'd have to think harder about all the other > scripts, too, but it seems reasonable on the surface?).
Definitions: d -- the OP_CSV delay T -- top OP_CLTV timeout C -- the commitment tx is signed by B and given to A at time C P -- the commitment tx is published at time P S -- the commitment tx is spent at time S X -- the timeout at which a refund can be forced
Constraints enforced by bitcoin:
P >= C S >= T S >= P+d
When A is paying the HTLC, and wants to claim a refund by unilaterally closing the channel, We want to enforce S >= P+d and S >= X.
If C+d >= X, then P+d >= C+d >= X, and we can just use OP_CSV <d>.
If C+d < X, then if the commitment is published immediately, P+d < X, and we still need OP_CSV <d> && OP_CLTV <T=X>.
However if the commitment gets updated while the HTLC remains open, C will increase over time while d and X remain constant, so eventually the OP_CLTV could be dropped. But I don't see how that does any good?
_But_ if you're updating a commitment and C+d >= X, then it seems like your counterparty should already have given up on trying to redeem it, not keep it open? If so, you're probably being cheated and should have closed the channel already.
Alice forwards to Bob who forward to Carol creates two HTLCs:
(Bob, x, R, T) (Carol, x-f, R, T-t)
What we want is to be able to say that "assuming Bob can close the HTLC with Carol by T-t+k, Alice can close the channel by time T+k", I think...
* At time T-t+k, Alice says "Bob, you haven't given me R. Do you give up?". If Bob has closed his channel with Carol and is cooperative, he reveals R (if Carol did) and is happy, or gives up and Alice times out the HTLC slightly early. (If Bob doesn't answer at this point, he's violating protocol, and we don't care if he loses money)
* Otherwise, Alice declares Bob uncooperative, and closes the channel, with an output spending to:
(Alice & OP_CLTV T & OP_CSV d) | (Bob & (R | Revoke_A))
* This enters the blockchain at time T-t+k+x (where x is some delay due to the time it takes to find a block, and whether the fee Alice's commitment pays is competitive or not)
* Alice can spend this HTLC at time max(T, T-t+k+x+d), which we'd like to no later than T. So T-t+k+x+d <= T, which just means d <= t-k-x If t >> d (ie, inter-hop-delta is much greater than revocation delay), that's satisfied.
* Depending on the fee Alice pays etc, her transaction then gets n confirmations by time T+y, or else Bob manages a double spend, revealing R. If we chose k such that y <= k here, we're done: if Bob tried at doublespend, then at time T+k Alice knows R, or Alice is confident she got her refund.
* Induction lets us note that Bob can apply the same argument and ensure that he complies with protocol, giving us the original assumption.
* For concreteness, then, assuming you pay fees that get your block accepted withing the next F blocks or so, k=y=10*(n+F) minutes, x=10*F minutes, and t >= d+10*(n+2F).
So normally your HTLC will expire at T-t+k = T-d-10*F and the channel will stay open; otherwise you'll close the channel and not know the answer for sure until T+k = T+10*(n+F).
For n=4, F=4, t >= d+2 hours, so normal expiry is no later than T-d-40m, and uncooperative expiry is at T+1h20, so d+2h later.
Quoted text (2 lines)
> > (3) > > As the absolute CLTV timeout has to increase with each hop, it can
(decrease with each hop; at least if you start counting at whoever's paying)
Quoted text (6 lines)
> It's not about DoS, really. The revocation time has to be long enough > so you're certain to spot someone cheating. And we've established that > the HTLC inter-hop-delta should be >> revocation time. > > If your node is online and robust, 3 hours should be sufficient for > revocation time (remember, you can jack up fees if you need to).
So if d=3h, then t>=5h above assuming 4 confirmations is enough to give you confidence you won't be reorged and doublespent, and you pay enough fees to get into one of the next 4 blocks.
Quoted text (4 lines)
> 12 > hours will give you plenty of time to take your claim transactions and > inject them into the bitcoin network via carrier pidgeon or whatever you > need to get around any DDoS.
d=3h and t=12h gives (n+2F)=54, so say n=24, F=15; meaning pay enough fees to get in in the next 15 blocks, and accept spends after 24 confirmations.
Quoted text (6 lines)
> Still, let's assume everyone uses 1 day for revocation time, and 2 days > for HTLC timeout delta, and we have 20 hops. > > The original HTLC (NODE 0 -> NODE 1) times out in 40 days. The last > HTLC (NODE 19 -> NODE 20) times out in 2 days, and that one falls to the > blockchain. NODE 19 takes 3 days to get a refund.
By my protocol above, node 19 would demand an answer from node 20 at time T-t+k, but T=2 days, and t=2 days, so that's just time 0+k. d<=t-k-x, so k<=t-d-x; ie, k<=1 day - x, and x=10*F minutes. So node 19 demands an answer from node 20 after, say, k=21 hours (F=18 blocks). Node 20 doesn't reply, so 19 closes the channel and posts the commitment to the blockchain, paying enough fee to be visible after 18 blocks, which happens to be 3h later, ie 1 day total time. After another day, both OP_CLTV and OP_CSV pass, so node 19 tries spending it, with enough fee to get in after 18 blocks, or 3h, so that's at about T=51h.
Meanwhile node 18 is wondering what's going on, and follows the same protocol. At time T'-t+k demanding an answer from node 19. T'=4 days, t=2 days, and k=21h, so T'-t+k = 69h. So node 19 has 18 hours to be confident its refund transaction isn't going to be reorged, or to see an attempt at a doublespend by node 20 revealing R. Either way, node 19 can respond to node 18's request and keep the channel open. Node 18 can pre-emptively pass on R or close 17's HTLC at that point and so on.
So by my count, node 19 has a refund within 2 days 21 hours.
Quoted text (1 line)
> So, worst case is 3 days unless there are multiple hop failures?
A single hop failure at node 3 immediately after the transaction gets passed on would be worse. Setup:
node 1/node 2 = 38 days node 2/node 3 = 36 days node 3/node 4 = 34 days ... node 19/node 20 = 2 days
t+1 second:
node 1/node 2 = 38 days node 2/node 3 = 36 days node 3/node 4 = 34 days [everyone else resolved]
Node 4 asks node 3 "wtf??". doesn't get an answer, closes the channel, publishing the commitment to the blockchain.
After a day, node 4 can reclaim uncommitted funds, and reveal R to claim the HTLC funds. node 3 is out of pocket.
Within the next 33 days, node 3 wakes up, sees R and finishes the transaction. Node 2, in particular, has funds held up for the 34 days node 3 was down.
Quoted text (2 lines)
> What do you think about reducing the OP_CHECKLOCKTIMEVERIFY argument if > it's followed by the revocation delay?
The revocation delay happens simultaneously, so I don't think this can be made to work usefully.
Don't think it's needed either though, so long as channels have parameters "t" and "k" as well as "d" to drop to the blockchain well before T actually comes around.
Cheers, aj
Anthony Towns <aj at erisian.com.au> writes:
Quoted text (7 lines)
> Definitions: > d -- the OP_CSV delay > T -- top OP_CLTV timeout > C -- the commitment tx is signed by B and given to A at time C > P -- the commitment tx is published at time P > S -- the commitment tx is spent at time S > X -- the timeout at which a refund can be forced
...
Quoted text (2 lines)
> For n=4, F=4, t >= d+2 hours, so normal expiry is no later than > T-d-40m, and uncooperative expiry is at T+1h20, so d+2h later.
Is the 2 hours here due to timer tolerance? If so, BIP 113 may allow us to squeeze that a little in practice, depending on one's risk tolerance.
Quoted text (4 lines)
>> So, worst case is 3 days unless there are multiple hop failures? > > A single hop failure at node 3 immediately after the transaction gets > passed on would be worse. Setup:
Indeed.
Quoted text (5 lines)
>> What do you think about reducing the OP_CHECKLOCKTIMEVERIFY argument if >> it's followed by the revocation delay? > > The revocation delay happens simultaneously, so I don't think this can > be made to work usefully.
Good point, it was a thinko.
Quoted text (3 lines)
> Don't think it's needed either though, so long as channels have parameters > "t" and "k" as well as "d" to drop to the blockchain well before T > actually comes around.
I'll be honest, I got lost somewhere in the alphabet reading your post.
However, you might be able to help me with a related question: I proposed previously that if you didn't get fast resolution on an HTLC you'd require proof that a commit tx was published, or you'd close the channel yourself to create such a proof to hand back.
How would this work, timeout-wise? We don't know how many hops are ahead of us. If the rule is "wait 1 minute, if you don't get a response, close the channel" then everyone in the chain will close the channel at once. If the timeout is in the HTLC offer, then it makes the message more traceable.
I can't see any obvious solution, can you?
Thanks, Rusty.
On Fri, Nov 27, 2015 at 02:12:55PM +1030, Rusty Russell wrote:
Quoted text (12 lines)
> Anthony Towns <aj at erisian.com.au> writes: > > Definitions: > > d -- the OP_CSV delay > > T -- top OP_CLTV timeout > > C -- the commitment tx is signed by B and given to A at time C > > P -- the commitment tx is published at time P > > S -- the commitment tx is spent at time S > > X -- the timeout at which a refund can be forced > ... > > For n=4, F=4, t >= d+2 hours, so normal expiry is no later than > > T-d-40m, and uncooperative expiry is at T+1h20, so d+2h later. > Is the 2 hours here due to timer tolerance?
Nope, it's just plugging in n and F into "t >= d+10*(n+2F)".
Quoted text (4 lines)
> > Don't think it's needed either though, so long as channels have parameters > > "t" and "k" as well as "d" to drop to the blockchain well before T > > actually comes around. > I'll be honest, I got lost somewhere in the alphabet reading your post.
Yeah; I'm surprised I didn't while writing it.
But while chatting with matsjj afterwards, I think I came up with a beter explanation:
T = the timeout in the HTLC
d = the CSV delay in the HTLC
n = how long it takes for you to be confident someone else can't
doublespend an input to your transaction (ie, 10 minutes * however
many confirmations you want to see)
f = how long it takes your transactions to get the first confirmation,
given how much fee you pay
(To translate to t and k, t=d+f and k=f+n))
At time T-d-f, if the HTLC hasn't been resolved off-chain, you want to publish your commitment transaction. It'll hit the chain at time T-d.
At time T both the CTLV and CSV clauses are satisfied, so you can spend it. You should attempt to do so immediately. Your tx will be confirmed at time T+f. At time T+f+n, you'll be satisfied your tx is safe.
T-d-f = T-t = when you decide to close the channel and hit the blockchain T+f+n = T+k = when you're 100% sure the HTLC is done with
Quoted text (12 lines)
> However, you might be able to help me with a related question: I > proposed previously that if you didn't get fast resolution on an HTLC > you'd require proof that a commit tx was published, or you'd close the > channel yourself to create such a proof to hand back. > > How would this work, timeout-wise? We don't know how many hops are > ahead of us. If the rule is "wait 1 minute, if you don't get a > response, close the channel" then everyone in the chain will close the > channel at once. If the timeout is in the HTLC offer, then it makes the > message more traceable. > > I can't see any obvious solution, can you?
I still hope the "fine people for keeping the channel open" idea (I think CJP proposed it?) is possible, but I haven't tried working out if you can make the details work when you have to drop to the blockchain, and have multiple HTLCs in flight.
If you made the rule be something like "if the HTLC was setup at time O with timeout T, wait until O+(T-O)/120 and close the channel" you wouldn't have privacy problems though.
You'll still have the problem that if you're near the end of the chain you might get left holding the bag though (ie, still having to pay the tx amount forward (despite closing that channel), but not being able to collect it from the other end (because you cancelled the HTLC you forwarded, in order to keep the channel open)).
Cheers, aj
On Fri, Nov 27, 2015 at 03:14:46PM +1000, Anthony Towns wrote:
Quoted text (16 lines)
> On Fri, Nov 27, 2015 at 02:12:55PM +1030, Rusty Russell wrote: > > Anthony Towns <aj at erisian.com.au> writes: > T = the timeout in the HTLC > d = the CSV delay in the HTLC > n = how long it takes for you to be confident someone else can't > doublespend an input to your transaction (ie, 10 minutes * however > many confirmations you want to see) > f = how long it takes your transactions to get the first confirmation, > given how much fee you pay > At time T-d-f, if the HTLC hasn't been resolved off-chain, you want to > publish your commitment transaction. It'll hit the chain at time T-d. > At time T both the CTLV and CSV clauses are satisfied, so you can spend > it. You should attempt to do so immediately. Your tx will be confirmed > at time T+f. At time T+f+n, you'll be satisfied your tx is safe. > T-d-f = T-t = when you decide to close the channel and hit the blockchain > T+f+n = T+k = when you're 100% sure the HTLC is done with
So in addition: if Bob is forwarding an HTLC from Alice to Carol, then, assuming:
Bob knows Alice's f, call it f_a; Bob knows the CSV delay on his channel with Alice, call it d_a; Bob knows the CSV delay on his channel with Carol, call it d_c; Bob knows his own f and n; Bob knows the timeout on the incoming HTLC, T_a
Bob wants to work out the timeout to set on the outgoing HTLC, T_b.
In order to keep the channel with Alice open, he needs to resolve the outgoing HTLC by time T_a-d_a-f_a. And the time he's sure his outgoing HTLC is done is T_b+f+n, so:
T_b+f+n <= T_a-d_a-f_a
T_b <= T_a - d_a - f_a - f - n
Also, Bob wants T_b-d-f > now, or there's no point forwarding the HTLC since he'd have to close the channel immediately, so:
now + d + f < T_b <= T_a - d_a - f_a - f - n
gives you constraints on the forwarded timeout based on, I think, fairly meaningful parameters.
(You might want to add further constraints on the timeout based on fees, of course -- the above is what you need to avoid being left holding the bag)
Cheers, aj
On Sat, Nov 28, 2015 at 07:27:42AM +1000, Anthony Towns wrote:
Quoted text (3 lines)
> (You might want to add further constraints on the timeout based on fees, > of course -- the above is what you need to avoid being left holding > the bag)
You might also want to batch the timeouts, so if you have 1000 concurrent HTLCs on a channel at any point in time [0] and expect 10% of them to timeout, you'd be better off expiring them in five batches of 20, rather than individually.
That way you can spend them all to a single output, saving you both 19*34 bytes in that transaction, but also something like 19*130 bytes when you want to actually spend the outputs. (With 40 satoshi/byte fees @ $350/BTC, that's an overall saving of 2c or more per batched HTLC)
Cheers, aj
[0] I think the 100kB standardness limit on transactions means you can
have at most about 3000 concurrent committed HTLCs on a channel,
assuming your anchor is a single multisig transaction, and your
outputs are all P2SH.