Skip to content

Commit dc3aef4

Browse files
committed
channeldb+lnwallet: add epoch history to OpenChannel
This commit takes the CommitChainEpochHistory defined in the last commit and adds it to the OpenChannel structure. As of this commit it is essentially redundant with the ChanCfgs but it will capture the history of the ChanCfgs when we add the ability to update them.
1 parent 5d4020f commit dc3aef4

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

channeldb/channel.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ type openChannelTlvData struct {
260260
// customBlob is an optional TLV encoded blob of data representing
261261
// custom channel funding information.
262262
customBlob tlv.OptionalRecordT[tlv.TlvType7, tlv.Blob]
263+
264+
// commitChainEpochHistory is the optional TLV encoded blob of data
265+
// representing the commit chain epoch history for the channel.
266+
commitChainEpochHistory tlv.OptionalRecordT[tlv.TlvType8, CommitChainEpochHistory] //nolint:lll
263267
}
264268

265269
// encode serializes the openChannelTlvData to the given io.Writer.
@@ -281,6 +285,11 @@ func (c *openChannelTlvData) encode(w io.Writer) error {
281285
c.customBlob.WhenSome(func(blob tlv.RecordT[tlv.TlvType7, tlv.Blob]) {
282286
tlvRecords = append(tlvRecords, blob.Record())
283287
})
288+
c.commitChainEpochHistory.WhenSome(
289+
func(hist tlv.RecordT[tlv.TlvType8, CommitChainEpochHistory]) {
290+
tlvRecords = append(tlvRecords, hist.Record())
291+
},
292+
)
284293

285294
// Create the tlv stream.
286295
tlvStream, err := tlv.NewStream(tlvRecords...)
@@ -296,6 +305,7 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
296305
memo := c.memo.Zero()
297306
tapscriptRoot := c.tapscriptRoot.Zero()
298307
blob := c.customBlob.Zero()
308+
commitChainEpochHistory := c.commitChainEpochHistory.Zero()
299309

300310
// Create the tlv stream.
301311
tlvStream, err := tlv.NewStream(
@@ -306,6 +316,7 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
306316
memo.Record(),
307317
tapscriptRoot.Record(),
308318
blob.Record(),
319+
commitChainEpochHistory.Record(),
309320
)
310321
if err != nil {
311322
return err
@@ -325,6 +336,10 @@ func (c *openChannelTlvData) decode(r io.Reader) error {
325336
if _, ok := tlvs[c.customBlob.TlvType()]; ok {
326337
c.customBlob = tlv.SomeRecordT(blob)
327338
}
339+
if _, ok := tlvs[c.commitChainEpochHistory.TlvType()]; ok {
340+
c.commitChainEpochHistory =
341+
tlv.SomeRecordT(commitChainEpochHistory)
342+
}
328343

329344
return nil
330345
}
@@ -947,6 +962,10 @@ type OpenChannel struct {
947962
// commitment.
948963
Commitments lntypes.Dual[ChannelCommitment]
949964

965+
// CommitChainEpochHistory is the history of the CommitmentParams for
966+
// each side of the channel.
967+
CommitChainEpochHistory CommitChainEpochHistory
968+
950969
// RemoteCurrentRevocation is the current revocation for their
951970
// commitment transaction. However, since this the derived public key,
952971
// we don't yet have the private key so we aren't yet able to verify
@@ -1207,6 +1226,11 @@ func (c *OpenChannel) amendTlvData(auxData openChannelTlvData) {
12071226
auxData.customBlob.WhenSomeV(func(blob tlv.Blob) {
12081227
c.CustomBlob = fn.Some(blob)
12091228
})
1229+
auxData.commitChainEpochHistory.WhenSomeV(
1230+
func(history CommitChainEpochHistory) {
1231+
c.CommitChainEpochHistory = history
1232+
},
1233+
)
12101234
}
12111235

12121236
// extractTlvData creates a new openChannelTlvData from the given channel.
@@ -1224,6 +1248,11 @@ func (c *OpenChannel) extractTlvData() openChannelTlvData {
12241248
realScid: tlv.NewRecordT[tlv.TlvType4](
12251249
c.confirmedScid,
12261250
),
1251+
commitChainEpochHistory: tlv.SomeRecordT(
1252+
tlv.NewRecordT[tlv.TlvType8](
1253+
c.CommitChainEpochHistory,
1254+
),
1255+
),
12271256
}
12281257

12291258
if len(c.Memo) != 0 {
@@ -4508,6 +4537,22 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
45084537
// open channel.
45094538
channel.amendTlvData(auxData)
45104539

4540+
// Now that we've extracted the aux data, we can initialize the
4541+
// CommitChainEpochHistory. If we don't find it in the aux data,
4542+
// then we initialize it with the original CommitmentParams from
4543+
// the ChannelConfig.
4544+
histVal := auxData.commitChainEpochHistory.ValOpt()
4545+
channel.CommitChainEpochHistory = histVal.UnwrapOr(
4546+
BeginChainEpochHistory(
4547+
lntypes.MapDual(
4548+
channel.ChanCfgs,
4549+
func(cfg ChannelConfig) CommitmentParams {
4550+
return cfg.CommitmentParams
4551+
},
4552+
),
4553+
),
4554+
)
4555+
45114556
channel.Packager = NewChannelPackager(channel.ShortChannelID)
45124557

45134558
// Finally, read the optional shutdown scripts.

channeldb/channel_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel {
343343
Local: localCfg,
344344
Remote: remoteCfg,
345345
},
346+
CommitChainEpochHistory: BeginChainEpochHistory(
347+
lntypes.Dual[CommitmentParams]{
348+
Local: localRenderingParams,
349+
Remote: remoteRenderingParams,
350+
},
351+
),
346352
TotalMSatSent: 8,
347353
TotalMSatReceived: 2,
348354
Commitments: lntypes.Dual[ChannelCommitment]{

lnwallet/channel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7712,6 +7712,7 @@ func (lc *LightningChannel) ForceClose() (*LocalForceCloseSummary, error) {
77127712

77137713
return summary, nil
77147714
}
7715+
77157716
// NewLocalForceCloseSummary generates a LocalForceCloseSummary from the given
77167717
// channel state. The passed commitTx must be a fully signed commitment
77177718
// transaction corresponding to localCommit.

lnwallet/test_utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ func CreateTestChannels(t *testing.T, chanType channeldb.ChannelType,
321321
Local: aliceCfg,
322322
Remote: bobCfg,
323323
},
324+
CommitChainEpochHistory: channeldb.BeginChainEpochHistory(
325+
lntypes.Dual[channeldb.CommitmentParams]{
326+
Local: aliceCfg.CommitmentParams,
327+
Remote: bobCfg.CommitmentParams,
328+
},
329+
),
324330
IdentityPub: aliceKeys[0].PubKey(),
325331
FundingOutpoint: *prevOut,
326332
ShortChannelID: shortChanID,
@@ -343,6 +349,12 @@ func CreateTestChannels(t *testing.T, chanType channeldb.ChannelType,
343349
Local: bobCfg,
344350
Remote: aliceCfg,
345351
},
352+
CommitChainEpochHistory: channeldb.BeginChainEpochHistory(
353+
lntypes.Dual[channeldb.CommitmentParams]{
354+
Local: bobCfg.CommitmentParams,
355+
Remote: aliceCfg.CommitmentParams,
356+
},
357+
),
346358
IdentityPub: bobKeys[0].PubKey(),
347359
FundingOutpoint: *prevOut,
348360
ShortChannelID: shortChanID,

lnwallet/wallet.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,16 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
23212321
// he stored within the database.
23222322
res.partialState.ChanCfgs.Local = res.ourContribution.toChanConfig()
23232323
res.partialState.ChanCfgs.Remote = res.theirContribution.toChanConfig()
2324+
res.partialState.CommitChainEpochHistory =
2325+
channeldb.BeginChainEpochHistory(
2326+
lntypes.MapDual(
2327+
res.partialState.ChanCfgs,
2328+
//nolint:lll
2329+
func(cfg channeldb.ChannelConfig) channeldb.CommitmentParams {
2330+
return cfg.CommitmentParams
2331+
},
2332+
),
2333+
)
23242334

23252335
// We'll also record the finalized funding txn, which will allow us to
23262336
// rebroadcast on startup in case we fail.
@@ -2532,6 +2542,16 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
25322542
pendingReservation.ourContribution.toChanConfig()
25332543
chanState.ChanCfgs.Remote =
25342544
pendingReservation.theirContribution.toChanConfig()
2545+
chanState.CommitChainEpochHistory =
2546+
channeldb.BeginChainEpochHistory(
2547+
lntypes.MapDual(
2548+
chanState.ChanCfgs,
2549+
//nolint:lll
2550+
func(cfg channeldb.ChannelConfig) channeldb.CommitmentParams {
2551+
return cfg.CommitmentParams
2552+
},
2553+
),
2554+
)
25352555

25362556
chanState.RevocationKeyLocator = pendingReservation.nextRevocationKeyLoc
25372557

0 commit comments

Comments
 (0)