Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

HTLCs using OP_CHECKSEQUENCEVERIFY/OP_LOCKTIMEVERIFY and revocation hashes.

14 messages, Jul 6, 2015, to Feb 11, 2016. Started by Rusty Russell.

Rusty Russell (6), CJP (4), Anthony Towns (3), Adam Back

Permalink First message in the Wayback Machine

Rusty Russell permalink original

Hi all,

        To recap: each side maintains a commitment transaction with two
outputs: one paying to self (with some delay), and the second paying to
the other side.
        To generate hash time-locked contracts (required for lightning
to be a network), both commitment transactions get an additional output.
This output is spendable under four conditions:
1) Recipient knows the R value (funds go to recipient), OR
2) The HTLC has timed out (funds return to initiator), OR
3) The HTLC has been revoked (funds to go "non-cheating" side), OR
4) The Commit transaction has been revoked (funds to go "non-cheating" side)

The last two failure modes are separate from each other, because HTLCs have different lifetimes from commit transactions.

Since we use "revocation preimages" to revoke transactions (rather than sending pubkeys as the original draft paper), the result of this for A's commitment transaction look like this:

HTLC from A to B: 1) R value and B's signature, OR 2) HTLC timeout and A's signature[*], OR 3) HTLC revocation preimage and B's signature, OR 4) Commitment transaction revocation preimage and B's signature.

HTLC from B to A: 1) R value and A's signature[*], OR 2) HTLC timeout and B's signature, OR 3) HTLC revocation preimate and B's signature, OR 4) Commitment transaction revocation preimage and B's signature.

We need to ensure delays in the cases where payment can go to A (marked with [*]) so that B has a chance to steal the funds if the HTLC or commitment tx has been revoked.

The result is one of the following scriptPubKey from the commitment tx for the HTLC like so (note: unchecked and unoptimized!):

HTLC from US to THEM: ---------------------

# They present HTLC's R value, or either revocation hash:
OP_DUP OP_HASH160 <HTLC-R-HASH> OP_EQUAL
OP_SWAP <HTLC-REVOCATION-HASH> OP_EQUAL
OP_ADD OP_SWAP <COMMIT-REVOCATION-HASH> OP_EQUAL
OP_ADD
OP_IF
        # One hash matched, pay to them.
        OP_DUP OP_HASH160 <THEM-pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
        # Must be us, with HTLC timed out.
        <HTLC-ABSOLUTE-TIMEOUT> OP_CHECKLOCKTIMEVERIFY OP_DROP
        <VERIFICATION-RELATIVE-TIMEOUT> OP_CHECKSEQUENCEVERIFY OP_DROP
        OP_DUP OP_HASH160 <US-pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
HTLC from THEM to US:
---------------------
# Revocation cases:
OP_DUP OP_HASH160 <HTLC-REVOCATION-HASH> OP_EQUAL
OP_SWAP <COMMIT-REVOCATION-HASH> OP_EQUAL
OP_ADD
OP_IF
        # One hash matched, pay to them.
        OP_DUP OP_HASH160 <THEM-pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
        # Us with R value?
        3 OP_DEPTH OP_EQUAL OP_IF
                OP_DUP OP_HASH160 <HTLC-R-HASH> OP_EQUALVERIFY
                <VERIFICATION-RELATIVE-TIMEOUT> OP_CHECKSEQUENCEVERIFY OP_DROP
                OP_DUP OP_HASH160 <US-pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
        OP_ELSE
                # Them with timeout.
                <HTLC-ABSOLUTE-TIMEOUT> OP_CHECKLOCKTIMEVERIFY OP_DROP
                OP_DUP OP_HASH160 <THEM-pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
        OP_ENDIF
OP_ENDIF

If you've read this far, congratulations!

AFAICT we don't need new SIGOPS here; the logic has all been moved to the commitment transaction output (thanks to OP_CLV and OP_CSV), so each side can generate the HTLC spending transaction with needing a signature from the other.

Feedback, fixed and optimizations welcome... Rusty.

