Skip to content

Commit 5d18c65

Browse files
committed
channeldb: add optional TapscriptRoot field + feature bit
1 parent 784d236 commit 5d18c65

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

channeldb/channel.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ type chanAuxData struct {
236236
realScid tlv.RecordT[tlv.TlvType4, lnwire.ShortChannelID]
237237

238238
memo tlv.OptionalRecordT[tlv.TlvType5, []byte]
239+
240+
tapscriptRoot tlv.OptionalRecordT[tlv.TlvType6, [32]byte]
239241
}
240242

241243
// toOpeChan converts the chanAuxData to an OpenChannel by setting the relevant
@@ -248,6 +250,9 @@ func (c *chanAuxData) toOpenChan(o *OpenChannel) {
248250
c.memo.WhenSomeV(func(memo []byte) {
249251
o.Memo = memo
250252
})
253+
c.tapscriptRoot.WhenSomeV(func(h [32]byte) {
254+
o.TapscriptRoot = fn.Some(chainhash.Hash(h))
255+
})
251256
}
252257

253258
// newChanAuxDataFromChan creates a new chanAuxData from the given channel.
@@ -267,11 +272,16 @@ func newChanAuxDataFromChan(openChan *OpenChannel) *chanAuxData {
267272
),
268273
}
269274

270-
if len(openChan.Memo) == 0 {
275+
if len(openChan.Memo) != 0 {
271276
c.memo = tlv.SomeRecordT(
272277
tlv.NewPrimitiveRecord[tlv.TlvType5](openChan.Memo),
273278
)
274279
}
280+
openChan.TapscriptRoot.WhenSome(func(h chainhash.Hash) {
281+
c.tapscriptRoot = tlv.SomeRecordT(
282+
tlv.NewPrimitiveRecord[tlv.TlvType6]([32]byte(h)),
283+
)
284+
})
275285

276286
return c
277287
}
@@ -353,6 +363,11 @@ const (
353363
// SimpleTaprootFeatureBit indicates that the simple-taproot-chans
354364
// feature bit was negotiated during the lifetime of the channel.
355365
SimpleTaprootFeatureBit ChannelType = 1 << 10
366+
367+
// TapscriptRootBit indicates that this is a musig2 channel with a top
368+
// level tapscript commitment. This MUST be set along with the
369+
// SimpleTaprootFeatureBit.
370+
TapscriptRootBit ChannelType = 1 << 11
356371
)
357372

358373
// IsSingleFunder returns true if the channel type if one of the known single
@@ -423,6 +438,12 @@ func (c ChannelType) IsTaproot() bool {
423438
return c&SimpleTaprootFeatureBit == SimpleTaprootFeatureBit
424439
}
425440

441+
// HasTapscriptRoot returns true if the channel is using a top level tapscript
442+
// root commmitment.
443+
func (c ChannelType) HasTapscriptRoot() bool {
444+
return c&TapscriptRootBit == TapscriptRootBit
445+
}
446+
426447
// ChannelConstraints represents a set of constraints meant to allow a node to
427448
// limit their exposure, enact flow control and ensure that all HTLCs are
428449
// economically relevant. This struct will be mirrored for both sides of the
@@ -3980,6 +4001,9 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
39804001
auxData.memo.WhenSome(func(memo tlv.RecordT[tlv.TlvType5, []byte]) {
39814002
tlvRecords = append(tlvRecords, memo.Record())
39824003
})
4004+
auxData.tapscriptRoot.WhenSome(func(root tlv.RecordT[tlv.TlvType6, [32]byte]) { //nolint:lll
4005+
tlvRecords = append(tlvRecords, root.Record())
4006+
})
39834007

39844008
// Create the tlv stream.
39854009
tlvStream, err := tlv.NewStream(tlvRecords...)
@@ -4178,24 +4202,34 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
41784202
}
41794203

41804204
var auxData chanAuxData
4181-
zeroMemo := auxData.memo.Zero()
4205+
memo := auxData.memo.Zero()
4206+
tapscriptRoot := auxData.tapscriptRoot.Zero()
41824207

41834208
// Create the tlv stream.
41844209
tlvStream, err := tlv.NewStream(
41854210
auxData.revokeKeyLoc.Record(),
41864211
auxData.initialLocalBalance.Record(),
41874212
auxData.initialRemoteBalance.Record(),
41884213
auxData.realScid.Record(),
4189-
zeroMemo.Record(),
4214+
memo.Record(),
4215+
tapscriptRoot.Record(),
41904216
)
41914217
if err != nil {
41924218
return err
41934219
}
41944220

4195-
if err := tlvStream.Decode(r); err != nil {
4221+
tlvs, err := tlvStream.DecodeWithParsedTypes(r)
4222+
if err != nil {
41964223
return err
41974224
}
41984225

4226+
if _, ok := tlvs[memo.TlvType()]; ok {
4227+
auxData.memo = tlv.SomeRecordT(memo)
4228+
}
4229+
if _, ok := tlvs[tapscriptRoot.TlvType()]; ok {
4230+
auxData.tapscriptRoot = tlv.SomeRecordT(tapscriptRoot)
4231+
}
4232+
41994233
// Assign all the relevant fields from the aux data into the actual
42004234
// open channel.
42014235
auxData.toOpenChan(channel)

channeldb/channel_test.go

Lines changed: 6 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"
@@ -171,7 +172,7 @@ func fundingPointOption(chanPoint wire.OutPoint) testChannelOption {
171172
}
172173

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

315+
var tapscriptRoot chainhash.Hash
316+
copy(tapscriptRoot[:], bytes.Repeat([]byte{1}, 32))
317+
314318
return &OpenChannel{
315319
ChanType: SingleFunderBit | FrozenBit,
316320
ChainHash: key,
@@ -353,6 +357,7 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
353357
ThawHeight: uint32(defaultPendingHeight),
354358
InitialLocalBalance: lnwire.MilliSatoshi(9000),
355359
InitialRemoteBalance: lnwire.MilliSatoshi(3000),
360+
TapscriptRoot: fn.Some(tapscriptRoot),
356361
}
357362
}
358363

0 commit comments

Comments
 (0)