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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

73.56
/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 {
2✔
32

2✔
33
        return &missionControlState{
2✔
34
                lastPairResult:          make(map[route.Vertex]NodeResults),
2✔
35
                lastSecondChance:        make(map[DirectedNodePair]time.Time),
2✔
36
                minFailureRelaxInterval: minFailureRelaxInterval,
2✔
37
        }
2✔
38
}
2✔
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) {
2✔
44

2✔
45
        result, ok := m.lastPairResult[node]
2✔
46
        return result, ok
2✔
47
}
2✔
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() {
2✔
52
        m.lastPairResult = make(map[route.Vertex]NodeResults)
2✔
53
        m.lastSecondChance = make(map[DirectedNodePair]time.Time)
2✔
54
}
2✔
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) {
2✔
59

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

66
        current := nodePairs[toNode]
2✔
67

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

2✔
74
                // Only update the success amount if this amount is higher. This
2✔
75
                // prevents the success range from shrinking when there is no
2✔
76
                // reason to do so. For example: small amount probes shouldn't
2✔
77
                // affect a previous success for a much larger amount.
2✔
78
                if force || successAmt > current.SuccessAmt {
4✔
79
                        current.SuccessAmt = successAmt
2✔
80
                }
2✔
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() &&
2✔
88
                        successAmt >= current.FailAmt) {
4✔
89

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

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

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

×
UNCOV
114
                        return
×
UNCOV
115
                }
×
116

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

2✔
120
                switch {
2✔
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:
2✔
126
                        current.SuccessAmt = 0
2✔
127

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

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

2✔
138
        nodePairs[toNode] = current
2✔
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,
UNCOV
144
        timestamp time.Time) {
×
UNCOV
145

×
UNCOV
146
        for fromNode, nodePairs := range m.lastPairResult {
×
UNCOV
147
                for toNode := range nodePairs {
×
UNCOV
148
                        if fromNode == node || toNode == node {
×
UNCOV
149
                                nodePairs[toNode] = TimedPairResult{
×
UNCOV
150
                                        FailTime: timestamp,
×
UNCOV
151
                                }
×
UNCOV
152
                        }
×
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 {
2✔
161

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

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

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

2✔
176
                return true
2✔
177
        }
2✔
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",
2✔
183
                fromNode, toNode, timestamp.Sub(lastSecondChance))
2✔
184

2✔
185
        return false
2✔
186
}
187

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

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

×
UNCOV
196
        for fromNode, fromPairs := range m.lastPairResult {
×
UNCOV
197
                for toNode, result := range fromPairs {
×
UNCOV
198
                        pair := NewDirectedNodePair(fromNode, toNode)
×
UNCOV
199

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

×
UNCOV
205
                        pairs = append(pairs, pairSnapshot)
×
UNCOV
206
                }
×
207
        }
208

UNCOV
209
        snapshot := MissionControlSnapshot{
×
UNCOV
210
                Pairs: pairs,
×
UNCOV
211
        }
×
UNCOV
212

×
UNCOV
213
        return &snapshot
×
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 {
2✔
221

2✔
222
        var imported int
2✔
223

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

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

233
                lastResult := results[toNode]
2✔
234

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

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

248
        return imported
2✔
249
}
250

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

2✔
255
        if !force && currentTs.After(importedTs) {
2✔
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(
2✔
265
                fromNode, toNode, importedTs, &importedResult, force,
2✔
266
        )
2✔
267

2✔
268
        return 1
2✔
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