Skip to content

Commit 652ff81

Browse files
authored
Merge pull request #8622 from lightningnetwork/aux-funding
[3/5]: multi: add new AuxFundingController for custom external funding flows
2 parents 5d53431 + a350ccd commit 652ff81

File tree

18 files changed

+1064
-83
lines changed

18 files changed

+1064
-83
lines changed

channeldb/models/channel_edge_info.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/btcsuite/btcd/btcutil"
99
"github.com/btcsuite/btcd/chaincfg/chainhash"
1010
"github.com/btcsuite/btcd/wire"
11+
"github.com/lightningnetwork/lnd/fn"
1112
)
1213

1314
// ChannelEdgeInfo represents a fully authenticated channel along with all its
@@ -62,6 +63,11 @@ type ChannelEdgeInfo struct {
6263
// the value output in the outpoint that created this channel.
6364
Capacity btcutil.Amount
6465

66+
// TapscriptRoot is the optional Merkle root of the tapscript tree if
67+
// this channel is a taproot channel that also commits to a tapscript
68+
// tree (custom channel).
69+
TapscriptRoot fn.Option[chainhash.Hash]
70+
6571
// ExtraOpaqueData is the set of data that was appended to this
6672
// message, some of which we may not actually know how to iterate or
6773
// parse. By holding onto this data, we ensure that we're able to

config_builder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/lightningnetwork/lnd/channeldb"
3636
"github.com/lightningnetwork/lnd/clock"
3737
"github.com/lightningnetwork/lnd/fn"
38+
"github.com/lightningnetwork/lnd/funding"
3839
"github.com/lightningnetwork/lnd/invoices"
3940
"github.com/lightningnetwork/lnd/keychain"
4041
"github.com/lightningnetwork/lnd/kvdb"
@@ -44,6 +45,7 @@ import (
4445
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
4546
"github.com/lightningnetwork/lnd/lnwallet/rpcwallet"
4647
"github.com/lightningnetwork/lnd/macaroons"
48+
"github.com/lightningnetwork/lnd/protofsm"
4749
"github.com/lightningnetwork/lnd/routing"
4850
"github.com/lightningnetwork/lnd/rpcperms"
4951
"github.com/lightningnetwork/lnd/signal"
@@ -162,6 +164,16 @@ type AuxComponents struct {
162164
// TrafficShaper is an optional traffic shaper that can be used to
163165
// control the outgoing channel of a payment.
164166
TrafficShaper fn.Option[routing.TlvTrafficShaper]
167+
168+
// MsgRouter is an optional message router that if set will be used in
169+
// place of a new blank default message router.
170+
MsgRouter fn.Option[protofsm.MsgRouter]
171+
172+
// AuxFundingController is an optional controller that can be used to
173+
// modify the way we handle certain custom chanenl types. It's also
174+
// able to automatically handle new custom protocol messages related to
175+
// the funding process.
176+
AuxFundingController fn.Option[funding.AuxFundingController]
165177
}
166178

167179
// DefaultWalletImpl is the default implementation of our normal, btcwallet

discovery/gossiper.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightningnetwork/lnd/chainntnfs"
2121
"github.com/lightningnetwork/lnd/channeldb"
2222
"github.com/lightningnetwork/lnd/channeldb/models"
23+
"github.com/lightningnetwork/lnd/fn"
2324
"github.com/lightningnetwork/lnd/keychain"
2425
"github.com/lightningnetwork/lnd/kvdb"
2526
"github.com/lightningnetwork/lnd/lnpeer"
@@ -82,9 +83,10 @@ var (
8283
// can provide that serve useful when processing a specific network
8384
// announcement.
8485
type optionalMsgFields struct {
85-
capacity *btcutil.Amount
86-
channelPoint *wire.OutPoint
87-
remoteAlias *lnwire.ShortChannelID
86+
capacity *btcutil.Amount
87+
channelPoint *wire.OutPoint
88+
remoteAlias *lnwire.ShortChannelID
89+
tapscriptRoot fn.Option[chainhash.Hash]
8890
}
8991

9092
// apply applies the optional fields within the functional options.
@@ -115,6 +117,14 @@ func ChannelPoint(op wire.OutPoint) OptionalMsgField {
115117
}
116118
}
117119

120+
// TapscriptRoot is an optional field that lets the gossiper know of the root of
121+
// the tapscript tree for a custom channel.
122+
func TapscriptRoot(root fn.Option[chainhash.Hash]) OptionalMsgField {
123+
return func(f *optionalMsgFields) {
124+
f.tapscriptRoot = root
125+
}
126+
}
127+
118128
// RemoteAlias is an optional field that lets the gossiper know that a locally
119129
// sent channel update is actually an update for the peer that should replace
120130
// the ShortChannelID field with the remote's alias. This is only used for
@@ -2513,6 +2523,9 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
25132523
cp := *nMsg.optionalMsgFields.channelPoint
25142524
edge.ChannelPoint = cp
25152525
}
2526+
2527+
// Optional tapscript root for custom channels.
2528+
edge.TapscriptRoot = nMsg.optionalMsgFields.tapscriptRoot
25162529
}
25172530

