-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
601 additions
and
0 deletions.
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
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") | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.