Files
prysm/validator/client/beacon-api/submit_signed_aggregate_proof_test.go
Sammy Rosso 4ff91bebf8 Switch gomock library (#13639)
* Update gomock

* Update mockgen

* Gaz

* Go mod

* Cleanup

* Regenerate gomock

* Manually fix import
2024-02-21 18:37:17 +00:00

104 lines
3.4 KiB
Go

package beacon_api
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/validator/client/beacon-api/mock"
test_helpers "github.com/prysmaticlabs/prysm/v5/validator/client/beacon-api/test-helpers"
"go.uber.org/mock/gomock"
)
func TestSubmitSignedAggregateSelectionProof_Valid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
signedAggregateAndProof := generateSignedAggregateAndProofJson()
marshalledSignedAggregateSignedAndProof, err := json.Marshal([]*structs.SignedAggregateAttestationAndProof{jsonifySignedAggregateAndProof(signedAggregateAndProof)})
require.NoError(t, err)
ctx := context.Background()
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().Post(
ctx,
"/eth/v1/validator/aggregate_and_proofs",
nil,
bytes.NewBuffer(marshalledSignedAggregateSignedAndProof),
nil,
).Return(
nil,
).Times(1)
attestationDataRoot, err := signedAggregateAndProof.Message.Aggregate.Data.HashTreeRoot()
require.NoError(t, err)
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
resp, err := validatorClient.submitSignedAggregateSelectionProof(ctx, &ethpb.SignedAggregateSubmitRequest{
SignedAggregateAndProof: signedAggregateAndProof,
})
require.NoError(t, err)
assert.DeepEqual(t, attestationDataRoot[:], resp.AttestationDataRoot)
}
func TestSubmitSignedAggregateSelectionProof_BadRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
signedAggregateAndProof := generateSignedAggregateAndProofJson()
marshalledSignedAggregateSignedAndProof, err := json.Marshal([]*structs.SignedAggregateAttestationAndProof{jsonifySignedAggregateAndProof(signedAggregateAndProof)})
require.NoError(t, err)
ctx := context.Background()
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().Post(
ctx,
"/eth/v1/validator/aggregate_and_proofs",
nil,
bytes.NewBuffer(marshalledSignedAggregateSignedAndProof),
nil,
).Return(
errors.New("bad request"),
).Times(1)
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
_, err = validatorClient.submitSignedAggregateSelectionProof(ctx, &ethpb.SignedAggregateSubmitRequest{
SignedAggregateAndProof: signedAggregateAndProof,
})
assert.ErrorContains(t, "bad request", err)
}
func generateSignedAggregateAndProofJson() *ethpb.SignedAggregateAttestationAndProof {
return &ethpb.SignedAggregateAttestationAndProof{
Message: &ethpb.AggregateAttestationAndProof{
AggregatorIndex: 72,
Aggregate: &ethpb.Attestation{
AggregationBits: test_helpers.FillByteSlice(4, 74),
Data: &ethpb.AttestationData{
Slot: 75,
CommitteeIndex: 76,
BeaconBlockRoot: test_helpers.FillByteSlice(32, 38),
Source: &ethpb.Checkpoint{
Epoch: 78,
Root: test_helpers.FillByteSlice(32, 79),
},
Target: &ethpb.Checkpoint{
Epoch: 80,
Root: test_helpers.FillByteSlice(32, 81),
},
},
Signature: test_helpers.FillByteSlice(96, 82),
},
SelectionProof: test_helpers.FillByteSlice(96, 82),
},
Signature: test_helpers.FillByteSlice(96, 82),
}
}