11package api
22
33import (
4+ "fmt"
45 "math/big"
6+ "math/rand"
7+ "net/http"
8+ "net/http/httptest"
59 "testing"
610
711 v1 "github.com/attestantio/go-eth2-client/api/v1"
@@ -11,6 +15,7 @@ import (
1115 "github.com/ethereum/go-ethereum/common"
1216 "github.com/ethereum/go-ethereum/common/hexutil"
1317 "github.com/ethereum/go-ethereum/core/types"
18+ "github.com/go-delve/delve/pkg/config"
1419 "github.com/stretchr/testify/require"
1520)
1621
@@ -313,3 +318,120 @@ func Test_ApiEndpoint(t *testing.T) {
313318 require.Equal(t, 1, 1)
314319 */
315320}
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.SummarizedBlock {
410+ return oracle.SummarizedBlock {
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.SummarizedBlock {
419+ return oracle.SummarizedBlock {
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.SummarizedBlock {
428+ return oracle.SummarizedBlock {
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