@@ -2,7 +2,11 @@ package api
2
2
3
3
import (
4
4
"encoding/hex"
5
+ "fmt"
5
6
"math/big"
7
+ "math/rand"
8
+ "net/http"
9
+ "net/http/httptest"
6
10
"testing"
7
11
8
12
v1 "github.com/attestantio/go-eth2-client/api/v1"
@@ -314,3 +318,120 @@ func Test_ApiEndpoint(t *testing.T) {
314
318
require.Equal(t, 1, 1)
315
319
*/
316
320
}
321
+
322
+ func TestHandleGetOrderedBlocksByValidatorKey (t * testing.T ) {
323
+
324
+ // Create a mock config
325
+ mockConfig := & config.Config {
326
+ ConsensusEndpoint : "http://consensus_endpoint" ,
327
+ ExecutionEndpoint : "http://execution_endpoint" ,
328
+ Network : "testnet" ,
329
+ PoolAddress : "pool_address" ,
330
+ DeployedSlot : 1000 ,
331
+ DeployedBlock : 10000 ,
332
+ CheckPointSizeInSlots : 10 ,
333
+ PoolFeesPercent : 150 ,
334
+ PoolFeesAddress : "fees_address" ,
335
+ DryRun : true ,
336
+ NumRetries : 3 ,
337
+ CollateralInWei : big .NewInt (1000 ),
338
+ UpdaterKeyPass : "key_pass" ,
339
+ UpdaterKeyPath : "key_path" ,
340
+ }
341
+
342
+ // Create a mock Oracle using the NewOracle function
343
+ mockOracle := oracle .NewOracle (mockConfig )
344
+ oracle .NewOracleState (mockConfig )
345
+ numBlocks := 100
346
+ for i := 0 ; i < numBlocks ; i ++ {
347
+ // Generate a random ValidatorIndex between 0 and 99
348
+ validatorIndex := rand .Intn (100 )
349
+
350
+ // Create mock blocks and add them to the OracleState's ProposedBlocks
351
+ mockBlock := blockOkProposal (
352
+ rand .Uint64 ()% 101 ,
353
+ uint64 (validatorIndex ),
354
+ "PUBKEY" ,
355
+ big .NewInt (int64 (rand .Intn (1000 )+ 10000 )),
356
+ "0xaaa0000000000000000000000000000000000000" ,
357
+ )
358
+ mockOracle .State ().HandleCorrectBlockProposal (mockBlock )
359
+
360
+ // Create mock missed blocks and add them to the OracleState's MissedBlocks
361
+ mockMissedBlock := MissedBlock (
362
+ rand .Uint64 ()% 101 ,
363
+ uint64 (validatorIndex ),
364
+ "PUBKEY" ,
365
+ )
366
+ mockOracle .State ().HandleMissedBlock (mockMissedBlock )
367
+
368
+ // Create mock wrong fee blocks and add them to the OracleState's WrongFeeBlocks
369
+ mockWrongFeeBlock := WrongFeeBlock (
370
+ rand .Uint64 ()% 101 ,
371
+ uint64 (validatorIndex ),
372
+ "PUBKEY" ,
373
+ )
374
+ mockOracle .State ().HandleBanValidator (mockWrongFeeBlock )
375
+ }
376
+ // Create an instance of your ApiService with the mock Oracle
377
+ apiService := & ApiService {
378
+ oracle : mockOracle ,
379
+ // other fields initialization...
380
+ }
381
+ req , err := http .NewRequest ("GET" , "/memory/validatorblocks" , nil )
382
+ if err != nil {
383
+ t .Fatal (err )
384
+ }
385
+ rr := httptest .NewRecorder ()
386
+ // Call the handler function directly, passing in the ResponseRecorder and the Request
387
+ handler := http .HandlerFunc (apiService .handleMemoryValidatorBlocks )
388
+ handler .ServeHTTP (rr , req )
389
+ fmt .Println (rr .Body )
390
+
391
+ // handler2 := http.HandlerFunc(apiService.handleMemoryAllBlocks)
392
+ // handler2.ServeHTTP(rr, req)
393
+ // fmt.Print(rr.Body.String())
394
+
395
+ // Perform assertions on the response
396
+ if status := rr .Code ; status != http .StatusOK {
397
+ t .Errorf ("handler returned wrong status code: got %v, want %v" , status , http .StatusOK )
398
+ }
399
+ // Perform additional assertions on the response body or headers if needed
400
+ // For example, you can check the response body for expected JSON data
401
+
402
+ // Example assertion for JSON response
403
+ // expectedResponse := `{"message":"success"}`
404
+ // if rr.Body.String() != expectedResponse {
405
+ // t.Errorf("handler returned unexpected body: got %v, want %v", rr.Body.String(), expectedResponse)
406
+ // }
407
+ }
408
+
409
+ func MissedBlock (slot uint64 , valIndex uint64 , pubKey string ) oracle.Block {
410
+ return oracle.Block {
411
+ Slot : slot ,
412
+ ValidatorIndex : valIndex ,
413
+ ValidatorKey : pubKey ,
414
+ BlockType : oracle .MissedProposal ,
415
+ }
416
+ }
417
+
418
+ func WrongFeeBlock (slot uint64 , valIndex uint64 , pubKey string ) oracle.Block {
419
+ return oracle.Block {
420
+ Slot : slot ,
421
+ ValidatorIndex : valIndex ,
422
+ ValidatorKey : pubKey ,
423
+ BlockType : oracle .WrongFeeRecipient ,
424
+ }
425
+ }
426
+
427
+ func blockOkProposal (slot uint64 , valIndex uint64 , pubKey string , reward * big.Int , withAddress string ) oracle.Block {
428
+ return oracle.Block {
429
+ Slot : slot ,
430
+ ValidatorIndex : valIndex ,
431
+ ValidatorKey : pubKey ,
432
+ BlockType : oracle .OkPoolProposal ,
433
+ Reward : reward ,
434
+ RewardType : oracle .MevBlock ,
435
+ WithdrawalAddress : withAddress ,
436
+ }
437
+ }
0 commit comments