-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreputation_tracker_test.go
251 lines (219 loc) · 7.11 KB
/
reputation_tracker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package lrc
import (
"testing"
"time"
"github.com/lightningnetwork/lnd/clock"
"github.com/stretchr/testify/require"
)
// TestReputationTracker tests tracking of reputation scores for nodes that
// are forwarding us htlcs. Since we have testing for our decaying average
// elsewhere, we fix our time here so we don't need to worry about decaying
// averages.
func TestReputationTracker(t *testing.T) {
clock := clock.NewTestClock(testTime)
tracker := newReputationTracker(
clock, testParams, &TestLogger{}, nil,
)
require.Len(t, tracker.inFlightHTLCs, 0)
totalReputation := 0
// First, we'll test the impact of unendorsed HTLCs. We should have
// no impact on our revenue at all, except for an increase when we
// successfully resolve within our resolution period.
// Unendorsed - slow failure, no reputation impact
assertHtlcLifecycle(
t, tracker, 0, false, false, time.Hour,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Unendorsed - fast failure, no reputation impact
assertHtlcLifecycle(
t, tracker, 1, false, false, time.Second,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Unendorsed - slow success, no reputation impact
assertHtlcLifecycle(
t, tracker, 2, false, true, time.Hour,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Unendorsed - fast success, reputation increases by fee
totalReputation += mockProposedFee
assertHtlcLifecycle(
t, tracker, 3, false, true, time.Second*30,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Endorsed, fast failure - no reputation impact
assertHtlcLifecycle(
t, tracker, 4, true, false, time.Second,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Endorsed, slow failure - reputation decreases by fee (for one period)
totalReputation -= mockProposedFee
assertHtlcLifecycle(
t, tracker, 5, true, false, time.Second*90+1,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Endorsed, fast success - reputation increases by fee
totalReputation += mockProposedFee
assertHtlcLifecycle(
t, tracker, 6, true, true, time.Second,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Endorsed, slow success (one period) - net zero impact
assertHtlcLifecycle(
t, tracker, 7, true, true, time.Second*90+1,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
// Endorsed, slow failure (multiple periods) - negative reputation
totalReputation -= mockProposedFee * 4
assertHtlcLifecycle(
t, tracker, 8, true, false, time.Second*90*5,
)
require.EqualValues(t, totalReputation, tracker.revenue.getValue())
}
func assertHtlcLifecycle(t *testing.T, tracker *reputationTracker, idx int,
incomingEndorsed, settle bool, resolveTime time.Duration) {
outgoingDecision := ForwardOutcomeUnendorsed
if incomingEndorsed {
outgoingDecision = ForwardOutcomeUnendorsed
}
// Note, we're just setting the outgoing endorsed to whatever our
// incoming endorsed is - we're not testing reputation here.
htlc0 := mockProposedHtlc(100, 200, idx, incomingEndorsed)
err := tracker.AddInFlight(htlc0, outgoingDecision)
require.NoError(t, err)
require.Len(t, tracker.inFlightHTLCs, 1)
res0 := resolutionForProposed(htlc0, settle, testTime.Add(resolveTime))
_, err = tracker.ResolveInFlight(res0)
require.NoError(t, err)
require.Len(t, tracker.inFlightHTLCs, 0)
}
// TestReputationTrackerErrs tests the error cases for a reputation tracker.
func TestReputationTrackerErrs(t *testing.T) {
clock := clock.NewTestClock(testTime)
tracker := newReputationTracker(
clock, testParams, &TestLogger{}, nil,
)
htlc0 := mockProposedHtlc(100, 200, 0, true)
err := tracker.AddInFlight(htlc0, ForwardOutcomeEndorsed)
require.NoError(t, err)
// Assert that we error on duplicate htlcs.
err = tracker.AddInFlight(htlc0, ForwardOutcomeEndorsed)
require.ErrorIs(t, err, ErrDuplicateIndex)
htlc1 := mockProposedHtlc(100, 200, 1, true)
res1 := resolutionForProposed(htlc1, true, testTime)
_, err = tracker.ResolveInFlight(res1)
require.ErrorIs(t, err, ErrResolutionNotFound)
}
// TestEffectiveFees tests calculation of effective fees for HTLCs.
func TestEffectiveFees(t *testing.T) {
tests := []struct {
name string
holdTime time.Duration
endorsed bool
success bool
effectiveFee float64
}{
{
name: "endoresd, fast success",
holdTime: time.Second,
endorsed: true,
success: true,
effectiveFee: 1,
},
{
name: "endorsed, slow success",
holdTime: time.Minute * 5,
endorsed: true,
success: true,
// Held for 4 periods, but we get the fee.
effectiveFee: -3,
},
{
name: "endorsed, fast failure",
holdTime: time.Second,
endorsed: true,
success: false,
effectiveFee: 0,
},
{
name: "endorsed, slow failure",
holdTime: time.Minute * 5,
endorsed: true,
success: false,
// Held for 4 periods.
effectiveFee: -4,
},
{
name: "unendorsed, fast success",
holdTime: time.Second,
endorsed: false,
success: true,
effectiveFee: 1,
},
{
name: "unendorsed, slow success",
holdTime: time.Hour,
endorsed: false,
success: true,
effectiveFee: 0,
},
{
name: "unendorsed, slow failure",
holdTime: time.Hour,
endorsed: false,
success: false,
effectiveFee: 0,
},
{
name: "unendorsed, fast failure",
holdTime: time.Second,
endorsed: false,
success: false,
effectiveFee: 0,
},
}
for _, testCase := range tests {
htlc := &InFlightHTLC{
TimestampAdded: testTime,
ProposedHTLC: &ProposedHTLC{
IncomingEndorsed: NewEndorsementSignal(
testCase.endorsed,
),
// Make fee = 1 for easy calc.
IncomingAmount: 2,
OutgoingAmount: 1,
},
}
settled := htlc.TimestampAdded.Add(testCase.holdTime)
actual := effectiveFees(
time.Minute, settled, htlc, testCase.success,
)
require.Equal(t, testCase.effectiveFee, actual)
}
}
// TestInFlightHTLCs tests the impact of in-flight HTLCs on reputation.
func TestInFlightHTLCs(t *testing.T) {
clock := clock.NewTestClock(testTime)
tracker := newReputationTracker(
clock, testParams, &TestLogger{}, nil,
)
// When we have an endorsed htlc, there should be in-flight risk
// associated.
htlc0 := mockProposedHtlc(100, 200, 0, true)
err := tracker.AddInFlight(htlc0, ForwardOutcomeEndorsed)
require.NoError(t, err)
r0 := tracker.IncomingReputation()
require.NotZero(t, r0.InFlightRisk)
// An unendorsed htlc should not change the in-flight risk.
htlc1 := mockProposedHtlc(100, 200, 1, false)
err = tracker.AddInFlight(htlc1, ForwardOutcomeUnendorsed)
require.NoError(t, err)
r1 := tracker.IncomingReputation()
require.Equal(t, r0.InFlightRisk, r1.InFlightRisk)
// Once the endorsed htlc is resolved, there is zero in flight risk
// even though there's still one unendorsed htlc.
res0 := resolutionForProposed(htlc0, true, testTime.Add(time.Second))
_, err = tracker.ResolveInFlight(res0)
require.NoError(t, err)
r0 = tracker.IncomingReputation()
require.Zero(t, r0.InFlightRisk)
}