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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

2.07 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 {
4✔
32

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

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

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

66
        current := nodePairs[toNode]
4✔
67

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

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

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

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

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

×
114
                        return
×
115
                }
×
116

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

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

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

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

4✔
138
        nodePairs[toNode] = current
4✔
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) {
×
145

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

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

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

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

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

4✔
185
        return false
4✔
186
}
187

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

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

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

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

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

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

×
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 {
4✔
221

4✔
222
        var imported int
4✔
223

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

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

233
                lastResult := results[toNode]
4✔
234

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

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

248
        return imported
4✔
249
}
250

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

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

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