CJP in reply to Rusty Russell permalink original
Quoted text (2 lines)
> Since we use "revocation preimages" to revoke transactions (rather than
> sending pubkeys as the original draft paper)

I remember the original paper mentioned sending private keys. I suppose you mean that the preimages replace the *private* keys, not the pubkeys?

I was not aware of that design change, but it sounds like an improvement.

CJP

Adam Back in reply to CJP permalink original

The use of the private key sending function, is just intended to be a more compact and efficient way of sending the other end of the channel a signed message, it says "well here, have the private key, you can sign it yourself".

Now that being the case, and as the transaction has another signature anyway it occurred to me that you should be able to use a chain of hash preimages to do it more efficiently again.

Rusty generalised that to have a lower setup by using a short of hypercube multi-dimensional array of preimages. (If you want 1million hash preimages, then you have to actually compute 1 million of them at setup.) I had the idea that Rusty might've written a blog post on this preimage generalisation however I dont seem to be able to find it now.

Adam

On 7 July 2015 at 21:51, CJP <cjp at ultimatestunts.nl> wrote:

Quoted text (16 lines)
>> Since we use "revocation preimages" to revoke transactions (rather than
>> sending pubkeys as the original draft paper)
>
> I remember the original paper mentioned sending private keys. I suppose
> you mean that the preimages replace the *private* keys, not the pubkeys?
>
> I was not aware of that design change, but it sounds like an
> improvement.
>
> CJP
>
>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
Rusty Russell in reply to Adam Back permalink original

Adam Back <adam at cypherspace.org> writes:

Quoted text (4 lines)
> The use of the private key sending function, is just intended to be a
> more compact and efficient way of sending the other end of the channel
> a signed message, it says "well here, have the private key, you can
> sign it yourself".

Yep, the creation of revocable transactions was one of the reasons I'm still in awe of the LN paper.

The general construct of a "revocable transaction" is any output like:

        I can take the money after some delay, OR
        You can take the money immediately if you know some secret.

In the paper the secret was a temp private key; Adam points out it could have been a pre-signed 2 of 2 transaction, but privkeys can be generated algorithmically so you can store millions of these trivially. But it's even easier to use hashes instead of privkeys, so that's what I did.

Quoted text (10 lines)
> Now that being the case, and as the transaction has another signature
> anyway it occurred to me that you should be able to use a chain of
> hash preimages to do it more efficiently again.
>
> Rusty generalised that to have a lower setup by using a short of
> hypercube multi-dimensional array of preimages.  (If you want 1million
> hash preimages, then you have to actually compute 1 million of them at
> setup.)  I had the idea that Rusty might've written a blog post on
> this preimage generalisation however I dont seem to be able to find it
> now.

It's on github, in the ccan/crypto/shachain module:

https://github.com/rustyrussell/ccan/blob/master/ccan/crypto/shachain/design.txt

Sorry if it's a bit opaque :(

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On 6 July 2015 at 16:41, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (6 lines)
>         To recap: each side maintains a commitment transaction with two
> outputs: one paying to self (with some delay), and the second paying to
> the other side.
>         To generate hash time-locked contracts (required for lightning
> to be a network), both commitment transactions get an additional output.
>

​That is, an additional output per HTLC, no?​

Quoted text (10 lines)
This output is spendable under four conditions:

1) Recipient knows the R value (funds go to recipient), OR
> 2) The HTLC has timed out (funds return to initiator), OR
> 3) The HTLC has been revoked (funds to go "non-cheating" side), OR
> 4) The Commit transaction has been revoked (funds to go "non-cheating"
> side)
>

> The last two failure modes are separate from each other, because HTLCs
> have different lifetimes from commit transactions.
>

​I'm not sure that makes sense? It seems to me there's two options:

a) HTLC resolved off-chain b) HTLC resolved on-chain ​ If (a) then both parties (nominally) agree which way the funds should go, and both the commitment and HTLC are expired simultaneously.

