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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

95.98
/routing/missioncontrol_state.go
1
package routing
2

3
import (
4
        "time"
5

6
        "github.com/lightningnetwork/lnd/routing/route"
7
)
8

9
// missionControlState is an object that manages the internal mission control
10
// state. Note that it isn't thread safe and synchronization needs to be
11
// enforced externally.
12
type missionControlState struct {
13
        // lastPairResult tracks the last payment result (on a pair basis) for
14
        // each transited node. This is a multi-layer map that allows us to look
15
        // up the failure history of all connected channels (node pairs) for a
16
        // particular node.
17
        lastPairResult map[route.Vertex]NodeResults
18

19
        // lastSecondChance tracks the last time a second chance was granted for
20
        // a directed node pair.
21
        lastSecondChance map[DirectedNodePair]time.Time
22

23
        // minFailureRelaxInterval is the minimum time that must have passed
24
        // since the previously recorded failure before the failure amount may
25
        // be raised.
26
        minFailureRelaxInterval time.Duration
27
}
28

29
// newMissionControlState instantiates a new mission control state object.
30
func newMissionControlState(
31
        minFailureRelaxInterval time.Duration) *missionControlState {
37✔
32

37✔
33
        return &missionControlState{
37✔
34
                lastPairResult:          make(map[route.Vertex]NodeResults),
37✔
35
                lastSecondChance:        make(map[DirectedNodePair]time.Time),
37✔
36
                minFailureRelaxInterval: minFailureRelaxInterval,
37✔
37
        }
37✔
38
}
37✔
39

40
// getLastPairResult returns the current state for connections to the given
41
// node.
42
func (m *missionControlState) getLastPairResult(node route.Vertex) (NodeResults,
43
        bool) {
671✔
44

671✔
45
        result, ok := m.lastPairResult[node]
671✔
46
        return result, ok
671✔
47
}
671✔
48

49
// ResetHistory resets the history of missionControlState returning it to a
50
// state as if no payment attempts have been made.
51
func (m *missionControlState) resetHistory() {
6✔
52
        m.lastPairResult = make(map[route.Vertex]NodeResults)
6✔
53
        m.lastSecondChance = make(map[DirectedNodePair]time.Time)
6✔
54
}
6✔
55

