prysm-pulse/validator/client/validator_aggregate_test.go
terence tsao 07f6894db1
Use signature for SignedAggregateAttestationAndProof (#5605)
* Use right signature

* Minor refactor and a regression test

* Use proper DomainRequest for mock

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-04-24 13:12:47 -05:00

105 lines
3.2 KiB
Go

package client
import (
"context"
"testing"
"github.com/golang/mock/gomock"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/roughtime"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestSubmitAggregateAndProof_GetDutiesRequestFailure(t *testing.T) {
hook := logTest.NewGlobal()
validator, _, finish := setup(t)
validator.duties = &ethpb.DutiesResponse{Duties: []*ethpb.DutiesResponse_Duty{}}
defer finish()
validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey)
testutil.AssertLogsContain(t, hook, "Could not fetch validator assignment")
}
func TestSubmitAggregateAndProof_Ok(t *testing.T) {
validator, m, finish := setup(t)
defer finish()
validator.duties = &ethpb.DutiesResponse{
Duties: []*ethpb.DutiesResponse_Duty{
{
PublicKey: validatorKey.PublicKey.Marshal(),
},
},
}
m.validatorClient.EXPECT().DomainData(
gomock.Any(), // ctx
gomock.Any(), // epoch
).Return(&ethpb.DomainResponse{}, nil /*err*/)
m.validatorClient.EXPECT().SubmitAggregateSelectionProof(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&ethpb.AggregateSelectionRequest{}),
).Return(&ethpb.AggregateSelectionResponse{
AggregateAndProof: &ethpb.AggregateAttestationAndProof{
AggregatorIndex: 0,
Aggregate: &ethpb.Attestation{Data: &ethpb.AttestationData{}},
SelectionProof: nil,
},
}, nil)
m.validatorClient.EXPECT().DomainData(
gomock.Any(), // ctx
gomock.Any(), // epoch
).Return(&ethpb.DomainResponse{}, nil /*err*/)
m.validatorClient.EXPECT().SubmitSignedAggregateSelectionProof(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&ethpb.SignedAggregateSubmitRequest{}),
).Return(&ethpb.SignedAggregateSubmitResponse{}, nil)
validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey)
}
func TestWaitForSlotTwoThird_WaitCorrectly(t *testing.T) {
validator, _, finish := setup(t)
defer finish()
currentTime := uint64(roughtime.Now().Unix())
numOfSlots := uint64(4)
validator.genesisTime = currentTime - (numOfSlots * params.BeaconConfig().SecondsPerSlot)
timeToSleep := params.BeaconConfig().SecondsPerSlot * 2 / 3
twoThirdTime := currentTime + timeToSleep
validator.waitToSlotTwoThirds(context.Background(), numOfSlots)
currentTime = uint64(roughtime.Now().Unix())
if currentTime != twoThirdTime {
t.Errorf("Wanted %d time for slot two third but got %d", twoThirdTime, currentTime)
}
}
func TestAggregateAndProofSignature_CanSignValidSignature(t *testing.T) {
validator, m, finish := setup(t)
defer finish()
m.validatorClient.EXPECT().DomainData(
gomock.Any(), // ctx
&ethpb.DomainRequest{Epoch: 0, Domain: params.BeaconConfig().DomainAggregateAndProof[:]},
).Return(&ethpb.DomainResponse{}, nil /*err*/)
agg := &ethpb.AggregateAttestationAndProof{
AggregatorIndex: 0,
Aggregate: &ethpb.Attestation{Data: &ethpb.AttestationData{}},
SelectionProof: nil,
}
sig, err := validator.aggregateAndProofSig(context.Background(), validatorPubKey, agg)
if err != nil {
t.Fatal(err)
}
if _, err := bls.SignatureFromBytes(sig); err != nil {
t.Fatal(err)
}
}