generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom json marshalling for versioned structs #493
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,11 @@ import ( | |
|
||
"github.com/attestantio/go-builder-client/api" | ||
"github.com/attestantio/go-builder-client/api/capella" | ||
"github.com/attestantio/go-builder-client/api/deneb" | ||
"github.com/attestantio/go-builder-client/spec" | ||
consensusapi "github.com/attestantio/go-eth2-client/api" | ||
apiv1capella "github.com/attestantio/go-eth2-client/api/v1/capella" | ||
apiv1deneb "github.com/attestantio/go-eth2-client/api/v1/deneb" | ||
consensusspec "github.com/attestantio/go-eth2-client/spec" | ||
consensuscapella "github.com/attestantio/go-eth2-client/spec/capella" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
|
@@ -36,7 +39,7 @@ var NilResponse = struct{}{} | |
|
||
var ZeroU256 = boostTypes.IntToU256(0) | ||
|
||
func BuildGetHeaderResponse(payload *spec.VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) { | ||
func BuildGetHeaderResponse(payload *VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) { | ||
if payload == nil { | ||
return nil, ErrMissingRequest | ||
} | ||
|
@@ -59,7 +62,7 @@ func BuildGetHeaderResponse(payload *spec.VersionedSubmitBlockRequest, sk *bls.S | |
return nil, ErrEmptyPayload | ||
} | ||
|
||
func BuildGetPayloadResponse(payload *spec.VersionedSubmitBlockRequest) (*api.VersionedExecutionPayload, error) { | ||
func BuildGetPayloadResponse(payload *VersionedSubmitBlockRequest) (*api.VersionedExecutionPayload, error) { | ||
if payload.Capella != nil { | ||
return &api.VersionedExecutionPayload{ | ||
Version: consensusspec.DataVersionCapella, | ||
|
@@ -70,7 +73,7 @@ func BuildGetPayloadResponse(payload *spec.VersionedSubmitBlockRequest) (*api.Ve | |
return nil, ErrEmptyPayload | ||
} | ||
|
||
func BuilderSubmitBlockRequestToSignedBuilderBid(req *spec.VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) { | ||
func BuilderSubmitBlockRequestToSignedBuilderBid(req *VersionedSubmitBlockRequest, sk *bls.SecretKey, pubkey *phase0.BLSPubKey, domain phase0.Domain) (*spec.VersionedSignedBuilderBid, error) { | ||
value, err := req.Value() | ||
if err != nil { | ||
return nil, err | ||
|
@@ -167,8 +170,8 @@ func CapellaPayloadToPayloadHeader(p *consensuscapella.ExecutionPayload) (*conse | |
}, nil | ||
} | ||
|
||
func SignedBlindedBeaconBlockToBeaconBlock(signedBlindedBeaconBlock *consensusapi.VersionedSignedBlindedBeaconBlock, executionPayload *api.VersionedExecutionPayload) *consensusspec.VersionedSignedBeaconBlock { | ||
var signedBeaconBlock consensusspec.VersionedSignedBeaconBlock | ||
func SignedBlindedBeaconBlockToBeaconBlock(signedBlindedBeaconBlock *VersionedSignedBlindedBlockRequest, executionPayload *api.VersionedExecutionPayload) *VersionedSignedBlockRequest { | ||
var signedBeaconBlock VersionedSignedBlockRequest | ||
capellaBlindedBlock := signedBlindedBeaconBlock.Capella | ||
if capellaBlindedBlock != nil { | ||
signedBeaconBlock.Capella = &consensuscapella.SignedBeaconBlock{ | ||
|
@@ -198,20 +201,12 @@ func SignedBlindedBeaconBlockToBeaconBlock(signedBlindedBeaconBlock *consensusap | |
} | ||
|
||
type BuilderBlockValidationRequest struct { | ||
spec.VersionedSubmitBlockRequest | ||
VersionedSubmitBlockRequest | ||
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"` | ||
} | ||
|
||
func (r *BuilderBlockValidationRequest) MarshalJSON() ([]byte, error) { | ||
var blockRequest []byte | ||
var err error | ||
|
||
switch r.VersionedSubmitBlockRequest.Version { //nolint:exhaustive | ||
case consensusspec.DataVersionCapella: | ||
blockRequest, err = r.VersionedSubmitBlockRequest.Capella.MarshalJSON() | ||
default: | ||
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%d is not supported", r.VersionedSubmitBlockRequest.Version)) | ||
} | ||
blockRequest, err := json.Marshal(r.VersionedSubmitBlockRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -226,3 +221,129 @@ func (r *BuilderBlockValidationRequest) MarshalJSON() ([]byte, error) { | |
gasLimit[0] = ',' | ||
return append(blockRequest[:len(blockRequest)-1], gasLimit...), nil | ||
} | ||
|
||
type VersionedSubmitBlockRequest struct { | ||
spec.VersionedSubmitBlockRequest | ||
} | ||
|
||
func (r *VersionedSubmitBlockRequest) UnmarshalSSZ(input []byte) error { | ||
var err error | ||
|
||
deneb := new(deneb.SubmitBlockRequest) | ||
if err = deneb.UnmarshalSSZ(input); err == nil { | ||
r.Version = consensusspec.DataVersionDeneb | ||
r.Deneb = deneb | ||
return nil | ||
} | ||
|
||
capella := new(capella.SubmitBlockRequest) | ||
if err = capella.UnmarshalSSZ(input); err == nil { | ||
r.Version = consensusspec.DataVersionCapella | ||
r.Capella = capella | ||
return nil | ||
} | ||
return errors.Wrap(err, "failed to unmarshal SubmitBlockRequest SSZ") | ||
} | ||
|
||
func (r *VersionedSubmitBlockRequest) MarshalJSON() ([]byte, error) { | ||
switch r.Version { | ||
case consensusspec.DataVersionCapella: | ||
return json.Marshal(r.Capella) | ||
case consensusspec.DataVersionDeneb: | ||
return json.Marshal(r.Deneb) | ||
case consensusspec.DataVersionUnknown, consensusspec.DataVersionPhase0, consensusspec.DataVersionAltair, consensusspec.DataVersionBellatrix: | ||
fallthrough | ||
default: | ||
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%d is not supported", r.Version)) | ||
} | ||
} | ||
|
||
func (r *VersionedSubmitBlockRequest) UnmarshalJSON(input []byte) error { | ||
var err error | ||
|
||
deneb := new(deneb.SubmitBlockRequest) | ||
if err = json.Unmarshal(input, deneb); err == nil { | ||
r.Version = consensusspec.DataVersionDeneb | ||
r.Deneb = deneb | ||
return nil | ||
} | ||
capella := new(capella.SubmitBlockRequest) | ||
if err = json.Unmarshal(input, capella); err == nil { | ||
r.Version = consensusspec.DataVersionCapella | ||
r.Capella = capella | ||
return nil | ||
} | ||
return errors.Wrap(err, "failed to unmarshal SubmitBlockRequest") | ||
} | ||
|
||
type VersionedSignedBlockRequest struct { | ||
consensusapi.VersionedBlockRequest | ||
} | ||
|
||
func (r *VersionedSignedBlockRequest) MarshalJSON() ([]byte, error) { | ||
switch r.Version { | ||
case consensusspec.DataVersionCapella: | ||
return json.Marshal(r.Capella) | ||
case consensusspec.DataVersionDeneb: | ||
return json.Marshal(r.Deneb) | ||
case consensusspec.DataVersionUnknown, consensusspec.DataVersionPhase0, consensusspec.DataVersionAltair, consensusspec.DataVersionBellatrix: | ||
fallthrough | ||
default: | ||
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%d is not supported", r.Version)) | ||
} | ||
} | ||
|
||
func (r *VersionedSignedBlockRequest) UnmarshalJSON(input []byte) error { | ||
var err error | ||
|
||
deneb := new(apiv1deneb.SignedBlockContents) | ||
if err = json.Unmarshal(input, deneb); err == nil { | ||
r.Version = consensusspec.DataVersionDeneb | ||
r.Deneb = deneb | ||
return nil | ||
} | ||
|
||
capella := new(consensuscapella.SignedBeaconBlock) | ||
if err = json.Unmarshal(input, capella); err == nil { | ||
r.Version = consensusspec.DataVersionCapella | ||
r.Capella = capella | ||
return nil | ||
} | ||
return errors.Wrap(err, "failed to unmarshal SignedBeaconBlockRequest") | ||
} | ||
|
||
type VersionedSignedBlindedBlockRequest struct { | ||
consensusapi.VersionedBlindedBlockRequest | ||
} | ||
|
||
func (r *VersionedSignedBlindedBlockRequest) MarshalJSON() ([]byte, error) { | ||
switch r.Version { | ||
case consensusspec.DataVersionCapella: | ||
return json.Marshal(r.Capella) | ||
case consensusspec.DataVersionDeneb: | ||
return json.Marshal(r.Deneb) | ||
case consensusspec.DataVersionUnknown, consensusspec.DataVersionPhase0, consensusspec.DataVersionAltair, consensusspec.DataVersionBellatrix: | ||
fallthrough | ||
default: | ||
return nil, errors.Wrap(ErrInvalidVersion, fmt.Sprintf("%d is not supported", r.Version)) | ||
} | ||
} | ||
|
||
func (r *VersionedSignedBlindedBlockRequest) UnmarshalJSON(input []byte) error { | ||
var err error | ||
|
||
deneb := new(apiv1deneb.SignedBlindedBlockContents) | ||
if err = json.Unmarshal(input, deneb); err == nil { | ||
r.Version = consensusspec.DataVersionDeneb | ||
r.Deneb = deneb | ||
return nil | ||
} | ||
|
||
capella := new(apiv1capella.SignedBlindedBeaconBlock) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: these variables ( I think we should fix this by renaming the packages (in a later PR). |
||
if err = json.Unmarshal(input, capella); err == nil { | ||
r.Version = consensusspec.DataVersionCapella | ||
r.Capella = capella | ||
return nil | ||
} | ||
return errors.Wrap(err, "failed to unmarshal SignedBlindedBeaconBlock") | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: for consistency with the other funcs, please add a blank line between these two blocks.
Or delete the blank lines between these blocks of code in the other functions.