If (b) then either party decides to go to the blockchain, publishing tSheir side of the commitment, so both the commitment and HTLC are executed simultaneously.

So afaics the commit transaction might as well always revoked before or simultaneously with the HTLC.

In that case the conditions are just:

 - Recipient knows R value
 - Sender sees HTLC has timed out
 - Commit transaction has been revoked

Scripts for whatever it's worth:

HTLC from US to THEM:

Quoted text (1 line)
>

---------------------

Quoted text (1 line)
>
(R | Us_Commit_Revoke) Them | TIMEOUT Us DELAY
​===>​
​OP_HASH160 OP_DUP
   rhash OP_EQUAL OP_SWAP​
   revhash OP_EQUAL
OP_ADD
OP_IF
   OP_DUP OP_HASH160   thempubkeyhash   OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
   timeout  OP_CHECKTIMELOCKVERIFY OP_DROP
   OP_DUP OP_HASH160   mypubkeyhash     OP_EQUALVERIFY OP_CHECKSIG
​OP_ENDIF​

​Spend: R TheirHash TheirSig Revoke TheirHash TheirSig​ 0 OurHash OurSig

HTLC from THEM to US:

Quoted text (2 lines)
> ---------------------
>
​R Us​ DELAY | (Us_Commit_Revoke | TIMEOUT) Them
OP_HASH160 OP_DUP
rhash OP_EQUAL
OP_IF
    OP_DROP
    delay OP_CHECKSEQUENCEVERIFY
    OP_DUP OP_HASH160  mypubkeyhash  OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
    revhash OP_EQUAL
    OP_IF OP_ELSE
      timeout OP_CHECKTIMELOCKVERIFY OP_DROP
    OP_ENDIF
    OP_DUP OP_HASH160  theirpubkeyhash OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF

Cheers, aj

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (10 lines)
> On 6 July 2015 at 16:41, Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>>         To recap: each side maintains a commitment transaction with two
>> outputs: one paying to self (with some delay), and the second paying to
>> the other side.
>>         To generate hash time-locked contracts (required for lightning
>> to be a network), both commitment transactions get an additional output.
>>
>
> ​That is, an additional output per HTLC, no?​

Yep.

Quoted text (15 lines)
> This output is spendable under four conditions:
>
> 1) Recipient knows the R value (funds go to recipient), OR
>> 2) The HTLC has timed out (funds return to initiator), OR
>> 3) The HTLC has been revoked (funds to go "non-cheating" side), OR
>> 4) The Commit transaction has been revoked (funds to go "non-cheating"
>> side)
>>
>
>
>> The last two failure modes are separate from each other, because HTLCs
>> have different lifetimes from commit transactions.
>>
>
> ​I'm not sure that makes sense? It seems to me there's two options:

Yes, the paper removed the unneceesary HTLC revocation. It was a leftover from before HTLCs were reduced to a single output.

See:

        https://github.com/ElementsProject/lightning/blob/master/doc/deployable-lightning.pdf

(Still haven't written the code, so these scripts are untested. I'm hoping to finish the dual-anchor code soon though, then HTLCs are next).

Cheers, Rusty.

CJP in reply to Rusty Russell permalink original
Quoted text (3 lines)
> See:
>
>         https://github.com/ElementsProject/lightning/blob/master/doc/deployable-lightning.pdf

Sorry for replying to a very old e-mail, but I've finally had a really close look at this, and the way I see it, there could be a potential problem.

Suppose Eve (E) has two channels with Alice (A). She intends to steal funds from Alice. This is what she does:

She performs a payment to herself, routed through Alice with the two channels. When the funds are locked in the two channels, she is supposed to reveal the transaction R value as a payee, but she doesn't. So, the transaction times out on both channels, leading to a channel update on both: the HTLCs are removed, reverting back to the original situation.

The following actions primarily take place on the channel where Eve was on the RECEIVING side of the transaction. First, Eve spends all her coins on that channel, e.g. by sending them to herself on the other channel. This way, she doesn't risk losing them.

