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

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	types "github.com/prysmaticlabs/eth2-types"
    10  	"github.com/prysmaticlabs/go-bitfield"
    11  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    12  	"github.com/prysmaticlabs/prysm/shared/bls"
    13  	"github.com/prysmaticlabs/prysm/shared/params"
    14  	"github.com/prysmaticlabs/prysm/shared/slotutil"
    15  	"github.com/prysmaticlabs/prysm/shared/testutil"
    16  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    17  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    18  	"github.com/prysmaticlabs/prysm/shared/timeutils"
    19  	logTest "github.com/sirupsen/logrus/hooks/test"
    20  )
    21  
    22  func TestSubmitAggregateAndProof_GetDutiesRequestFailure(t *testing.T) {
    23  	hook := logTest.NewGlobal()
    24  	validator, _, validatorKey, finish := setup(t)
    25  	validator.duties = &ethpb.DutiesResponse{Duties: []*ethpb.DutiesResponse_Duty{}}
    26  	defer finish()
    27  
    28  	pubKey := [48]byte{}
    29  	copy(pubKey[:], validatorKey.PublicKey().Marshal())
    30  	validator.SubmitAggregateAndProof(context.Background(), 0, pubKey)
    31  
    32  	require.LogsContain(t, hook, "Could not fetch validator assignment")
    33  }
    34  
    35  func TestSubmitAggregateAndProof_SignFails(t *testing.T) {
    36  	validator, m, validatorKey, finish := setup(t)
    37  	defer finish()
    38  	pubKey := [48]byte{}
    39  	copy(pubKey[:], validatorKey.PublicKey().Marshal())
    40  	validator.duties = &ethpb.DutiesResponse{
    41  		Duties: []*ethpb.DutiesResponse_Duty{
    42  			{
    43  				PublicKey: validatorKey.PublicKey().Marshal(),
    44  			},
    45  		},
    46  	}
    47  
    48  	m.validatorClient.EXPECT().DomainData(
    49  		gomock.Any(), // ctx
    50  		gomock.Any(), // epoch
    51  	).Return(&ethpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
    52  
    53  	m.validatorClient.EXPECT().SubmitAggregateSelectionProof(
    54  		gomock.Any(), // ctx
    55  		gomock.AssignableToTypeOf(&ethpb.AggregateSelectionRequest{}),
    56  	).Return(&ethpb.AggregateSelectionResponse{
    57  		AggregateAndProof: &ethpb.AggregateAttestationAndProof{
    58  			AggregatorIndex: 0,
    59  			Aggregate: testutil.HydrateAttestation(&ethpb.Attestation{
    60  				AggregationBits: make([]byte, 1),
    61  			}),
    62  			SelectionProof: make([]byte, 96),
    63  		},
    64  	}, nil)
    65  
    66  	m.validatorClient.EXPECT().DomainData(
    67  		gomock.Any(), // ctx
    68  		gomock.Any(), // epoch
    69  	).Return(&ethpb.DomainResponse{SignatureDomain: nil}, errors.New("bad domain root"))
    70  
    71  	validator.SubmitAggregateAndProof(context.Background(), 0, pubKey)
    72  }
    73  
    74  func TestSubmitAggregateAndProof_Ok(t *testing.T) {
    75  	validator, m, validatorKey, finish := setup(t)
    76  	defer finish()
    77  	pubKey := [48]byte{}
    78  	copy(pubKey[:], validatorKey.PublicKey().Marshal())
    79  	validator.duties = &ethpb.DutiesResponse{
    80  		Duties: []*ethpb.DutiesResponse_Duty{
    81  			{
    82  				PublicKey: validatorKey.PublicKey().Marshal(),
    83  			},
    84  		},
    85  	}
    86  
    87  	m.validatorClient.EXPECT().DomainData(
    88  		gomock.Any(), // ctx
    89  		gomock.Any(), // epoch
    90  	).Return(&ethpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
    91  
    92  	m.validatorClient.EXPECT().SubmitAggregateSelectionProof(
    93  		gomock.Any(), // ctx
    94  		gomock.AssignableToTypeOf(&ethpb.AggregateSelectionRequest{}),
    95  	).Return(&ethpb.AggregateSelectionResponse{
    96  		AggregateAndProof: &ethpb.AggregateAttestationAndProof{
    97  			AggregatorIndex: 0,
    98  			Aggregate: testutil.HydrateAttestation(&ethpb.Attestation{
    99  				AggregationBits: make([]byte, 1),
   100  			}),
   101  			SelectionProof: make([]byte, 96),
   102  		},
   103  	}, nil)
   104  
   105  	m.validatorClient.EXPECT().DomainData(
   106  		gomock.Any(), // ctx
   107  		gomock.Any(), // epoch
   108  	).Return(&ethpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
   109  
   110  	m.validatorClient.EXPECT().SubmitSignedAggregateSelectionProof(
   111  		gomock.Any(), // ctx
   112  		gomock.AssignableToTypeOf(&ethpb.SignedAggregateSubmitRequest{}),
   113  	).Return(&ethpb.SignedAggregateSubmitResponse{AttestationDataRoot: make([]byte, 32)}, nil)
   114  
   115  	validator.SubmitAggregateAndProof(context.Background(), 0, pubKey)
   116  }
   117  
   118  func TestWaitForSlotTwoThird_WaitCorrectly(t *testing.T) {
   119  	validator, _, _, finish := setup(t)
   120  	defer finish()
   121  	currentTime := timeutils.Now()
   122  	numOfSlots := types.Slot(4)
   123  	validator.genesisTime = uint64(currentTime.Unix()) - uint64(numOfSlots.Mul(params.BeaconConfig().SecondsPerSlot))
   124  	oneThird := slotutil.DivideSlotBy(3 /* one third of slot duration */)
   125  	timeToSleep := oneThird + oneThird
   126  
   127  	twoThirdTime := currentTime.Add(timeToSleep)
   128  	validator.waitToSlotTwoThirds(context.Background(), numOfSlots)
   129  	currentTime = timeutils.Now()
   130  	assert.Equal(t, twoThirdTime.Unix(), currentTime.Unix())
   131  }
   132  
   133  func TestWaitForSlotTwoThird_DoneContext_ReturnsImmediately(t *testing.T) {
   134  	validator, _, _, finish := setup(t)
   135  	defer finish()
   136  	currentTime := timeutils.Now()
   137  	numOfSlots := types.Slot(4)
   138  	validator.genesisTime = uint64(currentTime.Unix()) - uint64(numOfSlots.Mul(params.BeaconConfig().SecondsPerSlot))
   139  
   140  	expectedTime := timeutils.Now()
   141  	ctx, cancel := context.WithCancel(context.Background())
   142  	cancel()
   143  	validator.waitToSlotTwoThirds(ctx, numOfSlots)
   144  	currentTime = timeutils.Now()
   145  	assert.Equal(t, expectedTime.Unix(), currentTime.Unix())
   146  }
   147  
   148  func TestAggregateAndProofSignature_CanSignValidSignature(t *testing.T) {
   149  	validator, m, validatorKey, finish := setup(t)
   150  	defer finish()
   151  
   152  	pubKey := [48]byte{}
   153  	copy(pubKey[:], validatorKey.PublicKey().Marshal())
   154  	m.validatorClient.EXPECT().DomainData(
   155  		gomock.Any(), // ctx
   156  		&ethpb.DomainRequest{Epoch: 0, Domain: params.BeaconConfig().DomainAggregateAndProof[:]},
   157  	).Return(&ethpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
   158  
   159  	agg := &ethpb.AggregateAttestationAndProof{
   160  		AggregatorIndex: 0,
   161  		Aggregate: testutil.HydrateAttestation(&ethpb.Attestation{
   162  			AggregationBits: bitfield.NewBitlist(1),
   163  		}),
   164  		SelectionProof: make([]byte, 96),
   165  	}
   166  	sig, err := validator.aggregateAndProofSig(context.Background(), pubKey, agg)
   167  	require.NoError(t, err)
   168  	_, err = bls.SignatureFromBytes(sig)
   169  	require.NoError(t, err)
   170  }