mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-15 03:12:26 +02:00
Merge pull request #4148 from carlaKC/lnwallet-evaluatehtlccoverage
lnwallet/test: add coverage for evaluateHtlcView
This commit is contained in:
commit
3769f74795
@ -2494,10 +2494,106 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
|
|||||||
skipUs := make(map[uint64]struct{})
|
skipUs := make(map[uint64]struct{})
|
||||||
skipThem := make(map[uint64]struct{})
|
skipThem := make(map[uint64]struct{})
|
||||||
|
|
||||||
// fetchParentEntry is a helper method that will fetch the parent of
|
// First we run through non-add entries in both logs, populating the
|
||||||
// entry from the corresponding update log.
|
// skip sets and mutating the current chain state (crediting balances,
|
||||||
fetchParentEntry := func(entry *PaymentDescriptor,
|
// etc) to reflect the settle/timeout entry encountered.
|
||||||
remoteLog bool) (*PaymentDescriptor, error) {
|
for _, entry := range view.ourUpdates {
|
||||||
|
switch entry.EntryType {
|
||||||
|
// Skip adds for now. They will be processed below.
|
||||||
|
case Add:
|
||||||
|
continue
|
||||||
|
|
||||||
|
// Process fee updates, updating the current feePerKw.
|
||||||
|
case FeeUpdate:
|
||||||
|
processFeeUpdate(
|
||||||
|
entry, nextHeight, remoteChain, mutateState,
|
||||||
|
newView,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're settling an inbound HTLC, and it hasn't been
|
||||||
|
// processed yet, then increment our state tracking the total
|
||||||
|
// number of satoshis we've received within the channel.
|
||||||
|
if mutateState && entry.EntryType == Settle && !remoteChain &&
|
||||||
|
entry.removeCommitHeightLocal == 0 {
|
||||||
|
lc.channelState.TotalMSatReceived += entry.Amount
|
||||||
|
}
|
||||||
|
|
||||||
|
addEntry, err := lc.fetchParent(entry, remoteChain, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
skipThem[addEntry.HtlcIndex] = struct{}{}
|
||||||
|
processRemoveEntry(entry, ourBalance, theirBalance,
|
||||||
|
nextHeight, remoteChain, true, mutateState)
|
||||||
|
}
|
||||||
|
for _, entry := range view.theirUpdates {
|
||||||
|
switch entry.EntryType {
|
||||||
|
// Skip adds for now. They will be processed below.
|
||||||
|
case Add:
|
||||||
|
continue
|
||||||
|
|
||||||
|
// Process fee updates, updating the current feePerKw.
|
||||||
|
case FeeUpdate:
|
||||||
|
processFeeUpdate(
|
||||||
|
entry, nextHeight, remoteChain, mutateState,
|
||||||
|
newView,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the remote party is settling one of our outbound HTLC's,
|
||||||
|
// and it hasn't been processed, yet, the increment our state
|
||||||
|
// tracking the total number of satoshis we've sent within the
|
||||||
|
// channel.
|
||||||
|
if mutateState && entry.EntryType == Settle && !remoteChain &&
|
||||||
|
entry.removeCommitHeightLocal == 0 {
|
||||||
|
lc.channelState.TotalMSatSent += entry.Amount
|
||||||
|
}
|
||||||
|
|
||||||
|
addEntry, err := lc.fetchParent(entry, remoteChain, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
skipUs[addEntry.HtlcIndex] = struct{}{}
|
||||||
|
processRemoveEntry(entry, ourBalance, theirBalance,
|
||||||
|
nextHeight, remoteChain, false, mutateState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next we take a second pass through all the log entries, skipping any
|
||||||
|
// settled HTLCs, and debiting the chain state balance due to any newly
|
||||||
|
// added HTLCs.
|
||||||
|
for _, entry := range view.ourUpdates {
|
||||||
|
isAdd := entry.EntryType == Add
|
||||||
|
if _, ok := skipUs[entry.HtlcIndex]; !isAdd || ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
|
||||||
|
remoteChain, false, mutateState)
|
||||||
|
newView.ourUpdates = append(newView.ourUpdates, entry)
|
||||||
|
}
|
||||||
|
for _, entry := range view.theirUpdates {
|
||||||
|
isAdd := entry.EntryType == Add
|
||||||
|
if _, ok := skipThem[entry.HtlcIndex]; !isAdd || ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
|
||||||
|
remoteChain, true, mutateState)
|
||||||
|
newView.theirUpdates = append(newView.theirUpdates, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newView, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getFetchParent is a helper that looks up update log parent entries in the
|
||||||
|
// appropriate log.
|
||||||
|
func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor,
|
||||||
|
remoteChain, remoteLog bool) (*PaymentDescriptor, error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
updateLog *updateLog
|
updateLog *updateLog
|
||||||
@ -2545,102 +2641,6 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance,
|
|||||||
return addEntry, nil
|
return addEntry, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// First we run through non-add entries in both logs, populating the
|
|
||||||
// skip sets and mutating the current chain state (crediting balances,
|
|
||||||
// etc) to reflect the settle/timeout entry encountered.
|
|
||||||
for _, entry := range view.ourUpdates {
|
|
||||||
switch entry.EntryType {
|
|
||||||
// Skip adds for now. They will be processed below.
|
|
||||||
case Add:
|
|
||||||
continue
|
|
||||||
|
|
||||||
// Process fee updates, updating the current feePerKw.
|
|
||||||
case FeeUpdate:
|
|
||||||
processFeeUpdate(
|
|
||||||
entry, nextHeight, remoteChain, mutateState,
|
|
||||||
newView,
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're settling an inbound HTLC, and it hasn't been
|
|
||||||
// processed yet, then increment our state tracking the total
|
|
||||||
// number of satoshis we've received within the channel.
|
|
||||||
if mutateState && entry.EntryType == Settle && !remoteChain &&
|
|
||||||
entry.removeCommitHeightLocal == 0 {
|
|
||||||
lc.channelState.TotalMSatReceived += entry.Amount
|
|
||||||
}
|
|
||||||
|
|
||||||
addEntry, err := fetchParentEntry(entry, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
skipThem[addEntry.HtlcIndex] = struct{}{}
|
|
||||||
processRemoveEntry(entry, ourBalance, theirBalance,
|
|
||||||
nextHeight, remoteChain, true, mutateState)
|
|
||||||
}
|
|
||||||
for _, entry := range view.theirUpdates {
|
|
||||||
switch entry.EntryType {
|
|
||||||
// Skip adds for now. They will be processed below.
|
|
||||||
case Add:
|
|
||||||
continue
|
|
||||||
|
|
||||||
// Process fee updates, updating the current feePerKw.
|
|
||||||
case FeeUpdate:
|
|
||||||
processFeeUpdate(
|
|
||||||
entry, nextHeight, remoteChain, mutateState,
|
|
||||||
newView,
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the remote party is settling one of our outbound HTLC's,
|
|
||||||
// and it hasn't been processed, yet, the increment our state
|
|
||||||
// tracking the total number of satoshis we've sent within the
|
|
||||||
// channel.
|
|
||||||
if mutateState && entry.EntryType == Settle && !remoteChain &&
|
|
||||||
entry.removeCommitHeightLocal == 0 {
|
|
||||||
lc.channelState.TotalMSatSent += entry.Amount
|
|
||||||
}
|
|
||||||
|
|
||||||
addEntry, err := fetchParentEntry(entry, false)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
skipUs[addEntry.HtlcIndex] = struct{}{}
|
|
||||||
processRemoveEntry(entry, ourBalance, theirBalance,
|
|
||||||
nextHeight, remoteChain, false, mutateState)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next we take a second pass through all the log entries, skipping any
|
|
||||||
// settled HTLCs, and debiting the chain state balance due to any newly
|
|
||||||
// added HTLCs.
|
|
||||||
for _, entry := range view.ourUpdates {
|
|
||||||
isAdd := entry.EntryType == Add
|
|
||||||
if _, ok := skipUs[entry.HtlcIndex]; !isAdd || ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
|
|
||||||
remoteChain, false, mutateState)
|
|
||||||
newView.ourUpdates = append(newView.ourUpdates, entry)
|
|
||||||
}
|
|
||||||
for _, entry := range view.theirUpdates {
|
|
||||||
isAdd := entry.EntryType == Add
|
|
||||||
if _, ok := skipThem[entry.HtlcIndex]; !isAdd || ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
processAddEntry(entry, ourBalance, theirBalance, nextHeight,
|
|
||||||
remoteChain, true, mutateState)
|
|
||||||
newView.theirUpdates = append(newView.theirUpdates, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return newView, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// processAddEntry evaluates the effect of an add entry within the HTLC log.
|
// processAddEntry evaluates the effect of an add entry within the HTLC log.
|
||||||
// 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
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user