Next, on the channel where Eve was on the RECEIVING side of the transaction, Eve signs and broadcasts the version of the commit transaction that contained the HTLC. Of course, since revocation pre-images have been exchanged, Alice can immediately spend the HTLC, using either the HTLC-TIMEOUT & SIG-PAYER clause or the REVOCATION-E & SIG A clause. However, this is not guaranteed to work: Eve has the transaction R value, so Eve can *also* try to spend it, using the R-VALUE & SIG-PAYEE clause. In fact, since Eve knows sooner about the commit transaction than Alice, Eve is more likely to be the first to spend the HTLC, especially when Alice is offline for a moment (Eve can know that).

Now, if Eve succeeds in spending the HTLC, what can Alice do? She can of course claim her own balance, and using the revocation pre-image, claim Eve's balance in the commit transaction, but that way, she receives less than the total channel capacity, which is what she should have received.

On the other channel, Alice could try to perform the same trick on Eve that Eve performed on her, since Alice now has the transaction R value. However, that is not guaranteed to work. Worse: Eve could also have depleted her own balance on *that* channel, e.g. with a payment to herself through a third party. That way, Alice will never be able to get all the funds that belong to her, and Eve gets more than she deserves.

How to solve this issue? My first thought was that we should add an OP_CSV to the R-VALUE & SIG-PAYEE clause, but that would break its legitimate use. I don't see how this can be solved without having a two-transaction set-up, and hence something like SIGHASH_NOINPUT (or maybe SegWitness?). So it would have to be a bit more like "Poon-Dryja channels".

Maybe a "never completely deplete the channel" rule could be a work-around? You could define a maximum for the sum of all active HTLCs in one direction in a commit transaction, and require the other side to always have at least one or two times that maximum as remaining balance. Would that work?

BTW, apologies if this was already known. In that case, my question is: where can I find an updated design?

CJP

Rusty Russell in reply to CJP permalink original

CJP <cjp at ultimatestunts.nl> writes:

Quoted text (33 lines)
>> See:
>>
>>         https://github.com/ElementsProject/lightning/blob/master/doc/deployable-lightning.pdf
>
> Sorry for replying to a very old e-mail, but I've finally had a really
> close look at this, and the way I see it, there could be a potential
> problem.
>
> Suppose Eve (E) has two channels with Alice (A). She intends to steal
> funds from Alice. This is what she does:
>
> She performs a payment to herself, routed through Alice with the two
> channels. When the funds are locked in the two channels, she is supposed
> to reveal the transaction R value as a payee, but she doesn't. So, the
> transaction times out on both channels, leading to a channel update on
> both: the HTLCs are removed, reverting back to the original situation.
>
> The following actions primarily take place on the channel where Eve was
> on the RECEIVING side of the transaction. First, Eve spends all her
> coins on that channel, e.g. by sending them to herself on the other
> channel. This way, she doesn't risk losing them.
>
> Next, on the channel where Eve was on the RECEIVING side of the
> transaction, Eve signs and broadcasts the version of the commit
> transaction that contained the HTLC. Of course, since revocation
> pre-images have been exchanged, Alice can immediately spend the HTLC,
> using either the HTLC-TIMEOUT & SIG-PAYER clause or the REVOCATION-E &
> SIG A clause. However, this is not guaranteed to work: Eve has the
> transaction R value, so Eve can *also* try to spend it, using the
> R-VALUE & SIG-PAYEE clause. In fact, since Eve knows sooner about the
> commit transaction than Alice, Eve is more likely to be the first to
> spend the HTLC, especially when Alice is offline for a moment (Eve can
> know that).

For this reason, the rule is that all outputs to A in A's commit transaction must be delayed (via OP_CSV). This includes HTLC outputs.

Referring to Appendix A of the paper, under "HTLC Receiver Redeemscript" (since Alice offers Eve the HTLC, Eve is B):

