Skip to content

Commit 8077862

Browse files
committed
lnwallet: pack commit chains into Dual
This commit packs the LightningChannel's localCommitmentChain and remoteCommitmentChain into a Dual structure for better symmetric access. This will be leveraged by an upcoming commit where we want to more concisely express how we compute the number of pending updates.
1 parent a0515a1 commit 8077862

File tree

2 files changed

+93
-85
lines changed

2 files changed

+93
-85
lines changed

lnwallet/channel.go

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,13 @@ type LightningChannel struct {
858858
// accepted.
859859
currentHeight uint64
860860

861-
// remoteCommitChain is the remote node's commitment chain. Any new
862-
// commitments we initiate are added to the tip of this chain.
863-
remoteCommitChain *commitmentChain
864-
865-
// localCommitChain is our local commitment chain. Any new commitments
866-
// received are added to the tip of this chain. The tail (or lowest
867-
// height) in this chain is our current accepted state, which we are
868-
// able to broadcast safely.
869-
localCommitChain *commitmentChain
861+
// commitChains is a Dual of the local and remote node's commitment
862+
// chains. Any new commitments we initiate are added to Remote chain's
863+
// tip. The Local portion of this field is our local commitment chain.
864+
// Any new commitments received are added to the tip of this chain.
865+
// The tail (or lowest height) in this chain is our current accepted
866+
// state, which we are able to broadcast safely.
867+
commitChains lntypes.Dual[*commitmentChain]
870868

871869
channelState *channeldb.OpenChannel
872870

@@ -992,14 +990,18 @@ func NewLightningChannel(signer input.Signer,
992990
return nil, fmt.Errorf("unable to derive shachain: %w", err)
993991
}
994992

993+
commitChains := lntypes.Dual[*commitmentChain]{
994+
Local: newCommitmentChain(),
995+
Remote: newCommitmentChain(),
996+
}
997+
995998
lc := &LightningChannel{
996-
Signer: signer,
997-
leafStore: opts.leafStore,
998-
sigPool: sigPool,
999-
currentHeight: localCommit.CommitHeight,
1000-
remoteCommitChain: newCommitmentChain(),
1001-
localCommitChain: newCommitmentChain(),
1002-
channelState: state,
999+
Signer: signer,
1000+
leafStore: opts.leafStore,
1001+
sigPool: sigPool,
1002+
currentHeight: localCommit.CommitHeight,
1003+
commitChains: commitChains,
1004+
channelState: state,
10031005
commitBuilder: NewCommitmentBuilder(
10041006
state, opts.leafStore,
10051007
),
@@ -1463,10 +1465,10 @@ func (lc *LightningChannel) restoreCommitState(
14631465
if err != nil {
14641466
return err
14651467
}
1466-
lc.localCommitChain.addCommitment(localCommit)
1468+
lc.commitChains.Local.addCommitment(localCommit)
14671469

14681470
lc.log.Tracef("starting local commitment: %v",
1469-
lnutils.SpewLogClosure(lc.localCommitChain.tail()))
1471+
lnutils.SpewLogClosure(lc.commitChains.Local.tail()))
14701472

14711473
// We'll also do the same for the remote commitment chain.
14721474
remoteCommit, err := lc.diskCommitToMemCommit(
@@ -1476,10 +1478,10 @@ func (lc *LightningChannel) restoreCommitState(
14761478
if err != nil {
14771479
return err
14781480
}
1479-
lc.remoteCommitChain.addCommitment(remoteCommit)
1481+
lc.commitChains.Remote.addCommitment(remoteCommit)
14801482

14811483
lc.log.Tracef("starting remote commitment: %v",
1482-
lnutils.SpewLogClosure(lc.remoteCommitChain.tail()))
1484+
lnutils.SpewLogClosure(lc.commitChains.Remote.tail()))
14831485

14841486
var (
14851487
pendingRemoteCommit *commitment
@@ -1508,10 +1510,10 @@ func (lc *LightningChannel) restoreCommitState(
15081510
if err != nil {
15091511
return err
15101512
}
1511-
lc.remoteCommitChain.addCommitment(pendingRemoteCommit)
1513+
lc.commitChains.Remote.addCommitment(pendingRemoteCommit)
15121514

15131515
lc.log.Debugf("pending remote commitment: %v",
1514-
lnutils.SpewLogClosure(lc.remoteCommitChain.tip()))
1516+
lnutils.SpewLogClosure(lc.commitChains.Remote.tip()))
15151517

15161518
// We'll also re-create the set of commitment keys needed to
15171519
// fully re-derive the state.
@@ -2655,10 +2657,10 @@ func (lc *LightningChannel) fetchCommitmentView(
26552657
ourLogIndex, ourHtlcIndex, theirLogIndex, theirHtlcIndex uint64,
26562658
keyRing *CommitmentKeyRing) (*commitment, error) {
26572659

2658-
commitChain := lc.localCommitChain
2660+
commitChain := lc.commitChains.Local
26592661
dustLimit := lc.channelState.LocalChanCfg.DustLimit
26602662
if whoseCommitChain.IsRemote() {
2661-
commitChain = lc.remoteCommitChain
2663+
commitChain = lc.commitChains.Remote
26622664
dustLimit = lc.channelState.RemoteChanCfg.DustLimit
26632665
}
26642666

@@ -3480,10 +3482,10 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
34803482
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
34813483

34823484
// Fetch the last remote update that we have signed for.
3483-
lastRemoteCommitted := lc.remoteCommitChain.tail().theirMessageIndex
3485+
lastRemoteCommitted := lc.commitChains.Remote.tail().theirMessageIndex
34843486

34853487
// Fetch the last remote update that we have acked.
3486-
lastLocalCommitted := lc.localCommitChain.tail().theirMessageIndex
3488+
lastLocalCommitted := lc.commitChains.Local.tail().theirMessageIndex
34873489

34883490
// We'll now run through the remote update log to locate the items that
34893491
// we haven't signed for yet. This will be the set of items we need to
@@ -3709,9 +3711,9 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
37093711
) error {
37103712

37113713
// First fetch the initial balance before applying any updates.
3712-
commitChain := lc.localCommitChain
3714+
commitChain := lc.commitChains.Local
37133715
if whoseCommitChain.IsRemote() {
3714-
commitChain = lc.remoteCommitChain
3716+
commitChain = lc.commitChains.Remote
37153717
}
37163718
ourInitialBalance := commitChain.tip().ourBalance
37173719
theirInitialBalance := commitChain.tip().theirBalance
@@ -3961,16 +3963,16 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
39613963
// party, then we're unable to create new states. Each time we create a
39623964
// new state, we consume a prior revocation point.
39633965
commitPoint := lc.channelState.RemoteNextRevocation
3964-
unacked := lc.remoteCommitChain.hasUnackedCommitment()
3966+
unacked := lc.commitChains.Remote.hasUnackedCommitment()
39653967
if unacked || commitPoint == nil {
39663968
lc.log.Tracef("waiting for remote ack=%v, nil "+
39673969
"RemoteNextRevocation: %v", unacked, commitPoint == nil)
39683970
return nil, ErrNoWindow
39693971
}
39703972

39713973
// Determine the last update on the remote log that has been locked in.
3972-
remoteACKedIndex := lc.localCommitChain.tail().theirMessageIndex
3973-
remoteHtlcIndex := lc.localCommitChain.tail().theirHtlcIndex
3974+
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex
3975+
remoteHtlcIndex := lc.commitChains.Local.tail().theirHtlcIndex
39743976

39753977
// Before we extend this new commitment to the remote commitment chain,
39763978
// ensure that we aren't violating any of the constraints the remote
@@ -4117,7 +4119,7 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
41174119

41184120
// Extend the remote commitment chain by one with the addition of our
41194121
// latest commitment update.
4120-
lc.remoteCommitChain.addCommitment(newCommitView)
4122+
lc.commitChains.Remote.addCommitment(newCommitView)
41214123

41224124
return &NewCommitState{
41234125
CommitSigs: &CommitSigs{
@@ -4233,9 +4235,9 @@ func (lc *LightningChannel) ProcessChanSyncMsg(
42334235
// Take note of our current commit chain heights before we begin adding
42344236
// more to them.
42354237
var (
4236-
localTailHeight = lc.localCommitChain.tail().height
4237-
remoteTailHeight = lc.remoteCommitChain.tail().height
4238-
remoteTipHeight = lc.remoteCommitChain.tip().height
4238+
localTailHeight = lc.commitChains.Local.tail().height
4239+
remoteTailHeight = lc.commitChains.Remote.tail().height
4240+
remoteTipHeight = lc.commitChains.Remote.tip().height
42394241
)
42404242

42414243
// We'll now check that their view of our local chain is up-to-date.
@@ -4503,10 +4505,10 @@ func (lc *LightningChannel) computeView(view *HtlcView,
45034505
dryRunFee fn.Option[chainfee.SatPerKWeight]) (lnwire.MilliSatoshi,
45044506
lnwire.MilliSatoshi, lntypes.WeightUnit, *HtlcView, error) {
45054507

4506-
commitChain := lc.localCommitChain
4508+
commitChain := lc.commitChains.Local
45074509
dustLimit := lc.channelState.LocalChanCfg.DustLimit
45084510
if whoseCommitChain.IsRemote() {
4509-
commitChain = lc.remoteCommitChain
4511+
commitChain = lc.commitChains.Remote
45104512
dustLimit = lc.channelState.RemoteChanCfg.DustLimit
45114513
}
45124514

@@ -4969,8 +4971,8 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
49694971
}
49704972

49714973
// Determine the last update on the local log that has been locked in.
4972-
localACKedIndex := lc.remoteCommitChain.tail().ourMessageIndex
4973-
localHtlcIndex := lc.remoteCommitChain.tail().ourHtlcIndex
4974+
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex
4975+
localHtlcIndex := lc.commitChains.Remote.tail().ourHtlcIndex
49744976

49754977
// Ensure that this new local update from the remote node respects all
49764978
// the constraints we specified during initial channel setup. If not,
@@ -5207,7 +5209,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
52075209
localCommitmentView.sig = commitSigs.CommitSig.ToSignatureBytes() //nolint:lll
52085210
}
52095211

5210-
lc.localCommitChain.addCommitment(localCommitmentView)
5212+
lc.commitChains.Local.addCommitment(localCommitmentView)
52115213

52125214
return nil
52135215
}
@@ -5225,13 +5227,13 @@ func (lc *LightningChannel) IsChannelClean() bool {
52255227
defer lc.RUnlock()
52265228

52275229
// Check whether we have a pending commitment for our local state.
5228-
if lc.localCommitChain.hasUnackedCommitment() {
5230+
if lc.commitChains.Local.hasUnackedCommitment() {
52295231
return false
52305232
}
52315233

52325234
// Check whether our counterparty has a pending commitment for their
52335235
// state.
5234-
if lc.remoteCommitChain.hasUnackedCommitment() {
5236+
if lc.commitChains.Remote.hasUnackedCommitment() {
52355237
return false
52365238
}
52375239

@@ -5284,8 +5286,8 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
52845286
var (
52855287
remoteUpdatesPending, localUpdatesPending bool
52865288

5287-
lastLocalCommit = lc.localCommitChain.tip()
5288-
lastRemoteCommit = lc.remoteCommitChain.tip()
5289+
lastLocalCommit = lc.commitChains.Local.tip()
5290+
lastRemoteCommit = lc.commitChains.Remote.tip()
52895291

52905292
perspective string
52915293
)
@@ -5337,7 +5339,7 @@ func (lc *LightningChannel) PendingLocalUpdateCount() uint64 {
53375339
lc.RLock()
53385340
defer lc.RUnlock()
53395341

5340-
lastRemoteCommit := lc.remoteCommitChain.tip()
5342+
lastRemoteCommit := lc.commitChains.Remote.tip()
53415343

53425344
return lc.localUpdateLog.logIndex - lastRemoteCommit.ourMessageIndex
53435345
}
@@ -5362,16 +5364,16 @@ func (lc *LightningChannel) RevokeCurrentCommitment() (*lnwire.RevokeAndAck,
53625364
}
53635365

53645366
lc.log.Tracef("revoking height=%v, now at height=%v",
5365-
lc.localCommitChain.tail().height,
5367+
lc.commitChains.Local.tail().height,
53665368
lc.currentHeight+1)
53675369

53685370
// Advance our tail, as we've revoked our previous state.
5369-
lc.localCommitChain.advanceTail()
5371+
lc.commitChains.Local.advanceTail()
53705372
lc.currentHeight++
53715373

53725374
// Additionally, generate a channel delta for this state transition for
53735375
// persistent storage.
5374-
chainTail := lc.localCommitChain.tail()
5376+
chainTail := lc.commitChains.Local.tail()
53755377
newCommitment := chainTail.toDiskCommit(lntypes.Local)
53765378

53775379
// Get the unsigned acked remotes updates that are currently in memory.
@@ -5451,13 +5453,13 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
54515453

54525454
lc.log.Tracef("remote party accepted state transition, revoked height "+
54535455
"%v, now at %v",
5454-
lc.remoteCommitChain.tail().height,
5455-
lc.remoteCommitChain.tail().height+1)
5456+
lc.commitChains.Remote.tail().height,
5457+
lc.commitChains.Remote.tail().height+1)
54565458

54575459
// Add one to the remote tail since this will be height *after* we write
54585460
// the revocation to disk, the local height will remain unchanged.
5459-
remoteChainTail := lc.remoteCommitChain.tail().height + 1
5460-
localChainTail := lc.localCommitChain.tail().height
5461+
remoteChainTail := lc.commitChains.Remote.tail().height + 1
5462+
localChainTail := lc.commitChains.Local.tail().height
54615463

54625464
source := lc.ShortChanID()
54635465
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
@@ -5598,8 +5600,8 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
55985600

55995601
// We use the remote commitment chain's tip as it will soon become the tail
56005602
// once advanceTail is called.
5601-
remoteMessageIndex := lc.remoteCommitChain.tip().ourMessageIndex
5602-
localMessageIndex := lc.localCommitChain.tail().ourMessageIndex
5603+
remoteMessageIndex := lc.commitChains.Remote.tip().ourMessageIndex
5604+
localMessageIndex := lc.commitChains.Local.tail().ourMessageIndex
56035605

56045606
localPeerUpdates := lc.unsignedLocalUpdates(
56055607
remoteMessageIndex, localMessageIndex, chanID,
@@ -5660,7 +5662,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
56605662

56615663
// Since they revoked the current lowest height in their commitment
56625664
// chain, we can advance their chain by a single commitment.
5663-
lc.remoteCommitChain.advanceTail()
5665+
lc.commitChains.Remote.advanceTail()
56645666

56655667
// As we've just completed a new state transition, attempt to see if we
56665668
// can remove any entries from the update log which have been removed
@@ -5921,7 +5923,7 @@ func (lc *LightningChannel) validateAddHtlc(pd *PaymentDescriptor,
59215923
buffer BufferType) error {
59225924
// Make sure adding this HTLC won't violate any of the constraints we
59235925
// must keep on the commitment transactions.
5924-
remoteACKedIndex := lc.localCommitChain.tail().theirMessageIndex
5926+
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex
59255927

59265928
// First we'll check whether this HTLC can be added to the remote
59275929
// commitment transaction without violation any of the constraints.
@@ -5977,7 +5979,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64,
59775979
// PR).
59785980
}
59795981

5980-
localACKedIndex := lc.remoteCommitChain.tail().ourMessageIndex
5982+
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex
59815983

59825984
// Clamp down on the number of HTLC's we can receive by checking the
59835985
// commitment sanity.
@@ -8123,7 +8125,7 @@ func (lc *LightningChannel) availableBalance(
81238125

81248126
// We'll grab the current set of log updates that the remote has
81258127
// ACKed.
8126-
remoteACKedIndex := lc.localCommitChain.tip().theirMessageIndex
8128+
remoteACKedIndex := lc.commitChains.Local.tip().theirMessageIndex
81278129
htlcView := lc.fetchHTLCView(remoteACKedIndex,
81288130
lc.localUpdateLog.logIndex)
81298131

@@ -8308,7 +8310,7 @@ func (lc *LightningChannel) validateFeeRate(feePerKw chainfee.SatPerKWeight) err
83088310
availableBalance, txWeight := lc.availableBalance(AdditionalHtlc)
83098311

83108312
oldFee := lnwire.NewMSatFromSatoshis(
8311-
lc.localCommitChain.tip().feePerKw.FeeForWeight(txWeight),
8313+
lc.commitChains.Local.tip().feePerKw.FeeForWeight(txWeight),
83128314
)
83138315

83148316
// Our base balance is the total amount of satoshis we can commit
@@ -8641,7 +8643,7 @@ func (lc *LightningChannel) MaxFeeRate(
86418643
// exactly why it was introduced to react for sharp fee changes.
86428644
availableBalance, weight := lc.availableBalance(AdditionalHtlc)
86438645

8644-
currentFee := lc.localCommitChain.tip().feePerKw.FeeForWeight(weight)
8646+
currentFee := lc.commitChains.Local.tip().feePerKw.FeeForWeight(weight)
86458647

86468648
// baseBalance is the maximum amount available for us to spend on fees.
86478649
baseBalance := availableBalance.ToSatoshis() + currentFee

0 commit comments

Comments
 (0)