forked from livepeer/go-livepeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendermonitor_test.go
501 lines (412 loc) · 14.2 KB
/
sendermonitor_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package pm
import (
"fmt"
"math/big"
"testing"
"time"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setTime is a helper to set the time during tests
var setTime = func(time int64) {
unixNow = func() int64 {
return time
}
}
// increaseTime is a helper to increase the time during tests
var increaseTime = func(sec int64) {
time := unixNow()
setTime(time + sec)
}
func TestMaxFloat(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
tm.transcoderPoolSize = big.NewInt(50)
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
// Test ClaimedReserve() error
smgr.err = errors.New("ClaimedReserve error")
_, err := sm.MaxFloat(RandAddress())
assert.EqualError(err, "ClaimedReserve error")
// Test value cached
smgr.err = nil
reserve := new(big.Int).Add(smgr.info[addr].Reserve.FundsRemaining, smgr.info[addr].Reserve.ClaimedInCurrentRound)
reserveAlloc := new(big.Int).Sub(new(big.Int).Div(reserve, tm.transcoderPoolSize), smgr.claimedReserve[addr])
mf, err := sm.MaxFloat(addr)
assert.Equal(reserveAlloc, mf)
// test race conditions
addrs := make([]ethcommon.Address, 5)
for i := range addrs {
addr := RandAddress()
addrs[i] = addr
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
}
for _, addr := range addrs {
go sm.MaxFloat(addr)
}
}
func TestSubFloat(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
tm.transcoderPoolSize = big.NewInt(50)
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
require := require.New(t)
reserve := new(big.Int).Add(smgr.info[addr].Reserve.FundsRemaining, smgr.info[addr].Reserve.ClaimedInCurrentRound)
reserveAlloc := new(big.Int).Sub(new(big.Int).Div(reserve, tm.transcoderPoolSize), smgr.claimedReserve[addr])
amount := big.NewInt(5)
sm.SubFloat(addr, amount)
mf, err := sm.MaxFloat(addr)
require.Nil(err)
assert.Equal(new(big.Int).Sub(reserveAlloc, amount), mf)
sm.SubFloat(addr, amount)
assert.Nil(err)
mf, err = sm.MaxFloat(addr)
require.Nil(err)
assert.Equal(
new(big.Int).Sub(reserveAlloc, new(big.Int).Mul(amount, big.NewInt(2))),
mf,
)
}
func TestAddFloat(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
tm.transcoderPoolSize = big.NewInt(1)
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
require := require.New(t)
// Test value not cached and insufficient pendingAmount error
smgr.err = nil
reserve := new(big.Int).Add(smgr.info[addr].Reserve.FundsRemaining, smgr.info[addr].Reserve.ClaimedInCurrentRound)
reserveAlloc := new(big.Int).Sub(new(big.Int).Div(reserve, tm.transcoderPoolSize), smgr.claimedReserve[addr])
amount := big.NewInt(20)
err := sm.AddFloat(addr, amount)
assert.EqualError(err, "cannot subtract from insufficient pendingAmount")
// Test value cached and no pendingAmount error
sm.SubFloat(addr, amount)
err = sm.AddFloat(addr, amount)
assert.Nil(err)
mf, err := sm.MaxFloat(addr)
require.Nil(err)
assert.Equal(mf, reserveAlloc)
// Test cached value update
smgr.info[addr].Reserve.FundsRemaining = big.NewInt(1000)
reserve = new(big.Int).Add(smgr.info[addr].Reserve.FundsRemaining, smgr.info[addr].Reserve.ClaimedInCurrentRound)
reserveAlloc = new(big.Int).Sub(new(big.Int).Div(reserve, tm.transcoderPoolSize), smgr.claimedReserve[addr])
sm.SubFloat(addr, amount)
err = sm.AddFloat(addr, amount)
assert.Nil(err)
mf, err = sm.MaxFloat(addr)
require.Nil(err)
assert.Equal(reserveAlloc, mf)
}
func TestQueueTicketAndSignalNewBlock(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(5000),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
// Test queue ticket
sm.QueueTicket(addr, defaultSignedTicket(uint32(0)))
time.Sleep(20 * time.Millisecond)
assert.Equal(sm.(*senderMonitor).senders[addr].queue.Length(), int32(1))
qc := &queueConsumer{}
go qc.Wait(1, sm)
tm.blockNumSink <- big.NewInt(5)
time.Sleep(20 * time.Millisecond)
// check that ticket is now removed from queue
assert.Equal(sm.(*senderMonitor).senders[addr].queue.Length(), int32(0))
tickets := qc.Redeemable()
assert.Equal(1, len(tickets))
assert.Equal(uint32(0), tickets[0].SenderNonce)
// Test queue tickets from multiple senders
addr2 := RandAddress()
smgr.info[addr2] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(5000),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr2] = big.NewInt(100)
qc = &queueConsumer{}
go qc.Wait(2, sm)
sm.QueueTicket(addr, defaultSignedTicket(uint32(2)))
time.Sleep(20 * time.Millisecond)
assert.Equal(sm.(*senderMonitor).senders[addr].queue.Length(), int32(1))
tm.blockNumSink <- big.NewInt(5)
time.Sleep(20 * time.Millisecond)
sm.QueueTicket(addr2, defaultSignedTicket(uint32(3)))
time.Sleep(20 * time.Millisecond)
assert.Equal(sm.(*senderMonitor).senders[addr2].queue.Length(), int32(1))
tm.blockNumSink <- big.NewInt(5)
time.Sleep(20 * time.Millisecond)
tickets = qc.Redeemable()
assert.Equal(2, len(tickets))
assert.Equal(uint32(2), tickets[0].Ticket.SenderNonce)
assert.Equal(uint32(3), tickets[1].Ticket.SenderNonce)
}
func TestCleanup(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
require := require.New(t)
setTime(0)
// TODO: Test ticker?
// Test clean up
addr1 := RandAddress()
addr2 := RandAddress()
smgr.info[addr1] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr1] = big.NewInt(100)
smgr.info[addr2] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(500),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr2] = big.NewInt(100)
// Set lastAccess
_, err := sm.MaxFloat(addr1)
require.Nil(err)
_, err = sm.MaxFloat(addr2)
require.Nil(err)
increaseTime(10)
// Change stub SenderManager values
// SenderMonitor should no longer use cached values
// since they have been cleaned up
sm.(*senderMonitor).cleanup()
smgr.Clear(addr1)
smgr.Clear(addr2)
assert.Nil(smgr.info[addr1])
assert.Nil(smgr.claimedReserve[addr1])
assert.Nil(smgr.info[addr2])
assert.Nil(smgr.claimedReserve[addr2])
reserve2 := big.NewInt(1000)
smgr.info[addr1] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: reserve2,
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr1] = big.NewInt(100)
smgr.info[addr2] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: reserve2,
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr2] = big.NewInt(100)
mf1, err := sm.MaxFloat(addr1)
require.Nil(err)
mf2, err := sm.MaxFloat(addr2)
require.Nil(err)
expectedReserve := new(big.Int).Add(smgr.info[addr1].Reserve.FundsRemaining, smgr.info[addr1].Reserve.ClaimedInCurrentRound)
expectedAlloc := new(big.Int).Sub(new(big.Int).Div(expectedReserve, tm.transcoderPoolSize), smgr.claimedReserve[addr1])
assert.Equal(expectedAlloc, mf1)
assert.Equal(expectedAlloc, mf2)
// Test clean up after excluding items
// with updated lastAccess due to MaxFloat()
// Update lastAccess for addr1
increaseTime(4)
_, err = sm.MaxFloat(addr1)
require.Nil(err)
increaseTime(1)
// Change stub broker value
// SenderMonitor should:
// - Use cached value for addr1 because it was accessed recently via MaxFloat()
// - Use new value for addr2 because it was cleaned up
reserve3 := big.NewInt(100)
smgr.info[addr2].Reserve.FundsRemaining = reserve3
sm.(*senderMonitor).cleanup()
mf1, err = sm.MaxFloat(addr1)
require.Nil(err)
mf2, err = sm.MaxFloat(addr2)
require.Nil(err)
expectedReserve2 := new(big.Int).Add(smgr.info[addr2].Reserve.FundsRemaining, smgr.info[addr2].Reserve.ClaimedInCurrentRound)
expectedAlloc2 := new(big.Int).Sub(new(big.Int).Div(expectedReserve2, tm.transcoderPoolSize), smgr.claimedReserve[addr2])
assert.Equal(expectedAlloc, mf1)
assert.Equal(expectedAlloc2, mf2)
// Test clean up excluding items
// with updated lastAccess due to AddFloat()
// Update lastAccess for addr2
increaseTime(4)
err = sm.AddFloat(addr2, big.NewInt(0))
require.Nil(err)
increaseTime(1)
// Change stub broker value
// SenderMonitor should:
// - Use new value for addr1 because it was cleaned up
// - Use cached value for addr2 because it was accessed recently via AddFloat()
reserve4 := big.NewInt(101)
smgr.info[addr1].Reserve.FundsRemaining = reserve4
sm.(*senderMonitor).cleanup()
mf1, err = sm.MaxFloat(addr1)
require.Nil(err)
mf2, err = sm.MaxFloat(addr2)
require.Nil(err)
expectedReserve3 := new(big.Int).Add(smgr.info[addr1].Reserve.FundsRemaining, smgr.info[addr1].Reserve.ClaimedInCurrentRound)
expectedAlloc3 := new(big.Int).Sub(new(big.Int).Div(expectedReserve3, tm.transcoderPoolSize), smgr.claimedReserve[addr1])
assert.Equal(expectedAlloc3, mf1)
assert.Equal(expectedAlloc2, mf2)
// Test clean up excluding items
// with updated lastAccess due to SubFloat()
// Update lastAccess for addr1
increaseTime(4)
sm.SubFloat(addr1, big.NewInt(0))
increaseTime(1)
// Change stub broker value
// SenderMonitor should:
// - Use cached value for addr1 because it was accessed recently via SubFloat()
// - Use new value for addr2 because it was cleaned up
reserve5 := big.NewInt(999)
smgr.info[addr2].Reserve.FundsRemaining = reserve5
sm.(*senderMonitor).cleanup()
mf1, err = sm.MaxFloat(addr1)
require.Nil(err)
mf2, err = sm.MaxFloat(addr2)
require.Nil(err)
expectedReserve4 := new(big.Int).Add(smgr.info[addr2].Reserve.FundsRemaining, smgr.info[addr2].Reserve.ClaimedInCurrentRound)
expectedAlloc4 := new(big.Int).Sub(new(big.Int).Div(expectedReserve4, tm.transcoderPoolSize), smgr.claimedReserve[addr2])
assert.Equal(expectedAlloc3, mf1)
assert.Equal(expectedAlloc4, mf2)
}
func TestReserveAlloc(t *testing.T) {
assert := assert.New(t)
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
Deposit: big.NewInt(500),
WithdrawRound: big.NewInt(0),
Reserve: &ReserveInfo{
FundsRemaining: big.NewInt(5000),
ClaimedInCurrentRound: big.NewInt(0),
},
}
smgr.claimedReserve[addr] = big.NewInt(100)
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600).(*senderMonitor)
// test GetSenderInfo error
smgr.err = errors.New("GetSenderInfo error")
_, err := sm.reserveAlloc(addr)
assert.EqualError(err, smgr.err.Error())
// test reserveAlloc correctly calculated
smgr.err = nil
expectedReserve := new(big.Int).Add(smgr.info[addr].Reserve.FundsRemaining, smgr.info[addr].Reserve.ClaimedInCurrentRound)
expectedAlloc := new(big.Int).Sub(new(big.Int).Div(expectedReserve, tm.transcoderPoolSize), smgr.claimedReserve[addr])
alloc, err := sm.reserveAlloc(addr)
assert.Nil(err)
assert.Zero(expectedAlloc.Cmp(alloc))
}
func TestSenderMonitor_ValidateSender(t *testing.T) {
claimant, b, smgr, tm := senderMonitorFixture()
addr := RandAddress()
smgr.info[addr] = &SenderInfo{
WithdrawRound: big.NewInt(10),
}
sm := NewSenderMonitor(claimant, b, smgr, tm, 5*time.Minute, 3600)
sm.Start()
defer sm.Stop()
assert := assert.New(t)
// SenderManager.GetSenderInfo error
smgr.err = errors.New("GetSenderInfo error")
expErr := fmt.Sprintf("could not get sender info for %v: %v", addr.Hex(), smgr.err.Error())
err := sm.ValidateSender(addr)
assert.EqualError(err, expErr)
smgr.err = nil
// WithdrawRound = 0 (not unlocked) -> No error
tm.round = big.NewInt(2)
smgr.info[addr].WithdrawRound = big.NewInt(0)
err = sm.ValidateSender(addr)
assert.NoError(err)
smgr.info[addr].WithdrawRound = big.NewInt(10)
// currentRound + 1 < WithdrawRound -> No error
tm.round = big.NewInt(1)
err = sm.ValidateSender(addr)
assert.Nil(err)
// currentRound + 1 == WithdrawRound -> Error
expErr = fmt.Sprintf("deposit and reserve for sender %v is set to unlock soon", addr.Hex())
tm.round = big.NewInt(9)
err = sm.ValidateSender(addr)
assert.EqualError(err, expErr)
// currentRound +1 > WithdrawRound -> Error
tm.round = big.NewInt(10)
err = sm.ValidateSender(addr)
assert.EqualError(err, expErr)
}
func senderMonitorFixture() (ethcommon.Address, *stubBroker, *stubSenderManager, *stubTimeManager) {
claimant := RandAddress()
b := newStubBroker()
smgr := newStubSenderManager()
tm := &stubTimeManager{
transcoderPoolSize: big.NewInt(5),
}
return claimant, b, smgr, tm
}