htlcswitch+lnwallet: quarantine paymentDescriptor to lnwallet

The objective of this commit is to make paymentDescriptor a private
data structure so we can quarantine it to the lnwallet package.
To accomplish this we had to prevent it from leaking out via the
arguments or return values of the public functions in lnwallet.
This naturally had consequences for the htlcswitch package as we
choose other mechanisms for tracking the data that paymentDescriptor
was responsible for.

Astoundingly, this was highly successful and allowed us to remove
a ton of redundant code. The diff for this commit represents a
substantial reduction in total lines of code as well as extraneous
arguments and return values from key functions.

This also sets the stage for future commits where we actually will
be attempting to rid lnwallet of paymentDescriptor completely.
This commit is contained in:
Keagan McClelland 2024-06-14 16:30:28 -07:00
parent 27ff5f085a
commit 4fadbb09bd
No known key found for this signature in database
GPG key ID: FA7E65C951F12439
5 changed files with 150 additions and 148 deletions

View file

@ -234,11 +234,11 @@ type commitment struct {
// outgoingHTLCs is a slice of all the outgoing HTLC's (from our PoV) // outgoingHTLCs is a slice of all the outgoing HTLC's (from our PoV)
// on this commitment transaction. // on this commitment transaction.
outgoingHTLCs []PaymentDescriptor outgoingHTLCs []paymentDescriptor
// incomingHTLCs is a slice of all the incoming HTLC's (from our PoV) // incomingHTLCs is a slice of all the incoming HTLC's (from our PoV)
// on this commitment transaction. // on this commitment transaction.
incomingHTLCs []PaymentDescriptor incomingHTLCs []paymentDescriptor
// customBlob stores opaque bytes that may be used by custom channels // customBlob stores opaque bytes that may be used by custom channels
// to store extra data for a given commitment state. // to store extra data for a given commitment state.
@ -254,8 +254,8 @@ type commitment struct {
// this map in order to locate the details needed to validate an HTLC // this map in order to locate the details needed to validate an HTLC
// signature while iterating of the outputs in the local commitment // signature while iterating of the outputs in the local commitment
// view. // view.
outgoingHTLCIndex map[int32]*PaymentDescriptor outgoingHTLCIndex map[int32]*paymentDescriptor
incomingHTLCIndex map[int32]*PaymentDescriptor incomingHTLCIndex map[int32]*paymentDescriptor
} }
// locateOutputIndex is a small helper function to locate the output index of a // locateOutputIndex is a small helper function to locate the output index of a
@ -263,7 +263,7 @@ type commitment struct {
// passed in is to be retained for each output within the commitment // passed in is to be retained for each output within the commitment
// transition. This ensures that we don't assign multiple HTLCs to the same // transition. This ensures that we don't assign multiple HTLCs to the same
// index within the commitment transaction. // index within the commitment transaction.
func locateOutputIndex(p *PaymentDescriptor, tx *wire.MsgTx, func locateOutputIndex(p *paymentDescriptor, tx *wire.MsgTx,
whoseCommit lntypes.ChannelParty, dups map[PaymentHash][]int32, whoseCommit lntypes.ChannelParty, dups map[PaymentHash][]int32,
cltvs []uint32) (int32, error) { cltvs []uint32) (int32, error) {
@ -303,7 +303,7 @@ func locateOutputIndex(p *PaymentDescriptor, tx *wire.MsgTx,
// populateHtlcIndexes modifies the set of HTLCs locked-into the target view // populateHtlcIndexes modifies the set of HTLCs locked-into the target view
// to have full indexing information populated. This information is required as // to have full indexing information populated. This information is required as
// we need to keep track of the indexes of each HTLC in order to properly write // we need to keep track of the indexes of each HTLC in order to properly write
// the current state to disk, and also to locate the PaymentDescriptor // the current state to disk, and also to locate the paymentDescriptor
// corresponding to HTLC outputs in the commitment transaction. // corresponding to HTLC outputs in the commitment transaction.
func (c *commitment) populateHtlcIndexes(chanType channeldb.ChannelType, func (c *commitment) populateHtlcIndexes(chanType channeldb.ChannelType,
cltvs []uint32) error { cltvs []uint32) error {
@ -313,12 +313,12 @@ func (c *commitment) populateHtlcIndexes(chanType channeldb.ChannelType,
// must keep this index so we can validate the HTLC signatures sent to // must keep this index so we can validate the HTLC signatures sent to
// us. // us.
dups := make(map[PaymentHash][]int32) dups := make(map[PaymentHash][]int32)
c.outgoingHTLCIndex = make(map[int32]*PaymentDescriptor) c.outgoingHTLCIndex = make(map[int32]*paymentDescriptor)
c.incomingHTLCIndex = make(map[int32]*PaymentDescriptor) c.incomingHTLCIndex = make(map[int32]*paymentDescriptor)
// populateIndex is a helper function that populates the necessary // populateIndex is a helper function that populates the necessary
// indexes within the commitment view for a particular HTLC. // indexes within the commitment view for a particular HTLC.
populateIndex := func(htlc *PaymentDescriptor, incoming bool) error { populateIndex := func(htlc *paymentDescriptor, incoming bool) error {
isDust := HtlcIsDust( isDust := HtlcIsDust(
chanType, incoming, c.whoseCommit, c.feePerKw, chanType, incoming, c.whoseCommit, c.feePerKw,
htlc.Amount.ToSatoshis(), c.dustLimit, htlc.Amount.ToSatoshis(), c.dustLimit,
@ -482,15 +482,15 @@ func (c *commitment) toDiskCommit(
func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
htlc *channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing], htlc *channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing],
whoseCommit lntypes.ChannelParty, whoseCommit lntypes.ChannelParty,
auxLeaf input.AuxTapLeaf) (PaymentDescriptor, error) { auxLeaf input.AuxTapLeaf) (paymentDescriptor, error) {
// The proper pkScripts for this PaymentDescriptor must be // The proper pkScripts for this paymentDescriptor must be
// generated so we can easily locate them within the commitment // generated so we can easily locate them within the commitment
// transaction in the future. // transaction in the future.
var ( var (
ourP2WSH, theirP2WSH []byte ourP2WSH, theirP2WSH []byte
ourWitnessScript, theirWitnessScript []byte ourWitnessScript, theirWitnessScript []byte
pd PaymentDescriptor pd paymentDescriptor
chanType = lc.channelState.ChanType chanType = lc.channelState.ChanType
) )
@ -550,7 +550,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
// With the scripts reconstructed (depending on if this is our commit // With the scripts reconstructed (depending on if this is our commit
// vs theirs or a pending commit for the remote party), we can now // vs theirs or a pending commit for the remote party), we can now
// re-create the original payment descriptor. // re-create the original payment descriptor.
return PaymentDescriptor{ return paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
RHash: htlc.RHash, RHash: htlc.RHash,
Timeout: htlc.RefundTimeout, Timeout: htlc.RefundTimeout,
@ -577,16 +577,16 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
func (lc *LightningChannel) extractPayDescs(feeRate chainfee.SatPerKWeight, func (lc *LightningChannel) extractPayDescs(feeRate chainfee.SatPerKWeight,
htlcs []channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing], htlcs []channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing],
whoseCommit lntypes.ChannelParty, whoseCommit lntypes.ChannelParty,
auxLeaves fn.Option[CommitAuxLeaves]) ([]PaymentDescriptor, auxLeaves fn.Option[CommitAuxLeaves]) ([]paymentDescriptor,
[]PaymentDescriptor, error) { []paymentDescriptor, error) {
var ( var (
incomingHtlcs []PaymentDescriptor incomingHtlcs []paymentDescriptor
outgoingHtlcs []PaymentDescriptor outgoingHtlcs []paymentDescriptor
) )
// For each included HTLC within this commitment state, we'll convert // For each included HTLC within this commitment state, we'll convert
// the disk format into our in memory PaymentDescriptor format, // the disk format into our in memory paymentDescriptor format,
// partitioning based on if we offered or received the HTLC. // partitioning based on if we offered or received the HTLC.
for _, htlc := range htlcs { for _, htlc := range htlcs {
// TODO(roasbeef): set isForwarded to false for all? need to // TODO(roasbeef): set isForwarded to false for all? need to
@ -668,7 +668,7 @@ func (lc *LightningChannel) diskCommitToMemCommit(
} }
// With the key rings re-created, we'll now convert all the on-disk // With the key rings re-created, we'll now convert all the on-disk
// HTLC"s into PaymentDescriptor's so we can re-insert them into our // HTLC"s into paymentDescriptor's so we can re-insert them into our
// update log. // update log.
incomingHtlcs, outgoingHtlcs, err := lc.extractPayDescs( incomingHtlcs, outgoingHtlcs, err := lc.extractPayDescs(
chainfee.SatPerKWeight(diskCommit.FeePerKw), chainfee.SatPerKWeight(diskCommit.FeePerKw),
@ -1024,7 +1024,7 @@ func (lc *LightningChannel) ResetState() {
lc.Unlock() lc.Unlock()
} }
// logUpdateToPayDesc converts a LogUpdate into a matching PaymentDescriptor // logUpdateToPayDesc converts a LogUpdate into a matching paymentDescriptor
// entry that can be re-inserted into the update log. This method is used when // entry that can be re-inserted into the update log. This method is used when
// we extended a state to the remote party, but the connection was obstructed // we extended a state to the remote party, but the connection was obstructed
// before we could finish the commitment dance. In this case, we need to // before we could finish the commitment dance. In this case, we need to
@ -1034,25 +1034,25 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
remoteUpdateLog *updateLog, commitHeight uint64, remoteUpdateLog *updateLog, commitHeight uint64,
feeRate chainfee.SatPerKWeight, remoteCommitKeys *CommitmentKeyRing, feeRate chainfee.SatPerKWeight, remoteCommitKeys *CommitmentKeyRing,
remoteDustLimit btcutil.Amount, remoteDustLimit btcutil.Amount,
auxLeaves fn.Option[CommitAuxLeaves]) (*PaymentDescriptor, error) { auxLeaves fn.Option[CommitAuxLeaves]) (*paymentDescriptor, error) {
// Depending on the type of update message we'll map that to a distinct // Depending on the type of update message we'll map that to a distinct
// PaymentDescriptor instance. // paymentDescriptor instance.
var pd *PaymentDescriptor var pd *paymentDescriptor
switch wireMsg := logUpdate.UpdateMsg.(type) { switch wireMsg := logUpdate.UpdateMsg.(type) {
// For offered HTLC's, we'll map that to a PaymentDescriptor with the // For offered HTLC's, we'll map that to a paymentDescriptor with the
// type Add, ensuring we restore the necessary fields. From the PoV of // type Add, ensuring we restore the necessary fields. From the PoV of
// the commitment chain, this HTLC was included in the remote chain, // the commitment chain, this HTLC was included in the remote chain,
// but not the local chain. // but not the local chain.
case *lnwire.UpdateAddHTLC: case *lnwire.UpdateAddHTLC:
// First, we'll map all the relevant fields in the // First, we'll map all the relevant fields in the
// UpdateAddHTLC message to their corresponding fields in the // UpdateAddHTLC message to their corresponding fields in the
// PaymentDescriptor struct. We also set addCommitHeightRemote // paymentDescriptor struct. We also set addCommitHeightRemote
// as we've included this HTLC in our local commitment chain // as we've included this HTLC in our local commitment chain
// for the remote party. // for the remote party.
pd = &PaymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
RHash: wireMsg.PaymentHash, RHash: wireMsg.PaymentHash,
Timeout: wireMsg.Expiry, Timeout: wireMsg.Expiry,
@ -1093,11 +1093,11 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
// For HTLC's we're offered we'll fetch the original offered HTLC // For HTLC's we're offered we'll fetch the original offered HTLC
// from the remote party's update log so we can retrieve the same // from the remote party's update log so we can retrieve the same
// PaymentDescriptor that SettleHTLC would produce. // paymentDescriptor that SettleHTLC would produce.
case *lnwire.UpdateFulfillHTLC: case *lnwire.UpdateFulfillHTLC:
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
pd = &PaymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1115,7 +1115,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
case *lnwire.UpdateFailHTLC: case *lnwire.UpdateFailHTLC:
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
pd = &PaymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1132,7 +1132,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
// TODO(roasbeef): err if nil? // TODO(roasbeef): err if nil?
pd = &PaymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1151,7 +1151,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
// height to the same value, as we consider the fee update locked in by // height to the same value, as we consider the fee update locked in by
// adding and removing it at the same height. // adding and removing it at the same height.
case *lnwire.UpdateFee: case *lnwire.UpdateFee:
pd = &PaymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
@ -1166,29 +1166,29 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
return pd, nil return pd, nil
} }
// localLogUpdateToPayDesc converts a LogUpdate into a matching PaymentDescriptor // localLogUpdateToPayDesc converts a LogUpdate into a matching
// entry that can be re-inserted into the local update log. This method is used // paymentDescriptor entry that can be re-inserted into the local update log.
// when we sent an update+sig, receive a revocation, but drop right before the // This method is used when we sent an update+sig, receive a revocation, but
// counterparty can sign for the update we just sent. In this case, we need to // drop right before the counterparty can sign for the update we just sent. In
// re-insert the original entries back into the update log so we'll be expecting // this case, we need to re-insert the original entries back into the update
// the peer to sign them. The height of the remote commitment is expected to be // log so we'll be expecting the peer to sign them. The height of the remote
// provided and we restore all log update entries with this height, even though // commitment is expected to be provided and we restore all log update entries
// the real height may be lower. In the way these fields are used elsewhere, this // with this height, even though the real height may be lower. In the way these
// doesn't change anything. // fields are used elsewhere, this doesn't change anything.
func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate, func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
remoteUpdateLog *updateLog, commitHeight uint64) (*PaymentDescriptor, remoteUpdateLog *updateLog, commitHeight uint64) (*paymentDescriptor,
error) { error) {
// Since Add updates aren't saved to disk under this key, the update will // Since Add updates aren't saved to disk under this key, the update will
// never be an Add. // never be an Add.
switch wireMsg := logUpdate.UpdateMsg.(type) { switch wireMsg := logUpdate.UpdateMsg.(type) {
// For HTLCs that we settled, we'll fetch the original offered HTLC from // For HTLCs that we settled, we'll fetch the original offered HTLC from
// the remote update log so we can retrieve the same PaymentDescriptor that // the remote update log so we can retrieve the same paymentDescriptor
// ReceiveHTLCSettle would produce. // that ReceiveHTLCSettle would produce.
case *lnwire.UpdateFulfillHTLC: case *lnwire.UpdateFulfillHTLC:
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1205,7 +1205,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
case *lnwire.UpdateFailHTLC: case *lnwire.UpdateFailHTLC:
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1221,7 +1221,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
case *lnwire.UpdateFailMalformedHTLC: case *lnwire.UpdateFailMalformedHTLC:
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1234,7 +1234,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
}, nil }, nil
case *lnwire.UpdateFee: case *lnwire.UpdateFee:
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
@ -1251,7 +1251,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
} }
// remoteLogUpdateToPayDesc converts a LogUpdate into a matching // remoteLogUpdateToPayDesc converts a LogUpdate into a matching
// PaymentDescriptor entry that can be re-inserted into the update log. This // paymentDescriptor entry that can be re-inserted into the update log. This
// method is used when we revoked a local commitment, but the connection was // method is used when we revoked a local commitment, but the connection was
// obstructed before we could sign a remote commitment that contains these // obstructed before we could sign a remote commitment that contains these
// updates. In this case, we need to re-insert the original entries back into // updates. In this case, we need to re-insert the original entries back into
@ -1261,12 +1261,12 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
// may be lower. In the way these fields are used elsewhere, this doesn't change // may be lower. In the way these fields are used elsewhere, this doesn't change
// anything. // anything.
func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate, func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
localUpdateLog *updateLog, commitHeight uint64) (*PaymentDescriptor, localUpdateLog *updateLog, commitHeight uint64) (*paymentDescriptor,
error) { error) {
switch wireMsg := logUpdate.UpdateMsg.(type) { switch wireMsg := logUpdate.UpdateMsg.(type) {
case *lnwire.UpdateAddHTLC: case *lnwire.UpdateAddHTLC:
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
RHash: wireMsg.PaymentHash, RHash: wireMsg.PaymentHash,
Timeout: wireMsg.Expiry, Timeout: wireMsg.Expiry,
@ -1287,11 +1287,11 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
// For HTLCs that the remote party settled, we'll fetch the original // For HTLCs that the remote party settled, we'll fetch the original
// offered HTLC from the local update log so we can retrieve the same // offered HTLC from the local update log so we can retrieve the same
// PaymentDescriptor that ReceiveHTLCSettle would produce. // paymentDescriptor that ReceiveHTLCSettle would produce.
case *lnwire.UpdateFulfillHTLC: case *lnwire.UpdateFulfillHTLC:
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1308,7 +1308,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
case *lnwire.UpdateFailHTLC: case *lnwire.UpdateFailHTLC:
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1324,7 +1324,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
case *lnwire.UpdateFailMalformedHTLC: case *lnwire.UpdateFailMalformedHTLC:
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
@ -1343,7 +1343,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
// height to the same value, as we consider the fee update locked in by // height to the same value, as we consider the fee update locked in by
// adding and removing it at the same height. // adding and removing it at the same height.
case *lnwire.UpdateFee: case *lnwire.UpdateFee:
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
@ -2536,10 +2536,10 @@ type HtlcView struct {
NextHeight uint64 NextHeight uint64
// OurUpdates are our outgoing HTLCs. // OurUpdates are our outgoing HTLCs.
OurUpdates []*PaymentDescriptor OurUpdates []*paymentDescriptor
// TheirUpdates are their incoming HTLCs. // TheirUpdates are their incoming HTLCs.
TheirUpdates []*PaymentDescriptor TheirUpdates []*paymentDescriptor
// FeePerKw is the fee rate in sat/kw of the commitment transaction. // FeePerKw is the fee rate in sat/kw of the commitment transaction.
FeePerKw chainfee.SatPerKWeight FeePerKw chainfee.SatPerKWeight
@ -2551,7 +2551,7 @@ type HtlcView struct {
func (lc *LightningChannel) fetchHTLCView(theirLogIndex, func (lc *LightningChannel) fetchHTLCView(theirLogIndex,
ourLogIndex uint64) *HtlcView { ourLogIndex uint64) *HtlcView {
var ourHTLCs []*PaymentDescriptor var ourHTLCs []*paymentDescriptor
for e := lc.updateLogs.Local.Front(); e != nil; e = e.Next() { for e := lc.updateLogs.Local.Front(); e != nil; e = e.Next() {
htlc := e.Value htlc := e.Value
@ -2563,7 +2563,7 @@ func (lc *LightningChannel) fetchHTLCView(theirLogIndex,
} }
} }
var theirHTLCs []*PaymentDescriptor var theirHTLCs []*paymentDescriptor
for e := lc.updateLogs.Remote.Front(); e != nil; e = e.Next() { for e := lc.updateLogs.Remote.Front(); e != nil; e = e.Next() {
htlc := e.Value htlc := e.Value
@ -2701,13 +2701,13 @@ func (lc *LightningChannel) fetchCommitmentView(
// commitment are mutated, we'll manually copy over each HTLC to its // commitment are mutated, we'll manually copy over each HTLC to its
// respective slice. // respective slice.
c.outgoingHTLCs = make( c.outgoingHTLCs = make(
[]PaymentDescriptor, len(filteredHTLCView.OurUpdates), []paymentDescriptor, len(filteredHTLCView.OurUpdates),
) )
for i, htlc := range filteredHTLCView.OurUpdates { for i, htlc := range filteredHTLCView.OurUpdates {
c.outgoingHTLCs[i] = *htlc c.outgoingHTLCs[i] = *htlc
} }
c.incomingHTLCs = make( c.incomingHTLCs = make(
[]PaymentDescriptor, len(filteredHTLCView.TheirUpdates), []paymentDescriptor, len(filteredHTLCView.TheirUpdates),
) )
for i, htlc := range filteredHTLCView.TheirUpdates { for i, htlc := range filteredHTLCView.TheirUpdates {
c.incomingHTLCs[i] = *htlc c.incomingHTLCs[i] = *htlc
@ -2882,9 +2882,9 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
// fetchParent is a helper that looks up update log parent entries in the // fetchParent is a helper that looks up update log parent entries in the
// appropriate log. // appropriate log.
func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor, func (lc *LightningChannel) fetchParent(entry *paymentDescriptor,
whoseCommitChain, whoseUpdateLog lntypes.ChannelParty, whoseCommitChain, whoseUpdateLog lntypes.ChannelParty,
) (*PaymentDescriptor, error) { ) (*paymentDescriptor, error) {
var ( var (
updateLog *updateLog updateLog *updateLog
@ -2937,7 +2937,7 @@ func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor,
// If the HTLC hasn't yet been committed in either chain, then the height it // If the HTLC hasn't yet been committed in either chain, then the height it
// was committed is updated. Keeping track of this inclusion height allows us to // was committed is updated. Keeping track of this inclusion height allows us to
// later compact the log once the change is fully committed in both chains. // later compact the log once the change is fully committed in both chains.
func processAddEntry(htlc *PaymentDescriptor, ourBalance, func processAddEntry(htlc *paymentDescriptor, ourBalance,
theirBalance *lnwire.MilliSatoshi, nextHeight uint64, theirBalance *lnwire.MilliSatoshi, nextHeight uint64,
whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) { whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) {
@ -2975,7 +2975,7 @@ func processAddEntry(htlc *PaymentDescriptor, ourBalance,
// processRemoveEntry processes a log entry which settles or times out a // processRemoveEntry processes a log entry which settles or times out a
// previously added HTLC. If the removal entry has already been processed, it // previously added HTLC. If the removal entry has already been processed, it
// is skipped. // is skipped.
func processRemoveEntry(htlc *PaymentDescriptor, ourBalance, func processRemoveEntry(htlc *paymentDescriptor, ourBalance,
theirBalance *lnwire.MilliSatoshi, nextHeight uint64, theirBalance *lnwire.MilliSatoshi, nextHeight uint64,
whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) { whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) {
@ -3024,7 +3024,7 @@ func processRemoveEntry(htlc *PaymentDescriptor, ourBalance,
// processFeeUpdate processes a log update that updates the current commitment // processFeeUpdate processes a log update that updates the current commitment
// fee. // fee.
func processFeeUpdate(feeUpdate *PaymentDescriptor, nextHeight uint64, func processFeeUpdate(feeUpdate *paymentDescriptor, nextHeight uint64,
whoseCommitChain lntypes.ChannelParty, mutateState bool, whoseCommitChain lntypes.ChannelParty, mutateState bool,
view *HtlcView) { view *HtlcView) {
@ -3298,7 +3298,7 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
continue continue
} }
// We'll map the type of the PaymentDescriptor to one of the // We'll map the type of the paymentDescriptor to one of the
// four messages that it corresponds to. With this set of // four messages that it corresponds to. With this set of
// messages obtained, we can simply read from disk and re-send // messages obtained, we can simply read from disk and re-send
// them in the case of a needed channel sync. // them in the case of a needed channel sync.
@ -3333,7 +3333,7 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
// Nothing special to do. // Nothing special to do.
} }
logUpdates = append(logUpdates, pd.ToLogUpdate()) logUpdates = append(logUpdates, pd.toLogUpdate())
} }
// With the set of log updates mapped into wire messages, we'll now // With the set of log updates mapped into wire messages, we'll now
@ -3391,7 +3391,7 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
continue continue
} }
logUpdates = append(logUpdates, pd.ToLogUpdate()) logUpdates = append(logUpdates, pd.toLogUpdate())
} }
return logUpdates return logUpdates
@ -3535,11 +3535,11 @@ func (lc *LightningChannel) applyCommitFee(
// commitment transaction in terms of the ChannelConstraints that we and our // commitment transaction in terms of the ChannelConstraints that we and our
// remote peer agreed upon during the funding workflow. The // remote peer agreed upon during the funding workflow. The
// predict[Our|Their]Add should parameters should be set to a valid // predict[Our|Their]Add should parameters should be set to a valid
// PaymentDescriptor if we are validating in the state when adding a new HTLC, // paymentDescriptor if we are validating in the state when adding a new HTLC,
// or nil otherwise. // or nil otherwise.
func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
ourLogCounter uint64, whoseCommitChain lntypes.ChannelParty, ourLogCounter uint64, whoseCommitChain lntypes.ChannelParty,
buffer BufferType, predictOurAdd, predictTheirAdd *PaymentDescriptor, buffer BufferType, predictOurAdd, predictTheirAdd *paymentDescriptor,
) error { ) error {
// First fetch the initial balance before applying any updates. // First fetch the initial balance before applying any updates.
@ -3666,7 +3666,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
// validateUpdates take a set of updates, and validates them against // validateUpdates take a set of updates, and validates them against
// the passed channel constraints. // the passed channel constraints.
validateUpdates := func(updates []*PaymentDescriptor, validateUpdates := func(updates []*paymentDescriptor,
constraints *channeldb.ChannelConfig) error { constraints *channeldb.ChannelConfig) error {
// We keep track of the number of HTLCs in flight for the // We keep track of the number of HTLCs in flight for the
@ -5390,7 +5390,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
// updates that we will eventually put into the // updates that we will eventually put into the
// FwdPkg at this height. // FwdPkg at this height.
addUpdatesToForward = append( addUpdatesToForward = append(
addUpdatesToForward, pd.ToLogUpdate(), addUpdatesToForward, pd.toLogUpdate(),
) )
case pd.EntryType != Add && committedRmv && shouldFwdRmv: case pd.EntryType != Add && committedRmv && shouldFwdRmv:
@ -5410,10 +5410,12 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
// updates that we will eventually put into the // updates that we will eventually put into the
// FwdPkg at this height. // FwdPkg at this height.
settleFailUpdatesToForward = append( settleFailUpdatesToForward = append(
settleFailUpdatesToForward, pd.ToLogUpdate(), settleFailUpdatesToForward, pd.toLogUpdate(),
) )
default: default:
// The update was not "freshly locked in" so we will
// ignore it as we construct the forwarding package.
continue continue
} }
} }
@ -5720,9 +5722,9 @@ func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
// htlcAddDescriptor returns a payment descriptor for the htlc and open key // htlcAddDescriptor returns a payment descriptor for the htlc and open key
// provided to add to our local update log. // provided to add to our local update log.
func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC, func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC,
openKey *models.CircuitKey) *PaymentDescriptor { openKey *models.CircuitKey) *paymentDescriptor {
return &PaymentDescriptor{ return &paymentDescriptor{
ChanID: htlc.ChanID, ChanID: htlc.ChanID,
EntryType: Add, EntryType: Add,
RHash: PaymentHash(htlc.PaymentHash), RHash: PaymentHash(htlc.PaymentHash),
@ -5739,7 +5741,7 @@ func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC,
// validateAddHtlc validates the addition of an outgoing htlc to our local and // validateAddHtlc validates the addition of an outgoing htlc to our local and
// remote commitments. // remote commitments.
func (lc *LightningChannel) validateAddHtlc(pd *PaymentDescriptor, func (lc *LightningChannel) validateAddHtlc(pd *paymentDescriptor,
buffer BufferType) error { buffer BufferType) error {
// Make sure adding this HTLC won't violate any of the constraints we // Make sure adding this HTLC won't violate any of the constraints we
// must keep on the commitment transactions. // must keep on the commitment transactions.
@ -5785,7 +5787,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64,
"ID %d", htlc.ID, lc.updateLogs.Remote.htlcCounter) "ID %d", htlc.ID, lc.updateLogs.Remote.htlcCounter)
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: htlc.ChanID, ChanID: htlc.ChanID,
EntryType: Add, EntryType: Add,
RHash: PaymentHash(htlc.PaymentHash), RHash: PaymentHash(htlc.PaymentHash),
@ -5866,7 +5868,7 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte,
return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]} return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]}
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
Amount: htlc.Amount, Amount: htlc.Amount,
RPreimage: preimage, RPreimage: preimage,
@ -5912,7 +5914,7 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, htlcIndex uint6
return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]} return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]}
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
Amount: htlc.Amount, Amount: htlc.Amount,
RPreimage: preimage, RPreimage: preimage,
@ -5974,7 +5976,7 @@ func (lc *LightningChannel) FailHTLC(htlcIndex uint64, reason []byte,
return ErrHtlcIndexAlreadyFailed(htlcIndex) return ErrHtlcIndexAlreadyFailed(htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
Amount: htlc.Amount, Amount: htlc.Amount,
RHash: htlc.RHash, RHash: htlc.RHash,
@ -6025,7 +6027,7 @@ func (lc *LightningChannel) MalformedFailHTLC(htlcIndex uint64,
return ErrHtlcIndexAlreadyFailed(htlcIndex) return ErrHtlcIndexAlreadyFailed(htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
Amount: htlc.Amount, Amount: htlc.Amount,
RHash: htlc.RHash, RHash: htlc.RHash,
@ -6068,7 +6070,7 @@ func (lc *LightningChannel) ReceiveFailHTLC(htlcIndex uint64, reason []byte,
return ErrHtlcIndexAlreadyFailed(htlcIndex) return ErrHtlcIndexAlreadyFailed(htlcIndex)
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
Amount: htlc.Amount, Amount: htlc.Amount,
RHash: htlc.RHash, RHash: htlc.RHash,
@ -8188,7 +8190,7 @@ func (lc *LightningChannel) UpdateFee(feePerKw chainfee.SatPerKWeight) error {
return err return err
} }
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
LogIndex: lc.updateLogs.Local.logIndex, LogIndex: lc.updateLogs.Local.logIndex,
Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)),
@ -8261,7 +8263,7 @@ func (lc *LightningChannel) ReceiveUpdateFee(feePerKw chainfee.SatPerKWeight) er
} }
// TODO(roasbeef): or just modify to use the other balance? // TODO(roasbeef): or just modify to use the other balance?
pd := &PaymentDescriptor{ pd := &paymentDescriptor{
ChanID: lc.ChannelID(), ChanID: lc.ChannelID(),
LogIndex: lc.updateLogs.Remote.logIndex, LogIndex: lc.updateLogs.Remote.logIndex,
Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)),
@ -8752,7 +8754,7 @@ func (lc *LightningChannel) unsignedLocalUpdates(remoteMessageIndex,
// sends. // sends.
if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex { if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex {
localPeerUpdates = append( localPeerUpdates = append(
localPeerUpdates, pd.ToLogUpdate(), localPeerUpdates, pd.toLogUpdate(),
) )
} }
} }

View file

@ -6934,8 +6934,8 @@ func TestNewBreachRetributionSkipsDustHtlcs(t *testing.T) {
} }
} }
// compareHtlcs compares two PaymentDescriptors. // compareHtlcs compares two paymentDescriptors.
func compareHtlcs(htlc1, htlc2 *PaymentDescriptor) error { func compareHtlcs(htlc1, htlc2 *paymentDescriptor) error {
if htlc1.LogIndex != htlc2.LogIndex { if htlc1.LogIndex != htlc2.LogIndex {
return fmt.Errorf("htlc log index did not match") return fmt.Errorf("htlc log index did not match")
} }
@ -6953,7 +6953,7 @@ func compareHtlcs(htlc1, htlc2 *PaymentDescriptor) error {
} }
// compareIndexes is a helper method to compare two index maps. // compareIndexes is a helper method to compare two index maps.
func compareIndexes(a, b map[uint64]*fn.Node[*PaymentDescriptor]) error { func compareIndexes(a, b map[uint64]*fn.Node[*paymentDescriptor]) error {
for k1, e1 := range a { for k1, e1 := range a {
e2, ok := b[k1] e2, ok := b[k1]
if !ok { if !ok {
@ -7422,7 +7422,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) {
t.Fatalf("unable to create new channel: %v", err) t.Fatalf("unable to create new channel: %v", err)
} }
var pd *PaymentDescriptor var pd *paymentDescriptor
if remoteLog { if remoteLog {
h := newChannel.updateLogs.Local.lookupHtlc(htlcIndex) h := newChannel.updateLogs.Local.lookupHtlc(htlcIndex)
if h != nil { if h != nil {
@ -8191,8 +8191,8 @@ func TestFetchParent(t *testing.T) {
name string name string
whoseCommitChain lntypes.ChannelParty whoseCommitChain lntypes.ChannelParty
whoseUpdateLog lntypes.ChannelParty whoseUpdateLog lntypes.ChannelParty
localEntries []*PaymentDescriptor localEntries []*paymentDescriptor
remoteEntries []*PaymentDescriptor remoteEntries []*paymentDescriptor
// parentIndex is the parent index of the entry that we will // parentIndex is the parent index of the entry that we will
// lookup with fetch parent. // lookup with fetch parent.
@ -8226,7 +8226,7 @@ func TestFetchParent(t *testing.T) {
{ {
name: "remote log + chain, remote add height 0", name: "remote log + chain, remote add height 0",
localEntries: nil, localEntries: nil,
remoteEntries: []*PaymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8248,7 +8248,7 @@ func TestFetchParent(t *testing.T) {
}, },
{ {
name: "remote log, local chain, local add height 0", name: "remote log, local chain, local add height 0",
remoteEntries: []*PaymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8271,7 +8271,7 @@ func TestFetchParent(t *testing.T) {
}, },
{ {
name: "local log + chain, local add height 0", name: "local log + chain, local add height 0",
localEntries: []*PaymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8295,7 +8295,7 @@ func TestFetchParent(t *testing.T) {
{ {
name: "local log + remote chain, remote add height 0", name: "local log + remote chain, remote add height 0",
localEntries: []*PaymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8319,7 +8319,7 @@ func TestFetchParent(t *testing.T) {
{ {
name: "remote log found", name: "remote log found",
localEntries: nil, localEntries: nil,
remoteEntries: []*PaymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8342,7 +8342,7 @@ func TestFetchParent(t *testing.T) {
}, },
{ {
name: "local log found", name: "local log found",
localEntries: []*PaymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
@ -8388,7 +8388,7 @@ func TestFetchParent(t *testing.T) {
} }
parent, err := lc.fetchParent( parent, err := lc.fetchParent(
&PaymentDescriptor{ &paymentDescriptor{
ParentIndex: test.parentIndex, ParentIndex: test.parentIndex,
}, },
test.whoseCommitChain, test.whoseCommitChain,
@ -8451,8 +8451,8 @@ func TestEvaluateView(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ourHtlcs []*PaymentDescriptor ourHtlcs []*paymentDescriptor
theirHtlcs []*PaymentDescriptor theirHtlcs []*paymentDescriptor
whoseCommitChain lntypes.ChannelParty whoseCommitChain lntypes.ChannelParty
mutateState bool mutateState bool
@ -8484,7 +8484,7 @@ func TestEvaluateView(t *testing.T) {
name: "our fee update is applied", name: "our fee update is applied",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
Amount: ourFeeUpdateAmt, Amount: ourFeeUpdateAmt,
EntryType: FeeUpdate, EntryType: FeeUpdate,
@ -8501,8 +8501,8 @@ func TestEvaluateView(t *testing.T) {
name: "their fee update is applied", name: "their fee update is applied",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*PaymentDescriptor{}, ourHtlcs: []*paymentDescriptor{},
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
Amount: theirFeeUpdateAmt, Amount: theirFeeUpdateAmt,
EntryType: FeeUpdate, EntryType: FeeUpdate,
@ -8519,14 +8519,14 @@ func TestEvaluateView(t *testing.T) {
name: "htlcs adds without settles", name: "htlcs adds without settles",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
}, },
}, },
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8553,7 +8553,7 @@ func TestEvaluateView(t *testing.T) {
name: "our htlc settled, state mutated", name: "our htlc settled, state mutated",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: true, mutateState: true,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8561,7 +8561,7 @@ func TestEvaluateView(t *testing.T) {
addCommitHeightLocal: addHeight, addCommitHeightLocal: addHeight,
}, },
}, },
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8588,7 +8588,7 @@ func TestEvaluateView(t *testing.T) {
name: "our htlc settled, state not mutated", name: "our htlc settled, state not mutated",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8596,7 +8596,7 @@ func TestEvaluateView(t *testing.T) {
addCommitHeightLocal: addHeight, addCommitHeightLocal: addHeight,
}, },
}, },
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8623,7 +8623,7 @@ func TestEvaluateView(t *testing.T) {
name: "their htlc settled, state mutated", name: "their htlc settled, state mutated",
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: true, mutateState: true,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8638,7 +8638,7 @@ func TestEvaluateView(t *testing.T) {
ParentIndex: 1, ParentIndex: 1,
}, },
}, },
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8667,7 +8667,7 @@ func TestEvaluateView(t *testing.T) {
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*PaymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8682,7 +8682,7 @@ func TestEvaluateView(t *testing.T) {
ParentIndex: 0, ParentIndex: 0,
}, },
}, },
theirHtlcs: []*PaymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
@ -8789,7 +8789,7 @@ func TestEvaluateView(t *testing.T) {
// checkExpectedHtlcs checks that a set of htlcs that we have contains all the // checkExpectedHtlcs checks that a set of htlcs that we have contains all the
// htlcs we expect. // htlcs we expect.
func checkExpectedHtlcs(t *testing.T, actual []*PaymentDescriptor, func checkExpectedHtlcs(t *testing.T, actual []*paymentDescriptor,
expected map[uint64]bool) { expected map[uint64]bool) {
if len(expected) != len(actual) { if len(expected) != len(actual) {
@ -8981,7 +8981,7 @@ func TestProcessFeeUpdate(t *testing.T) {
// Create a fee update with add and remove heights as // Create a fee update with add and remove heights as
// set in the test. // set in the test.
heights := test.startHeights heights := test.startHeights
update := &PaymentDescriptor{ update := &paymentDescriptor{
Amount: ourFeeUpdateAmt, Amount: ourFeeUpdateAmt,
addCommitHeightRemote: heights.remoteAdd, addCommitHeightRemote: heights.remoteAdd,
addCommitHeightLocal: heights.localAdd, addCommitHeightLocal: heights.localAdd,
@ -9008,7 +9008,7 @@ func TestProcessFeeUpdate(t *testing.T) {
} }
} }
func checkHeights(t *testing.T, update *PaymentDescriptor, expected heights) { func checkHeights(t *testing.T, update *paymentDescriptor, expected heights) {
updateHeights := heights{ updateHeights := heights{
localAdd: update.addCommitHeightLocal, localAdd: update.addCommitHeightLocal,
localRemove: update.removeCommitHeightLocal, localRemove: update.removeCommitHeightLocal,
@ -9377,7 +9377,7 @@ func TestProcessAddRemoveEntry(t *testing.T) {
t.Parallel() t.Parallel()
heights := test.startHeights heights := test.startHeights
update := &PaymentDescriptor{ update := &paymentDescriptor{
Amount: updateAmount, Amount: updateAmount,
addCommitHeightLocal: heights.localAdd, addCommitHeightLocal: heights.localAdd,
addCommitHeightRemote: heights.remoteAdd, addCommitHeightRemote: heights.remoteAdd,
@ -10572,7 +10572,7 @@ func testNewBreachRetribution(t *testing.T, chanType channeldb.ChannelType) {
} }
// TestExtractPayDescs asserts that `extractPayDescs` can correctly turn a // TestExtractPayDescs asserts that `extractPayDescs` can correctly turn a
// slice of htlcs into two slices of PaymentDescriptors. // slice of htlcs into two slices of paymentDescriptors.
func TestExtractPayDescs(t *testing.T) { func TestExtractPayDescs(t *testing.T) {
t.Parallel() t.Parallel()
@ -10609,22 +10609,22 @@ func TestExtractPayDescs(t *testing.T) {
) )
require.NoError(t, err) require.NoError(t, err)
// Assert the incoming PaymentDescriptors are matched. // Assert the incoming paymentDescriptors are matched.
for i, pd := range incomingPDs { for i, pd := range incomingPDs {
htlc := incomings[i] htlc := incomings[i]
assertPayDescMatchHTLC(t, pd, htlc) assertPayDescMatchHTLC(t, pd, htlc)
} }
// Assert the outgoing PaymentDescriptors are matched. // Assert the outgoing paymentDescriptors are matched.
for i, pd := range outgoingPDs { for i, pd := range outgoingPDs {
htlc := outgoings[i] htlc := outgoings[i]
assertPayDescMatchHTLC(t, pd, htlc) assertPayDescMatchHTLC(t, pd, htlc)
} }
} }
// assertPayDescMatchHTLC compares a PaymentDescriptor to a channeldb.HTLC and // assertPayDescMatchHTLC compares a paymentDescriptor to a channeldb.HTLC and
// asserts that the fields are matched. // asserts that the fields are matched.
func assertPayDescMatchHTLC(t *testing.T, pd PaymentDescriptor, func assertPayDescMatchHTLC(t *testing.T, pd paymentDescriptor,
htlc channeldb.HTLC) { htlc channeldb.HTLC) {
require := require.New(t) require := require.New(t)

View file

@ -1229,10 +1229,10 @@ func genHtlcScript(chanType channeldb.ChannelType, isIncoming bool,
// is incoming and if it's being applied to our commitment transaction or that // is incoming and if it's being applied to our commitment transaction or that
// of the remote node's. Additionally, in order to be able to efficiently // of the remote node's. Additionally, in order to be able to efficiently
// locate the added HTLC on the commitment transaction from the // locate the added HTLC on the commitment transaction from the
// PaymentDescriptor that generated it, the generated script is stored within // paymentDescriptor that generated it, the generated script is stored within
// the descriptor itself. // the descriptor itself.
func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty, func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty,
isIncoming bool, paymentDesc *PaymentDescriptor, isIncoming bool, paymentDesc *paymentDescriptor,
keyRing *CommitmentKeyRing, chanType channeldb.ChannelType, keyRing *CommitmentKeyRing, chanType channeldb.ChannelType,
auxLeaf input.AuxTapLeaf) error { auxLeaf input.AuxTapLeaf) error {
@ -1253,7 +1253,7 @@ func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty,
amountPending := int64(paymentDesc.Amount.ToSatoshis()) amountPending := int64(paymentDesc.Amount.ToSatoshis())
commitTx.AddTxOut(wire.NewTxOut(amountPending, pkScript)) commitTx.AddTxOut(wire.NewTxOut(amountPending, pkScript))
// Store the pkScript of this particular PaymentDescriptor so we can // Store the pkScript of this particular paymentDescriptor so we can
// quickly locate it within the commitment transaction later. // quickly locate it within the commitment transaction later.
if whoseCommit.IsLocal() { if whoseCommit.IsLocal() {
paymentDesc.ourPkScript = pkScript paymentDesc.ourPkScript = pkScript

View file

@ -62,17 +62,17 @@ func (u updateType) String() string {
} }
} }
// PaymentDescriptor represents a commitment state update which either adds, // paymentDescriptor represents a commitment state update which either adds,
// settles, or removes an HTLC. PaymentDescriptors encapsulate all necessary // settles, or removes an HTLC. paymentDescriptors encapsulate all necessary
// metadata w.r.t to an HTLC, and additional data pairing a settle message to // metadata w.r.t to an HTLC, and additional data pairing a settle message to
// the original added HTLC. // the original added HTLC.
// //
// TODO(roasbeef): LogEntry interface?? // TODO(roasbeef): LogEntry interface??
// - need to separate attrs for cancel/add/settle/feeupdate // - need to separate attrs for cancel/add/settle/feeupdate
type PaymentDescriptor struct { type paymentDescriptor struct {
// ChanID is the ChannelID of the LightningChannel that this // ChanID is the ChannelID of the LightningChannel that this
// PaymentDescriptor belongs to. We track this here so we can // paymentDescriptor belongs to. We track this here so we can
// reconstruct the Messages that this PaymentDescriptor is built from. // reconstruct the Messages that this paymentDescriptor is built from.
ChanID lnwire.ChannelID ChanID lnwire.ChannelID
// RHash is the payment hash for this HTLC. The HTLC can be settled iff // RHash is the payment hash for this HTLC. The HTLC can be settled iff
@ -170,7 +170,7 @@ type PaymentDescriptor struct {
// removeCommitHeight[Remote|Local] encodes the height of the // removeCommitHeight[Remote|Local] encodes the height of the
// commitment which removed the parent pointer of this // commitment which removed the parent pointer of this
// PaymentDescriptor either due to a timeout or a settle. Once both // paymentDescriptor either due to a timeout or a settle. Once both
// these heights are below the tail of both chains, the log entries can // these heights are below the tail of both chains, the log entries can
// safely be removed. // safely be removed.
removeCommitHeightRemote uint64 removeCommitHeightRemote uint64
@ -199,7 +199,7 @@ type PaymentDescriptor struct {
// [our|their|]PkScript are the raw public key scripts that encodes the // [our|their|]PkScript are the raw public key scripts that encodes the
// redemption rules for this particular HTLC. These fields will only be // redemption rules for this particular HTLC. These fields will only be
// populated iff the EntryType of this PaymentDescriptor is Add. // populated iff the EntryType of this paymentDescriptor is Add.
// ourPkScript is the ourPkScript from the context of our local // ourPkScript is the ourPkScript from the context of our local
// commitment chain. theirPkScript is the latest pkScript from the // commitment chain. theirPkScript is the latest pkScript from the
// context of the remote commitment chain. // context of the remote commitment chain.
@ -212,7 +212,7 @@ type PaymentDescriptor struct {
theirPkScript []byte theirPkScript []byte
theirWitnessScript []byte theirWitnessScript []byte
// EntryType denotes the exact type of the PaymentDescriptor. In the // EntryType denotes the exact type of the paymentDescriptor. In the
// case of a Timeout, or Settle type, then the Parent field will point // case of a Timeout, or Settle type, then the Parent field will point
// into the log to the HTLC being modified. // into the log to the HTLC being modified.
EntryType updateType EntryType updateType
@ -232,11 +232,11 @@ type PaymentDescriptor struct {
CustomRecords lnwire.CustomRecords CustomRecords lnwire.CustomRecords
} }
// ToLogUpdate recovers the underlying LogUpdate from the paymentDescriptor. // toLogUpdate recovers the underlying LogUpdate from the paymentDescriptor.
// This operation is lossy and will forget some extra information tracked by the // This operation is lossy and will forget some extra information tracked by the
// paymentDescriptor but the function is total in that all paymentDescriptors // paymentDescriptor but the function is total in that all paymentDescriptors
// can be converted back to LogUpdates. // can be converted back to LogUpdates.
func (pd *PaymentDescriptor) ToLogUpdate() channeldb.LogUpdate { func (pd *paymentDescriptor) toLogUpdate() channeldb.LogUpdate {
var msg lnwire.Message var msg lnwire.Message
switch pd.EntryType { switch pd.EntryType {
case Add: case Add:

View file

@ -29,16 +29,16 @@ type updateLog struct {
// List is the updatelog itself, we embed this value so updateLog has // List is the updatelog itself, we embed this value so updateLog has
// access to all the method of a list.List. // access to all the method of a list.List.
*fn.List[*PaymentDescriptor] *fn.List[*paymentDescriptor]
// updateIndex maps a `logIndex` to a particular update entry. It // updateIndex maps a `logIndex` to a particular update entry. It
// deals with the four update types: // deals with the four update types:
// `Fail|MalformedFail|Settle|FeeUpdate` // `Fail|MalformedFail|Settle|FeeUpdate`
updateIndex map[uint64]*fn.Node[*PaymentDescriptor] updateIndex map[uint64]*fn.Node[*paymentDescriptor]
// htlcIndex maps a `htlcCounter` to an offered HTLC entry, hence the // htlcIndex maps a `htlcCounter` to an offered HTLC entry, hence the
// `Add` update. // `Add` update.
htlcIndex map[uint64]*fn.Node[*PaymentDescriptor] htlcIndex map[uint64]*fn.Node[*paymentDescriptor]
// modifiedHtlcs is a set that keeps track of all the current modified // modifiedHtlcs is a set that keeps track of all the current modified
// htlcs, hence update types `Fail|MalformedFail|Settle`. A modified // htlcs, hence update types `Fail|MalformedFail|Settle`. A modified
@ -50,9 +50,9 @@ type updateLog struct {
// newUpdateLog creates a new updateLog instance. // newUpdateLog creates a new updateLog instance.
func newUpdateLog(logIndex, htlcCounter uint64) *updateLog { func newUpdateLog(logIndex, htlcCounter uint64) *updateLog {
return &updateLog{ return &updateLog{
List: fn.NewList[*PaymentDescriptor](), List: fn.NewList[*paymentDescriptor](),
updateIndex: make(map[uint64]*fn.Node[*PaymentDescriptor]), updateIndex: make(map[uint64]*fn.Node[*paymentDescriptor]),
htlcIndex: make(map[uint64]*fn.Node[*PaymentDescriptor]), htlcIndex: make(map[uint64]*fn.Node[*paymentDescriptor]),
logIndex: logIndex, logIndex: logIndex,
htlcCounter: htlcCounter, htlcCounter: htlcCounter,
modifiedHtlcs: fn.NewSet[uint64](), modifiedHtlcs: fn.NewSet[uint64](),
@ -64,7 +64,7 @@ func newUpdateLog(logIndex, htlcCounter uint64) *updateLog {
// state. This function differs from appendHtlc in that it won't increment // state. This function differs from appendHtlc in that it won't increment
// either of log's counters. If the HTLC is already present, then it is // either of log's counters. If the HTLC is already present, then it is
// ignored. // ignored.
func (u *updateLog) restoreHtlc(pd *PaymentDescriptor) { func (u *updateLog) restoreHtlc(pd *paymentDescriptor) {
if _, ok := u.htlcIndex[pd.HtlcIndex]; ok { if _, ok := u.htlcIndex[pd.HtlcIndex]; ok {
return return
} }
@ -74,7 +74,7 @@ func (u *updateLog) restoreHtlc(pd *PaymentDescriptor) {
// appendUpdate appends a new update to the tip of the updateLog. The entry is // appendUpdate appends a new update to the tip of the updateLog. The entry is
// also added to index accordingly. // also added to index accordingly.
func (u *updateLog) appendUpdate(pd *PaymentDescriptor) { func (u *updateLog) appendUpdate(pd *paymentDescriptor) {
u.updateIndex[u.logIndex] = u.PushBack(pd) u.updateIndex[u.logIndex] = u.PushBack(pd)
u.logIndex++ u.logIndex++
} }
@ -82,13 +82,13 @@ func (u *updateLog) appendUpdate(pd *PaymentDescriptor) {
// restoreUpdate appends a new update to the tip of the updateLog. The entry is // restoreUpdate appends a new update to the tip of the updateLog. The entry is
// also added to index accordingly. This function differs from appendUpdate in // also added to index accordingly. This function differs from appendUpdate in
// that it won't increment the log index counter. // that it won't increment the log index counter.
func (u *updateLog) restoreUpdate(pd *PaymentDescriptor) { func (u *updateLog) restoreUpdate(pd *paymentDescriptor) {
u.updateIndex[pd.LogIndex] = u.PushBack(pd) u.updateIndex[pd.LogIndex] = u.PushBack(pd)
} }
// appendHtlc appends a new HTLC offer to the tip of the update log. The entry // appendHtlc appends a new HTLC offer to the tip of the update log. The entry
// is also added to the offer index accordingly. // is also added to the offer index accordingly.
func (u *updateLog) appendHtlc(pd *PaymentDescriptor) { func (u *updateLog) appendHtlc(pd *paymentDescriptor) {
u.htlcIndex[u.htlcCounter] = u.PushBack(pd) u.htlcIndex[u.htlcCounter] = u.PushBack(pd)
u.htlcCounter++ u.htlcCounter++
@ -97,7 +97,7 @@ func (u *updateLog) appendHtlc(pd *PaymentDescriptor) {
// lookupHtlc attempts to look up an offered HTLC according to its offer // lookupHtlc attempts to look up an offered HTLC according to its offer
// index. If the entry isn't found, then a nil pointer is returned. // index. If the entry isn't found, then a nil pointer is returned.
func (u *updateLog) lookupHtlc(i uint64) *PaymentDescriptor { func (u *updateLog) lookupHtlc(i uint64) *paymentDescriptor {
htlc, ok := u.htlcIndex[i] htlc, ok := u.htlcIndex[i]
if !ok { if !ok {
return nil return nil
@ -145,7 +145,7 @@ func compactLogs(ourLog, theirLog *updateLog,
localChainTail, remoteChainTail uint64) { localChainTail, remoteChainTail uint64) {
compactLog := func(logA, logB *updateLog) { compactLog := func(logA, logB *updateLog) {
var nextA *fn.Node[*PaymentDescriptor] var nextA *fn.Node[*paymentDescriptor]
for e := logA.Front(); e != nil; e = nextA { for e := logA.Front(); e != nil; e = nextA {
// Assign next iteration element at top of loop because // Assign next iteration element at top of loop because
// we may remove the current element from the list, // we may remove the current element from the list,