2020-04-02 03:08:23 +00:00
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-04-02 03:08:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/mock"
|
2020-08-18 12:41:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-08-13 16:22:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-04-02 03:08:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/cache"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_RequestValidator(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
logrus.SetLevel(logrus.TraceLevel)
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
client := mock.NewMockBeaconChainClient(ctrl)
|
|
|
|
validatorCache, err := cache.NewPublicKeyCache(0, nil)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Could not create new cache")
|
2020-04-02 03:08:23 +00:00
|
|
|
bs := Service{
|
2021-03-21 17:53:17 +00:00
|
|
|
cfg: &Config{BeaconClient: client},
|
2020-04-02 03:08:23 +00:00
|
|
|
publicKeyCache: validatorCache,
|
|
|
|
}
|
|
|
|
wanted := ðpb.Validators{
|
|
|
|
ValidatorList: []*ethpb.Validators_ValidatorContainer{
|
|
|
|
{
|
|
|
|
Index: 0, Validator: ðpb.Validator{PublicKey: []byte{1, 2, 3}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Index: 1, Validator: ðpb.Validator{PublicKey: []byte{2, 4, 5}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
wanted2 := ðpb.Validators{
|
|
|
|
ValidatorList: []*ethpb.Validators_ValidatorContainer{
|
|
|
|
{
|
|
|
|
Index: 3, Validator: ðpb.Validator{PublicKey: []byte{3, 4, 5}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client.EXPECT().ListValidators(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(wanted, nil)
|
|
|
|
|
|
|
|
client.EXPECT().ListValidators(
|
|
|
|
gomock.Any(),
|
|
|
|
gomock.Any(),
|
|
|
|
).Return(wanted2, nil)
|
|
|
|
|
|
|
|
// We request public key of validator id 0,1.
|
2021-02-23 00:14:50 +00:00
|
|
|
res, err := bs.FindOrGetPublicKeys(context.Background(), []types.ValidatorIndex{0, 1})
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 03:08:23 +00:00
|
|
|
for i, v := range wanted.ValidatorList {
|
2020-08-18 12:41:25 +00:00
|
|
|
assert.DeepEqual(t, wanted.ValidatorList[i].Validator.PublicKey, res[v.Index])
|
2020-04-02 03:08:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Retrieved validators id public key map:")
|
|
|
|
require.LogsDoNotContain(t, hook, "Retrieved validators public keys from cache:")
|
2020-04-02 03:08:23 +00:00
|
|
|
// We expect public key of validator id 0 to be in cache.
|
2021-02-23 00:14:50 +00:00
|
|
|
res, err = bs.FindOrGetPublicKeys(context.Background(), []types.ValidatorIndex{0, 3})
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 03:08:23 +00:00
|
|
|
|
|
|
|
for i, v := range wanted2.ValidatorList {
|
2020-08-18 12:41:25 +00:00
|
|
|
assert.DeepEqual(t, wanted2.ValidatorList[i].Validator.PublicKey, res[v.Index])
|
2020-04-02 03:08:23 +00:00
|
|
|
}
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Retrieved validators public keys from cache: map[0:[1 2 3]]")
|
2020-04-02 03:08:23 +00:00
|
|
|
}
|