Skip to content

Commit cdad5d9

Browse files
authored
Merge pull request #9072 from lightningnetwork/extract-part3-from-staging-branch
[custom channels 3/5]: Extract PART3 from mega staging branch
2 parents 13a7bec + 52e50d8 commit cdad5d9

40 files changed

+1926
-162
lines changed

chainreg/chainregistry.go

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type Config struct {
6868
// leaves for certain custom channel types.
6969
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
7070

71+
// AuxSigner is an optional signer that can be used to sign auxiliary
72+
// leaves for certain custom channel types.
73+
AuxSigner fn.Option[lnwallet.AuxSigner]
74+
7175
// BlockCache is the main cache for storing block information.
7276
BlockCache *blockcache.BlockCache
7377

channeldb/models/channel_edge_info.go

+6
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/lightningnetwork/lnd/channeldb"
3535
"github.com/lightningnetwork/lnd/clock"
3636
"github.com/lightningnetwork/lnd/fn"
37+
"github.com/lightningnetwork/lnd/funding"
3738
"github.com/lightningnetwork/lnd/invoices"
3839
"github.com/lightningnetwork/lnd/keychain"
3940
"github.com/lightningnetwork/lnd/kvdb"
@@ -167,6 +168,20 @@ type AuxComponents struct {
167168
// MsgRouter is an optional message router that if set will be used in
168169
// place of a new blank default message router.
169170
MsgRouter fn.Option[msgmux.Router]
171+
172+
// AuxFundingController is an optional controller that can be used to
173+
// modify the way we handle certain custom channel 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]
177+
178+
// AuxSigner is an optional signer that can be used to sign auxiliary
179+
// leaves for certain custom channel types.
180+
AuxSigner fn.Option[lnwallet.AuxSigner]
181+
182+
// AuxDataParser is an optional data parser that can be used to parse
183+
// auxiliary data for certain custom channel types.
184+
AuxDataParser fn.Option[AuxDataParser]
170185
}
171186

172187
// DefaultWalletImpl is the default implementation of our normal, btcwallet
@@ -573,6 +588,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
573588
ChanStateDB: dbs.ChanStateDB.ChannelStateDB(),
574589
NeutrinoCS: neutrinoCS,
575590
AuxLeafStore: aux.AuxLeafStore,
591+
AuxSigner: aux.AuxSigner,
576592
ActiveNetParams: d.cfg.ActiveNetParams,
577593
FeeURL: d.cfg.FeeURL,
578594
Fee: &lncfg.Fee{
@@ -730,6 +746,7 @@ func (d *DefaultWalletImpl) BuildChainControl(
730746
NetParams: *walletConfig.NetParams,
731747
CoinSelectionStrategy: walletConfig.CoinSelectionStrategy,
732748
AuxLeafStore: partialChainControl.Cfg.AuxLeafStore,
749+
AuxSigner: partialChainControl.Cfg.AuxSigner,
733750
}
734751

735752
// The broadcast is already always active for neutrino nodes, so we
@@ -912,10 +929,6 @@ type DatabaseInstances struct {
912929
// for native SQL queries for tables that already support it. This may
913930
// be nil if the use-native-sql flag was not set.
914931
NativeSQLStore *sqldb.BaseDB
915-
916-
// AuxLeafStore is an optional data source that can be used by custom
917-
// channels to fetch+store various data.
918-
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
919932
}
920933

921934
// DefaultDatabaseBuilder is a type that builds the default database backends

contractcourt/breach_arbitrator_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -2360,9 +2360,12 @@ func createInitChannels(t *testing.T) (
23602360
)
23612361
bobSigner := input.NewMockSigner([]*btcec.PrivateKey{bobKeyPriv}, nil)
23622362

2363+
signerMock := lnwallet.NewDefaultAuxSignerMock(t)
23632364
alicePool := lnwallet.NewSigPool(1, aliceSigner)
23642365
channelAlice, err := lnwallet.NewLightningChannel(
23652366
aliceSigner, aliceChannelState, alicePool,
2367+
lnwallet.WithLeafStore(&lnwallet.MockAuxLeafStore{}),
2368+
lnwallet.WithAuxSigner(signerMock),
23662369
)
23672370
if err != nil {
23682371
return nil, nil, err
@@ -2375,6 +2378,8 @@ func createInitChannels(t *testing.T) (
23752378
bobPool := lnwallet.NewSigPool(1, bobSigner)
23762379
channelBob, err := lnwallet.NewLightningChannel(
23772380
bobSigner, bobChannelState, bobPool,
2381+
lnwallet.WithLeafStore(&lnwallet.MockAuxLeafStore{}),
2382+
lnwallet.WithAuxSigner(signerMock),
23782383
)
23792384
if err != nil {
23802385
return nil, nil, err

contractcourt/chain_arbitrator.go

+10
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ type ChainArbitratorConfig struct {
221221
// AuxLeafStore is an optional store that can be used to store auxiliary
222222
// leaves for certain custom channel types.
223223
AuxLeafStore fn.Option[lnwallet.AuxLeafStore]
224+
225+
// AuxSigner is an optional signer that can be used to sign auxiliary
226+
// leaves for certain custom channel types.
227+
AuxSigner fn.Option[lnwallet.AuxSigner]
224228
}
225229

226230
// ChainArbitrator is a sub-system that oversees the on-chain resolution of all
@@ -307,6 +311,9 @@ func (a *arbChannel) NewAnchorResolutions() (*lnwallet.AnchorResolutions,
307311
a.c.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
308312
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
309313
})
314+
a.c.cfg.AuxSigner.WhenSome(func(s lnwallet.AuxSigner) {
315+
chanOpts = append(chanOpts, lnwallet.WithAuxSigner(s))
316+
})
310317

311318
chanMachine, err := lnwallet.NewLightningChannel(
312319
a.c.cfg.Signer, channel, nil, chanOpts...,
@@ -357,6 +364,9 @@ func (a *arbChannel) ForceCloseChan() (*lnwallet.LocalForceCloseSummary, error)
357364
a.c.cfg.AuxLeafStore.WhenSome(func(s lnwallet.AuxLeafStore) {
358365
chanOpts = append(chanOpts, lnwallet.WithLeafStore(s))
359366
})
367+
a.c.cfg.AuxSigner.WhenSome(func(s lnwallet.AuxSigner) {
368+
chanOpts = append(chanOpts, lnwallet.WithAuxSigner(s))
369+
})
360370

361371
// Finally, we'll force close the channel completing
362372
// the force close workflow.

discovery/gossiper.go

+16-3
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/graph"
2425
"github.com/lightningnetwork/lnd/keychain"
2526
"github.com/lightningnetwork/lnd/kvdb"
@@ -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
@@ -2598,6 +2608,9 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg,
25982608
cp := *nMsg.optionalMsgFields.channelPoint
25992609
edge.ChannelPoint = cp
26002610
}
2611+
2612+
// Optional tapscript root for custom channels.
2613+
edge.TapscriptRoot = nMsg.optionalMsgFields.tapscriptRoot
26012614
}
26022615

26032616
log.Debugf("Adding edge for short_chan_id: %v", scid.ToUint64())

funding/aux_funding.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package funding
2+
3+
import (
4+
"github.com/btcsuite/btcd/chaincfg/chainhash"
5+
"github.com/lightningnetwork/lnd/fn"
6+
"github.com/lightningnetwork/lnd/lntypes"
7+
"github.com/lightningnetwork/lnd/lnwallet"
8+
"github.com/lightningnetwork/lnd/msgmux"
9+
)
10+
11+
// AuxFundingDescResult is a type alias for a function that returns an optional
12+
// aux funding desc.
13+
type AuxFundingDescResult = fn.Result[fn.Option[lnwallet.AuxFundingDesc]]
14+
15+
// AuxTapscriptResult is a type alias for a function that returns an optional
16+
// tapscript root.
17+
type AuxTapscriptResult = fn.Result[fn.Option[chainhash.Hash]]
18+
19+
// AuxFundingController permits the implementation of the funding of custom
20+
// channels types. The controller serves as a MsgEndpoint which allows it to
21+
// intercept custom messages, or even the regular funding messages. The
22+
// controller might also pass along an aux funding desc based on an existing
23+
// pending channel ID.
24+
type AuxFundingController interface {
25+
// Endpoint is the embedded interface that signals that the funding
26+
// controller is also a message endpoint. This'll allow it to handle
27+
// custom messages specific to the funding type.
28+
msgmux.Endpoint
29+
30+
// DescFromPendingChanID takes a pending channel ID, that may already be
31+
// known due to prior custom channel messages, and maybe returns an aux
32+
// funding desc which can be used to modify how a channel is funded.
33+
DescFromPendingChanID(pid PendingChanID, openChan lnwallet.AuxChanState,
34+
keyRing lntypes.Dual[lnwallet.CommitmentKeyRing],
35+
initiator bool) AuxFundingDescResult
36+
37+
// DeriveTapscriptRoot takes a pending channel ID and maybe returns a
38+
// tapscript root that should be used when creating any MuSig2 sessions
39+
// for a channel.
40+
DeriveTapscriptRoot(PendingChanID) AuxTapscriptResult
41+
42+
// ChannelReady is called when a channel has been fully opened (multiple
43+
// confirmations) and is ready to be used. This can be used to perform
44+
// any final setup or cleanup.
45+
ChannelReady(openChan lnwallet.AuxChanState) error
46+
47+
// ChannelFinalized is called when a channel has been fully finalized.
48+
// In this state, we've received the commitment sig from the remote
49+
// party, so we are safe to broadcast the funding transaction.
50+
ChannelFinalized(PendingChanID) error
51+
}

0 commit comments

Comments
 (0)