prysm-pulse/validator/client/beacon-api/prepare_beacon_proposer_test.go
Patrice Vignola 4eb4cd9756
Add REST implementation for Validator's PrepareBeaconProposer (#11784)
* Add REST implementation for Validator's PrepareBeaconProposer

* Change fee recipients

* Handle error in a better way

* Move endpoint into constant

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2022-12-19 12:23:54 +00:00

104 lines
2.7 KiB
Go

package beacon_api
import (
"bytes"
"encoding/json"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
"github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock"
)
const prepareBeaconProposerTestEndpoint = "/eth/v1/validator/prepare_beacon_proposer"
func TestPrepareBeaconProposer_Valid(t *testing.T) {
const feeRecipient1 = "0xca008b199c03a2a2f6bc2ed52d6404c4d8510b35"
const feeRecipient2 = "0x8145d80111309e4621ed7632319664ac440b0198"
const feeRecipient3 = "0x085f2adb1295821838910be402b3c8cdc118bd86"
ctrl := gomock.NewController(t)
defer ctrl.Finish()
jsonRecipients := []*apimiddleware.FeeRecipientJson{
{
ValidatorIndex: "1",
FeeRecipient: feeRecipient1,
},
{
ValidatorIndex: "2",
FeeRecipient: feeRecipient2,
},
{
ValidatorIndex: "3",
FeeRecipient: feeRecipient3,
},
}
marshalledJsonRecipients, err := json.Marshal(jsonRecipients)
require.NoError(t, err)
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
jsonRestHandler.EXPECT().PostRestJson(
prepareBeaconProposerTestEndpoint,
nil,
bytes.NewBuffer(marshalledJsonRecipients),
nil,
).Return(
nil,
nil,
).Times(1)
decodedFeeRecipient1, err := hexutil.Decode(feeRecipient1)
require.NoError(t, err)
decodedFeeRecipient2, err := hexutil.Decode(feeRecipient2)
require.NoError(t, err)
decodedFeeRecipient3, err := hexutil.Decode(feeRecipient3)
require.NoError(t, err)
protoRecipients := []*ethpb.PrepareBeaconProposerRequest_FeeRecipientContainer{
{
ValidatorIndex: 1,
FeeRecipient: decodedFeeRecipient1,
},
{
ValidatorIndex: 2,
FeeRecipient: decodedFeeRecipient2,
},
{
ValidatorIndex: 3,
FeeRecipient: decodedFeeRecipient3,
},
}
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
err = validatorClient.prepareBeaconProposer(protoRecipients)
require.NoError(t, err)
}
func TestPrepareBeaconProposer_BadRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
jsonRestHandler.EXPECT().PostRestJson(
prepareBeaconProposerTestEndpoint,
nil,
gomock.Any(),
nil,
).Return(
nil,
errors.New("foo error"),
).Times(1)
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
err := validatorClient.prepareBeaconProposer(nil)
assert.ErrorContains(t, "failed to send POST data to REST endpoint", err)
assert.ErrorContains(t, "foo error", err)
}