Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keymanager mock cleanup #12341

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions validator/client/attest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
validatorpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1/validator-client"
Expand Down Expand Up @@ -444,9 +443,6 @@ func TestSignAttestation(t *testing.T) {
validator, m, _, finish := setup(t)
defer finish()

secretKey, err := bls.SecretKeyFromBytes(bytesutil.PadTo([]byte{1}, 32))
require.NoError(t, err, "Failed to generate key from bytes")
publicKey := secretKey.PublicKey()
wantedFork := &ethpb.Fork{
PreviousVersion: []byte{'a', 'b', 'c', 'd'},
CurrentVersion: []byte{'d', 'e', 'f', 'f'},
Expand All @@ -464,15 +460,10 @@ func TestSignAttestation(t *testing.T) {
att.Data.Target.Epoch = 200
att.Data.Slot = 999
att.Data.BeaconBlockRoot = bytesutil.PadTo([]byte("blockRoot"), 32)
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], publicKey.Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
pubKey: secretKey,
},
}
validator.keyManager = km
sig, sr, err := validator.signAtt(ctx, pubKey, att.Data, att.Data.Slot)

pk := testKeyFromBytes(t, []byte{1})
validator.keyManager = newMockKeymanager(t, pk)
sig, sr, err := validator.signAtt(ctx, pk.pub, att.Data, att.Data.Slot)
require.NoError(t, err, "%x,%x,%v", sig, sr, err)
require.Equal(t, "b6a60f8497bd328908be83634d045"+
"dd7a32f5e246b2c4031fc2f316983f362e36fc27fd3d6d5a2b15"+
Expand Down
59 changes: 16 additions & 43 deletions validator/client/key_reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/testing/assert"
"github.com/prysmaticlabs/prysm/v4/testing/require"
Expand All @@ -23,40 +22,30 @@ func TestValidator_HandleKeyReload(t *testing.T) {
t.Run("active", func(t *testing.T) {
hook := logTest.NewGlobal()

inactivePrivKey, err := bls.RandKey()
require.NoError(t, err)
var inactivePubKey [fieldparams.BLSPubkeyLength]byte
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
activePrivKey, err := bls.RandKey()
require.NoError(t, err)
var activePubKey [fieldparams.BLSPubkeyLength]byte
copy(activePubKey[:], activePrivKey.PublicKey().Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
inactivePubKey: inactivePrivKey,
},
}
inactive := randKeypair(t)
active := randKeypair(t)

client := validatormock.NewMockValidatorClient(ctrl)
beaconClient := validatormock.NewMockBeaconChainClient(ctrl)
v := validator{
validatorClient: client,
keyManager: km,
keyManager: newMockKeymanager(t, inactive),
genesisTime: 1,
beaconClient: beaconClient,
}

resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:], activePubKey[:]})
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactive.pub[:], active.pub[:]})
resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
resp.Statuses[1].Status = ethpb.ValidatorStatus_ACTIVE
client.EXPECT().MultipleValidatorStatus(
gomock.Any(),
&ethpb.MultipleValidatorStatusRequest{
PublicKeys: [][]byte{inactivePubKey[:], activePubKey[:]},
PublicKeys: [][]byte{inactive.pub[:], active.pub[:]},
},
).Return(resp, nil)
beaconClient.EXPECT().ListValidators(gomock.Any(), gomock.Any()).Return(&ethpb.Validators{}, nil)

anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey, activePubKey})
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactive.pub, active.pub})
require.NoError(t, err)
assert.Equal(t, true, anyActive)
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
Expand All @@ -66,66 +55,50 @@ func TestValidator_HandleKeyReload(t *testing.T) {
t.Run("no active", func(t *testing.T) {
hook := logTest.NewGlobal()

inactivePrivKey, err := bls.RandKey()
require.NoError(t, err)
var inactivePubKey [fieldparams.BLSPubkeyLength]byte
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
inactivePubKey: inactivePrivKey,
},
}
client := validatormock.NewMockValidatorClient(ctrl)
beaconClient := validatormock.NewMockBeaconChainClient(ctrl)
kp := randKeypair(t)
v := validator{
validatorClient: client,
keyManager: km,
keyManager: newMockKeymanager(t, kp),
genesisTime: 1,
beaconClient: beaconClient,
}

resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{inactivePubKey[:]})
resp := testutil.GenerateMultipleValidatorStatusResponse([][]byte{kp.pub[:]})
resp.Statuses[0].Status = ethpb.ValidatorStatus_UNKNOWN_STATUS
client.EXPECT().MultipleValidatorStatus(
gomock.Any(),
&ethpb.MultipleValidatorStatusRequest{
PublicKeys: [][]byte{inactivePubKey[:]},
PublicKeys: [][]byte{kp.pub[:]},
},
).Return(resp, nil)
beaconClient.EXPECT().ListValidators(gomock.Any(), gomock.Any()).Return(&ethpb.Validators{}, nil)

anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey})
anyActive, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{kp.pub})
require.NoError(t, err)
assert.Equal(t, false, anyActive)
assert.LogsContain(t, hook, "Waiting for deposit to be observed by beacon node")
assert.LogsDoNotContain(t, hook, "Validator activated")
})

t.Run("error when getting status", func(t *testing.T) {
inactivePrivKey, err := bls.RandKey()
require.NoError(t, err)
var inactivePubKey [fieldparams.BLSPubkeyLength]byte
copy(inactivePubKey[:], inactivePrivKey.PublicKey().Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
inactivePubKey: inactivePrivKey,
},
}
kp := randKeypair(t)
client := validatormock.NewMockValidatorClient(ctrl)
v := validator{
validatorClient: client,
keyManager: km,
keyManager: newMockKeymanager(t, kp),
genesisTime: 1,
}

client.EXPECT().MultipleValidatorStatus(
gomock.Any(),
&ethpb.MultipleValidatorStatusRequest{
PublicKeys: [][]byte{inactivePubKey[:]},
PublicKeys: [][]byte{kp.pub[:]},
},
).Return(nil, errors.New("error"))

_, err = v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{inactivePubKey})
_, err := v.HandleKeyReload(context.Background(), [][fieldparams.BLSPubkeyLength]byte{kp.pub})
assert.ErrorContains(t, "error", err)
})
}
65 changes: 21 additions & 44 deletions validator/client/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (m mockSignature) Copy() bls.Signature {
return m
}

func testKeyFromBytes(t *testing.T, b []byte) keypair {
pri, err := bls.SecretKeyFromBytes(bytesutil.PadTo(b, 32))
require.NoError(t, err, "Failed to generate key from bytes")
return keypair{pub: bytesutil.ToBytes48(pri.PublicKey().Marshal()), pri: pri}
}

func setup(t *testing.T) (*validator, *mocks, bls.SecretKey, func()) {
validatorKey, err := bls.RandKey()
require.NoError(t, err)
Expand All @@ -78,17 +84,11 @@ func setupWithKey(t *testing.T, validatorKey bls.SecretKey) (*validator, *mocks,
return mockSignature{}, nil
},
}

aggregatedSlotCommitteeIDCache := lruwrpr.New(int(params.BeaconConfig().MaxCommitteesPerSlot))
copy(pubKey[:], validatorKey.PublicKey().Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
pubKey: validatorKey,
},
}

validator := &validator{
db: valDB,
keyManager: km,
keyManager: newMockKeymanager(t, keypair{pub: pubKey, pri: validatorKey}),
validatorClient: m.validatorClient,
slashingProtectionClient: m.slasherClient,
graffiti: []byte{},
Expand Down Expand Up @@ -823,9 +823,6 @@ func TestSignBlock(t *testing.T) {
validator, m, _, finish := setup(t)
defer finish()

secretKey, err := bls.SecretKeyFromBytes(bytesutil.PadTo([]byte{1}, 32))
require.NoError(t, err, "Failed to generate key from bytes")
publicKey := secretKey.PublicKey()
proposerDomain := make([]byte, 32)
m.validatorClient.EXPECT().
DomainData(gomock.Any(), gomock.Any()).
Expand All @@ -834,17 +831,13 @@ func TestSignBlock(t *testing.T) {
blk := util.NewBeaconBlock()
blk.Block.Slot = 1
blk.Block.ProposerIndex = 100
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], publicKey.Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
pubKey: secretKey,
},
}
validator.keyManager = km

