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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 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 {
3✔
36
        // If either is true, return true. It is not possible to have different
3✔
37
        // "non-zero" values like the other cases.
3✔
38
        return current || newValue
3✔
39
}
3✔
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) {
3✔
44
        switch {
3✔
45
        case current == 0:
3✔
46
                return newValue, nil
3✔
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) {
3✔
63

3✔
64
        switch {
3✔
65
        case current == 0:
3✔
66
                return newValue, nil
3✔
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) {
3✔
83

3✔
84
        switch {
3✔
85
        case current == nil:
3✔
86
                return newValue, nil
3✔
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) {
3✔
104

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

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

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

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

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

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

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

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

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

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