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

lightningnetwork / lnd / 15155511119

21 May 2025 06:52AM UTC coverage: 57.389% (-11.6%) from 68.996%
15155511119

Pull #9844

github

web-flow
Merge 8658c8597 into c52a6ddeb
Pull Request #9844: Refactor Payment PR 3

346 of 493 new or added lines in 4 files covered. (70.18%)

30172 existing lines in 456 files now uncovered.

95441 of 166305 relevant lines covered (57.39%)

0.61 hits per line

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

63.46
/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.
UNCOV
29
func fieldMismatchError(name string, current, newValue interface{}) error {
×
UNCOV
30
        return fmt.Errorf("multiple values set for: %v, %v and %v",
×
UNCOV
31
                name, current, newValue)
×
UNCOV
32
}
×
33

34
// mergeBool merges two boolean values.
35
func mergeBool(current, newValue bool) bool {
1✔
36
        // If either is true, return true. It is not possible to have different
1✔
37
        // "non-zero" values like the other cases.
1✔
38
        return current || newValue
1✔
39
}
1✔
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) {
1✔
44
        switch {
1✔
45
        case current == 0:
1✔
46
                return newValue, nil
1✔
47

UNCOV
48
        case newValue == 0:
×
UNCOV
49
                return current, nil
×
50

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

UNCOV
54
        default:
×
UNCOV
55
                return newValue, nil
×
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) {
1✔
63

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

UNCOV
68
        case newValue == 0:
×
UNCOV
69
                return current, nil
×
70

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

UNCOV
74
        default:
×
UNCOV
75
                return newValue, nil
×
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) {
1✔
83

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

UNCOV
88
        case newValue == nil:
×
UNCOV
89
                return current, nil
×
90

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

UNCOV
94
        default:
×
UNCOV
95
                return newValue, nil
×
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) {
1✔
104

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

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

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

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

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

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

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

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

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

169
        return current, nil
1✔
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