Skip to content

Commit 89cf4c2

Browse files
Roasbeefguggero
authored andcommitted
channeldb: add optional TapscriptRoot field + feature bit
1 parent 1f110dc commit 89cf4c2

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

channeldb/channel.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ type chanAuxData struct {
247247
// memo is an optional text field that gives context to the user about
248248
// the channel.
249249
memo tlv.OptionalRecordT[tlv.TlvType5, []byte]
250+
251+
// tapscriptRoot is the optional Tapscript root the channel funding
252+
// output commits to.
253+
tapscriptRoot tlv.OptionalRecordT[tlv.TlvType6, [32]byte]
250254
}
251255

252256
// encode serializes the chanAuxData to the given io.Writer.
@@ -260,6 +264,11 @@ func (c *chanAuxData) encode(w io.Writer) error {
260264
c.memo.WhenSome(func(memo tlv.RecordT[tlv.TlvType5, []byte]) {
261265
tlvRecords = append(tlvRecords, memo.Record())
262266
})
267+
c.tapscriptRoot.WhenSome(
268+
func(root tlv.RecordT[tlv.TlvType6, [32]byte]) {
269+
tlvRecords = append(tlvRecords, root.Record())
270+
},
271+
)
263272

264273
// Create the tlv stream.
265274
tlvStream, err := tlv.NewStream(tlvRecords...)
@@ -273,6 +282,7 @@ func (c *chanAuxData) encode(w io.Writer) error {
273282
// decode deserializes the chanAuxData from the given io.Reader.
274283
func (c *chanAuxData) decode(r io.Reader) error {
275284
memo := c.memo.Zero()
285+
tapscriptRoot := c.tapscriptRoot.Zero()
276286

277287
// Create the tlv stream.
278288
tlvStream, err := tlv.NewStream(
@@ -281,6 +291,7 @@ func (c *chanAuxData) decode(r io.Reader) error {
281291
c.initialRemoteBalance.Record(),
282292
c.realScid.Record(),
283293
memo.Record(),
294+
tapscriptRoot.Record(),
284295
)
285296
if err != nil {
286297
return err
@@ -294,6 +305,9 @@ func (c *chanAuxData) decode(r io.Reader) error {
294305
if _, ok := tlvs[memo.TlvType()]; ok {
295306
c.memo = tlv.SomeRecordT(memo)
296307
}
308+
if _, ok := tlvs[tapscriptRoot.TlvType()]; ok {
309+
c.tapscriptRoot = tlv.SomeRecordT(tapscriptRoot)
310+
}
297311

298312
return nil
299313
}
@@ -308,6 +322,9 @@ func (c *chanAuxData) toOpenChan(o *OpenChannel) {
308322
c.memo.WhenSomeV(func(memo []byte) {
309323
o.Memo = memo
310324
})
325+
c.tapscriptRoot.WhenSomeV(func(h [32]byte) {
326+
o.TapscriptRoot = fn.Some[chainhash.Hash](h)
327+
})
311328
}
312329

313330
// newChanAuxDataFromChan creates a new chanAuxData from the given channel.
@@ -332,6 +349,11 @@ func newChanAuxDataFromChan(openChan *OpenChannel) *chanAuxData {
332349
tlv.NewPrimitiveRecord[tlv.TlvType5](openChan.Memo),
333350
)
334351
}
352+
openChan.TapscriptRoot.WhenSome(func(h chainhash.Hash) {
353+
c.tapscriptRoot = tlv.SomeRecordT(
354+
tlv.NewPrimitiveRecord[tlv.TlvType6, [32]byte](h),
355+
)
356+
})
335357

336358
return c
337359
}
@@ -413,6 +435,11 @@ const (
413435
// SimpleTaprootFeatureBit indicates that the simple-taproot-chans
414436
// feature bit was negotiated during the lifetime of the channel.
415437
SimpleTaprootFeatureBit ChannelType = 1 << 10
438+
439+
// TapscriptRootBit indicates that this is a MuSig2 channel with a top
440+
// level tapscript commitment. This MUST be set along with the
441+
// SimpleTaprootFeatureBit.
442+
TapscriptRootBit ChannelType = 1 << 11
416443
)
417444

418445
// IsSingleFunder returns true if the channel type if one of the known single
@@ -483,6 +510,12 @@ func (c ChannelType) IsTaproot() bool {
483510
return c&SimpleTaprootFeatureBit == SimpleTaprootFeatureBit
484511
}
485512

513+
// HasTapscriptRoot returns true if the channel is using a top level tapscript
514+
// root commitment.
515+
func (c ChannelType) HasTapscriptRoot() bool {
516+
return c&TapscriptRootBit == TapscriptRootBit
517+
}
518+
486519
// ChannelConstraints represents a set of constraints meant to allow a node to
487520
// limit their exposure, enact flow control and ensure that all HTLCs are
488521
// economically relevant. This struct will be mirrored for both sides of the

channeldb/channel_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/davecgh/go-spew/spew"
1818
"github.com/lightningnetwork/lnd/channeldb/models"
1919
"github.com/lightningnetwork/lnd/clock"
20+
"github.com/lightningnetwork/lnd/fn"
2021
"github.com/lightningnetwork/lnd/keychain"
2122
"github.com/lightningnetwork/lnd/kvdb"
2223
"github.com/lightningnetwork/lnd/lnmock"
@@ -172,7 +173,7 @@ func fundingPointOption(chanPoint wire.OutPoint) testChannelOption {
172173
}
173174

174175
// channelIDOption is an option which sets the short channel ID of the channel.
175-
var channelIDOption = func(chanID lnwire.ShortChannelID) testChannelOption {
176+
func channelIDOption(chanID lnwire.ShortChannelID) testChannelOption {
176177
return func(params *testChannelParams) {
177178
params.channel.ShortChannelID = chanID
178179
}
@@ -312,6 +313,9 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
312313
uniqueOutputIndex.Add(1)
313314
op := wire.OutPoint{Hash: key, Index: uniqueOutputIndex.Load()}
314315

316+
var tapscriptRoot chainhash.Hash
317+
copy(tapscriptRoot[:], bytes.Repeat([]byte{1}, 32))
318+
315319
return &OpenChannel{
316320
ChanType: SingleFunderBit | FrozenBit,
317321
ChainHash: key,
@@ -354,6 +358,8 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
354358
ThawHeight: uint32(defaultPendingHeight),
355359
InitialLocalBalance: lnwire.MilliSatoshi(9000),
356360
InitialRemoteBalance: lnwire.MilliSatoshi(3000),
361+
Memo: []byte("test"),
362+
TapscriptRoot: fn.Some(tapscriptRoot),
357363
}
358364
}
359365

0 commit comments

Comments
 (0)