HTLC Receiver Redeemscript
        OP_HASH160 OP_DUP Replace top element with two copies of its hash
        <R-HASH> OP_EQUAL B redeeming the contract, using R preimage?
        OP_IF
                <DELAY> OP_CHECKSEQUENCEVERIFY Delay gives A enough
                        time to use revocation if it has it.
                OP_2DROP Drop extra hash and delay from the stack
                <KEY-B> Pay to B
        OP_ELSE
                <COMMIT-REVOCATION-HASH> OP_EQUAL If the commit
                                        has been revoked.
                OP_NOTIF If not, you need to wait for timeout.
                        <HTLC-TIMEOUT> OP_CHECKLOCKTIMEVERIFY OP_DROP
                                       Ensure (absolute) time has passed.
                OP_ENDIF
                <KEY-A> Pay to A
        OP_ENDIF
        OP_CHECKSIG Verify A or B's signature is correct.

Here you can see the CSV delay on the "using R preimage" path.

I have a subtler suggestion, though: perhaps we should OP_CSV delay all the non-revocation paths in HTLCs and in the straight payment outputs. Otherwise, if you want to do a unilateral close, there's some game theory as you'd rather convince the other side to do it so your own funds aren't locked up. Delaying all non-revocation paths makes it an arbitrary choice.

I know Joseph will have some thoughts about that :)

Cheers, Rusty.

Anthony Towns in reply to Rusty Russell permalink original

On Tue, Feb 09, 2016 at 10:09:12AM +1030, Rusty Russell wrote:

Quoted text (3 lines)
> Otherwise, if you want to do a unilateral close, there's some game
> theory as you'd rather convince the other side to do it so your own
> funds aren't locked up.

I think the options are:

a) keep the channel open b) they close the channel unilaterally c) you close the channel unilaterally d) you both close the channel cooperatively

At any point, I believe the preferences are strictly: d > b > c

(b) is better than (c) because of the OP_CSV delay; and (d) is better than (b) if you can use a lower transaction fee than you use for your commitment transactions, or spend directly to a useful output address (opening up a new channel eg).

If you find yourself trying to convince the other person to do (b) to avoid doing (c) yourself, I think it's a dominating strategy to simply do (d) -- you prefer that over (b) anyway, and they will prefer it over (c).

With the current arrangement, I don't think your counterparty can realistically make any threats: "you'll close the channel? okay, that's better than me closing it!" and "you'll close the channel unilaterally? well, that's a lot worse for you as it is for me, so whatever".

With an OP_CSV on both sides of HTLCs, you can make a somewhat realistic threat: "if you don't pay me $x to do a cooperative close, I'll close unilaterally which will lock your funds up. sure you can close unilaterally yourself, but your funds will still be locked up that way too."

So changing seems like it would make things marginally worse, but no better, to me.

Cheers, aj

CJP in reply to Rusty Russell permalink original

Rusty Russell schreef op di 09-02-2016 om 10:09 [+1030]:

Quoted text (7 lines)
>
> For this reason, the rule is that all outputs to A in A's commit
> transaction must be delayed (via OP_CSV).  This includes HTLC outputs.
>
> Referring to Appendix A of the paper, under "HTLC Receiver Redeemscript"
> (since Alice offers Eve the HTLC, Eve is B):
> [..]

So, summarized, the "HTLC Receiver Redeemscript" is actually something like:

R-VALUE & OP_CSV & SIG-PAYEE OR HTLC-TIMEOUT & SIG-PAYER OR REVOCATION-B & SIG A

Now, I thought it wouldn't be OK to add an OP_CSV there, so let's see if I can exploit it.

Same situation: Alice and Eve, connected with two channels. Eve performs a payment to herself, routed through these two channels.

Now, Eve allows the transaction to succeed on the channel where she receives: Eve sends the R value to Alice, and Alice sends Eve a commit transaction update where the HTLC is removed and Eve receives the funds.

On the other channel, Alice sends the R value to Eve, but Eve does not send Alice a commit transaction update. In fact, Eve becomes completely unresponsive on this channel. This forces Alice to broadcast her commit transaction, containing the HTLC. She has to do this *before* the CLTV of the HTLC times out, or otherwise Eve can try to claim the HTLC funds.

