Skip to content

Commit dd8820d

Browse files
authored
Merge pull request #7346 from Roasbeef/simple-taproot-chans-funding-mgr
[7/?] - funding: update funding manager w/ new musig2+taproot funding flow
2 parents 09200ca + ea25054 commit dd8820d

File tree

16 files changed

+814
-170
lines changed

16 files changed

+814
-170
lines changed

contractcourt/chain_watcher.go

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ type chainWatcher struct {
207207
// the current state number on the commitment transactions.
208208
stateHintObfuscator [lnwallet.StateHintSize]byte
209209

210+
// fundingPkScript is the pkScript of the funding output.
211+
fundingPkScript []byte
212+
213+
// heightHint is the height hint used to checkpoint scans on chain for
214+
// conf/spend events.
215+
heightHint uint32
216+
210217
// All the fields below are protected by this mutex.
211218
sync.Mutex
212219

@@ -267,9 +274,9 @@ func (c *chainWatcher) Start() error {
267274
// As a height hint, we'll try to use the opening height, but if the
268275
// channel isn't yet open, then we'll use the height it was broadcast
269276
// at. This may be an unconfirmed zero-conf channel.
270-
heightHint := c.cfg.chanState.ShortChanID().BlockHeight
271-
if heightHint == 0 {
272-
heightHint = chanState.BroadcastHeight()
277+
c.heightHint = c.cfg.chanState.ShortChanID().BlockHeight
278+
if c.heightHint == 0 {
279+
c.heightHint = chanState.BroadcastHeight()
273280
}
274281

275282
// Since no zero-conf state is stored in a channel backup, the below
@@ -279,29 +286,43 @@ func (c *chainWatcher) Start() error {
279286
if chanState.ZeroConfConfirmed() {
280287
// If the zero-conf channel is confirmed, we'll use the
281288
// confirmed SCID's block height.
282-
heightHint = chanState.ZeroConfRealScid().BlockHeight
289+
c.heightHint = chanState.ZeroConfRealScid().BlockHeight
283290
} else {
284291
// The zero-conf channel is unconfirmed. We'll need to
285292
// use the FundingBroadcastHeight.
286-
heightHint = chanState.BroadcastHeight()
293+
c.heightHint = chanState.BroadcastHeight()
287294
}
288295
}
289296

290-
localKey := chanState.LocalChanCfg.MultiSigKey.PubKey.SerializeCompressed()
291-
remoteKey := chanState.RemoteChanCfg.MultiSigKey.PubKey.SerializeCompressed()
292-
multiSigScript, err := input.GenMultiSigScript(
293-
localKey, remoteKey,
297+
localKey := chanState.LocalChanCfg.MultiSigKey.PubKey
298+
remoteKey := chanState.RemoteChanCfg.MultiSigKey.PubKey
299+
300+
var (
301+
err error
294302
)
295-
if err != nil {
296-
return err
297-
}
298-
pkScript, err := input.WitnessScriptHash(multiSigScript)
299-
if err != nil {
300-
return err
303+
if chanState.ChanType.IsTaproot() {
304+
c.fundingPkScript, _, err = input.GenTaprootFundingScript(
305+
localKey, remoteKey, 0,
306+
)
307+
if err != nil {
308+
return err
309+
}
310+
} else {
311+
multiSigScript, err := input.GenMultiSigScript(
312+
localKey.SerializeCompressed(),
313+
remoteKey.SerializeCompressed(),
314+
)
315+
if err != nil {
316+
return err
317+
}
318+
c.fundingPkScript, err = input.WitnessScriptHash(multiSigScript)
319+
if err != nil {
320+
return err
321+
}
301322
}
302323

303324
spendNtfn, err := c.cfg.notifier.RegisterSpendNtfn(
304-
fundingOut, pkScript, heightHint,
325+
fundingOut, c.fundingPkScript, c.heightHint,
305326
)
306327
if err != nil {
307328
return err
@@ -567,6 +588,33 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
567588
log.Infof("Close observer for ChannelPoint(%v) active",
568589
c.cfg.chanState.FundingOutpoint)
569590

591+
// If this is a taproot channel, before we proceed, we want to ensure
592+
// that the expected funding output has confirmed on chain.
593+
if c.cfg.chanState.ChanType.IsTaproot() {
594+
fundingPoint := c.cfg.chanState.FundingOutpoint
595+
596+
confNtfn, err := c.cfg.notifier.RegisterConfirmationsNtfn(
597+
&fundingPoint.Hash, c.fundingPkScript, 1, c.heightHint,
598+
)
599+
if err != nil {
600+
log.Warnf("unable to register for conf: %v", err)
601+
}
602+
603+
log.Infof("Waiting for taproot ChannelPoint(%v) to confirm...",
604+
c.cfg.chanState.FundingOutpoint)
605+
606+
select {
607+
case _, ok := <-confNtfn.Confirmed:
608+
// If the channel was closed, then this means that the
609+
// notifier exited, so we will as well.
610+
if !ok {
611+
return
612+
}
613+
case <-c.quit:
614+
return
615+
}
616+
}
617+
570618
select {
571619
// We've detected a spend of the channel onchain! Depending on the type
572620
// of spend, we'll act accordingly, so we'll examine the spending
@@ -833,6 +881,9 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
833881
}
834882

835883
// Create an AnchorResolution for the breached state.
884+
//
885+
// TODO(roasbeef): make keyring for taproot chans to pass in instead of
886+
// nil
836887
anchorRes, err := lnwallet.NewAnchorResolution(
837888
c.cfg.chanState, commitSpend.SpendingTx, nil,
838889
)

discovery/mock_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/btcsuite/btcd/btcec/v2"
99
"github.com/btcsuite/btcd/wire"
10-
"github.com/lightningnetwork/lnd/channeldb"
1110
"github.com/lightningnetwork/lnd/lnpeer"
1211
"github.com/lightningnetwork/lnd/lnwire"
1312
)
@@ -42,7 +41,9 @@ func (p *mockPeer) SendMessageLazy(sync bool, msgs ...lnwire.Message) error {
4241
return p.SendMessage(sync, msgs...)
4342
}
4443

45-
func (p *mockPeer) AddNewChannel(_ *channeldb.OpenChannel, _ <-chan struct{}) error {
44+
func (p *mockPeer) AddNewChannel(_ *lnpeer.NewChannel,
45+
_ <-chan struct{}) error {
46+
4647
return nil
4748
}
4849
func (p *mockPeer) WipeChannel(_ *wire.OutPoint) {}

feature/default_sets.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ var defaultSetDesc = setDesc{
8383
SetInit: {}, // I
8484
SetNodeAnn: {}, // N
8585
},
86+
lnwire.SimpleTaprootChannelsOptional: {
87+
SetInit: {}, // I
88+
SetNodeAnn: {}, // N
89+
},
8690
}

feature/deps.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ var deps = depDesc{
7575
lnwire.ZeroConfOptional: {
7676
lnwire.ScidAliasOptional: {},
7777
},
78+
lnwire.SimpleTaprootChannelsOptional: {
79+
lnwire.AnchorsZeroFeeHtlcTxOptional: {},
80+
lnwire.ExplicitChannelTypeOptional: {},
81+
},
7882
}
7983

8084
// ValidateDeps asserts that a feature vector sets all features and their

funding/commitment_type_negotiation.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,52 @@ func explicitNegotiateCommitmentType(channelType lnwire.ChannelType, local,
223223
}
224224
return lnwallet.CommitmentTypeTweakless, nil
225225

226+
// Simple taproot channels only.
227+
case channelFeatures.OnlyContains(lnwire.SimpleTaprootChannelsRequired):
228+
229+
if !hasFeatures(
230+
local, remote, lnwire.SimpleTaprootChannelsOptional,
231+
) {
232+
233+
return 0, errUnsupportedChannelType
234+
}
235+
236+
return lnwallet.CommitmentTypeSimpleTaproot, nil
237+
238+
// Simple taproot channels with scid only.
239+
case channelFeatures.OnlyContains(
240+
lnwire.SimpleTaprootChannelsRequired,
241+
lnwire.ScidAliasRequired,
242+
):
243+
244+
if !hasFeatures(
245+
local, remote,
246+
lnwire.SimpleTaprootChannelsOptional,
247+
lnwire.ScidAliasOptional,
248+
) {
249+
250+
return 0, errUnsupportedChannelType
251+
}
252+
253+
return lnwallet.CommitmentTypeSimpleTaproot, nil
254+
255+
// Simple taproot channels with zero conf only.
256+
case channelFeatures.OnlyContains(
257+
lnwire.SimpleTaprootChannelsRequired,
258+
lnwire.ZeroConfRequired,
259+
):
260+
261+
if !hasFeatures(
262+
local, remote,
263+
lnwire.SimpleTaprootChannelsOptional,
264+
lnwire.ZeroConfOptional,
265+
) {
266+
267+
return 0, errUnsupportedChannelType
268+
}
269+
270+
return lnwallet.CommitmentTypeSimpleTaproot, nil
271+
226272
// No features, use legacy commitment type.
227273
case channelFeatures.IsEmpty():
228274
return lnwallet.CommitmentTypeLegacy, nil

0 commit comments

Comments
 (0)