• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/chanacceptor/merge.go
1
package chanacceptor
2

3
import (
4
        "bytes"
5
        "fmt"
6

7
        "github.com/btcsuite/btcd/btcutil"
8
        "github.com/lightningnetwork/lnd/lnwire"
9
)
10

11
const (
12
        // We use field names in our errors for more readable errors. Create
13
        // consts for them here so that we can exactly match in our unit tests.
14
        fieldCSV             = "csv delay"
15
        fieldHtlcLimit       = "htlc limit"
16
        fieldMinDep          = "min depth"
17
        fieldReserve         = "reserve"
18
        fieldMinIn           = "min htlc in"
19
        fieldInFlightTotal   = "in flight total"
20
        fieldUpfrontShutdown = "upfront shutdown"
21
)
22

23
var (
24
        errZeroConf = fmt.Errorf("zero-conf set with non-zero min-depth")
25
)
26

27
// fieldMismatchError returns a merge error for a named field when we get two
28
// channel acceptor responses which have different values set.
29
func fieldMismatchError(name string, current, newValue interface{}) error {
14✔
30
        return fmt.Errorf("multiple values set for: %v, %v and %v",
14✔
31
                name, current, newValue)
14✔
32
}
14✔
33

34
// mergeBool merges two boolean values.
35
func mergeBool(current, newValue bool) bool {
13✔
36
        // If either is true, return true. It is not possible to have different
13✔
37
        // "non-zero" values like the other cases.
13✔
38
        return current || newValue
13✔
39
}
13✔
40

41
// mergeInt64 merges two int64 values, failing if they have different non-zero
42
// values.
43
func mergeInt64(name string, current, newValue int64) (int64, error) {
48✔
44
        switch {
48✔
45
        case current == 0:
37✔
46
                return newValue, nil
37✔
47

48
        case newValue == 0:
2✔
49
                return current, nil
2✔
50

51
        case current != newValue:
4✔
52
                return 0, fieldMismatchError(name, current, newValue)
4✔
53

54
        default:
5✔
55
                return newValue, nil
5✔
56
        }
57
}
58

59
// mergeMillisatoshi merges two msat values, failing if they have different
60
// non-zero values.
61
func mergeMillisatoshi(name string, current,
62
        newValue lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
18✔
63

18✔
64
        switch {
18✔
65
        case current == 0:
12✔
66
                return newValue, nil
12✔
67

68
        case newValue == 0:
2✔
69
                return current, nil
2✔
70

71
        case current != newValue:
2✔
72
                return 0, fieldMismatchError(name, current, newValue)
2✔
73

74
        default:
2✔
75
                return newValue, nil
2✔
76
        }
77
}
78

79
// mergeDeliveryAddress merges two delivery address values, failing if they have
80
// different non-zero values.
81
func mergeDeliveryAddress(name string, current,
82
        newValue lnwire.DeliveryAddress) (lnwire.DeliveryAddress, error) {
9✔
83

9✔
84
        switch {
9✔
85
        case current == nil:
6✔
86
                return newValue, nil
6✔
87

88
        case newValue == nil:
1✔
89
                return current, nil
1✔
90

91
        case !bytes.Equal(current, newValue):
1✔
92
                return nil, fieldMismatchError(name, current, newValue)
1✔
93

94
        default:
1✔
95
                return newValue, nil
1✔
96
        }
97
}
98

99
// mergeResponse takes two channel accept responses, and attempts to merge their
100
// fields, failing if any fields conflict (are non-zero and not equal). It
101
// returns a new response that has all the merged fields in it.
102
func mergeResponse(current,
103
        newValue ChannelAcceptResponse) (ChannelAcceptResponse, error) {
16✔
104

16✔
105
        csv, err := mergeInt64(
16✔
106
                fieldCSV, int64(current.CSVDelay), int64(newValue.CSVDelay),
16✔
107
        )
16✔
108
        if err != nil {
17✔
109
                return current, err
1✔
110
        }
1✔
111
        current.CSVDelay = uint16(csv)
15✔
112

15✔
113
        htlcLimit, err := mergeInt64(
15✔
114
                fieldHtlcLimit, int64(current.HtlcLimit),
15✔
115
                int64(newValue.HtlcLimit),
15✔
116
        )
15✔
117
        if err != nil {
16✔
118
                return current, err
1✔
119
        }
1✔
120
        current.HtlcLimit = uint16(htlcLimit)
14✔
121

14✔
122
        minDepth, err := mergeInt64(
14✔
123
                fieldMinDep, int64(current.MinAcceptDepth),
14✔
124
                int64(newValue.MinAcceptDepth),
14✔
125
        )
14✔
126
        if err != nil {
15✔
127
                return current, err
1✔
128
        }
1✔
129
        current.MinAcceptDepth = uint16(minDepth)
13✔
130

13✔
131
        current.ZeroConf = mergeBool(current.ZeroConf, newValue.ZeroConf)
13✔
132

13✔
133
        // Assert that if zero-conf is set, min-depth is zero.
13✔
134
        if current.ZeroConf && current.MinAcceptDepth != 0 {
14✔
135
                return current, errZeroConf
1✔
136
        }
1✔
137

138
        reserve, err := mergeInt64(
12✔
139
                fieldReserve, int64(current.Reserve), int64(newValue.Reserve),
12✔
140
        )
12✔
141
        if err != nil {
13✔
142
                return current, err
1✔
143
        }
1✔
144
        current.Reserve = btcutil.Amount(reserve)
11✔
145

11✔
146
        current.MinHtlcIn, err = mergeMillisatoshi(
11✔
147
                fieldMinIn, current.MinHtlcIn, newValue.MinHtlcIn,
11✔
148
        )
11✔
149
        if err != nil {
12✔
150
                return current, err
1✔
151
        }
1✔
152

153
        current.InFlightTotal, err = mergeMillisatoshi(
10✔
154
                fieldInFlightTotal, current.InFlightTotal,
10✔
155
                newValue.InFlightTotal,
10✔
156
        )
10✔
157
        if err != nil {
11✔
158
                return current, err
1✔
159
        }
1✔
160

161
        current.UpfrontShutdown, err = mergeDeliveryAddress(
9✔
162
                fieldUpfrontShutdown, current.UpfrontShutdown,
9✔
163
                newValue.UpfrontShutdown,
9✔
164
        )
9✔
165
        if err != nil {
10✔
166
                return current, err
1✔
167
        }
1✔
168

169
        return current, nil
8✔
170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc