diff --git a/hack/update-mockgen.sh b/hack/update-mockgen.sh index 9c771927d..fbe35eaf6 100755 --- a/hack/update-mockgen.sh +++ b/hack/update-mockgen.sh @@ -68,3 +68,19 @@ done goimports -w "$mock_path/." gofmt -s -w "$mock_path/." +# github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api +# -------------------------------------------------------- +beacon_api_mock_path="validator/client/beacon-api/mock" +beacon_api_mocks=( + "$beacon_api_mock_path/genesis_mock.go genesis.go" +) + +for ((i = 0; i < ${#beacon_api_mocks[@]}; i++)); do + file=${beacon_api_mocks[i]% *}; + source=${beacon_api_mocks[i]#* }; + echo "generating $file for file: $source"; + GO11MODULE=on mockgen -package=mock --build_flags="--tags=use_beacon_api" -source="validator/client/beacon-api/$source" -destination="$file" +done + +goimports -w "$beacon_api_mock_path/." +gofmt -s -w "$beacon_api_mock_path/." diff --git a/testing/mock/validator_client_mock.go b/testing/mock/validator_client_mock.go index 89e6679c9..36a3f94c3 100644 --- a/testing/mock/validator_client_mock.go +++ b/testing/mock/validator_client_mock.go @@ -9,185 +9,185 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" - v1alpha1 "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" + eth "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" emptypb "google.golang.org/protobuf/types/known/emptypb" ) -// MockValidatorClient is a mock of ValidatorClient interface +// MockValidatorClient is a mock of ValidatorClient interface. type MockValidatorClient struct { ctrl *gomock.Controller recorder *MockValidatorClientMockRecorder } -// MockValidatorClientMockRecorder is the mock recorder for MockValidatorClient +// MockValidatorClientMockRecorder is the mock recorder for MockValidatorClient. type MockValidatorClientMockRecorder struct { mock *MockValidatorClient } -// NewMockValidatorClient creates a new mock instance +// NewMockValidatorClient creates a new mock instance. func NewMockValidatorClient(ctrl *gomock.Controller) *MockValidatorClient { mock := &MockValidatorClient{ctrl: ctrl} mock.recorder = &MockValidatorClientMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockValidatorClient) EXPECT() *MockValidatorClientMockRecorder { return m.recorder } -// CheckDoppelGanger mocks base method -func (m *MockValidatorClient) CheckDoppelGanger(arg0 context.Context, arg1 *v1alpha1.DoppelGangerRequest) (*v1alpha1.DoppelGangerResponse, error) { +// CheckDoppelGanger mocks base method. +func (m *MockValidatorClient) CheckDoppelGanger(arg0 context.Context, arg1 *eth.DoppelGangerRequest) (*eth.DoppelGangerResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CheckDoppelGanger", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.DoppelGangerResponse) + ret0, _ := ret[0].(*eth.DoppelGangerResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// CheckDoppelGanger indicates an expected call of CheckDoppelGanger +// CheckDoppelGanger indicates an expected call of CheckDoppelGanger. func (mr *MockValidatorClientMockRecorder) CheckDoppelGanger(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckDoppelGanger", reflect.TypeOf((*MockValidatorClient)(nil).CheckDoppelGanger), arg0, arg1) } -// DomainData mocks base method -func (m *MockValidatorClient) DomainData(arg0 context.Context, arg1 *v1alpha1.DomainRequest) (*v1alpha1.DomainResponse, error) { +// DomainData mocks base method. +func (m *MockValidatorClient) DomainData(arg0 context.Context, arg1 *eth.DomainRequest) (*eth.DomainResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DomainData", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.DomainResponse) + ret0, _ := ret[0].(*eth.DomainResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// DomainData indicates an expected call of DomainData +// DomainData indicates an expected call of DomainData. func (mr *MockValidatorClientMockRecorder) DomainData(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DomainData", reflect.TypeOf((*MockValidatorClient)(nil).DomainData), arg0, arg1) } -// GetAttestationData mocks base method -func (m *MockValidatorClient) GetAttestationData(arg0 context.Context, arg1 *v1alpha1.AttestationDataRequest) (*v1alpha1.AttestationData, error) { +// GetAttestationData mocks base method. +func (m *MockValidatorClient) GetAttestationData(arg0 context.Context, arg1 *eth.AttestationDataRequest) (*eth.AttestationData, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetAttestationData", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.AttestationData) + ret0, _ := ret[0].(*eth.AttestationData) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetAttestationData indicates an expected call of GetAttestationData +// GetAttestationData indicates an expected call of GetAttestationData. func (mr *MockValidatorClientMockRecorder) GetAttestationData(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAttestationData", reflect.TypeOf((*MockValidatorClient)(nil).GetAttestationData), arg0, arg1) } -// GetBeaconBlock mocks base method -func (m *MockValidatorClient) GetBeaconBlock(arg0 context.Context, arg1 *v1alpha1.BlockRequest) (*v1alpha1.GenericBeaconBlock, error) { +// GetBeaconBlock mocks base method. +func (m *MockValidatorClient) GetBeaconBlock(arg0 context.Context, arg1 *eth.BlockRequest) (*eth.GenericBeaconBlock, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetBeaconBlock", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.GenericBeaconBlock) + ret0, _ := ret[0].(*eth.GenericBeaconBlock) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetBeaconBlock indicates an expected call of GetBeaconBlock +// GetBeaconBlock indicates an expected call of GetBeaconBlock. func (mr *MockValidatorClientMockRecorder) GetBeaconBlock(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBeaconBlock", reflect.TypeOf((*MockValidatorClient)(nil).GetBeaconBlock), arg0, arg1) } -// GetDuties mocks base method -func (m *MockValidatorClient) GetDuties(arg0 context.Context, arg1 *v1alpha1.DutiesRequest) (*v1alpha1.DutiesResponse, error) { +// GetDuties mocks base method. +func (m *MockValidatorClient) GetDuties(arg0 context.Context, arg1 *eth.DutiesRequest) (*eth.DutiesResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetDuties", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.DutiesResponse) + ret0, _ := ret[0].(*eth.DutiesResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetDuties indicates an expected call of GetDuties +// GetDuties indicates an expected call of GetDuties. func (mr *MockValidatorClientMockRecorder) GetDuties(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDuties", reflect.TypeOf((*MockValidatorClient)(nil).GetDuties), arg0, arg1) } -// GetFeeRecipientByPubKey mocks base method -func (m *MockValidatorClient) GetFeeRecipientByPubKey(arg0 context.Context, arg1 *v1alpha1.FeeRecipientByPubKeyRequest) (*v1alpha1.FeeRecipientByPubKeyResponse, error) { +// GetFeeRecipientByPubKey mocks base method. +func (m *MockValidatorClient) GetFeeRecipientByPubKey(arg0 context.Context, arg1 *eth.FeeRecipientByPubKeyRequest) (*eth.FeeRecipientByPubKeyResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetFeeRecipientByPubKey", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.FeeRecipientByPubKeyResponse) + ret0, _ := ret[0].(*eth.FeeRecipientByPubKeyResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetFeeRecipientByPubKey indicates an expected call of GetFeeRecipientByPubKey +// GetFeeRecipientByPubKey indicates an expected call of GetFeeRecipientByPubKey. func (mr *MockValidatorClientMockRecorder) GetFeeRecipientByPubKey(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFeeRecipientByPubKey", reflect.TypeOf((*MockValidatorClient)(nil).GetFeeRecipientByPubKey), arg0, arg1) } -// GetSyncCommitteeContribution mocks base method -func (m *MockValidatorClient) GetSyncCommitteeContribution(arg0 context.Context, arg1 *v1alpha1.SyncCommitteeContributionRequest) (*v1alpha1.SyncCommitteeContribution, error) { +// GetSyncCommitteeContribution mocks base method. +func (m *MockValidatorClient) GetSyncCommitteeContribution(arg0 context.Context, arg1 *eth.SyncCommitteeContributionRequest) (*eth.SyncCommitteeContribution, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSyncCommitteeContribution", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.SyncCommitteeContribution) + ret0, _ := ret[0].(*eth.SyncCommitteeContribution) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetSyncCommitteeContribution indicates an expected call of GetSyncCommitteeContribution +// GetSyncCommitteeContribution indicates an expected call of GetSyncCommitteeContribution. func (mr *MockValidatorClientMockRecorder) GetSyncCommitteeContribution(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSyncCommitteeContribution", reflect.TypeOf((*MockValidatorClient)(nil).GetSyncCommitteeContribution), arg0, arg1) } -// GetSyncMessageBlockRoot mocks base method -func (m *MockValidatorClient) GetSyncMessageBlockRoot(arg0 context.Context, arg1 *emptypb.Empty) (*v1alpha1.SyncMessageBlockRootResponse, error) { +// GetSyncMessageBlockRoot mocks base method. +func (m *MockValidatorClient) GetSyncMessageBlockRoot(arg0 context.Context, arg1 *emptypb.Empty) (*eth.SyncMessageBlockRootResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSyncMessageBlockRoot", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.SyncMessageBlockRootResponse) + ret0, _ := ret[0].(*eth.SyncMessageBlockRootResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetSyncMessageBlockRoot indicates an expected call of GetSyncMessageBlockRoot +// GetSyncMessageBlockRoot indicates an expected call of GetSyncMessageBlockRoot. func (mr *MockValidatorClientMockRecorder) GetSyncMessageBlockRoot(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSyncMessageBlockRoot", reflect.TypeOf((*MockValidatorClient)(nil).GetSyncMessageBlockRoot), arg0, arg1) } -// GetSyncSubcommitteeIndex mocks base method -func (m *MockValidatorClient) GetSyncSubcommitteeIndex(arg0 context.Context, arg1 *v1alpha1.SyncSubcommitteeIndexRequest) (*v1alpha1.SyncSubcommitteeIndexResponse, error) { +// GetSyncSubcommitteeIndex mocks base method. +func (m *MockValidatorClient) GetSyncSubcommitteeIndex(arg0 context.Context, arg1 *eth.SyncSubcommitteeIndexRequest) (*eth.SyncSubcommitteeIndexResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetSyncSubcommitteeIndex", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.SyncSubcommitteeIndexResponse) + ret0, _ := ret[0].(*eth.SyncSubcommitteeIndexResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetSyncSubcommitteeIndex indicates an expected call of GetSyncSubcommitteeIndex +// GetSyncSubcommitteeIndex indicates an expected call of GetSyncSubcommitteeIndex. func (mr *MockValidatorClientMockRecorder) GetSyncSubcommitteeIndex(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSyncSubcommitteeIndex", reflect.TypeOf((*MockValidatorClient)(nil).GetSyncSubcommitteeIndex), arg0, arg1) } -// MultipleValidatorStatus mocks base method -func (m *MockValidatorClient) MultipleValidatorStatus(arg0 context.Context, arg1 *v1alpha1.MultipleValidatorStatusRequest) (*v1alpha1.MultipleValidatorStatusResponse, error) { +// MultipleValidatorStatus mocks base method. +func (m *MockValidatorClient) MultipleValidatorStatus(arg0 context.Context, arg1 *eth.MultipleValidatorStatusRequest) (*eth.MultipleValidatorStatusResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "MultipleValidatorStatus", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.MultipleValidatorStatusResponse) + ret0, _ := ret[0].(*eth.MultipleValidatorStatusResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// MultipleValidatorStatus indicates an expected call of MultipleValidatorStatus +// MultipleValidatorStatus indicates an expected call of MultipleValidatorStatus. func (mr *MockValidatorClientMockRecorder) MultipleValidatorStatus(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MultipleValidatorStatus", reflect.TypeOf((*MockValidatorClient)(nil).MultipleValidatorStatus), arg0, arg1) } -// PrepareBeaconProposer mocks base method -func (m *MockValidatorClient) PrepareBeaconProposer(arg0 context.Context, arg1 *v1alpha1.PrepareBeaconProposerRequest) (*emptypb.Empty, error) { +// PrepareBeaconProposer mocks base method. +func (m *MockValidatorClient) PrepareBeaconProposer(arg0 context.Context, arg1 *eth.PrepareBeaconProposerRequest) (*emptypb.Empty, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "PrepareBeaconProposer", arg0, arg1) ret0, _ := ret[0].(*emptypb.Empty) @@ -195,119 +195,119 @@ func (m *MockValidatorClient) PrepareBeaconProposer(arg0 context.Context, arg1 * return ret0, ret1 } -// PrepareBeaconProposer indicates an expected call of PrepareBeaconProposer +// PrepareBeaconProposer indicates an expected call of PrepareBeaconProposer. func (mr *MockValidatorClientMockRecorder) PrepareBeaconProposer(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PrepareBeaconProposer", reflect.TypeOf((*MockValidatorClient)(nil).PrepareBeaconProposer), arg0, arg1) } -// ProposeAttestation mocks base method -func (m *MockValidatorClient) ProposeAttestation(arg0 context.Context, arg1 *v1alpha1.Attestation) (*v1alpha1.AttestResponse, error) { +// ProposeAttestation mocks base method. +func (m *MockValidatorClient) ProposeAttestation(arg0 context.Context, arg1 *eth.Attestation) (*eth.AttestResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ProposeAttestation", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.AttestResponse) + ret0, _ := ret[0].(*eth.AttestResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// ProposeAttestation indicates an expected call of ProposeAttestation +// ProposeAttestation indicates an expected call of ProposeAttestation. func (mr *MockValidatorClientMockRecorder) ProposeAttestation(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProposeAttestation", reflect.TypeOf((*MockValidatorClient)(nil).ProposeAttestation), arg0, arg1) } -// ProposeBeaconBlock mocks base method -func (m *MockValidatorClient) ProposeBeaconBlock(arg0 context.Context, arg1 *v1alpha1.GenericSignedBeaconBlock) (*v1alpha1.ProposeResponse, error) { +// ProposeBeaconBlock mocks base method. +func (m *MockValidatorClient) ProposeBeaconBlock(arg0 context.Context, arg1 *eth.GenericSignedBeaconBlock) (*eth.ProposeResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ProposeBeaconBlock", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.ProposeResponse) + ret0, _ := ret[0].(*eth.ProposeResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// ProposeBeaconBlock indicates an expected call of ProposeBeaconBlock +// ProposeBeaconBlock indicates an expected call of ProposeBeaconBlock. func (mr *MockValidatorClientMockRecorder) ProposeBeaconBlock(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProposeBeaconBlock", reflect.TypeOf((*MockValidatorClient)(nil).ProposeBeaconBlock), arg0, arg1) } -// ProposeExit mocks base method -func (m *MockValidatorClient) ProposeExit(arg0 context.Context, arg1 *v1alpha1.SignedVoluntaryExit) (*v1alpha1.ProposeExitResponse, error) { +// ProposeExit mocks base method. +func (m *MockValidatorClient) ProposeExit(arg0 context.Context, arg1 *eth.SignedVoluntaryExit) (*eth.ProposeExitResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ProposeExit", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.ProposeExitResponse) + ret0, _ := ret[0].(*eth.ProposeExitResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// ProposeExit indicates an expected call of ProposeExit +// ProposeExit indicates an expected call of ProposeExit. func (mr *MockValidatorClientMockRecorder) ProposeExit(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProposeExit", reflect.TypeOf((*MockValidatorClient)(nil).ProposeExit), arg0, arg1) } -// StreamBlocksAltair mocks base method -func (m *MockValidatorClient) StreamBlocksAltair(arg0 context.Context, arg1 *v1alpha1.StreamBlocksRequest) (v1alpha1.BeaconNodeValidator_StreamBlocksAltairClient, error) { +// StreamBlocksAltair mocks base method. +func (m *MockValidatorClient) StreamBlocksAltair(arg0 context.Context, arg1 *eth.StreamBlocksRequest) (eth.BeaconNodeValidator_StreamBlocksAltairClient, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "StreamBlocksAltair", arg0, arg1) - ret0, _ := ret[0].(v1alpha1.BeaconNodeValidator_StreamBlocksAltairClient) + ret0, _ := ret[0].(eth.BeaconNodeValidator_StreamBlocksAltairClient) ret1, _ := ret[1].(error) return ret0, ret1 } -// StreamBlocksAltair indicates an expected call of StreamBlocksAltair +// StreamBlocksAltair indicates an expected call of StreamBlocksAltair. func (mr *MockValidatorClientMockRecorder) StreamBlocksAltair(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StreamBlocksAltair", reflect.TypeOf((*MockValidatorClient)(nil).StreamBlocksAltair), arg0, arg1) } -// StreamDuties mocks base method -func (m *MockValidatorClient) StreamDuties(arg0 context.Context, arg1 *v1alpha1.DutiesRequest) (v1alpha1.BeaconNodeValidator_StreamDutiesClient, error) { +// StreamDuties mocks base method. +func (m *MockValidatorClient) StreamDuties(arg0 context.Context, arg1 *eth.DutiesRequest) (eth.BeaconNodeValidator_StreamDutiesClient, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "StreamDuties", arg0, arg1) - ret0, _ := ret[0].(v1alpha1.BeaconNodeValidator_StreamDutiesClient) + ret0, _ := ret[0].(eth.BeaconNodeValidator_StreamDutiesClient) ret1, _ := ret[1].(error) return ret0, ret1 } -// StreamDuties indicates an expected call of StreamDuties +// StreamDuties indicates an expected call of StreamDuties. func (mr *MockValidatorClientMockRecorder) StreamDuties(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StreamDuties", reflect.TypeOf((*MockValidatorClient)(nil).StreamDuties), arg0, arg1) } -// SubmitAggregateSelectionProof mocks base method -func (m *MockValidatorClient) SubmitAggregateSelectionProof(arg0 context.Context, arg1 *v1alpha1.AggregateSelectionRequest) (*v1alpha1.AggregateSelectionResponse, error) { +// SubmitAggregateSelectionProof mocks base method. +func (m *MockValidatorClient) SubmitAggregateSelectionProof(arg0 context.Context, arg1 *eth.AggregateSelectionRequest) (*eth.AggregateSelectionResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitAggregateSelectionProof", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.AggregateSelectionResponse) + ret0, _ := ret[0].(*eth.AggregateSelectionResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// SubmitAggregateSelectionProof indicates an expected call of SubmitAggregateSelectionProof +// SubmitAggregateSelectionProof indicates an expected call of SubmitAggregateSelectionProof. func (mr *MockValidatorClientMockRecorder) SubmitAggregateSelectionProof(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitAggregateSelectionProof", reflect.TypeOf((*MockValidatorClient)(nil).SubmitAggregateSelectionProof), arg0, arg1) } -// SubmitSignedAggregateSelectionProof mocks base method -func (m *MockValidatorClient) SubmitSignedAggregateSelectionProof(arg0 context.Context, arg1 *v1alpha1.SignedAggregateSubmitRequest) (*v1alpha1.SignedAggregateSubmitResponse, error) { +// SubmitSignedAggregateSelectionProof mocks base method. +func (m *MockValidatorClient) SubmitSignedAggregateSelectionProof(arg0 context.Context, arg1 *eth.SignedAggregateSubmitRequest) (*eth.SignedAggregateSubmitResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedAggregateSelectionProof", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.SignedAggregateSubmitResponse) + ret0, _ := ret[0].(*eth.SignedAggregateSubmitResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// SubmitSignedAggregateSelectionProof indicates an expected call of SubmitSignedAggregateSelectionProof +// SubmitSignedAggregateSelectionProof indicates an expected call of SubmitSignedAggregateSelectionProof. func (mr *MockValidatorClientMockRecorder) SubmitSignedAggregateSelectionProof(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitSignedAggregateSelectionProof", reflect.TypeOf((*MockValidatorClient)(nil).SubmitSignedAggregateSelectionProof), arg0, arg1) } -// SubmitSignedContributionAndProof mocks base method -func (m *MockValidatorClient) SubmitSignedContributionAndProof(arg0 context.Context, arg1 *v1alpha1.SignedContributionAndProof) (*emptypb.Empty, error) { +// SubmitSignedContributionAndProof mocks base method. +func (m *MockValidatorClient) SubmitSignedContributionAndProof(arg0 context.Context, arg1 *eth.SignedContributionAndProof) (*emptypb.Empty, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedContributionAndProof", arg0, arg1) ret0, _ := ret[0].(*emptypb.Empty) @@ -315,14 +315,14 @@ func (m *MockValidatorClient) SubmitSignedContributionAndProof(arg0 context.Cont return ret0, ret1 } -// SubmitSignedContributionAndProof indicates an expected call of SubmitSignedContributionAndProof +// SubmitSignedContributionAndProof indicates an expected call of SubmitSignedContributionAndProof. func (mr *MockValidatorClientMockRecorder) SubmitSignedContributionAndProof(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitSignedContributionAndProof", reflect.TypeOf((*MockValidatorClient)(nil).SubmitSignedContributionAndProof), arg0, arg1) } -// SubmitSyncMessage mocks base method -func (m *MockValidatorClient) SubmitSyncMessage(arg0 context.Context, arg1 *v1alpha1.SyncCommitteeMessage) (*emptypb.Empty, error) { +// SubmitSyncMessage mocks base method. +func (m *MockValidatorClient) SubmitSyncMessage(arg0 context.Context, arg1 *eth.SyncCommitteeMessage) (*emptypb.Empty, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSyncMessage", arg0, arg1) ret0, _ := ret[0].(*emptypb.Empty) @@ -330,14 +330,14 @@ func (m *MockValidatorClient) SubmitSyncMessage(arg0 context.Context, arg1 *v1al return ret0, ret1 } -// SubmitSyncMessage indicates an expected call of SubmitSyncMessage +// SubmitSyncMessage indicates an expected call of SubmitSyncMessage. func (mr *MockValidatorClientMockRecorder) SubmitSyncMessage(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitSyncMessage", reflect.TypeOf((*MockValidatorClient)(nil).SubmitSyncMessage), arg0, arg1) } -// SubmitValidatorRegistrations mocks base method -func (m *MockValidatorClient) SubmitValidatorRegistrations(arg0 context.Context, arg1 *v1alpha1.SignedValidatorRegistrationsV1) (*emptypb.Empty, error) { +// SubmitValidatorRegistrations mocks base method. +func (m *MockValidatorClient) SubmitValidatorRegistrations(arg0 context.Context, arg1 *eth.SignedValidatorRegistrationsV1) (*emptypb.Empty, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitValidatorRegistrations", arg0, arg1) ret0, _ := ret[0].(*emptypb.Empty) @@ -345,14 +345,14 @@ func (m *MockValidatorClient) SubmitValidatorRegistrations(arg0 context.Context, return ret0, ret1 } -// SubmitValidatorRegistrations indicates an expected call of SubmitValidatorRegistrations +// SubmitValidatorRegistrations indicates an expected call of SubmitValidatorRegistrations. func (mr *MockValidatorClientMockRecorder) SubmitValidatorRegistrations(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitValidatorRegistrations", reflect.TypeOf((*MockValidatorClient)(nil).SubmitValidatorRegistrations), arg0, arg1) } -// SubscribeCommitteeSubnets mocks base method -func (m *MockValidatorClient) SubscribeCommitteeSubnets(arg0 context.Context, arg1 *v1alpha1.CommitteeSubnetsSubscribeRequest) (*emptypb.Empty, error) { +// SubscribeCommitteeSubnets mocks base method. +func (m *MockValidatorClient) SubscribeCommitteeSubnets(arg0 context.Context, arg1 *eth.CommitteeSubnetsSubscribeRequest) (*emptypb.Empty, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubscribeCommitteeSubnets", arg0, arg1) ret0, _ := ret[0].(*emptypb.Empty) @@ -360,67 +360,67 @@ func (m *MockValidatorClient) SubscribeCommitteeSubnets(arg0 context.Context, ar return ret0, ret1 } -// SubscribeCommitteeSubnets indicates an expected call of SubscribeCommitteeSubnets +// SubscribeCommitteeSubnets indicates an expected call of SubscribeCommitteeSubnets. func (mr *MockValidatorClientMockRecorder) SubscribeCommitteeSubnets(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubscribeCommitteeSubnets", reflect.TypeOf((*MockValidatorClient)(nil).SubscribeCommitteeSubnets), arg0, arg1) } -// ValidatorIndex mocks base method -func (m *MockValidatorClient) ValidatorIndex(arg0 context.Context, arg1 *v1alpha1.ValidatorIndexRequest) (*v1alpha1.ValidatorIndexResponse, error) { +// ValidatorIndex mocks base method. +func (m *MockValidatorClient) ValidatorIndex(arg0 context.Context, arg1 *eth.ValidatorIndexRequest) (*eth.ValidatorIndexResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ValidatorIndex", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.ValidatorIndexResponse) + ret0, _ := ret[0].(*eth.ValidatorIndexResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// ValidatorIndex indicates an expected call of ValidatorIndex +// ValidatorIndex indicates an expected call of ValidatorIndex. func (mr *MockValidatorClientMockRecorder) ValidatorIndex(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatorIndex", reflect.TypeOf((*MockValidatorClient)(nil).ValidatorIndex), arg0, arg1) } -// ValidatorStatus mocks base method -func (m *MockValidatorClient) ValidatorStatus(arg0 context.Context, arg1 *v1alpha1.ValidatorStatusRequest) (*v1alpha1.ValidatorStatusResponse, error) { +// ValidatorStatus mocks base method. +func (m *MockValidatorClient) ValidatorStatus(arg0 context.Context, arg1 *eth.ValidatorStatusRequest) (*eth.ValidatorStatusResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ValidatorStatus", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.ValidatorStatusResponse) + ret0, _ := ret[0].(*eth.ValidatorStatusResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// ValidatorStatus indicates an expected call of ValidatorStatus +// ValidatorStatus indicates an expected call of ValidatorStatus. func (mr *MockValidatorClientMockRecorder) ValidatorStatus(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatorStatus", reflect.TypeOf((*MockValidatorClient)(nil).ValidatorStatus), arg0, arg1) } -// WaitForActivation mocks base method -func (m *MockValidatorClient) WaitForActivation(arg0 context.Context, arg1 *v1alpha1.ValidatorActivationRequest) (v1alpha1.BeaconNodeValidator_WaitForActivationClient, error) { +// WaitForActivation mocks base method. +func (m *MockValidatorClient) WaitForActivation(arg0 context.Context, arg1 *eth.ValidatorActivationRequest) (eth.BeaconNodeValidator_WaitForActivationClient, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "WaitForActivation", arg0, arg1) - ret0, _ := ret[0].(v1alpha1.BeaconNodeValidator_WaitForActivationClient) + ret0, _ := ret[0].(eth.BeaconNodeValidator_WaitForActivationClient) ret1, _ := ret[1].(error) return ret0, ret1 } -// WaitForActivation indicates an expected call of WaitForActivation +// WaitForActivation indicates an expected call of WaitForActivation. func (mr *MockValidatorClientMockRecorder) WaitForActivation(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WaitForActivation", reflect.TypeOf((*MockValidatorClient)(nil).WaitForActivation), arg0, arg1) } -// WaitForChainStart mocks base method -func (m *MockValidatorClient) WaitForChainStart(arg0 context.Context, arg1 *emptypb.Empty) (*v1alpha1.ChainStartResponse, error) { +// WaitForChainStart mocks base method. +func (m *MockValidatorClient) WaitForChainStart(arg0 context.Context, arg1 *emptypb.Empty) (*eth.ChainStartResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "WaitForChainStart", arg0, arg1) - ret0, _ := ret[0].(*v1alpha1.ChainStartResponse) + ret0, _ := ret[0].(*eth.ChainStartResponse) ret1, _ := ret[1].(error) return ret0, ret1 } -// WaitForChainStart indicates an expected call of WaitForChainStart +// WaitForChainStart indicates an expected call of WaitForChainStart. func (mr *MockValidatorClientMockRecorder) WaitForChainStart(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WaitForChainStart", reflect.TypeOf((*MockValidatorClient)(nil).WaitForChainStart), arg0, arg1) diff --git a/validator/client/beacon-api/BUILD.bazel b/validator/client/beacon-api/BUILD.bazel index fda31b5eb..074a78a8c 100644 --- a/validator/client/beacon-api/BUILD.bazel +++ b/validator/client/beacon-api/BUILD.bazel @@ -6,13 +6,18 @@ go_library( srcs = [ "beacon_api_helpers.go", "beacon_api_validator_client.go", + "domain_data.go", "genesis.go", ], importpath = "github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api", visibility = ["//validator:__subpackages__"], deps = [ "//api/gateway/apimiddleware:go_default_library", + "//beacon-chain/core/signing:go_default_library", "//beacon-chain/rpc/apimiddleware:go_default_library", + "//consensus-types/primitives:go_default_library", + "//encoding/bytesutil:go_default_library", + "//network/forks:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//validator/client/iface:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", @@ -27,6 +32,8 @@ go_test( size = "small", srcs = [ "beacon_api_helpers_test.go", + "beacon_api_validator_client_test.go", + "domain_data_test.go", "genesis_test.go", "handlers_test.go", "wait_for_chain_start_test.go", @@ -36,9 +43,14 @@ go_test( deps = [ "//api/gateway/apimiddleware:go_default_library", "//beacon-chain/rpc/apimiddleware:go_default_library", + "//config/params:go_default_library", + "//encoding/bytesutil:go_default_library", + "//proto/prysm/v1alpha1:go_default_library", "//testing/assert:go_default_library", "//testing/require:go_default_library", + "//validator/client/beacon-api/mock:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", + "@com_github_golang_mock//gomock:go_default_library", "@org_golang_google_protobuf//types/known/emptypb:go_default_library", ], ) diff --git a/validator/client/beacon-api/beacon_api_validator_client.go b/validator/client/beacon-api/beacon_api_validator_client.go index 5e7825126..b89dace9f 100644 --- a/validator/client/beacon-api/beacon_api_validator_client.go +++ b/validator/client/beacon-api/beacon_api_validator_client.go @@ -8,30 +8,35 @@ import ( "net/http" "time" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" iface "github.com/prysmaticlabs/prysm/v3/validator/client/iface" ) type beaconApiValidatorClient struct { - url string - httpClient http.Client - fallbackClient iface.ValidatorClient + url string + httpClient http.Client + fallbackClient iface.ValidatorClient + genesisProvider genesisProvider } -func NewBeaconApiValidatorClient(url string, timeout time.Duration) iface.ValidatorClient { +func NewBeaconApiValidatorClient(url string, timeout time.Duration) *beaconApiValidatorClient { + httpClient := http.Client{Timeout: timeout} + return &beaconApiValidatorClient{ - url: url, - httpClient: http.Client{Timeout: timeout}, + url: url, + httpClient: httpClient, + genesisProvider: beaconApiGenesisProvider{httpClient: httpClient, url: url}, } } -func NewBeaconApiValidatorClientWithFallback(url string, timeout time.Duration, fallbackClient iface.ValidatorClient) iface.ValidatorClient { - return &beaconApiValidatorClient{ - url: url, - httpClient: http.Client{Timeout: timeout}, - fallbackClient: fallbackClient, - } +func NewBeaconApiValidatorClientWithFallback(url string, timeout time.Duration, fallbackClient iface.ValidatorClient) *beaconApiValidatorClient { + beaconApiValidatorClient := NewBeaconApiValidatorClient(url, timeout) + beaconApiValidatorClient.fallbackClient = fallbackClient + return beaconApiValidatorClient } func (c *beaconApiValidatorClient) GetDuties(ctx context.Context, in *ethpb.DutiesRequest) (*ethpb.DutiesResponse, error) { @@ -52,13 +57,13 @@ func (c *beaconApiValidatorClient) CheckDoppelGanger(ctx context.Context, in *et panic("beaconApiValidatorClient.CheckDoppelGanger is not implemented. To use a fallback client, create this validator with NewBeaconApiValidatorClientWithFallback instead.") } -func (c *beaconApiValidatorClient) DomainData(ctx context.Context, in *ethpb.DomainRequest) (*ethpb.DomainResponse, error) { - if c.fallbackClient != nil { - return c.fallbackClient.DomainData(ctx, in) +func (c *beaconApiValidatorClient) DomainData(_ context.Context, in *ethpb.DomainRequest) (*ethpb.DomainResponse, error) { + if len(in.Domain) != 4 { + return nil, errors.Errorf("invalid domain type: %s", hexutil.Encode(in.Domain)) } - // TODO: Implement me - panic("beaconApiValidatorClient.DomainData is not implemented. To use a fallback client, create this validator with NewBeaconApiValidatorClientWithFallback instead.") + domainType := bytesutil.ToBytes4(in.Domain) + return c.getDomainData(in.Epoch, domainType) } func (c *beaconApiValidatorClient) GetAttestationData(ctx context.Context, in *ethpb.AttestationDataRequest) (*ethpb.AttestationData, error) { diff --git a/validator/client/beacon-api/beacon_api_validator_client_test.go b/validator/client/beacon-api/beacon_api_validator_client_test.go new file mode 100644 index 000000000..f8aacf283 --- /dev/null +++ b/validator/client/beacon-api/beacon_api_validator_client_test.go @@ -0,0 +1,52 @@ +//go:build use_beacon_api +// +build use_beacon_api + +package beacon_api + +import ( + "context" + "fmt" + "testing" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/golang/mock/gomock" + rpcmiddleware "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware" + "github.com/prysmaticlabs/prysm/v3/config/params" + "github.com/prysmaticlabs/prysm/v3/encoding/bytesutil" + ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/v3/testing/assert" + "github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock" +) + +// Check that the DomainData() returns whatever the internal getDomainData() returns +func TestBeaconApiValidatorClient_DomainDataValid(t *testing.T) { + const genesisValidatorRoot = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2" + epoch := params.BeaconConfig().AltairForkEpoch + domainType := params.BeaconConfig().DomainSyncCommittee[:] + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + genesisProvider := mock.NewMockgenesisProvider(ctrl) + genesisProvider.EXPECT().GetGenesis().Return( + &rpcmiddleware.GenesisResponse_GenesisJson{GenesisValidatorsRoot: genesisValidatorRoot}, + nil, + nil, + ).Times(2) + + validatorClient := beaconApiValidatorClient{genesisProvider: genesisProvider} + resp, err := validatorClient.DomainData(context.Background(), ðpb.DomainRequest{Epoch: epoch, Domain: domainType}) + + domainTypeArray := bytesutil.ToBytes4(domainType) + expectedResp, expectedErr := validatorClient.getDomainData(epoch, domainTypeArray) + assert.DeepEqual(t, expectedErr, err) + assert.DeepEqual(t, expectedResp, resp) +} + +func TestBeaconApiValidatorClient_DomainDataError(t *testing.T) { + epoch := params.BeaconConfig().AltairForkEpoch + domainType := make([]byte, 3) + validatorClient := beaconApiValidatorClient{} + _, err := validatorClient.DomainData(context.Background(), ðpb.DomainRequest{Epoch: epoch, Domain: domainType}) + assert.ErrorContains(t, fmt.Sprintf("invalid domain type: %s", hexutil.Encode(domainType)), err) +} diff --git a/validator/client/beacon-api/domain_data.go b/validator/client/beacon-api/domain_data.go new file mode 100644 index 000000000..d7ea290d7 --- /dev/null +++ b/validator/client/beacon-api/domain_data.go @@ -0,0 +1,43 @@ +//go:build use_beacon_api +// +build use_beacon_api + +package beacon_api + +import ( + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/signing" + types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v3/network/forks" + ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" +) + +func (c beaconApiValidatorClient) getDomainData(epoch types.Epoch, domainType [4]byte) (*ethpb.DomainResponse, error) { + // Get the fork version from the given epoch + fork, err := forks.Fork(epoch) + if err != nil { + return nil, errors.Wrapf(err, "failed to get fork version for epoch %d", epoch) + } + + // Get the genesis validator root + genesis, _, err := c.genesisProvider.GetGenesis() + if err != nil { + return nil, errors.Wrap(err, "failed to get genesis info") + } + + if !validRoot(genesis.GenesisValidatorsRoot) { + return nil, errors.Errorf("invalid genesis validators root: %s", genesis.GenesisValidatorsRoot) + } + + genesisValidatorRoot, err := hexutil.Decode(genesis.GenesisValidatorsRoot) + if err != nil { + return nil, errors.Wrap(err, "failed to decode genesis validators root") + } + + signatureDomain, err := signing.Domain(fork, epoch, domainType, genesisValidatorRoot) + if err != nil { + return nil, errors.Wrap(err, "failed to compute signature domain") + } + + return ðpb.DomainResponse{SignatureDomain: signatureDomain}, nil +} diff --git a/validator/client/beacon-api/domain_data_test.go b/validator/client/beacon-api/domain_data_test.go new file mode 100644 index 000000000..3c2941379 --- /dev/null +++ b/validator/client/beacon-api/domain_data_test.go @@ -0,0 +1,95 @@ +//go:build use_beacon_api +// +build use_beacon_api + +package beacon_api + +import ( + "errors" + "testing" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/golang/mock/gomock" + rpcmiddleware "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware" + "github.com/prysmaticlabs/prysm/v3/config/params" + 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" +) + +func TestGetDomainData_ValidDomainData(t *testing.T) { + const genesisValidatorRoot = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2" + forkVersion := params.BeaconConfig().AltairForkVersion + epoch := params.BeaconConfig().AltairForkEpoch + domainType := params.BeaconConfig().DomainBeaconProposer + + genesisValidatorRootBytes, err := hexutil.Decode(genesisValidatorRoot) + require.NoError(t, err) + + expectedForkDataRoot, err := (ðpb.ForkData{ + CurrentVersion: forkVersion, + GenesisValidatorsRoot: genesisValidatorRootBytes, + }).HashTreeRoot() + require.NoError(t, err) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + // Make sure that GetGenesis() is called exactly once + genesisProvider := mock.NewMockgenesisProvider(ctrl) + genesisProvider.EXPECT().GetGenesis().Return( + &rpcmiddleware.GenesisResponse_GenesisJson{GenesisValidatorsRoot: genesisValidatorRoot}, + nil, + nil, + ).Times(1) + + validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} + resp, err := validatorClient.getDomainData(epoch, domainType) + assert.NoError(t, err) + require.NotNil(t, resp) + + var expectedSignatureDomain []byte + expectedSignatureDomain = append(expectedSignatureDomain, domainType[:]...) + expectedSignatureDomain = append(expectedSignatureDomain, expectedForkDataRoot[:28]...) + + assert.Equal(t, len(expectedSignatureDomain), len(resp.SignatureDomain)) + assert.DeepEqual(t, expectedSignatureDomain, resp.SignatureDomain) +} + +func TestGetDomainData_GenesisError(t *testing.T) { + const genesisValidatorRoot = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2" + epoch := params.BeaconConfig().AltairForkEpoch + domainType := params.BeaconConfig().DomainBeaconProposer + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + // Make sure that GetGenesis() is called exactly once + genesisProvider := mock.NewMockgenesisProvider(ctrl) + genesisProvider.EXPECT().GetGenesis().Return(nil, nil, errors.New("")).Times(1) + + validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} + _, err := validatorClient.getDomainData(epoch, domainType) + assert.ErrorContains(t, "failed to get genesis info", err) +} + +func TestGetDomainData_InvalidGenesisRoot(t *testing.T) { + const genesisValidatorRoot = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2" + epoch := params.BeaconConfig().AltairForkEpoch + domainType := params.BeaconConfig().DomainBeaconProposer + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + // Make sure that GetGenesis() is called exactly once + genesisProvider := mock.NewMockgenesisProvider(ctrl) + genesisProvider.EXPECT().GetGenesis().Return( + &rpcmiddleware.GenesisResponse_GenesisJson{GenesisValidatorsRoot: "foo"}, + nil, + nil, + ).Times(1) + + validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} + _, err := validatorClient.getDomainData(epoch, domainType) + assert.ErrorContains(t, "invalid genesis validators root: foo", err) +} diff --git a/validator/client/beacon-api/genesis.go b/validator/client/beacon-api/genesis.go index c48ec82bc..0b094da90 100644 --- a/validator/client/beacon-api/genesis.go +++ b/validator/client/beacon-api/genesis.go @@ -17,8 +17,17 @@ import ( ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" ) +type genesisProvider interface { + GetGenesis() (*rpcmiddleware.GenesisResponse_GenesisJson, *apimiddleware.DefaultErrorJson, error) +} + +type beaconApiGenesisProvider struct { + httpClient http.Client + url string +} + func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb.ChainStartResponse, error) { - genesis, httpError, err := c.getGenesis() + genesis, httpError, err := c.genesisProvider.GetGenesis() for err != nil { if httpError == nil || httpError.Code != http.StatusNotFound { @@ -28,26 +37,26 @@ func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb // Error 404 means that the chain genesis info is not yet known, so we query it every second until it's ready select { case <-time.After(time.Second): - genesis, httpError, err = c.getGenesis() + genesis, httpError, err = c.genesisProvider.GetGenesis() case <-ctx.Done(): return nil, errors.New("context canceled") } } - genesisTime, err := strconv.ParseUint(genesis.Data.GenesisTime, 10, 64) + genesisTime, err := strconv.ParseUint(genesis.GenesisTime, 10, 64) if err != nil { - return nil, errors.Wrapf(err, "failed to parse genesis time: %s", genesis.Data.GenesisTime) + return nil, errors.Wrapf(err, "failed to parse genesis time: %s", genesis.GenesisTime) } chainStartResponse := ðpb.ChainStartResponse{} chainStartResponse.Started = true chainStartResponse.GenesisTime = genesisTime - if !validRoot(genesis.Data.GenesisValidatorsRoot) { - return nil, errors.Errorf("invalid genesis validators root: %s", genesis.Data.GenesisValidatorsRoot) + if !validRoot(genesis.GenesisValidatorsRoot) { + return nil, errors.Errorf("invalid genesis validators root: %s", genesis.GenesisValidatorsRoot) } - genesisValidatorRoot, err := hexutil.Decode(genesis.Data.GenesisValidatorsRoot) + genesisValidatorRoot, err := hexutil.Decode(genesis.GenesisValidatorsRoot) if err != nil { return nil, errors.Wrap(err, "failed to decode genesis validators root") } @@ -56,7 +65,7 @@ func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb return chainStartResponse, nil } -func (c beaconApiValidatorClient) getGenesis() (*rpcmiddleware.GenesisResponseJson, *apimiddleware.DefaultErrorJson, error) { +func (c beaconApiGenesisProvider) GetGenesis() (*rpcmiddleware.GenesisResponse_GenesisJson, *apimiddleware.DefaultErrorJson, error) { resp, err := c.httpClient.Get(c.url + "/eth/v1/beacon/genesis") if err != nil { return nil, nil, errors.Wrap(err, "failed to query REST API genesis endpoint") @@ -82,8 +91,8 @@ func (c beaconApiValidatorClient) getGenesis() (*rpcmiddleware.GenesisResponseJs } if genesisJson.Data == nil { - return nil, nil, errors.New("GenesisResponseJson.Data is nil") + return nil, nil, errors.New("genesis data is nil") } - return genesisJson, nil, nil + return genesisJson.Data, nil, nil } diff --git a/validator/client/beacon-api/genesis_test.go b/validator/client/beacon-api/genesis_test.go index 690966b00..510ce2ed0 100644 --- a/validator/client/beacon-api/genesis_test.go +++ b/validator/client/beacon-api/genesis_test.go @@ -22,24 +22,23 @@ func TestGetGenesis_ValidGenesis(t *testing.T) { })) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - resp, httpError, err := validatorClient.getGenesis() + genesisProvider := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + resp, httpError, err := genesisProvider.GetGenesis() assert.NoError(t, err) assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError) require.NotNil(t, resp) - require.NotNil(t, resp.Data) - assert.Equal(t, "1234", resp.Data.GenesisTime) - assert.Equal(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", resp.Data.GenesisValidatorsRoot) + assert.Equal(t, "1234", resp.GenesisTime) + assert.Equal(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", resp.GenesisValidatorsRoot) } func TestGetGenesis_NilData(t *testing.T) { server := httptest.NewServer(createGenesisHandler(nil)) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - _, httpError, err := validatorClient.getGenesis() + genesisProvider := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + _, httpError, err := genesisProvider.GetGenesis() assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError) - assert.ErrorContains(t, "GenesisResponseJson.Data is nil", err) + assert.ErrorContains(t, "genesis data is nil", err) } func TestGetGenesis_InvalidJsonGenesis(t *testing.T) { @@ -49,8 +48,8 @@ func TestGetGenesis_InvalidJsonGenesis(t *testing.T) { })) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - _, httpError, err := validatorClient.getGenesis() + genesisProvider := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + _, httpError, err := genesisProvider.GetGenesis() assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError) assert.ErrorContains(t, "failed to decode response body genesis json", err) } @@ -59,8 +58,8 @@ func TestGetGenesis_InvalidJsonError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(invalidJsonErrHandler)) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - _, httpError, err := validatorClient.getGenesis() + genesisProvider := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + _, httpError, err := genesisProvider.GetGenesis() assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError) assert.ErrorContains(t, "failed to decode response body genesis error json", err) } @@ -69,8 +68,8 @@ func TestGetGenesis_404Error(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(notFoundErrHandler)) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - _, httpError, err := validatorClient.getGenesis() + validatorClient := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + _, httpError, err := validatorClient.GetGenesis() require.NotNil(t, httpError) assert.Equal(t, http.StatusNotFound, httpError.Code) assert.Equal(t, "Not found", httpError.Message) @@ -81,8 +80,8 @@ func TestGetGenesis_500Error(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(internalServerErrHandler)) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} - _, httpError, err := validatorClient.getGenesis() + validatorClient := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: time.Second * 5}} + _, httpError, err := validatorClient.GetGenesis() require.NotNil(t, httpError) assert.Equal(t, http.StatusInternalServerError, httpError.Code) assert.Equal(t, "Internal server error", httpError.Message) @@ -96,8 +95,8 @@ func TestGetGenesis_Timeout(t *testing.T) { })) defer server.Close() - validatorClient := &beaconApiValidatorClient{url: server.URL, httpClient: http.Client{Timeout: 1}} - _, httpError, err := validatorClient.getGenesis() + genesisProvider := &beaconApiGenesisProvider{url: server.URL, httpClient: http.Client{Timeout: 1}} + _, httpError, err := genesisProvider.GetGenesis() assert.Equal(t, (*apimiddleware.DefaultErrorJson)(nil), httpError) assert.ErrorContains(t, "failed to query REST API genesis endpoint", err) } diff --git a/validator/client/beacon-api/handlers_test.go b/validator/client/beacon-api/handlers_test.go index 6f9ad8bd0..1327e001d 100644 --- a/validator/client/beacon-api/handlers_test.go +++ b/validator/client/beacon-api/handlers_test.go @@ -73,6 +73,13 @@ func invalidJsonErrHandler(w http.ResponseWriter, r *http.Request) { } } +func invalidJsonResultHandler(w http.ResponseWriter, r *http.Request) { + _, err := w.Write([]byte("foo")) + if err != nil { + panic(err) + } +} + func createGenesisHandler(data *rpcmiddleware.GenesisResponse_GenesisJson) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { genesisResponseJson := &rpcmiddleware.GenesisResponseJson{Data: data} diff --git a/validator/client/beacon-api/mock/BUILD.bazel b/validator/client/beacon-api/mock/BUILD.bazel new file mode 100644 index 000000000..c7e191497 --- /dev/null +++ b/validator/client/beacon-api/mock/BUILD.bazel @@ -0,0 +1,14 @@ +load("@prysm//tools/go:def.bzl", "go_library") + +# gazelle:build_tags use_beacon_api +go_library( + name = "go_default_library", + srcs = ["genesis_mock.go"], + importpath = "github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock", + visibility = ["//validator:__subpackages__"], + deps = [ + "//api/gateway/apimiddleware:go_default_library", + "//beacon-chain/rpc/apimiddleware:go_default_library", + "@com_github_golang_mock//gomock:go_default_library", + ], +) diff --git a/validator/client/beacon-api/mock/genesis_mock.go b/validator/client/beacon-api/mock/genesis_mock.go new file mode 100644 index 000000000..a6e281217 --- /dev/null +++ b/validator/client/beacon-api/mock/genesis_mock.go @@ -0,0 +1,52 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: validator/client/beacon-api/genesis.go + +// Package mock is a generated GoMock package. +package mock + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + apimiddleware "github.com/prysmaticlabs/prysm/v3/api/gateway/apimiddleware" + apimiddleware0 "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware" +) + +// MockgenesisProvider is a mock of genesisProvider interface. +type MockgenesisProvider struct { + ctrl *gomock.Controller + recorder *MockgenesisProviderMockRecorder +} + +// MockgenesisProviderMockRecorder is the mock recorder for MockgenesisProvider. +type MockgenesisProviderMockRecorder struct { + mock *MockgenesisProvider +} + +// NewMockgenesisProvider creates a new mock instance. +func NewMockgenesisProvider(ctrl *gomock.Controller) *MockgenesisProvider { + mock := &MockgenesisProvider{ctrl: ctrl} + mock.recorder = &MockgenesisProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockgenesisProvider) EXPECT() *MockgenesisProviderMockRecorder { + return m.recorder +} + +// GetGenesis mocks base method. +func (m *MockgenesisProvider) GetGenesis() (*apimiddleware0.GenesisResponse_GenesisJson, *apimiddleware.DefaultErrorJson, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetGenesis") + ret0, _ := ret[0].(*apimiddleware0.GenesisResponse_GenesisJson) + ret1, _ := ret[1].(*apimiddleware.DefaultErrorJson) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetGenesis indicates an expected call of GetGenesis. +func (mr *MockgenesisProviderMockRecorder) GetGenesis() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGenesis", reflect.TypeOf((*MockgenesisProvider)(nil).GetGenesis)) +}