lnd/chanacceptor/merge.go
Oliver Gugger 7dfe4018ce
multi: use btcd's btcec/v2 and btcutil modules
This commit was previously split into the following parts to ease
review:
 - 2d746f68: replace imports
 - 4008f0fd: use ecdsa.Signature
 - 849e33d1: remove btcec.S256()
 - b8f6ebbd: use v2 library correctly
 - fa80bca9: bump go modules
2022-03-09 19:02:37 +01:00

152 lines
3.6 KiB
Go

package chanacceptor
import (
"bytes"
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
)
const (
// We use field names in our errors for more readable errors. Create
// consts for them here so that we can exactly match in our unit tests.
fieldCSV = "csv delay"
fieldHtlcLimit = "htlc limit"
fieldMinDep = "min depth"
fieldReserve = "reserve"
fieldMinIn = "min htlc in"
fieldInFlightTotal = "in flight total"
fieldUpfrontShutdown = "upfront shutdown"
)
// fieldMismatchError returns a merge error for a named field when we get two
// channel acceptor responses which have different values set.
func fieldMismatchError(name string, current, newValue interface{}) error {
return fmt.Errorf("multiple values set for: %v, %v and %v",
name, current, newValue)
}
// mergeInt64 merges two int64 values, failing if they have different non-zero
// values.
func mergeInt64(name string, current, newValue int64) (int64, error) {
switch {
case current == 0:
return newValue, nil
case newValue == 0:
return current, nil
case current != newValue:
return 0, fieldMismatchError(name, current, newValue)
default:
return newValue, nil
}
}
// mergeMillisatoshi merges two msat values, failing if they have different
// non-zero values.
func mergeMillisatoshi(name string, current,
newValue lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
switch {
case current == 0:
return newValue, nil
case newValue == 0:
return current, nil
case current != newValue:
return 0, fieldMismatchError(name, current, newValue)
default:
return newValue, nil
}
}
// mergeDeliveryAddress merges two delivery address values, failing if they have
// different non-zero values.
func mergeDeliveryAddress(name string, current,
newValue lnwire.DeliveryAddress) (lnwire.DeliveryAddress, error) {
switch {
case current == nil:
return newValue, nil
case newValue == nil:
return current, nil
case !bytes.Equal(current, newValue):
return nil, fieldMismatchError(name, current, newValue)
default:
return newValue, nil
}
}
// mergeResponse takes two channel accept responses, and attempts to merge their
// fields, failing if any fields conflict (are non-zero and not equal). It
// returns a new response that has all the merged fields in it.
func mergeResponse(current,
newValue ChannelAcceptResponse) (ChannelAcceptResponse, error) {
csv, err := mergeInt64(
fieldCSV, int64(current.CSVDelay), int64(newValue.CSVDelay),
)
if err != nil {
return current, err
}
current.CSVDelay = uint16(csv)
htlcLimit, err := mergeInt64(
fieldHtlcLimit, int64(current.HtlcLimit),
int64(newValue.HtlcLimit),
)
if err != nil {
return current, err
}
current.HtlcLimit = uint16(htlcLimit)
minDepth, err := mergeInt64(
fieldMinDep, int64(current.MinAcceptDepth),
int64(newValue.MinAcceptDepth),
)
if err != nil {
return current, err
}
current.MinAcceptDepth = uint16(minDepth)
reserve, err := mergeInt64(
fieldReserve, int64(current.Reserve), int64(newValue.Reserve),
)
if err != nil {
return current, err
}
current.Reserve = btcutil.Amount(reserve)
current.MinHtlcIn, err = mergeMillisatoshi(
fieldMinIn, current.MinHtlcIn, newValue.MinHtlcIn,
)
if err != nil {
return current, err
}
current.InFlightTotal, err = mergeMillisatoshi(
fieldInFlightTotal, current.InFlightTotal,
newValue.InFlightTotal,
)
if err != nil {
return current, err
}
current.UpfrontShutdown, err = mergeDeliveryAddress(
fieldUpfrontShutdown, current.UpfrontShutdown,
newValue.UpfrontShutdown,
)
if err != nil {
return current, err
}
return current, nil
}