github.com/prysmaticlabs/prysm@v1.4.4/slasher/beaconclient/historical_data_retrieval_test.go (about)

     1  package beaconclient
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	types "github.com/prysmaticlabs/eth2-types"
    10  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    11  	"github.com/prysmaticlabs/prysm/shared/mock"
    12  	"github.com/prysmaticlabs/prysm/shared/params"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    14  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    15  	testDB "github.com/prysmaticlabs/prysm/slasher/db/testing"
    16  	logTest "github.com/sirupsen/logrus/hooks/test"
    17  )
    18  
    19  func TestService_RequestHistoricalAttestations(t *testing.T) {
    20  	params.SetupTestConfigCleanup(t)
    21  	hook := logTest.NewGlobal()
    22  	ctrl := gomock.NewController(t)
    23  	defer ctrl.Finish()
    24  	db := testDB.SetupSlasherDB(t, false)
    25  	client := mock.NewMockBeaconChainClient(ctrl)
    26  
    27  	bs := Service{
    28  		cfg: &Config{
    29  			BeaconClient: client,
    30  			SlasherDB:    db,
    31  		},
    32  	}
    33  
    34  	numAtts := 1000
    35  	wanted := make([]*ethpb.IndexedAttestation, numAtts)
    36  	for i := 0; i < numAtts; i++ {
    37  		wanted[i] = &ethpb.IndexedAttestation{
    38  			AttestingIndices: []uint64{1, 2, 3},
    39  			Data: &ethpb.AttestationData{
    40  				Slot: types.Slot(i),
    41  				Target: &ethpb.Checkpoint{
    42  					Epoch: 1,
    43  					Root:  make([]byte, 32),
    44  				},
    45  			},
    46  		}
    47  	}
    48  
    49  	// We override the page size in the requests to 100 so we will
    50  	// obtain 10 pages of indexed attestations from the server.
    51  	numPages := 100
    52  	perPage := numAtts / numPages
    53  	cfg := params.BeaconConfig()
    54  	cfg.DefaultPageSize = perPage
    55  	params.OverrideBeaconConfig(cfg)
    56  
    57  	// We expect there to be numPages calls to ListIndexedAttestations
    58  	// to retrieve all attestations for epoch 0.
    59  	for i := 0; i < numAtts; i += perPage {
    60  		if i+perPage >= numAtts {
    61  			client.EXPECT().ListIndexedAttestations(
    62  				gomock.Any(),
    63  				gomock.Any(),
    64  			).Return(&ethpb.ListIndexedAttestationsResponse{
    65  				IndexedAttestations: wanted[i:],
    66  				NextPageToken:       "",
    67  				TotalSize:           int32(numAtts),
    68  			}, nil)
    69  		} else {
    70  			client.EXPECT().ListIndexedAttestations(
    71  				gomock.Any(),
    72  				gomock.Any(),
    73  			).Return(&ethpb.ListIndexedAttestationsResponse{
    74  				IndexedAttestations: wanted[i : i+perPage],
    75  				NextPageToken:       strconv.Itoa(i + 1),
    76  				TotalSize:           int32(numAtts),
    77  			}, nil)
    78  		}
    79  	}
    80  
    81  	// We request attestations for epoch 0.
    82  	res, err := bs.RequestHistoricalAttestations(context.Background(), 0)
    83  	require.NoError(t, err)
    84  	assert.DeepSSZEqual(t, wanted, res)
    85  	require.LogsContain(t, hook, "Retrieved 100/1000 indexed attestations for epoch 0")
    86  	require.LogsContain(t, hook, "Retrieved 500/1000 indexed attestations for epoch 0")
    87  	require.LogsContain(t, hook, "Retrieved 1000/1000 indexed attestations for epoch 0")
    88  }