25182531
log.Debugf("Adding edge for short_chan_id: %v",

funding/aux_funding.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package funding
2+
3+
import (
4+
"github.com/btcsuite/btcd/chaincfg/chainhash"
5+
"github.com/lightningnetwork/lnd/channeldb"
6+
"github.com/lightningnetwork/lnd/fn"
7+
"github.com/lightningnetwork/lnd/lnwallet"
8+
"github.com/lightningnetwork/lnd/protofsm"
9+
)
10+
11+
// AuxFundingController permits the implementation of the funding of custom
12+
// channels types. The controller serves as a MsgEndpoint which allows it to
13+
// intercept custom messages, or even the regular funding messages. The
14+
// controller might also pass along an aux funding desc based on an existing
15+
// pending channel ID.
16+
type AuxFundingController interface {
17+
// MsgEndpoint is the embedded interface that signals that the funding
18+
// controller is also a message endpoint. This'll allow it to handle
19+
// custom messages specific to the funding type.
20+
protofsm.MsgEndpoint
21+
22+
// DescFromPendingChanID takes a pending channel ID, that may already be
23+
// known due to prior custom channel messages, and maybe returns an aux
24+
// funding desc which can be used to modify how a channel is funded.
25+
DescFromPendingChanID(pid PendingChanID,
26+
openChan *channeldb.OpenChannel,
27+
localKeyRing, remoteKeyRing lnwallet.CommitmentKeyRing,
28+
initiator bool) (fn.Option[lnwallet.AuxFundingDesc], error)
29+
30+
// DeriveTapscriptRoot takes a pending channel ID and maybe returns a
31+
// tapscript root that should be used when creating any MuSig2 sessions
32+
// for a channel.
33+
DeriveTapscriptRoot(PendingChanID) (fn.Option[chainhash.Hash], error)
34+
35+
// ChannelReady is called when a channel has been fully opened (multiple
36+
// confirmations) and is ready to be used. This can be used to perform
37+
// any final setup or cleanup.
38+
ChannelReady(openChan *channeldb.OpenChannel) error
39+
40+
// ChannelFinalized is called when a channel has been fully finalized.
41+
// In this state, we've received the commitment sig from the remote
42+
// party, so we are safe to broadcast the funding transaction.
43+
ChannelFinalized(PendingChanID) error
44+
}
45+
46+
// descFromPendingChanID takes a pending channel ID, that may already be known
47+
// due to prior custom channel messages, and maybe returns an aux funding desc
48+
// which can be used to modify how a channel is funded.
49+
func descFromPendingChanID(controller fn.Option[AuxFundingController],
50+
chanID PendingChanID, openChan *channeldb.OpenChannel,
51+
localKeyRing, remoteKeyRing lnwallet.CommitmentKeyRing,
52+
initiator bool) (fn.Option[lnwallet.AuxFundingDesc], error) {
53+
54+
var result fn.Option[lnwallet.AuxFundingDesc]
55+
mapErr := fn.MapOptionZ(controller, func(c AuxFundingController) error {
56+
var err error
57+
result, err = c.DescFromPendingChanID(
58+
chanID, openChan, localKeyRing, remoteKeyRing,
59+
initiator,
60+
)
61+
62+
return err
63+
})
64+
65+
return result, mapErr
66+
}
67+
68+
// deriveTapscriptRoot takes a pending channel ID and maybe returns a
69+
// tapscript root that should be used when creating any MuSig2 sessions for a
70+
// channel.
71+
func deriveTapscriptRoot(controller fn.Option[AuxFundingController],
72+
chanID PendingChanID) (fn.Option[chainhash.Hash], error) {
73+
74+
var result fn.Option[chainhash.Hash]
75+
mapErr := fn.MapOptionZ(controller, func(c AuxFundingController) error {
76+
var err error
77+
result, err = c.DeriveTapscriptRoot(chanID)
78+
return err
79+
})
80+
81+
return result, mapErr
82+
}

0 commit comments

Comments
 (0)