get10101 / 10101-PoC

The world needs more Lightning!
MIT License
17 stars 3 forks source link

Combine DLCs and Lightning Channels #8

Closed holzeis closed 1 year ago

holzeis commented 1 year ago

Goal

We want to build a wallet which speaks lightning and is able to do DLCs.

Possible approaches

Virtual channels

Probably the most complex solution is to have 2 virtual channels. One speaking lightning, the other one speaking DLCs. E.g. something like this:

Diagram ``` mermaid flowchart LR A1((Alice)) B1((Bob)) txf[tx_fund] txc_outer[tx_commit] txs[tx_settle] txcis[tx_commit_itchysats] txcln[tx_commit_ln] or{or} and{and} A1 -- fund --> txf B1 -- fund --> txf txf --> or or --> txs or --> txc_outer txc_outer --> and and --> txcis and --> txcln subgraph ItchySats txcet_is[tx_cetN] txr_is[tx_refund] txp_is[tx_punish] or_is{or} txcis --> or_is or_is --> txp_is or_is --> txcet_is or_is --> txr_is end subgraph LN htlc1((htlc1)) htlc2((htlc2)) htlcN((htlcN)) txp_ln[tx_punish] txcln --> txp_ln txcln --> htlc1 txcln --> htlc2 txcln --> htlcN end ```

Advantages:

Disadvantage:

Maia speak LN

We could implement the needed bolts into https://github.com/comit-network/maia/

Advantage:

Disadvantage:

LN speak DLC

We could add the DLC feature to a lightning implementation (e.g. use LDK and extend it). The it's ok to be odd would allow us to add features to our client without breaking compatibility with other LN implementations.

Advantage:

Disadvantage:

holzeis commented 1 year ago

blocked by https://github.com/itchysats/10101/issues/10

bonomat commented 1 year ago

I propose to not go for this approach. It would be better to either make https://github.com/comit-network/maia/ speak lightning or go for a virtual channel solution.

bonomat commented 1 year ago

I propose to not go for this approach.

It would be better to either make https://github.com/comit-network/maia/ speak lightning or go for a virtual channel solution.

Note: if we do this, we won't be able to open channels to other Lightning Nodes. If we want to support this, we will need to speak native lightning and add DLCs to a normal lightning channel.

bonomat commented 1 year ago

@luckysori : what do you think of going for option 3: make LN speak DLCs. There was an attempt in the past which never made it into the library. The PR is completely outdated but it doesn't look to complex. We could then build a LN wallet from LDK with this additional feature.

luckysori commented 1 year ago

what do you think of going for option 3: make LN speak DLCs. There was an attempt in the past which never made it into the library. The PR is completely outdated but it doesn't look to complex. We could then build a LN wallet from LDK with this additional feature.

My intuition tells me that option 3 is the hardest because we would have to integrate it into an existing codebase which we (I) don't understand. Potentially it is also the one that would reap the most benefits.

I think I have a rough idea of how each option would be implemented, but I do not fully understand how they compare in terms of functionality. Could you expand that information in the original post?

As an example of my confusion: I seem to remember that option 1 included making the fund transaction "look like" a Lightning funding transaction. But then the disadvantages talk about incompatibility with pre-existing Lightning nodes. What would be the point of that solution then if we cannot use it with other Lightning nodes in any way?

bonomat commented 1 year ago

@luckysori : I've added a few more words to the initial description.

My intuition tells me that option 3 is the hardest because we would have to integrate it into an existing codebase which we (I) don't understand. Potentially it is also the one that would reap the most benefits.

I think we should give it a try. I had a look at their source code and it looks a bit scary to be honest 😬 The thing I'm most afraid of is keeping our fork up2date. I think this is though the best solution longterm.

As an example of my confusion: I seem to remember that option 1 included making the fund transaction "look like" a Lightning funding transaction. But then the disadvantages talk about incompatibility with pre-existing Lightning nodes. What would be the point of that solution then if we cannot use it with other Lightning nodes in any way?

I've updated the description: we are able to receive payments from any LN node but we can only open channels to nodes with our implementation because of the custom transaction structure.

luckysori commented 1 year ago

The thing I'm most afraid of is keeping our fork up2date. I think this is though the best solution longterm.

Perhaps we shouldn't try to build something that we will need to maintain longterm. Maybe it's sufficient if we treat the protocol as a PoC, aiming to replace it with a more sustainable integration down the line.


@bonomat: I understand that for DLCs on Lightning to work we also need to implement dual-funded channels. Would it make sense to start with that? This is not yet implemented in rust-lightning, primarily because the BOLT hasn't merged yet.

luckysori commented 1 year ago

Meeting minutes of the discussion about Lightning DLC protocol

Attendants: @bonomat and @luckysori.

1. Choosing a protocol

Decision

We have settled on Option 3 aka LN speak DLC.

Reasoning

It is the only protocol out of the 3 outlined which would allow other Lightning Nodes not written by us to connect to our nodes and do all regular Lightning things. We thought it was insufficient to just be able to receive Lightning payments.

This approach is also the one that best aligns with our vision: extending the Lightning Network to support DLCs. We are not alone in this vision, as this is a topic that is frequently discussed among people involved in dlcspecs.

2. Approach

Extending the Rust LDK

We intend to extend the Rust LDK library to support DLC protocols including:

In the context of CFD trading, we purposefully leave out the rollover protocol for the time being because:

It is important to highlight that we do not expect to write code that will be maintainable. We intend to fork the repository and relatively quickly extend the codebase to support DLCs. We think this is acceptable because we understand that it would be okay to rewrite most of the code that we produce for the Legend of Lightning tournament.

The Rust LDK is a complex project and it is challenging to upstream new features. There have been previous efforts to add DLC support to it and very likely in the medium term we could help with that (after the Legends of Lightning tournament).

Rough plan

  1. Spend 2 iterations (weeks) hacking on our fork of the Rust LDK to add the DLC protocols mentioned above. Prove that it works in a Rust test.
    • As a very first sub-step, we are gonna work on adding a protocol which just adds an arbitrary output (not a HTLC or a DLC).
  2. Write a blog post for Legends of Lightning about our intermediate work.
  3. If we are successful with step 1, we would now be ready to build our own Lightning Node using our own extensions.

A note on dependencies

We think we need to be careful with using external dependencies because:

luckysori commented 1 year ago

I've generated a couple of transaction diagrams to illustrate what we are building:

Add a DLC to a Lightning channel

Add DLC LN protocol

Collaboratively settle a Lightning DLC into the channel

Collaborative settlement DLC LN protocol

Notes

No need to draw anything for non-collaborative settlement, because it just entails unilaterally publishing the transactions displayed in the first diagram, closing the overall Lightning channel in the process.

luckysori commented 1 year ago

Punishing the publication of revoked commitment transactions

We are familiar with this concept because we had to deal with a similar problem in itchysats. The difference lies in the fact that itchysats employed Generalized Bitcoin-Compatible Channels to create perpetual CFDs, whereas Lightning uses Lightning (duh) channels to create payment channels.

Since our approach is to effectively add new features to a full-fledged Lightning node, it appears to be necessary to conform to the ways in which Lightning deals with revoked state. In Lightning, when the state of the channel is updated the two parties also exchange revocation secret keys. The corresponding revocation public keys enable punish spend paths on particular outputs on the transaction ("all outputs that return funds to the owner of the commitment transaction"). For example, in the following diagram taken from the previous comment

image

the output that pays to Bob is called RSMC B, because it uses a Revocable Sequence Maturity Contract. This contract allows Bob to spend it after a timeout, if Alice doesn't use the revocation path first to punish Bob.

Similarly, HTLC outputs that pay to the owner of the commit transaction are also protected by a revocation key, although this is done in a "second-stage HTLC transaction".

This is all to say that our newly introduced DLC output will also need to be spendable by the counterparty of the owner of the commit transaction, if they provide the necessary revocation secret key. This just means we will have to design the DLC script to include that path. This is similar to what we already do in itchysats, but the format is different.

The nice thing is that, once we introduce the new CFD rollover protocol, we will already be covered for punisment since a rollover will just be replacing the DLC output on the commitment transaction with a new one, revoking the previous commitment transaction by sharing the revocation key (like any other Lightning channel update).

A note on terminology

Lightning calls punishment transactions penalty transactions. We should adopt this terminology when working with Lightning.

holzeis commented 1 year ago

The output of this ticket should go into https://github.com/itchysats/10101/issues/14.

ariard commented 1 year ago

what do you think of going for option 3: make LN speak DLCs. There was an attempt https://github.com/lightningdevkit/rust-lightning/pull/619 which never made it into the library. The PR is completely outdated but it doesn't look to complex. We could then build a LN wallet from LDK with this additional feature.

Seen this from the dual-funding issue. We're still thinking to support DLC in LDK and we had recent discussions about this subject at our last developer meetings, with a floating timeline of 2023. To be honest, in light of our historical progresses on complex features and current engineering resources, I don't think we'll get on it before second part of 2023, and realistically it might take 18 months to complete a "beta" support. Further, I think we'll go for long-term maintainability/high robustness and we aim to offer a generic API/custom scripts, not only to have DLC but also think like PeerSwap, for any type of L3 off-chain contracts.

All that said, I still believe LDK is an awesome choice, especially if you're looking for high-grade mobile support, it just mean you'll have to maintain a fork for a while. Ideally, valuable piece of codes could be backported in both directions. The DLC code could live in its own repository/crate, still under the "lightningdevkit" or "rust-bitcoin" orgs as they have been efforts already. There is also the long-term fact if you would like to benefit from advanced, low-level features we're currently working on with LDK, such as VLS, FROST, time-sensitive fee-bumping, high availability architecture.

IIRC, we have a "dlc" chan on our Discord. Feel free to poke for more feedbacks on if LDK could match your applications requirements :)

bonomat commented 1 year ago

Hi @ariard , thanks for your input. We indeed think that rust-lightning is the best choice to go for. Mostly because it's written in rust :D

I like the idea of having a more generic API. In fact, our first attempt will be to only add another output to the commit transaction and potentially leave the DLC management on application layer, so I think this goes pretty much into this direction.

We have started with our efforts in our fork here: https://github.com/itchysats/rust-lightning/tree/dlcs. Our first approach is going to be more of a PoC style and will then go for a feedback round to see how to do it properly with the intention to contribute it upstream to rust-lightning.

p.s. we are aware of https://github.com/p2pderivatives/rust-dlc and have our own implementation of custom dlc-channels here https://github.com/comit-network/maia/. They have been in production for almost 1 year now with https://github.com/itchysats/itchysats/. Give it a try if you have the chance ;)

bonomat commented 1 year ago

Note: we've enabled issues on our fork here https://github.com/itchysats/rust-lightning/issues/. I'll close this ticket in favor of the other repository.