Now, the question is: which times out first, Alice's CSV or Eve's CLTV? The one that times out first will be the first to be able to claim the funds. This *should* of course be Alice, so Alice's CSV should be the first to time out.

Let's assume the HTLC timeout is T0 + 1 day on the channel where Eve receives, T0 + 2 days on the channel where Eve sends. Eve wants to delay Alice's commit transaction as long as possible, so she sends the R value just before T0 + 1 day. That means Alice's CSV should have a delay *less than* one day. If the CSV delay is set to 0.5 day, that means Alice has a remaining 0.5 day time window in which her node must be up and running and connected to the Bitcoin network, to claim the HTLC funds.

So, I think it *is possible*. However, this leads to some uncomfortable time trade-offs. The CSV delay determines how often your node must be up and running, to catch the use of revoked commit transactions. Longer is better: I'd be much more comfortable with 30 days than with 1 day. The HTLC timeout increment determines how long funds can be locked up and payment status can stay undetermined (worst-case); it adds up over the entire route. For this, shorter is better: 1 day would be much better than 30 days.

Even if you assume "always-on, always connected" nodes, e.g. cell phones, service providers and home servers, you have to take into account that systems can fail, and manual intervention may be needed to restore them. The downtime / DoS attack -> theft escalation is something we don't want to happen.

CJP

Rusty Russell in reply to Anthony Towns permalink original

Anthony Towns <aj at erisian.com.au> writes:

Quoted text (38 lines)
> On Tue, Feb 09, 2016 at 10:09:12AM +1030, Rusty Russell wrote:
>> Otherwise, if you want to do a unilateral close, there's some game
>> theory as you'd rather convince the other side to do it so your own
>> funds aren't locked up.
>
> I think the options are:
>
>  a) keep the channel open
>  b) they close the channel unilaterally
>  c) you close the channel unilaterally
>  d) you both close the channel cooperatively
>
> At any point, I believe the preferences are strictly: d > b > c
>
> (b) is better than (c) because of the OP_CSV delay; and (d) is better
> than (b) if you can use a lower transaction fee than you use for your
> commitment transactions, or spend directly to a useful output address
> (opening up a new channel eg).
>
> If you find yourself trying to convince the other person to do (b)
> to avoid doing (c) yourself, I think it's a dominating strategy to
> simply do (d) -- you prefer that over (b) anyway, and they will prefer
> it over (c).
>
> With the current arrangement, I don't think your counterparty can
> realistically make any threats: "you'll close the channel? okay,
> that's better than me closing it!" and "you'll close the channel
> unilaterally? well, that's a lot worse for you as it is for me,
> so whatever".
>
> With an OP_CSV on both sides of HTLCs, you can make a somewhat
> realistic threat: "if you don't pay me $x to do a cooperative close,
> I'll close unilaterally which will lock your funds up. sure you can
> close unilaterally yourself, but your funds will still be locked up that
> way too."
>
> So changing seems like it would make things marginally worse, but no
> better, to me.

Fair point. The issue will improve when we have close with outstanding HTLCs. Meanwhile it disturbs me that the party which goes offline pays the least penalty; their counterparties have their funds tied though.

Cheers, Rusty.

Rusty Russell in reply to CJP permalink original

CJP <cjp at ultimatestunts.nl> writes:

Quoted text (17 lines)
> Rusty Russell schreef op di 09-02-2016 om 10:09 [+1030]:
>>
>> For this reason, the rule is that all outputs to A in A's commit
>> transaction must be delayed (via OP_CSV).  This includes HTLC outputs.
>>
>> Referring to Appendix A of the paper, under "HTLC Receiver Redeemscript"
>> (since Alice offers Eve the HTLC, Eve is B):
>> [..]
>
> So, summarized, the "HTLC Receiver Redeemscript" is actually something
> like:
>
> R-VALUE & OP_CSV & SIG-PAYEE
> OR
> HTLC-TIMEOUT & SIG-PAYER
> OR
> REVOCATION-B & SIG A