56
// setLastPairResult stores a result for a node pair.
57
func (m *missionControlState) setLastPairResult(fromNode, toNode route.Vertex,
58
        timestamp time.Time, result *pairResult, force bool) {
155✔
59

155✔
60
        nodePairs, ok := m.lastPairResult[fromNode]
155✔
61
        if !ok {
230✔
62
                nodePairs = make(NodeResults)
75✔
63
                m.lastPairResult[fromNode] = nodePairs
75✔
64
        }
75✔
65

66
        current := nodePairs[toNode]
155✔
67

155✔
68
        // Apply the new result to the existing data for this pair. If there is
155✔
69
        // no existing data, apply it to the default values for TimedPairResult.
155✔
70
        if result.success {
241✔
71
                successAmt := result.amt
86✔
72
                current.SuccessTime = timestamp
86✔
73

86✔
74
                // Only update the success amount if this amount is higher. This
86✔
75
                // prevents the success range from shrinking when there is no
86✔
76
                // reason to do so. For example: small amount probes shouldn't
86✔
77
                // affect a previous success for a much larger amount.
86✔
78
                if force || successAmt > current.SuccessAmt {
134✔
79
                        current.SuccessAmt = successAmt
48✔
80
                }
48✔
81

82
                // If the success amount goes into the failure range, move the
83
                // failure range up. Future attempts up to the success amount
84
                // are likely to succeed. We don't want to clear the failure
85
                // completely, because we haven't learnt much for amounts above
86
                // the current success amount.
87
                if force || (!current.FailTime.IsZero() &&
86✔
88
                        successAmt >= current.FailAmt) {
93✔
89

7✔
90
                        current.FailAmt = successAmt + 1
7✔
91
                }
7✔
92
        } else {
72✔
93
                // For failures we always want to update both the amount and the
72✔
94
                // time. Those need to relate to the same result, because the
72✔
95
                // time is used to gradually diminish the penalty for that
72✔
96
                // specific result. Updating the timestamp but not the amount
72✔
97
                // could cause a failure for a lower amount (a more severe
72✔
98
                // condition) to be revived as if it just happened.
72✔
99
                failAmt := result.amt
72✔
100

72✔
101
                // Drop result if it would increase the failure amount too soon
72✔
102
                // after a previous failure. This can happen if htlc results
72✔
103
                // come in out of order. This check makes it easier for payment
72✔
104
                // processes to converge to a final state.
72✔
105
                failInterval := timestamp.Sub(current.FailTime)
72✔
106
                if !force && failAmt > current.FailAmt &&
72✔
107
                        failInterval < m.minFailureRelaxInterval {
73✔
108

1✔
109
                        log.Debugf("Ignoring higher amount failure within min "+
1✔
110
                                "failure relaxation interval: prev_fail_amt=%v, "+
1✔
111
                                "fail_amt=%v, interval=%v",
1✔
112
                                current.FailAmt, failAmt, failInterval)
1✔
113

1✔
114
                        return
1✔
115
                }
1✔
116

117
                current.FailTime = timestamp
71✔
118
                current.FailAmt = failAmt
71✔
119

71✔
120
                switch {
71✔
121
                // The failure amount is set to zero when the failure is
122
                // amount-independent, meaning that the attempt would have
123
                // failed regardless of the amount. This should also reset the
124
                // success amount to zero.
125
                case failAmt == 0:
37✔
126
                        current.SuccessAmt = 0
37✔
127

128
                // If the failure range goes into the success range, move the
129
                // success range down.
130
                case failAmt <= current.SuccessAmt:
8✔
131
                        current.SuccessAmt = failAmt - 1
8✔
132
                }
133
        }
134

135
        log.Debugf("Setting %v->%v range to [%v-%v]",
154✔
136
                fromNode, toNode, current.SuccessAmt, current.FailAmt)
154✔
137

154✔
138
        nodePairs[toNode] = current
154✔
139
}
140

141
// setAllFail stores a fail result for all known connections to and from the
142
// given node.
143
func (m *missionControlState) setAllFail(node route.Vertex,
144
        timestamp time.Time) {
6✔
145

6✔
146
        for fromNode, nodePairs := range m.lastPairResult {
12✔
147
                for toNode := range nodePairs {
14✔
148
                        if fromNode == node || toNode == node {
12✔
149
                                nodePairs[toNode] = TimedPairResult{
4✔
150
                                        FailTime: timestamp,
4✔
151
                                }
4✔
152
                        }
4✔
153
                }
154
        }
155
}
156

157
// requestSecondChance checks whether the node fromNode can have a second chance
158
// at providing a channel update for its channel with toNode.
159
func (m *missionControlState) requestSecondChance(timestamp time.Time,
160
        fromNode, toNode route.Vertex) bool {
12✔
161

12✔
162
        // Look up previous second chance time.
12✔
163
        pair := DirectedNodePair{
12✔
164
                From: fromNode,
12✔
165
                To:   toNode,
12✔
166
        }
12✔
167
        lastSecondChance, ok := m.lastSecondChance[pair]
12✔
168

12✔
169
        // If the channel hasn't already be given a second chance or its last
12✔
170
        // second chance was long ago, we give it another chance.
12✔
171
        if !ok || timestamp.Sub(lastSecondChance) > minSecondChanceInterval {
21✔
172
                m.lastSecondChance[pair] = timestamp
9✔
173

9✔
174
                log.Debugf("Second chance granted for %v->%v", fromNode, toNode)
9✔
175

9✔
176
                return true
9✔
177
        }
9✔
178

179
        // Otherwise penalize the channel, because we don't allow channel
180
        // updates that are that frequent. This is to prevent nodes from keeping
181
        // us busy by continuously sending new channel updates.
182
        log.Debugf("Second chance denied for %v->%v, remaining interval: %v",
6✔
183
                fromNode, toNode, timestamp.Sub(lastSecondChance))
6✔
184

6✔
185
        return false
6✔
186
}
187

