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

     1  package beaconclient
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	types "github.com/prysmaticlabs/eth2-types"
     9  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    10  	"github.com/prysmaticlabs/prysm/shared/mock"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    13  	"github.com/prysmaticlabs/prysm/slasher/cache"
    14  	"github.com/sirupsen/logrus"
    15  	logTest "github.com/sirupsen/logrus/hooks/test"
    16  )
    17  
    18  func TestService_RequestValidator(t *testing.T) {
    19  	hook := logTest.NewGlobal()
    20  	logrus.SetLevel(logrus.TraceLevel)
    21  	ctrl := gomock.NewController(t)
    22  	defer ctrl.Finish()
    23  	client := mock.NewMockBeaconChainClient(ctrl)
    24  	validatorCache, err := cache.NewPublicKeyCache(0, nil)
    25  	require.NoError(t, err, "Could not create new cache")
    26  	bs := Service{
    27  		cfg:            &Config{BeaconClient: client},
    28  		publicKeyCache: validatorCache,
    29  	}
    30  	wanted := &ethpb.Validators{
    31  		ValidatorList: []*ethpb.Validators_ValidatorContainer{
    32  			{
    33  				Index: 0, Validator: &ethpb.Validator{PublicKey: []byte{1, 2, 3}},
    34  			},
    35  			{
    36  				Index: 1, Validator: &ethpb.Validator{PublicKey: []byte{2, 4, 5}},
    37  			},
    38  		},
    39  	}
    40  	wanted2 := &ethpb.Validators{
    41  		ValidatorList: []*ethpb.Validators_ValidatorContainer{
    42  			{
    43  				Index: 3, Validator: &ethpb.Validator{PublicKey: []byte{3, 4, 5}},
    44  			},
    45  		},
    46  	}
    47  	client.EXPECT().ListValidators(
    48  		gomock.Any(),
    49  		gomock.Any(),
    50  	).Return(wanted, nil)
    51  
    52  	client.EXPECT().ListValidators(
    53  		gomock.Any(),
    54  		gomock.Any(),
    55  	).Return(wanted2, nil)
    56  
    57  	// We request public key of validator id 0,1.
    58  	res, err := bs.FindOrGetPublicKeys(context.Background(), []types.ValidatorIndex{0, 1})
    59  	require.NoError(t, err)
    60  	for i, v := range wanted.ValidatorList {
    61  		assert.DeepEqual(t, wanted.ValidatorList[i].Validator.PublicKey, res[v.Index])
    62  	}
    63  
    64  	require.LogsContain(t, hook, "Retrieved validators id public key map:")
    65  	require.LogsDoNotContain(t, hook, "Retrieved validators public keys from cache:")
    66  	// We expect public key of validator id 0 to be in cache.
    67  	res, err = bs.FindOrGetPublicKeys(context.Background(), []types.ValidatorIndex{0, 3})
    68  	require.NoError(t, err)
    69  
    70  	for i, v := range wanted2.ValidatorList {
    71  		assert.DeepEqual(t, wanted2.ValidatorList[i].Validator.PublicKey, res[v.Index])
    72  	}
    73  	require.LogsContain(t, hook, "Retrieved validators public keys from cache: map[0:[1 2 3]]")
    74  }