Yes.

Quoted text (20 lines)
> Now, I thought it wouldn't be OK to add an OP_CSV there, so let's see if
> I can exploit it.
>
> Same situation: Alice and Eve, connected with two channels. Eve performs
> a payment to herself, routed through these two channels.
>
> Now, Eve allows the transaction to succeed on the channel where she
> receives: Eve sends the R value to Alice, and Alice sends Eve a commit
> transaction update where the HTLC is removed and Eve receives the funds.
>
> On the other channel, Alice sends the R value to Eve, but Eve does not
> send Alice a commit transaction update. In fact, Eve becomes completely
> unresponsive on this channel. This forces Alice to broadcast her commit
> transaction, containing the HTLC. She has to do this *before* the CLTV
> of the HTLC times out, or otherwise Eve can try to claim the HTLC funds.
>
> Now, the question is: which times out first, Alice's CSV or Eve's CLTV?
> The one that times out first will be the first to be able to claim the
> funds. This *should* of course be Alice, so Alice's CSV should be the
> first to time out.

Yes, unfortunately they're intertwined. I was sure we'd discussed this before, and I found it.

https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2015/breach-of-contract#m-0cc42820

We keep rediscovering this problem so I need to document it somewhere.

Quoted text (16 lines)
> Let's assume the HTLC timeout is T0 + 1 day on the channel where Eve
> receives, T0 + 2 days on the channel where Eve sends. Eve wants to delay
> Alice's commit transaction as long as possible, so she sends the R value
> just before T0 + 1 day. That means Alice's CSV should have a delay *less
> than* one day. If the CSV delay is set to 0.5 day, that means Alice has
> a remaining 0.5 day time window in which her node must be up and running
> and connected to the Bitcoin network, to claim the HTLC funds.
>
> So, I think it *is possible*. However, this leads to some uncomfortable
> time trade-offs. The CSV delay determines how often your node must be up
> and running, to catch the use of revoked commit transactions. Longer is
> better: I'd be much more comfortable with 30 days than with 1 day. The
> HTLC timeout increment determines how long funds can be locked up and
> payment status can stay undetermined (worst-case); it adds up over the
> entire route. For this, shorter is better: 1 day would be much better
> than 30 days.

Yes. If A insists that B use a 30 day CSV, A can't ask B to accept an HTLC with less than (say) 31 days lifetime.

In general, an HTLC timeout must comfortably exceed the highest CSV delay on the route.

It sucks, but at least it's not the sum of CSV delays. Keeping CSV time down is the motivation for outsourcing the enforcement (which I think SegWit gives us the ability to do, because the txids of the commit transactions won't be malleable).

Quoted text (5 lines)
> Even if you assume "always-on, always connected" nodes, e.g. cell
> phones, service providers and home servers, you have to take into
> account that systems can fail, and manual intervention may be needed to
> restore them. The downtime / DoS attack -> theft escalation is something
> we don't want to happen.