kp := testKeyFromBytes(t, []byte{1})

validator.keyManager = newMockKeymanager(t, kp)
b, err := blocks.NewBeaconBlock(blk.Block)
require.NoError(t, err)
sig, blockRoot, err := validator.signBlock(ctx, pubKey, 0, 0, b)
sig, blockRoot, err := validator.signBlock(ctx, kp.pub, 0, 0, b)
require.NoError(t, err, "%x,%v", sig, err)
require.Equal(t, "a049e1dc723e5a8b5bd14f292973572dffd53785ddb337"+
"82f20bf762cbe10ee7b9b4f5ae1ad6ff2089d352403750bed402b94b58469c072536"+
Expand All @@ -864,9 +857,7 @@ func TestSignAltairBlock(t *testing.T) {
validator, m, _, finish := setup(t)
defer finish()

secretKey, err := bls.SecretKeyFromBytes(bytesutil.PadTo([]byte{1}, 32))
require.NoError(t, err, "Failed to generate key from bytes")
publicKey := secretKey.PublicKey()
kp := testKeyFromBytes(t, []byte{1})
proposerDomain := make([]byte, 32)
m.validatorClient.EXPECT().
DomainData(gomock.Any(), gomock.Any()).
Expand All @@ -875,17 +866,10 @@ func TestSignAltairBlock(t *testing.T) {
blk := util.NewBeaconBlockAltair()
blk.Block.Slot = 1
blk.Block.ProposerIndex = 100
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], publicKey.Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
pubKey: secretKey,
},
}
validator.keyManager = km
validator.keyManager = newMockKeymanager(t, kp)
wb, err := blocks.NewBeaconBlock(blk.Block)
require.NoError(t, err)
sig, blockRoot, err := validator.signBlock(ctx, pubKey, 0, 0, wb)
sig, blockRoot, err := validator.signBlock(ctx, kp.pub, 0, 0, wb)
require.NoError(t, err, "%x,%v", sig, err)
// Verify the returned block root matches the expected root using the proposer signature
// domain.
Expand All @@ -900,28 +884,21 @@ func TestSignBellatrixBlock(t *testing.T) {
validator, m, _, finish := setup(t)
defer finish()

secretKey, err := bls.SecretKeyFromBytes(bytesutil.PadTo([]byte{1}, 32))
require.NoError(t, err, "Failed to generate key from bytes")
publicKey := secretKey.PublicKey()
proposerDomain := make([]byte, 32)
m.validatorClient.EXPECT().
DomainData(gomock.Any(), gomock.Any()).
Return(&ethpb.DomainResponse{SignatureDomain: proposerDomain}, nil)

ctx := context.Background()
blk := util.NewBeaconBlockBellatrix()
blk.Block.Slot = 1
blk.Block.ProposerIndex = 100
var pubKey [fieldparams.BLSPubkeyLength]byte
copy(pubKey[:], publicKey.Marshal())
km := &mockKeymanager{
keysMap: map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey{
pubKey: secretKey,
},
}
validator.keyManager = km

kp := randKeypair(t)
validator.keyManager = newMockKeymanager(t, kp)
wb, err := blocks.NewBeaconBlock(blk.Block)
require.NoError(t, err)
sig, blockRoot, err := validator.signBlock(ctx, pubKey, 0, 0, wb)
sig, blockRoot, err := validator.signBlock(ctx, kp.pub, 0, 0, wb)
require.NoError(t, err, "%x,%v", sig, err)
// Verify the returned block root matches the expected root using the proposer signature
// domain.
Expand Down
Loading