188
// GetHistorySnapshot takes a snapshot from the current mission control state
189
// and actual probability estimates.
190
func (m *missionControlState) getSnapshot() *MissionControlSnapshot {
1✔
191
        log.Debugf("Requesting history snapshot from mission control: "+
1✔
192
                "pair_result_count=%v", len(m.lastPairResult))
1✔
193

1✔
194
        pairs := make([]MissionControlPairSnapshot, 0, len(m.lastPairResult))
1✔
195

1✔
196
        for fromNode, fromPairs := range m.lastPairResult {
4✔
197
                for toNode, result := range fromPairs {
7✔
198
                        pair := NewDirectedNodePair(fromNode, toNode)
4✔
199

4✔
200
                        pairSnapshot := MissionControlPairSnapshot{
4✔
201
                                Pair:            pair,
4✔
202
                                TimedPairResult: result,
4✔
203
                        }
4✔
204

4✔
205
                        pairs = append(pairs, pairSnapshot)
4✔
206
                }
4✔
207
        }
208

209
        snapshot := MissionControlSnapshot{
1✔
210
                Pairs: pairs,
1✔
211
        }
1✔
212

1✔
213
        return &snapshot
1✔
214
}
215

216
// importSnapshot takes an existing snapshot and merges it with our current
217
// state if the result provided are fresher than our current results. It returns
218
// the number of pairs that were used.
219
func (m *missionControlState) importSnapshot(snapshot *MissionControlSnapshot,
220
        force bool) int {
3✔
221

3✔
222
        var imported int
3✔
223

3✔
224
        for _, pair := range snapshot.Pairs {
6✔
225
                fromNode := pair.Pair.From
3✔
226
                toNode := pair.Pair.To
3✔
227

3✔
228
                results, found := m.getLastPairResult(fromNode)
3✔
229
                if !found {
6✔
230
                        results = make(map[route.Vertex]TimedPairResult)
3✔
231
                }
3✔
232

233
                lastResult := results[toNode]
3✔
234

3✔
235
                failResult := failPairResult(pair.FailAmt)
3✔
236
                imported += m.importResult(
3✔
237
                        lastResult.FailTime, pair.FailTime, failResult,
3✔
238
                        fromNode, toNode, force,
3✔
239
                )
3✔
240

3✔
241
                successResult := successPairResult(pair.SuccessAmt)
3✔
242
                imported += m.importResult(
3✔
243
                        lastResult.SuccessTime, pair.SuccessTime, successResult,
3✔
244
                        fromNode, toNode, force,
3✔
245
                )
3✔
246
        }
247

248
        return imported
3✔
249
}
250

251
func (m *missionControlState) importResult(currentTs, importedTs time.Time,
252
        importedResult pairResult, fromNode, toNode route.Vertex,
253
        force bool) int {
3✔
254

3✔
255
        if !force && currentTs.After(importedTs) {
3✔
256
                log.Debugf("Not setting pair result for %v->%v (%v) "+
×
257
                        "success=%v, timestamp %v older than last result %v",
×
258
                        fromNode, toNode, importedResult.amt,
×
259
                        importedResult.success, importedTs, currentTs)
×
260

×
261
                return 0
×
262
        }
×
263

264
        m.setLastPairResult(
3✔
265
                fromNode, toNode, importedTs, &importedResult, force,
3✔
266
        )
3✔
267

3✔
268
        return 1
3✔
269
}
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