9
9
"time"
10
10
11
11
"github.com/Layr-Labs/eigenda/core"
12
- "github.com/Layr-Labs/eigenda/core/auth"
13
12
"github.com/Layr-Labs/eigenda/core/meterer"
14
13
"github.com/ethereum/go-ethereum/crypto"
15
14
"github.com/stretchr/testify/assert"
@@ -34,9 +33,8 @@ func TestNewAccountant(t *testing.T) {
34
33
35
34
privateKey1 , err := crypto .GenerateKey ()
36
35
assert .NoError (t , err )
37
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
38
- assert .NoError (t , err )
39
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
36
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
37
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
40
38
41
39
assert .NotNil (t , accountant )
42
40
assert .Equal (t , reservation , accountant .reservation )
@@ -65,15 +63,14 @@ func TestAccountBlob_Reservation(t *testing.T) {
65
63
66
64
privateKey1 , err := crypto .GenerateKey ()
67
65
assert .NoError (t , err )
68
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
69
- assert .NoError (t , err )
70
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
66
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
67
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
71
68
72
69
ctx := context .Background ()
73
70
symbolLength := uint64 (500 )
74
71
quorums := []uint8 {0 , 1 }
75
72
76
- header , _ , err := accountant .AccountBlob (ctx , symbolLength , quorums )
73
+ header , err := accountant .AccountBlob (ctx , symbolLength , quorums )
77
74
metadata := core .ConvertPaymentHeader (header )
78
75
79
76
assert .NoError (t , err )
@@ -83,7 +80,7 @@ func TestAccountBlob_Reservation(t *testing.T) {
83
80
84
81
symbolLength = uint64 (700 )
85
82
86
- header , _ , err = accountant .AccountBlob (ctx , symbolLength , quorums )
83
+ header , err = accountant .AccountBlob (ctx , symbolLength , quorums )
87
84
metadata = core .ConvertPaymentHeader (header )
88
85
89
86
assert .NoError (t , err )
@@ -92,7 +89,7 @@ func TestAccountBlob_Reservation(t *testing.T) {
92
89
assert .Equal (t , isRotation ([]uint64 {1200 , 0 , 200 }, mapRecordUsage (accountant .binRecords )), true )
93
90
94
91
// Second call should use on-demand payment
95
- header , _ , err = accountant .AccountBlob (ctx , 300 , quorums )
92
+ header , err = accountant .AccountBlob (ctx , 300 , quorums )
96
93
metadata = core .ConvertPaymentHeader (header )
97
94
98
95
assert .NoError (t , err )
@@ -117,15 +114,14 @@ func TestAccountBlob_OnDemand(t *testing.T) {
117
114
118
115
privateKey1 , err := crypto .GenerateKey ()
119
116
assert .NoError (t , err )
120
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
121
- assert .NoError (t , err )
122
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
117
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
118
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
123
119
124
120
ctx := context .Background ()
125
121
numSymbols := uint64 (1500 )
126
122
quorums := []uint8 {0 , 1 }
127
123
128
- header , _ , err := accountant .AccountBlob (ctx , numSymbols , quorums )
124
+ header , err := accountant .AccountBlob (ctx , numSymbols , quorums )
129
125
assert .NoError (t , err )
130
126
131
127
metadata := core .ConvertPaymentHeader (header )
@@ -147,15 +143,14 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {
147
143
148
144
privateKey1 , err := crypto .GenerateKey ()
149
145
assert .NoError (t , err )
150
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
151
- assert .NoError (t , err )
152
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
146
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
147
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
153
148
154
149
ctx := context .Background ()
155
150
numSymbols := uint64 (2000 )
156
151
quorums := []uint8 {0 , 1 }
157
152
158
- _ , _ , err = accountant .AccountBlob (ctx , numSymbols , quorums )
153
+ _ , err = accountant .AccountBlob (ctx , numSymbols , quorums )
159
154
assert .Contains (t , err .Error (), "neither reservation nor on-demand payment is available" )
160
155
}
161
156
@@ -176,37 +171,36 @@ func TestAccountBlobCallSeries(t *testing.T) {
176
171
177
172
privateKey1 , err := crypto .GenerateKey ()
178
173
assert .NoError (t , err )
179
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
180
- assert .NoError (t , err )
181
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
174
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
175
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
182
176
183
177
ctx := context .Background ()
184
178
quorums := []uint8 {0 , 1 }
185
179
now := time .Now ().Unix ()
186
180
187
181
// First call: Use reservation
188
- header , _ , err := accountant .AccountBlob (ctx , 800 , quorums )
182
+ header , err := accountant .AccountBlob (ctx , 800 , quorums )
189
183
metadata := core .ConvertPaymentHeader (header )
190
184
assert .NoError (t , err )
191
185
assert .Equal (t , meterer .GetBinIndex (uint64 (now ), reservationWindow ), header .BinIndex )
192
186
assert .Equal (t , big .NewInt (0 ), metadata .CumulativePayment )
193
187
194
188
// Second call: Use remaining reservation + overflow
195
- header , _ , err = accountant .AccountBlob (ctx , 300 , quorums )
189
+ header , err = accountant .AccountBlob (ctx , 300 , quorums )
196
190
metadata = core .ConvertPaymentHeader (header )
197
191
assert .NoError (t , err )
198
192
assert .Equal (t , meterer .GetBinIndex (uint64 (now ), reservationWindow ), header .BinIndex )
199
193
assert .Equal (t , big .NewInt (0 ), metadata .CumulativePayment )
200
194
201
195
// Third call: Use on-demand
202
- header , _ , err = accountant .AccountBlob (ctx , 500 , quorums )
196
+ header , err = accountant .AccountBlob (ctx , 500 , quorums )
203
197
metadata = core .ConvertPaymentHeader (header )
204
198
assert .NoError (t , err )
205
199
assert .Equal (t , uint32 (0 ), header .BinIndex )
206
200
assert .Equal (t , big .NewInt (500 ), metadata .CumulativePayment )
207
201
208
202
// Fourth call: Insufficient on-demand
209
- _ , _ , err = accountant .AccountBlob (ctx , 600 , quorums )
203
+ _ , err = accountant .AccountBlob (ctx , 600 , quorums )
210
204
assert .Error (t , err )
211
205
assert .Contains (t , err .Error (), "neither reservation nor on-demand payment is available" )
212
206
}
@@ -225,30 +219,30 @@ func TestAccountBlob_BinRotation(t *testing.T) {
225
219
reservationWindow := uint32 (1 ) // Set to 1 second for testing
226
220
pricePerSymbol := uint32 (1 )
227
221
minNumSymbols := uint32 (100 )
222
+
228
223
privateKey1 , err := crypto .GenerateKey ()
229
224
assert .NoError (t , err )
230
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
231
- assert .NoError (t , err )
232
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
225
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
226
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
233
227
234
228
ctx := context .Background ()
235
229
quorums := []uint8 {0 , 1 }
236
230
237
231
// First call
238
- _ , _ , err = accountant .AccountBlob (ctx , 800 , quorums )
232
+ _ , err = accountant .AccountBlob (ctx , 800 , quorums )
239
233
assert .NoError (t , err )
240
234
assert .Equal (t , isRotation ([]uint64 {800 , 0 , 0 }, mapRecordUsage (accountant .binRecords )), true )
241
235
242
236
// next reservation duration
243
237
time .Sleep (1000 * time .Millisecond )
244
238
245
239
// Second call
246
- _ , _ , err = accountant .AccountBlob (ctx , 300 , quorums )
240
+ _ , err = accountant .AccountBlob (ctx , 300 , quorums )
247
241
assert .NoError (t , err )
248
242
assert .Equal (t , isRotation ([]uint64 {800 , 300 , 0 }, mapRecordUsage (accountant .binRecords )), true )
249
243
250
244
// Third call
251
- _ , _ , err = accountant .AccountBlob (ctx , 500 , quorums )
245
+ _ , err = accountant .AccountBlob (ctx , 500 , quorums )
252
246
assert .NoError (t , err )
253
247
assert .Equal (t , isRotation ([]uint64 {800 , 800 , 0 }, mapRecordUsage (accountant .binRecords )), true )
254
248
}
@@ -270,9 +264,8 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {
270
264
271
265
privateKey1 , err := crypto .GenerateKey ()
272
266
assert .NoError (t , err )
273
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
274
- assert .NoError (t , err )
275
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
267
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
268
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
276
269
277
270
ctx := context .Background ()
278
271
quorums := []uint8 {0 , 1 }
@@ -285,7 +278,7 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {
285
278
defer wg .Done ()
286
279
// for j := 0; j < 5; j++ {
287
280
// fmt.Println("request ", i)
288
- _ , _ , err := accountant .AccountBlob (ctx , 100 , quorums )
281
+ _ , err := accountant .AccountBlob (ctx , 100 , quorums )
289
282
assert .NoError (t , err )
290
283
time .Sleep (500 * time .Millisecond )
291
284
// }
@@ -317,30 +310,30 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {
317
310
318
311
privateKey1 , err := crypto .GenerateKey ()
319
312
assert .NoError (t , err )
320
- paymentSigner , err := auth . NewPaymentSigner ( hex .EncodeToString (privateKey1 .D .Bytes () ))
321
- assert . NoError ( t , err )
322
- accountant := NewAccountant ( reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
313
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
314
+ accountant := NewAccountant ( accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
315
+
323
316
ctx := context .Background ()
324
317
quorums := []uint8 {0 , 1 }
325
318
now := time .Now ().Unix ()
326
319
327
320
// Okay reservation
328
- header , _ , err := accountant .AccountBlob (ctx , 800 , quorums )
321
+ header , err := accountant .AccountBlob (ctx , 800 , quorums )
329
322
assert .NoError (t , err )
330
323
assert .Equal (t , meterer .GetBinIndex (uint64 (now ), reservationWindow ), header .BinIndex )
331
324
metadata := core .ConvertPaymentHeader (header )
332
325
assert .Equal (t , big .NewInt (0 ), metadata .CumulativePayment )
333
326
assert .Equal (t , isRotation ([]uint64 {800 , 0 , 0 }, mapRecordUsage (accountant .binRecords )), true )
334
327
335
328
// Second call: Allow one overflow
336
- header , _ , err = accountant .AccountBlob (ctx , 500 , quorums )
329
+ header , err = accountant .AccountBlob (ctx , 500 , quorums )
337
330
assert .NoError (t , err )
338
331
metadata = core .ConvertPaymentHeader (header )
339
332
assert .Equal (t , big .NewInt (0 ), metadata .CumulativePayment )
340
333
assert .Equal (t , isRotation ([]uint64 {1300 , 0 , 300 }, mapRecordUsage (accountant .binRecords )), true )
341
334
342
335
// Third call: Should use on-demand payment
343
- header , _ , err = accountant .AccountBlob (ctx , 200 , quorums )
336
+ header , err = accountant .AccountBlob (ctx , 200 , quorums )
344
337
assert .NoError (t , err )
345
338
assert .Equal (t , uint32 (0 ), header .BinIndex )
346
339
metadata = core .ConvertPaymentHeader (header )
@@ -365,20 +358,19 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
365
358
366
359
privateKey1 , err := crypto .GenerateKey ()
367
360
assert .NoError (t , err )
368
- paymentSigner , err := auth .NewPaymentSigner (hex .EncodeToString (privateKey1 .D .Bytes ()))
369
- assert .NoError (t , err )
370
- accountant := NewAccountant (reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , paymentSigner , numBins )
361
+ accountId := hex .EncodeToString (privateKey1 .D .Bytes ())
362
+ accountant := NewAccountant (accountId , reservation , onDemand , reservationWindow , pricePerSymbol , minNumSymbols , numBins )
371
363
372
364
ctx := context .Background ()
373
365
quorums := []uint8 {0 , 1 }
374
366
375
367
// full reservation
376
- _ , _ , err = accountant .AccountBlob (ctx , 1000 , quorums )
368
+ _ , err = accountant .AccountBlob (ctx , 1000 , quorums )
377
369
assert .NoError (t , err )
378
370
assert .Equal (t , isRotation ([]uint64 {1000 , 0 , 0 }, mapRecordUsage (accountant .binRecords )), true )
379
371
380
372
// no overflow
381
- header , _ , err := accountant .AccountBlob (ctx , 500 , quorums )
373
+ header , err := accountant .AccountBlob (ctx , 500 , quorums )
382
374
assert .NoError (t , err )
383
375
assert .Equal (t , isRotation ([]uint64 {1000 , 0 , 0 }, mapRecordUsage (accountant .binRecords )), true )
384
376
metadata := core .ConvertPaymentHeader (header )
@@ -388,7 +380,7 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
388
380
time .Sleep (time .Duration (reservationWindow ) * time .Second )
389
381
390
382
// Third call: Should use new bin and allow overflow again
391
- _ , _ , err = accountant .AccountBlob (ctx , 500 , quorums )
383
+ _ , err = accountant .AccountBlob (ctx , 500 , quorums )
392
384
assert .NoError (t , err )
393
385
assert .Equal (t , isRotation ([]uint64 {1000 , 500 , 0 }, mapRecordUsage (accountant .binRecords )), true )
394
386
}
0 commit comments