diff --git a/api/client/beacon/BUILD.bazel b/api/client/beacon/BUILD.bazel index e160b5eac2..4508ad03a4 100644 --- a/api/client/beacon/BUILD.bazel +++ b/api/client/beacon/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "//api/client:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/rpc/apimiddleware:go_default_library", + "//beacon-chain/rpc/eth/shared:go_default_library", "//beacon-chain/state:go_default_library", "//consensus-types/interfaces:go_default_library", "//consensus-types/primitives:go_default_library", diff --git a/api/client/beacon/client.go b/api/client/beacon/client.go index 02cbf635e4..a1bec5516d 100644 --- a/api/client/beacon/client.go +++ b/api/client/beacon/client.go @@ -14,6 +14,7 @@ import ( "text/template" "github.com/prysmaticlabs/prysm/v4/api/client" + "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" "github.com/prysmaticlabs/prysm/v4/network/forks" v1 "github.com/prysmaticlabs/prysm/v4/proto/eth/v1" @@ -148,14 +149,14 @@ func (c *Client) GetFork(ctx context.Context, stateId StateOrBlockId) (*ethpb.Fo if err != nil { return nil, errors.Wrapf(err, "error requesting fork by state id = %s", stateId) } - fr := &forkResponse{} - dataWrapper := &struct{ Data *forkResponse }{Data: fr} + fr := &shared.Fork{} + dataWrapper := &struct{ Data *shared.Fork }{Data: fr} err = json.Unmarshal(body, dataWrapper) if err != nil { return nil, errors.Wrap(err, "error decoding json response in GetFork") } - return fr.Fork() + return fr.ToConsensus() } // GetForkSchedule retrieve all forks, past present and future, of which this node is aware. @@ -335,40 +336,8 @@ func (c *Client) GetBLStoExecutionChanges(ctx context.Context) (*apimiddleware.B return poolResponse, nil } -type forkResponse struct { - PreviousVersion string `json:"previous_version"` - CurrentVersion string `json:"current_version"` - Epoch string `json:"epoch"` -} - -func (f *forkResponse) Fork() (*ethpb.Fork, error) { - epoch, err := strconv.ParseUint(f.Epoch, 10, 64) - if err != nil { - return nil, err - } - cSlice, err := hexutil.Decode(f.CurrentVersion) - if err != nil { - return nil, err - } - if len(cSlice) != 4 { - return nil, fmt.Errorf("got %d byte version for CurrentVersion, expected 4 bytes. hex=%s", len(cSlice), f.CurrentVersion) - } - pSlice, err := hexutil.Decode(f.PreviousVersion) - if err != nil { - return nil, err - } - if len(pSlice) != 4 { - return nil, fmt.Errorf("got %d byte version, expected 4 bytes. version hex=%s", len(pSlice), f.PreviousVersion) - } - return ðpb.Fork{ - CurrentVersion: cSlice, - PreviousVersion: pSlice, - Epoch: primitives.Epoch(epoch), - }, nil -} - type forkScheduleResponse struct { - Data []forkResponse + Data []shared.Fork } func (fsr *forkScheduleResponse) OrderedForkSchedule() (forks.OrderedSchedule, error) { diff --git a/api/client/builder/BUILD.bazel b/api/client/builder/BUILD.bazel index cb27f6c6a3..a86e3c7d2c 100644 --- a/api/client/builder/BUILD.bazel +++ b/api/client/builder/BUILD.bazel @@ -54,7 +54,6 @@ go_test( "//testing/assert:go_default_library", "//testing/require:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", - "@com_github_golang_protobuf//proto:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", ], diff --git a/api/client/builder/client.go b/api/client/builder/client.go index ea09123216..9f8398d87c 100644 --- a/api/client/builder/client.go +++ b/api/client/builder/client.go @@ -17,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/v4/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v4/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v4/monitoring/tracing" "github.com/prysmaticlabs/prysm/v4/network" "github.com/prysmaticlabs/prysm/v4/network/authorization" @@ -269,9 +270,13 @@ func (c *Client) RegisterValidator(ctx context.Context, svr []*ethpb.SignedValid tracing.AnnotateError(span, err) return err } - vs := make([]*SignedValidatorRegistration, len(svr)) + vs := make([]*shared.SignedValidatorRegistration, len(svr)) for i := 0; i < len(svr); i++ { - vs[i] = &SignedValidatorRegistration{SignedValidatorRegistrationV1: svr[i]} + svrJson, err := shared.SignedValidatorRegistrationFromConsensus(svr[i]) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("failed to encode to SignedValidatorRegistration at index %d", i)) + } + vs[i] = svrJson } body, err := json.Marshal(vs) if err != nil { @@ -296,12 +301,14 @@ func (c *Client) SubmitBlindedBlock(ctx context.Context, sb interfaces.ReadOnlyS if err != nil { return nil, nil, errors.Wrapf(err, "could not get protobuf block") } - b := &SignedBlindedBeaconBlockBellatrix{SignedBlindedBeaconBlockBellatrix: psb} + b, err := shared.SignedBlindedBeaconBlockBellatrixFromConsensus(ðpb.SignedBlindedBeaconBlockBellatrix{Block: psb.Block, Signature: bytesutil.SafeCopyBytes(psb.Signature)}) + if err != nil { + return nil, nil, errors.Wrapf(err, "could not convert SignedBlindedBeaconBlockBellatrix to json marshalable type") + } body, err := json.Marshal(b) if err != nil { return nil, nil, errors.Wrap(err, "error encoding the SignedBlindedBeaconBlockBellatrix value body in SubmitBlindedBlock") } - versionOpt := func(r *http.Request) { r.Header.Add("Eth-Consensus-Version", version.String(version.Bellatrix)) } @@ -331,12 +338,14 @@ func (c *Client) SubmitBlindedBlock(ctx context.Context, sb interfaces.ReadOnlyS if err != nil { return nil, nil, errors.Wrapf(err, "could not get protobuf block") } - b := &SignedBlindedBeaconBlockCapella{SignedBlindedBeaconBlockCapella: psb} + b, err := shared.SignedBlindedBeaconBlockCapellaFromConsensus(ðpb.SignedBlindedBeaconBlockCapella{Block: psb.Block, Signature: bytesutil.SafeCopyBytes(psb.Signature)}) + if err != nil { + return nil, nil, errors.Wrapf(err, "could not convert SignedBlindedBeaconBlockCapella to json marshalable type") + } body, err := json.Marshal(b) if err != nil { return nil, nil, errors.Wrap(err, "error encoding the SignedBlindedBeaconBlockCapella value body in SubmitBlindedBlockCapella") } - versionOpt := func(r *http.Request) { r.Header.Add("Eth-Consensus-Version", version.String(version.Capella)) } diff --git a/api/client/builder/testdata/blinded-block-capella.json b/api/client/builder/testdata/blinded-block-capella.json index ff89dc3e8a..7306f20865 100644 --- a/api/client/builder/testdata/blinded-block-capella.json +++ b/api/client/builder/testdata/blinded-block-capella.json @@ -1 +1 @@ -{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body":{"randao_reveal":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505","eth1_data":{"deposit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","deposit_count":"23","block_hash":"0x0000000000000000000000000000000000000000000000000000000000000000"},"graffiti":"0xdeadbeefc0ffee","proposer_slashings":[{"signed_header_1":{"message":{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"},"signed_header_2":{"message":{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"attester_slashings":[{"attestation_1":{"attesting_indices":["1"],"data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"},"attestation_2":{"attesting_indices":["1"],"data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"attestations":[{"aggregation_bits":"0x01","data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}],"deposits":[{"proof":["0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"],"data":{"pubkey":"0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a","withdrawal_credentials":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","amount":"1","signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"voluntary_exits":[{"message":{"epoch":"1","validator_index":"1"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}],"bls_to_execution_changes":[],"sync_aggregate":{"sync_committee_bits":"0x01","sync_committee_signature":"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},"execution_payload_header":{"parent_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","fee_recipient":"0xabcf8e0d4e9587369b2301d0790347320302cc09","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","receipts_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","logs_bloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prev_randao":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","block_number":"1","gas_limit":"1","gas_used":"1","timestamp":"1","extra_data":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","base_fee_per_gas":"452312848583266388373324160190187140051835877600158453279131187530910662656","block_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","transactions_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","withdrawals_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}}} +{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body":{"randao_reveal":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505","eth1_data":{"deposit_root":"0x0000000000000000000000000000000000000000000000000000000000000000","deposit_count":"23","block_hash":"0x0000000000000000000000000000000000000000000000000000000000000000"},"graffiti":"0xdeadbeefc0ffee","proposer_slashings":[{"signed_header_1":{"message":{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"},"signed_header_2":{"message":{"slot":"1","proposer_index":"1","parent_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","body_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"attester_slashings":[{"attestation_1":{"attesting_indices":["1"],"data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"},"attestation_2":{"attesting_indices":["1"],"data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"attestations":[{"aggregation_bits":"0x01","data":{"slot":"1","index":"1","beacon_block_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","source":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"target":{"epoch":"1","root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"}},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}],"deposits":[{"proof":["0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"],"data":{"pubkey":"0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a","withdrawal_credentials":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","amount":"1","signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}}],"voluntary_exits":[{"message":{"epoch":"1","validator_index":"1"},"signature":"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505"}],"sync_aggregate":{"sync_committee_bits":"0x01","sync_committee_signature":"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},"execution_payload_header":{"parent_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","fee_recipient":"0xabcf8e0d4e9587369b2301d0790347320302cc09","state_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","receipts_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","logs_bloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","prev_randao":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","block_number":"1","gas_limit":"1","gas_used":"1","timestamp":"1","extra_data":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","base_fee_per_gas":"452312848583266388373324160190187140051835877600158453279131187530910662656","block_hash":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","transactions_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2","withdrawals_root":"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"},"bls_to_execution_changes":[]}} diff --git a/api/client/builder/types.go b/api/client/builder/types.go index f9ac52b3c6..a3c580332a 100644 --- a/api/client/builder/types.go +++ b/api/client/builder/types.go @@ -16,87 +16,6 @@ import ( eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" ) -// SignedValidatorRegistration a struct for signed validator registrations. -type SignedValidatorRegistration struct { - *eth.SignedValidatorRegistrationV1 -} - -// ValidatorRegistration a struct for validator registrations. -type ValidatorRegistration struct { - *eth.ValidatorRegistrationV1 -} - -// MarshalJSON returns a json representation copy of signed validator registration. -func (r *SignedValidatorRegistration) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Message *ValidatorRegistration `json:"message"` - Signature hexutil.Bytes `json:"signature"` - }{ - Message: &ValidatorRegistration{r.Message}, - Signature: r.SignedValidatorRegistrationV1.Signature, - }) -} - -// UnmarshalJSON returns a byte representation of signed validator registration from json. -func (r *SignedValidatorRegistration) UnmarshalJSON(b []byte) error { - if r.SignedValidatorRegistrationV1 == nil { - r.SignedValidatorRegistrationV1 = ð.SignedValidatorRegistrationV1{} - } - o := struct { - Message *ValidatorRegistration `json:"message"` - Signature hexutil.Bytes `json:"signature"` - }{} - if err := json.Unmarshal(b, &o); err != nil { - return err - } - r.Message = o.Message.ValidatorRegistrationV1 - r.Signature = o.Signature - return nil -} - -// MarshalJSON returns a json representation copy of validator registration. -func (r *ValidatorRegistration) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - FeeRecipient hexutil.Bytes `json:"fee_recipient"` - GasLimit string `json:"gas_limit"` - Timestamp string `json:"timestamp"` - Pubkey hexutil.Bytes `json:"pubkey"` - }{ - FeeRecipient: r.FeeRecipient, - GasLimit: fmt.Sprintf("%d", r.GasLimit), - Timestamp: fmt.Sprintf("%d", r.Timestamp), - Pubkey: r.Pubkey, - }) -} - -// UnmarshalJSON returns a byte representation of validator registration from json. -func (r *ValidatorRegistration) UnmarshalJSON(b []byte) error { - if r.ValidatorRegistrationV1 == nil { - r.ValidatorRegistrationV1 = ð.ValidatorRegistrationV1{} - } - o := struct { - FeeRecipient hexutil.Bytes `json:"fee_recipient"` - GasLimit string `json:"gas_limit"` - Timestamp string `json:"timestamp"` - Pubkey hexutil.Bytes `json:"pubkey"` - }{} - if err := json.Unmarshal(b, &o); err != nil { - return err - } - - r.FeeRecipient = o.FeeRecipient - r.Pubkey = o.Pubkey - var err error - if r.GasLimit, err = strconv.ParseUint(o.GasLimit, 10, 64); err != nil { - return errors.Wrap(err, "failed to parse gas limit") - } - if r.Timestamp, err = strconv.ParseUint(o.Timestamp, 10, 64); err != nil { - return errors.Wrap(err, "failed to parse timestamp") - } - - return nil -} - var errInvalidUint256 = errors.New("invalid Uint256") var errDecodeUint256 = errors.New("unable to decode into Uint256") @@ -636,44 +555,6 @@ type SignedBlindedBeaconBlockBellatrix struct { *eth.SignedBlindedBeaconBlockBellatrix } -// BlindedBeaconBlockBellatrix is a field in SignedBlindedBeaconBlockBellatrix. -type BlindedBeaconBlockBellatrix struct { - *eth.BlindedBeaconBlockBellatrix -} - -// BlindedBeaconBlockBodyBellatrix is a field in BlindedBeaconBlockBellatrix. -type BlindedBeaconBlockBodyBellatrix struct { - *eth.BlindedBeaconBlockBodyBellatrix -} - -// MarshalJSON returns a JSON byte array representation of SignedBlindedBeaconBlockBellatrix. -func (r *SignedBlindedBeaconBlockBellatrix) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Message *BlindedBeaconBlockBellatrix `json:"message"` - Signature hexutil.Bytes `json:"signature"` - }{ - Message: &BlindedBeaconBlockBellatrix{r.SignedBlindedBeaconBlockBellatrix.Block}, - Signature: r.SignedBlindedBeaconBlockBellatrix.Signature, - }) -} - -// MarshalJSON returns a JSON byte array representation of BlindedBeaconBlockBellatrix. -func (b *BlindedBeaconBlockBellatrix) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Slot string `json:"slot"` - ProposerIndex string `json:"proposer_index"` - ParentRoot hexutil.Bytes `json:"parent_root"` - StateRoot hexutil.Bytes `json:"state_root"` - Body *BlindedBeaconBlockBodyBellatrix `json:"body"` - }{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: b.ParentRoot, - StateRoot: b.StateRoot, - Body: &BlindedBeaconBlockBodyBellatrix{b.BlindedBeaconBlockBellatrix.Body}, - }) -} - // ProposerSlashing is a field in BlindedBeaconBlockBodyCapella. type ProposerSlashing struct { *eth.ProposerSlashing @@ -928,53 +809,6 @@ func (e *Eth1Data) MarshalJSON() ([]byte, error) { }) } -// MarshalJSON returns a JSON byte array representation of BlindedBeaconBlockBodyBellatrix. -func (b *BlindedBeaconBlockBodyBellatrix) MarshalJSON() ([]byte, error) { - sve := make([]*SignedVoluntaryExit, len(b.BlindedBeaconBlockBodyBellatrix.VoluntaryExits)) - for i := range b.BlindedBeaconBlockBodyBellatrix.VoluntaryExits { - sve[i] = &SignedVoluntaryExit{SignedVoluntaryExit: b.BlindedBeaconBlockBodyBellatrix.VoluntaryExits[i]} - } - deps := make([]*Deposit, len(b.BlindedBeaconBlockBodyBellatrix.Deposits)) - for i := range b.BlindedBeaconBlockBodyBellatrix.Deposits { - deps[i] = &Deposit{Deposit: b.BlindedBeaconBlockBodyBellatrix.Deposits[i]} - } - atts := make([]*Attestation, len(b.BlindedBeaconBlockBodyBellatrix.Attestations)) - for i := range b.BlindedBeaconBlockBodyBellatrix.Attestations { - atts[i] = &Attestation{Attestation: b.BlindedBeaconBlockBodyBellatrix.Attestations[i]} - } - atsl := make([]*AttesterSlashing, len(b.BlindedBeaconBlockBodyBellatrix.AttesterSlashings)) - for i := range b.BlindedBeaconBlockBodyBellatrix.AttesterSlashings { - atsl[i] = &AttesterSlashing{AttesterSlashing: b.BlindedBeaconBlockBodyBellatrix.AttesterSlashings[i]} - } - pros := make([]*ProposerSlashing, len(b.BlindedBeaconBlockBodyBellatrix.ProposerSlashings)) - for i := range b.BlindedBeaconBlockBodyBellatrix.ProposerSlashings { - pros[i] = &ProposerSlashing{ProposerSlashing: b.BlindedBeaconBlockBodyBellatrix.ProposerSlashings[i]} - } - return json.Marshal(struct { - RandaoReveal hexutil.Bytes `json:"randao_reveal"` - Eth1Data *Eth1Data `json:"eth1_data"` - Graffiti hexutil.Bytes `json:"graffiti"` - ProposerSlashings []*ProposerSlashing `json:"proposer_slashings"` - AttesterSlashings []*AttesterSlashing `json:"attester_slashings"` - Attestations []*Attestation `json:"attestations"` - Deposits []*Deposit `json:"deposits"` - VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` - SyncAggregate *SyncAggregate `json:"sync_aggregate"` - ExecutionPayloadHeader *ExecutionPayloadHeader `json:"execution_payload_header"` - }{ - RandaoReveal: b.RandaoReveal, - Eth1Data: &Eth1Data{b.BlindedBeaconBlockBodyBellatrix.Eth1Data}, - Graffiti: b.BlindedBeaconBlockBodyBellatrix.Graffiti, - ProposerSlashings: pros, - AttesterSlashings: atsl, - Attestations: atts, - Deposits: deps, - VoluntaryExits: sve, - SyncAggregate: &SyncAggregate{b.BlindedBeaconBlockBodyBellatrix.SyncAggregate}, - ExecutionPayloadHeader: &ExecutionPayloadHeader{ExecutionPayloadHeader: b.BlindedBeaconBlockBodyBellatrix.ExecutionPayloadHeader}, - }) -} - // SignedBLSToExecutionChange is a field in Beacon Block Body for capella and above. type SignedBLSToExecutionChange struct { *eth.SignedBLSToExecutionChange @@ -1009,102 +843,6 @@ func (ch *BLSToExecutionChange) MarshalJSON() ([]byte, error) { }) } -// SignedBlindedBeaconBlockCapella is part of the request object sent to builder API /eth/v1/builder/blinded_blocks for Capella. -type SignedBlindedBeaconBlockCapella struct { - *eth.SignedBlindedBeaconBlockCapella -} - -// BlindedBeaconBlockCapella is a field in SignedBlindedBeaconBlockCapella. -type BlindedBeaconBlockCapella struct { - *eth.BlindedBeaconBlockCapella -} - -// BlindedBeaconBlockBodyCapella is a field in BlindedBeaconBlockCapella. -type BlindedBeaconBlockBodyCapella struct { - *eth.BlindedBeaconBlockBodyCapella -} - -// MarshalJSON returns a JSON byte array representation of SignedBlindedBeaconBlockCapella. -func (b *SignedBlindedBeaconBlockCapella) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Message *BlindedBeaconBlockCapella `json:"message"` - Signature hexutil.Bytes `json:"signature"` - }{ - Message: &BlindedBeaconBlockCapella{b.Block}, - Signature: b.Signature, - }) -} - -// MarshalJSON returns a JSON byte array representation of BlindedBeaconBlockCapella -func (b *BlindedBeaconBlockCapella) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Slot string `json:"slot"` - ProposerIndex string `json:"proposer_index"` - ParentRoot hexutil.Bytes `json:"parent_root"` - StateRoot hexutil.Bytes `json:"state_root"` - Body *BlindedBeaconBlockBodyCapella `json:"body"` - }{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: b.ParentRoot, - StateRoot: b.StateRoot, - Body: &BlindedBeaconBlockBodyCapella{b.Body}, - }) -} - -// MarshalJSON returns a JSON byte array representation of BlindedBeaconBlockBodyCapella -func (b *BlindedBeaconBlockBodyCapella) MarshalJSON() ([]byte, error) { - sve := make([]*SignedVoluntaryExit, len(b.VoluntaryExits)) - for i := range b.VoluntaryExits { - sve[i] = &SignedVoluntaryExit{SignedVoluntaryExit: b.VoluntaryExits[i]} - } - deps := make([]*Deposit, len(b.Deposits)) - for i := range b.Deposits { - deps[i] = &Deposit{Deposit: b.Deposits[i]} - } - atts := make([]*Attestation, len(b.Attestations)) - for i := range b.Attestations { - atts[i] = &Attestation{Attestation: b.Attestations[i]} - } - atsl := make([]*AttesterSlashing, len(b.AttesterSlashings)) - for i := range b.AttesterSlashings { - atsl[i] = &AttesterSlashing{AttesterSlashing: b.AttesterSlashings[i]} - } - pros := make([]*ProposerSlashing, len(b.ProposerSlashings)) - for i := range b.ProposerSlashings { - pros[i] = &ProposerSlashing{ProposerSlashing: b.ProposerSlashings[i]} - } - chs := make([]*SignedBLSToExecutionChange, len(b.BlsToExecutionChanges)) - for i := range b.BlsToExecutionChanges { - chs[i] = &SignedBLSToExecutionChange{SignedBLSToExecutionChange: b.BlsToExecutionChanges[i]} - } - return json.Marshal(struct { - RandaoReveal hexutil.Bytes `json:"randao_reveal"` - Eth1Data *Eth1Data `json:"eth1_data"` - Graffiti hexutil.Bytes `json:"graffiti"` - ProposerSlashings []*ProposerSlashing `json:"proposer_slashings"` - AttesterSlashings []*AttesterSlashing `json:"attester_slashings"` - Attestations []*Attestation `json:"attestations"` - Deposits []*Deposit `json:"deposits"` - VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` - BLSToExecutionChanges []*SignedBLSToExecutionChange `json:"bls_to_execution_changes"` - SyncAggregate *SyncAggregate `json:"sync_aggregate"` - ExecutionPayloadHeader *ExecutionPayloadHeaderCapella `json:"execution_payload_header"` - }{ - RandaoReveal: b.RandaoReveal, - Eth1Data: &Eth1Data{b.Eth1Data}, - Graffiti: b.Graffiti, - ProposerSlashings: pros, - AttesterSlashings: atsl, - Attestations: atts, - Deposits: deps, - VoluntaryExits: sve, - BLSToExecutionChanges: chs, - SyncAggregate: &SyncAggregate{b.SyncAggregate}, - ExecutionPayloadHeader: &ExecutionPayloadHeaderCapella{ExecutionPayloadHeaderCapella: b.ExecutionPayloadHeader}, - }) -} - // ExecHeaderResponseDeneb is the header response for builder API /eth/v1/builder/header/{slot}/{parent_hash}/{pubkey}. type ExecHeaderResponseDeneb struct { Data struct { diff --git a/api/client/builder/types_test.go b/api/client/builder/types_test.go index 5ac9dbeb66..12468de9f5 100644 --- a/api/client/builder/types_test.go +++ b/api/client/builder/types_test.go @@ -12,8 +12,8 @@ import ( "testing" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/golang/protobuf/proto" "github.com/prysmaticlabs/go-bitfield" + "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams" "github.com/prysmaticlabs/prysm/v4/math" v1 "github.com/prysmaticlabs/prysm/v4/proto/engine/v1" @@ -38,7 +38,8 @@ func TestSignedValidatorRegistration_MarshalJSON(t *testing.T) { }, Signature: make([]byte, 96), } - a := &SignedValidatorRegistration{SignedValidatorRegistrationV1: svr} + a, err := shared.SignedValidatorRegistrationFromConsensus(svr) + require.NoError(t, err) je, err := json.Marshal(a) require.NoError(t, err) // decode with a struct w/ plain strings so we can check the string encoding of the hex fields @@ -55,11 +56,11 @@ func TestSignedValidatorRegistration_MarshalJSON(t *testing.T) { require.Equal(t, "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", un.Message.Pubkey) t.Run("roundtrip", func(t *testing.T) { - b := &SignedValidatorRegistration{} + b := &shared.SignedValidatorRegistration{} if err := json.Unmarshal(je, b); err != nil { require.NoError(t, err) } - require.Equal(t, proto.Equal(a.SignedValidatorRegistrationV1, b.SignedValidatorRegistrationV1), true) + require.DeepEqual(t, a, b) }) } @@ -1529,7 +1530,7 @@ func TestUint256UnmarshalTooBig(t *testing.T) { func TestMarshalBlindedBeaconBlockBodyBellatrix(t *testing.T) { expected, err := os.ReadFile("testdata/blinded-block.json") require.NoError(t, err) - b := &BlindedBeaconBlockBellatrix{BlindedBeaconBlockBellatrix: ð.BlindedBeaconBlockBellatrix{ + b, err := shared.BlindedBeaconBlockBellatrixFromConsensus(ð.BlindedBeaconBlockBellatrix{ Slot: 1, ProposerIndex: 1, ParentRoot: ezDecode(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"), @@ -1546,7 +1547,8 @@ func TestMarshalBlindedBeaconBlockBodyBellatrix(t *testing.T) { SyncAggregate: pbSyncAggregate(), ExecutionPayloadHeader: pbExecutionPayloadHeader(t), }, - }} + }) + require.NoError(t, err) m, err := json.Marshal(b) require.NoError(t, err) // string error output is easier to deal with @@ -1558,7 +1560,7 @@ func TestMarshalBlindedBeaconBlockBodyBellatrix(t *testing.T) { func TestMarshalBlindedBeaconBlockBodyCapella(t *testing.T) { expected, err := os.ReadFile("testdata/blinded-block-capella.json") require.NoError(t, err) - b := &BlindedBeaconBlockCapella{BlindedBeaconBlockCapella: ð.BlindedBeaconBlockCapella{ + b, err := shared.BlindedBeaconBlockCapellaFromConsensus(ð.BlindedBeaconBlockCapella{ Slot: 1, ProposerIndex: 1, ParentRoot: ezDecode(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2"), @@ -1575,7 +1577,8 @@ func TestMarshalBlindedBeaconBlockBodyCapella(t *testing.T) { SyncAggregate: pbSyncAggregate(), ExecutionPayloadHeader: pbExecutionPayloadHeaderCapella(t), }, - }} + }) + require.NoError(t, err) m, err := json.Marshal(b) require.NoError(t, err) // string error output is easier to deal with diff --git a/beacon-chain/rpc/eth/shared/structs.go b/beacon-chain/rpc/eth/shared/structs.go index 0179c8a927..d26aa2e459 100644 --- a/beacon-chain/rpc/eth/shared/structs.go +++ b/beacon-chain/rpc/eth/shared/structs.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/pkg/errors" fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v4/consensus-types/validator" @@ -189,6 +190,32 @@ func (s *ValidatorRegistration) ToConsensus() (*eth.ValidatorRegistrationV1, err }, nil } +func ValidatorRegistrationFromConsensus(vr *eth.ValidatorRegistrationV1) (*ValidatorRegistration, error) { + if vr == nil { + return nil, errors.New("ValidatorRegistrationV1 is empty") + } + return &ValidatorRegistration{ + FeeRecipient: hexutil.Encode(vr.FeeRecipient), + GasLimit: strconv.FormatUint(vr.GasLimit, 10), + Timestamp: strconv.FormatUint(vr.Timestamp, 10), + Pubkey: hexutil.Encode(vr.Pubkey), + }, nil +} + +func SignedValidatorRegistrationFromConsensus(vr *eth.SignedValidatorRegistrationV1) (*SignedValidatorRegistration, error) { + if vr == nil { + return nil, errors.New("SignedValidatorRegistrationV1 is empty") + } + v, err := ValidatorRegistrationFromConsensus(vr.Message) + if err != nil { + return nil, err + } + return &SignedValidatorRegistration{ + Message: v, + Signature: hexutil.Encode(vr.Signature), + }, nil +} + func (s *SignedContributionAndProof) ToConsensus() (*eth.SignedContributionAndProof, error) { msg, err := s.Message.ToConsensus() if err != nil { diff --git a/beacon-chain/rpc/eth/shared/structs_blocks_conversions.go b/beacon-chain/rpc/eth/shared/structs_blocks_conversions.go index 1ac90aa995..7afedf48fa 100644 --- a/beacon-chain/rpc/eth/shared/structs_blocks_conversions.go +++ b/beacon-chain/rpc/eth/shared/structs_blocks_conversions.go @@ -2162,6 +2162,17 @@ func BlindedBeaconBlockBellatrixFromConsensus(b *eth.BlindedBeaconBlockBellatrix }, nil } +func SignedBlindedBeaconBlockBellatrixFromConsensus(b *eth.SignedBlindedBeaconBlockBellatrix) (*SignedBlindedBeaconBlockBellatrix, error) { + blindedBlock, err := BlindedBeaconBlockBellatrixFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBlindedBeaconBlockBellatrix{ + Message: blindedBlock, + Signature: hexutil.Encode(b.Signature), + }, nil +} + func BeaconBlockBellatrixFromConsensus(b *eth.BeaconBlockBellatrix) (*BeaconBlockBellatrix, error) { proposerSlashings, err := ProposerSlashingsFromConsensus(b.Body.ProposerSlashings) if err != nil { @@ -2307,6 +2318,17 @@ func BlindedBeaconBlockCapellaFromConsensus(b *eth.BlindedBeaconBlockCapella) (* }, nil } +func SignedBlindedBeaconBlockCapellaFromConsensus(b *eth.SignedBlindedBeaconBlockCapella) (*SignedBlindedBeaconBlockCapella, error) { + blindedBlock, err := BlindedBeaconBlockCapellaFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBlindedBeaconBlockCapella{ + Message: blindedBlock, + Signature: hexutil.Encode(b.Signature), + }, nil +} + func BeaconBlockCapellaFromConsensus(b *eth.BeaconBlockCapella) (*BeaconBlockCapella, error) { proposerSlashings, err := ProposerSlashingsFromConsensus(b.Body.ProposerSlashings) if err != nil { diff --git a/testing/middleware/builder/BUILD.bazel b/testing/middleware/builder/BUILD.bazel index 1a5fb51312..5a584d9ac0 100644 --- a/testing/middleware/builder/BUILD.bazel +++ b/testing/middleware/builder/BUILD.bazel @@ -11,6 +11,7 @@ go_library( deps = [ "//api/client/builder:go_default_library", "//beacon-chain/core/signing:go_default_library", + "//beacon-chain/rpc/eth/shared:go_default_library", "//config/params:go_default_library", "//consensus-types/blocks:go_default_library", "//consensus-types/interfaces:go_default_library", diff --git a/testing/middleware/builder/builder.go b/testing/middleware/builder/builder.go index b4c2caaebe..e9cc943986 100644 --- a/testing/middleware/builder/builder.go +++ b/testing/middleware/builder/builder.go @@ -24,6 +24,7 @@ import ( gMux "github.com/gorilla/mux" builderAPI "github.com/prysmaticlabs/prysm/v4/api/client/builder" "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/signing" + "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" "github.com/prysmaticlabs/prysm/v4/config/params" "github.com/prysmaticlabs/prysm/v4/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces" @@ -243,13 +244,17 @@ func (p *Builder) isBuilderCall(req *http.Request) bool { } func (p *Builder) registerValidators(w http.ResponseWriter, req *http.Request) { - registrations := []builderAPI.SignedValidatorRegistration{} + registrations := []shared.SignedValidatorRegistration{} if err := json.NewDecoder(req.Body).Decode(®istrations); err != nil { http.Error(w, "invalid request", http.StatusBadRequest) return } for _, r := range registrations { - msg := r.Message + msg, err := r.Message.ToConsensus() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } p.validatorMap[string(r.Message.Pubkey)] = msg } // TODO: Verify Signatures from validators diff --git a/validator/client/beacon-api/registration.go b/validator/client/beacon-api/registration.go index 09f596638e..c7b2767fab 100644 --- a/validator/client/beacon-api/registration.go +++ b/validator/client/beacon-api/registration.go @@ -4,9 +4,8 @@ import ( "bytes" "context" "encoding/json" - "strconv" + "fmt" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" @@ -18,18 +17,11 @@ func (c *beaconApiValidatorClient) submitValidatorRegistrations(ctx context.Cont jsonRegistration := make([]*shared.SignedValidatorRegistration, len(registrations)) for index, registration := range registrations { - inMessage := registration.Message - outMessage := &shared.ValidatorRegistration{ - FeeRecipient: hexutil.Encode(inMessage.FeeRecipient), - GasLimit: strconv.FormatUint(inMessage.GasLimit, 10), - Timestamp: strconv.FormatUint(inMessage.Timestamp, 10), - Pubkey: hexutil.Encode(inMessage.Pubkey), - } - - jsonRegistration[index] = &shared.SignedValidatorRegistration{ - Message: outMessage, - Signature: hexutil.Encode(registration.Signature), + outMessage, err := shared.SignedValidatorRegistrationFromConsensus(registration) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("failed to encode to SignedValidatorRegistration at index %d", index)) } + jsonRegistration[index] = outMessage } marshalledJsonRegistration, err := json.Marshal(jsonRegistration)