mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
245c18784e
* Return if sign fails
163 lines
5.3 KiB
Go
163 lines
5.3 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
|
"github.com/prysmaticlabs/prysm/shared/slotutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestSubmitAggregateAndProof_GetDutiesRequestFailure(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, _, finish := setup(t)
|
|
validator.duties = ðpb.DutiesResponse{Duties: []*ethpb.DutiesResponse_Duty{}}
|
|
defer finish()
|
|
|
|
validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey)
|
|
|
|
require.LogsContain(t, hook, "Could not fetch validator assignment")
|
|
}
|
|
|
|
func TestSubmitAggregateAndProof_SignFails(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
validator.duties = ðpb.DutiesResponse{
|
|
Duties: []*ethpb.DutiesResponse_Duty{
|
|
{
|
|
PublicKey: validatorKey.PublicKey.Marshal(),
|
|
},
|
|
},
|
|
}
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
|
|
|
|
m.validatorClient.EXPECT().SubmitAggregateSelectionProof(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(ðpb.AggregateSelectionRequest{}),
|
|
).Return(ðpb.AggregateSelectionResponse{
|
|
AggregateAndProof: ðpb.AggregateAttestationAndProof{
|
|
AggregatorIndex: 0,
|
|
Aggregate: ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: make([]byte, 32),
|
|
Target: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
Source: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
},
|
|
Signature: make([]byte, 96),
|
|
AggregationBits: make([]byte, 1),
|
|
},
|
|
SelectionProof: make([]byte, 96),
|
|
},
|
|
}, nil)
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(ðpb.DomainResponse{SignatureDomain: nil}, errors.New("bad domain root"))
|
|
|
|
validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey)
|
|
}
|
|
|
|
func TestSubmitAggregateAndProof_Ok(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
validator.duties = ðpb.DutiesResponse{
|
|
Duties: []*ethpb.DutiesResponse_Duty{
|
|
{
|
|
PublicKey: validatorKey.PublicKey.Marshal(),
|
|
},
|
|
},
|
|
}
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
|
|
|
|
m.validatorClient.EXPECT().SubmitAggregateSelectionProof(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(ðpb.AggregateSelectionRequest{}),
|
|
).Return(ðpb.AggregateSelectionResponse{
|
|
AggregateAndProof: ðpb.AggregateAttestationAndProof{
|
|
AggregatorIndex: 0,
|
|
Aggregate: ðpb.Attestation{
|
|
Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: make([]byte, 32),
|
|
Target: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
Source: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
},
|
|
Signature: make([]byte, 96),
|
|
AggregationBits: make([]byte, 1),
|
|
},
|
|
SelectionProof: make([]byte, 96),
|
|
},
|
|
}, nil)
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
|
|
|
|
m.validatorClient.EXPECT().SubmitSignedAggregateSelectionProof(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(ðpb.SignedAggregateSubmitRequest{}),
|
|
).Return(ðpb.SignedAggregateSubmitResponse{AttestationDataRoot: make([]byte, 32)}, nil)
|
|
|
|
validator.SubmitAggregateAndProof(context.Background(), 0, validatorPubKey)
|
|
}
|
|
|
|
func TestWaitForSlotTwoThird_WaitCorrectly(t *testing.T) {
|
|
validator, _, finish := setup(t)
|
|
defer finish()
|
|
currentTime := roughtime.Now()
|
|
numOfSlots := uint64(4)
|
|
validator.genesisTime = uint64(currentTime.Unix()) - (numOfSlots * params.BeaconConfig().SecondsPerSlot)
|
|
oneThird := slotutil.DivideSlotBy(3 /* one third of slot duration */)
|
|
timeToSleep := oneThird + oneThird
|
|
|
|
twoThirdTime := currentTime.Add(timeToSleep)
|
|
validator.waitToSlotTwoThirds(context.Background(), numOfSlots)
|
|
currentTime = roughtime.Now()
|
|
assert.Equal(t, twoThirdTime.Unix(), currentTime.Unix())
|
|
}
|
|
|
|
func TestAggregateAndProofSignature_CanSignValidSignature(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
ðpb.DomainRequest{Epoch: 0, Domain: params.BeaconConfig().DomainAggregateAndProof[:]},
|
|
).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/)
|
|
|
|
agg := ðpb.AggregateAttestationAndProof{
|
|
AggregatorIndex: 0,
|
|
Aggregate: ðpb.Attestation{
|
|
AggregationBits: bitfield.NewBitlist(1), Data: ðpb.AttestationData{
|
|
BeaconBlockRoot: make([]byte, 32),
|
|
Target: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
Source: ðpb.Checkpoint{Root: make([]byte, 32)},
|
|
},
|
|
Signature: make([]byte, 96),
|
|
},
|
|
SelectionProof: make([]byte, 96),
|
|
}
|
|
sig, err := validator.aggregateAndProofSig(context.Background(), validatorPubKey, agg)
|
|
require.NoError(t, err)
|
|
_, err = bls.SignatureFromBytes(sig)
|
|
require.NoError(t, err)
|
|
}
|