Skip to content

Commit

Permalink
Add versioned submit block response
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Jul 31, 2023
1 parent 1c6bea5 commit 9bb45ba
Show file tree
Hide file tree
Showing 4 changed files with 601 additions and 0 deletions.
102 changes: 102 additions & 0 deletions api/versionedsubmitblindedblockresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright © 2022 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"github.com/attestantio/go-builder-client/api/deneb"
consensusspec "github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
)

// VersionedSubmitBlindedBlockResponse contains a versioned SubmitBlindedBlockResponse.
type VersionedSubmitBlindedBlockResponse struct {
Version consensusspec.DataVersion
Bellatrix *bellatrix.ExecutionPayload
Capella *capella.ExecutionPayload
Deneb *deneb.ExecutionPayloadAndBlobsBundle
}

// IsEmpty returns true if there is no payload.
func (v *VersionedSubmitBlindedBlockResponse) IsEmpty() bool {
switch v.Version {
case consensusspec.DataVersionBellatrix:
return v.Bellatrix == nil
case consensusspec.DataVersionCapella:
return v.Capella == nil
case consensusspec.DataVersionDeneb:
return v.Deneb == nil
default:
return true
}
}

func (v *VersionedSubmitBlindedBlockResponse) BlockHash() (phase0.Hash32, error) {
if v == nil {
return phase0.Hash32{}, errors.New("nil struct")
}
switch v.Version {
case consensusspec.DataVersionBellatrix:
if v.Bellatrix == nil {
return phase0.Hash32{}, errors.New("no data")
}
return v.Bellatrix.BlockHash, nil
case consensusspec.DataVersionCapella:
if v.Capella == nil {
return phase0.Hash32{}, errors.New("no data")
}
return v.Capella.BlockHash, nil
case consensusspec.DataVersionDeneb:
if v.Deneb == nil {
return phase0.Hash32{}, errors.New("no data")
}
if v.Deneb.ExecutionPayload == nil {
return phase0.Hash32{}, errors.New("no execution payload")
}
return v.Deneb.ExecutionPayload.BlockHash, nil
default:
return phase0.Hash32{}, errors.New("unsupported version")
}
}

// Transactions returns the transactions in the execution payload.
func (v *VersionedSubmitBlindedBlockResponse) Transactions() ([]bellatrix.Transaction, error) {
if v == nil {
return nil, errors.New("nil struct")
}
switch v.Version {
case consensusspec.DataVersionBellatrix:
if v.Bellatrix == nil {
return nil, errors.New("no data")
}
return v.Bellatrix.Transactions, nil
case consensusspec.DataVersionCapella:
if v.Capella == nil {
return nil, errors.New("no data")
}
return v.Capella.Transactions, nil
case consensusspec.DataVersionDeneb:
if v.Deneb == nil {
return nil, errors.New("no data")
}
if v.Deneb.ExecutionPayload == nil {
return nil, errors.New("no execution payload")
}
return v.Deneb.ExecutionPayload.Transactions, nil
default:
return nil, errors.New("unsupported version")
}
}
106 changes: 106 additions & 0 deletions api/versionedsubmitblindedblockresponse_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright © 2022 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"encoding/json"
"fmt"

"github.com/attestantio/go-builder-client/api/deneb"
"github.com/attestantio/go-eth2-client/spec"
"github.com/pkg/errors"
)

type denebVersionedExecutionPayloadAndBlobsBundleJSON struct {
Data *deneb.ExecutionPayloadAndBlobsBundle `json:"data"`
}

// MarshalJSON implements json.Marshaler.
func (v *VersionedSubmitBlindedBlockResponse) MarshalJSON() ([]byte, error) {
version := &versionJSON{
Version: v.Version,
}
switch v.Version {
case spec.DataVersionBellatrix:
if v.Bellatrix == nil {
return nil, errors.New("no bellatrix data")
}
data := &bellatrixVersionedExecutionPayloadJSON{
Data: v.Bellatrix,
}
payload := struct {
*versionJSON
*bellatrixVersionedExecutionPayloadJSON
}{version, data}
return json.Marshal(payload)
case spec.DataVersionCapella:
if v.Capella == nil {
return nil, errors.New("no capella data")
}
data := &capellaVersionedExecutionPayloadJSON{
Data: v.Capella,
}
payload := struct {
*versionJSON
*capellaVersionedExecutionPayloadJSON
}{version, data}
return json.Marshal(payload)
case spec.DataVersionDeneb:
if v.Deneb == nil {
return nil, errors.New("no deneb data")
}
data := &denebVersionedExecutionPayloadAndBlobsBundleJSON{
Data: v.Deneb,
}
payload := struct {
*versionJSON
*denebVersionedExecutionPayloadAndBlobsBundleJSON
}{version, data}
return json.Marshal(payload)
default:
return nil, fmt.Errorf("unsupported data version %v", v.Version)
}
}

// UnmarshalJSON implements json.Unmarshaler.
func (v *VersionedSubmitBlindedBlockResponse) UnmarshalJSON(input []byte) error {
var metadata versionJSON
if err := json.Unmarshal(input, &metadata); err != nil {
return errors.Wrap(err, "invalid JSON")
}
v.Version = metadata.Version
switch v.Version {
case spec.DataVersionBellatrix:
var data bellatrixVersionedExecutionPayloadJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
v.Bellatrix = data.Data
case spec.DataVersionCapella:
var data capellaVersionedExecutionPayloadJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
v.Capella = data.Data
case spec.DataVersionDeneb:
var data denebVersionedExecutionPayloadAndBlobsBundleJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
v.Deneb = data.Data
default:
return fmt.Errorf("unsupported data version %v", metadata.Version)
}
return nil
}
Loading

0 comments on commit 9bb45ba

Please sign in to comment.