Indeed! c-lightning requests a CSV timeout of 1 day, allows requests of up to 2 days (comment says 3, but it's wrong). It only allows HTLCS of up to 5 days in the future, but it wants a 6 hour window minimum[1].

If we want to allow CSV timeouts of 5 days[2], HTLC timeout decrements of up to 1 day per node over 20 hops, that means we need to allow HTLC timeouts of 26 days :(

We can play with those numbers a bit (20 hops is a bit much, 1 day seems excessive given you can always drop commit txs to blockchain if DDoS is severe), but I think the per-hop decrement will dominate.

Cheers,
Rusty.
[1] It should be "CSV + 6 hours", this is the bug.
[2] A 4-day long weekend plus one day of frantic panic; this was the
    default timeout in the Linux NAT code.
Anthony Towns in reply to CJP permalink original

On Tue, Feb 09, 2016 at 08:48:59PM +0100, CJP wrote:

Reference: https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2015/return-to-the-layered-commit-transactions#m-0132bf34

Quoted text (2 lines)
> Same situation: Alice and Eve, connected with two channels. Eve performs
> a payment to herself, routed through these two channels.

Okay, so the channels are:

   Eve <-> Alice
   Evelyn <-> Alice  (Eve's alias)

with the delay (OP_CSV) parameter for both channels set at the value "D". In addition, Alice's commitments include a fee that she expects will result in confirmation in less than a delay of "F", and she's confident that a transaction can be spent so long as its been in the blockchain for "N" units of time.

The HTLCs are:

   Eve pays Alice $X on receipt of R, CLTV at time T
   Alice pays Evelyn $X on receipt of R, CLTV at time T'

Per the mail above, when forwarding the HTLC, Alice calculates T' such that

   T' <= T - D - 2*F - N

and

   T' >= now + D + F
Quoted text (3 lines)
> Now, Eve allows the transaction to succeed on the channel where she
> receives: Eve sends the R value to Alice, and Alice sends Eve a commit
> transaction update where the HTLC is removed and Eve receives the funds.

Assuming that all works out, and Evelyn reveals R and collects $X straight away, the second HTLC is dealt with, and things are fine so far.

Quoted text (3 lines)
> On the other channel, Alice sends the R value to Eve, but Eve does not
> send Alice a commit transaction update. In fact, Eve becomes completely
> unresponsive on this channel.

At this point Alice should start the timer ticking. If she doesn't get an ack from Eve prior to T-D-F, she must go to the blockchain. Suppose she leaves it to the last minute.

Quoted text (3 lines)
> This forces Alice to broadcast her commit
> transaction, containing the HTLC. She has to do this *before* the CLTV
> of the HTLC times out, or otherwise Eve can try to claim the HTLC funds.

So she publishes her commitment transaction at T-D-F. The transaction gets mined before "F" time units transpire, so the time is now T-D-x. After an additional "D" time units, the time is T-x, at which point Alice can spend the transaction, but Eve cannot.

Quoted text (4 lines)
> Now, the question is: which times out first, Alice's CSV or Eve's CLTV?
> The one that times out first will be the first to be able to claim the
> funds. This *should* of course be Alice, so Alice's CSV should be the
> first to time out.

So I think that answers the question.

Quoted text (2 lines)
> Let's assume the HTLC timeout is T0 + 1 day on the channel where Eve
> receives, T0 + 2 days on the channel where Eve sends.

In that case, from the above inequalities:

   D + 2*F + N <= 1 day
Quoted text (3 lines)
> Eve wants to delay
> Alice's commit transaction as long as possible, so she sends the R value
> just before T0 + 1 day.

That doesn't work: if the forwarded HTLC isn't resolved by T'-D-F she has to close the channel on the blockchain to be safe. If her fees are high enough, D+F could perhaps be as low as an hour, though, so T0+23 hours might be reasonable.

Quoted text (4 lines)
> That means Alice's CSV should have a delay *less
> than* one day. If the CSV delay is set to 0.5 day, that means Alice has
> a remaining 0.5 day time window in which her node must be up and running
> and connected to the Bitcoin network, to claim the HTLC funds.

Yes, rearranging the inequality above gives D <= (T-T') - 2*F - N, so it has to be quite a bit smaller than the delta between timeouts across a given node.

Cheers, aj

CJP in reply to Rusty Russell permalink original
Quoted text (6 lines)
> Yes, unfortunately they're intertwined.  I was sure we'd discussed
> this before, and I found it.
>
> https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2015/breach-of-contract#m-0cc42820
>
> We keep rediscovering this problem so I need to document it somewhere.

Ah, I see. I think, at the time that message was sent, I didn't understand it yet. Now that I did the thinking myself, I guess I do.

Cool to see that the alternative solution (never completely depleting ones channel balance, to have some 'collateral') was also mentioned. I'm not sure yet which is better; if there is a demand for it, both types could be implemented and used next to each other in Amiko Pay.

CJP