github.com/prysmaticlabs/prysm@v1.4.4/validator/client/key_reload_test.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/pkg/errors"
     9  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    10  	"github.com/prysmaticlabs/prysm/shared/bls"
    11  	"github.com/prysmaticlabs/prysm/shared/mock"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    14  	"github.com/prysmaticlabs/prysm/validator/client/testutil"
    15  	logTest "github.com/sirupsen/logrus/hooks/test"
    16  )
    17  
    18  func TestValidator_HandleKeyReload(t *testing.T) {
    19  	ctrl := gomock.NewController(t)
    20  	defer ctrl.Finish()
    21  
    22  	t.Run("active", func(t *testing.T) {
    23  		hook := logTest.NewGlobal()
    24  
    25  		inactivePrivKey, err := bls.RandKey()
    26  		require.NoError(t, err)
    27  		inactivePubKey := [48]byte{}
    28  		copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
    29  		activePrivKey, err := bls.RandKey()
    30  		require.NoError(t, err)
    31  		activePubKey := [48]byte{}
    32  		copy(activePubKey[:], activePrivKey.PublicKey().Marshal())
    33  		km := &mockKeymanager{
    34  			keysMap: map[[48]byte]bls.SecretKey{
    35  				inactivePubKey: inactivePrivKey,
    36  			},
    37  		}
    38  		client := mock.NewMockBeaconNodeValidatorClient(ctrl)
    39  		v := validator{
    40  			validatorClient: client,
    41  			keyManager:      km,
    42  			genesisTime:     1,
    43  		}
    44  
    45  		resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:], activePubKey[:]})
    46  		resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
    47  		resp.Statuses[1].Status = ethpb.ValidatorStatus_ACTIVE
    48  		client.EXPECT().MultipleValidatorStatus(
    49  			gomock.Any(),
    50  			&ethpb.MultipleValidatorStatusRequest{
    51  				PublicKeys: [][]byte{inactivePubKey[:], activePubKey[:]},
    52  			},
    53  		).Return(resp, nil)
    54  
    55  		anyActive, err := v.HandleKeyReload(context.Background(), [][48]byte{inactivePubKey, activePubKey})
    56  		require.NoError(t, err)
    57  		assert.Equal(t, true, anyActive)
    58  		assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
    59  		assert.LogsContain(t, hook, "Validator activated")
    60  	})
    61  
    62  	t.Run("no active", func(t *testing.T) {
    63  		hook := logTest.NewGlobal()
    64  
    65  		inactivePrivKey, err := bls.RandKey()
    66  		require.NoError(t, err)
    67  		inactivePubKey := [48]byte{}
    68  		copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
    69  		km := &mockKeymanager{
    70  			keysMap: map[[48]byte]bls.SecretKey{
    71  				inactivePubKey: inactivePrivKey,
    72  			},
    73  		}
    74  		client := mock.NewMockBeaconNodeValidatorClient(ctrl)
    75  		v := validator{
    76  			validatorClient: client,
    77  			keyManager:      km,
    78  			genesisTime:     1,
    79  		}
    80  
    81  		resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:]})
    82  		resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
    83  		client.EXPECT().MultipleValidatorStatus(
    84  			gomock.Any(),
    85  			&ethpb.MultipleValidatorStatusRequest{
    86  				PublicKeys: [][]byte{inactivePubKey[:]},
    87  			},
    88  		).Return(resp, nil)
    89  
    90  		anyActive, err := v.HandleKeyReload(context.Background(), [][48]byte{inactivePubKey})
    91  		require.NoError(t, err)
    92  		assert.Equal(t, false, anyActive)
    93  		assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
    94  		assert.LogsDoNotContain(t, hook, "Validator activated")
    95  	})
    96  
    97  	t.Run("error when getting status", func(t *testing.T) {
    98  		inactivePrivKey, err := bls.RandKey()
    99  		require.NoError(t, err)
   100  		inactivePubKey := [48]byte{}
   101  		copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
   102  		km := &mockKeymanager{
   103  			keysMap: map[[48]byte]bls.SecretKey{
   104  				inactivePubKey: inactivePrivKey,
   105  			},
   106  		}
   107  		client := mock.NewMockBeaconNodeValidatorClient(ctrl)
   108  		v := validator{
   109  			validatorClient: client,
   110  			keyManager:      km,
   111  			genesisTime:     1,
   112  		}
   113  
   114  		client.EXPECT().MultipleValidatorStatus(
   115  			gomock.Any(),
   116  			&ethpb.MultipleValidatorStatusRequest{
   117  				PublicKeys: [][]byte{inactivePubKey[:]},
   118  			},
   119  		).Return(nil, errors.New("error"))
   120  
   121  		_, err = v.HandleKeyReload(context.Background(), [][48]byte{inactivePubKey})
   122  		assert.ErrorContains(t, "error", err)
   123  	})
   124  }