Skip to content

Commit

Permalink
Add more versioned structures
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed May 7, 2024
1 parent ed4b002 commit 7c48f0b
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 19 deletions.
42 changes: 37 additions & 5 deletions api/versionedblockrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (v *VersionedBlockRequest) StateRoot() (phase0.Root, error) {
}

// AttesterSlashings returns the attester slashings of the beacon block.
func (v *VersionedBlockRequest) AttesterSlashings() ([]*phase0.AttesterSlashing, error) {
func (v *VersionedBlockRequest) AttesterSlashings() ([]spec.VersionedAttesterSlashing, error) {
switch v.Version {
case spec.DataVersionBellatrix:
if v.Bellatrix == nil ||
Expand All @@ -342,31 +342,63 @@ func (v *VersionedBlockRequest) AttesterSlashings() ([]*phase0.AttesterSlashing,
return nil, ErrDataMissing
}

return v.Bellatrix.Message.Body.AttesterSlashings, nil
versionedAttesterSlashings := make([]spec.VersionedAttesterSlashing, len(v.Bellatrix.Message.Body.AttesterSlashings))
for i, attesterSlashing := range v.Bellatrix.Message.Body.AttesterSlashings {
versionedAttesterSlashings[i] = spec.VersionedAttesterSlashing{
Version: spec.DataVersionBellatrix,
Bellatrix: attesterSlashing,
}
}

return versionedAttesterSlashings, nil
case spec.DataVersionCapella:
if v.Capella == nil ||
v.Capella.Message == nil ||
v.Capella.Message.Body == nil {
return nil, ErrDataMissing
}

return v.Capella.Message.Body.AttesterSlashings, nil
versionedAttesterSlashings := make([]spec.VersionedAttesterSlashing, len(v.Capella.Message.Body.AttesterSlashings))
for i, attesterSlashing := range v.Capella.Message.Body.AttesterSlashings {
versionedAttesterSlashings[i] = spec.VersionedAttesterSlashing{
Version: spec.DataVersionCapella,
Capella: attesterSlashing,
}
}

return versionedAttesterSlashings, nil
case spec.DataVersionDeneb:
if v.Deneb == nil ||
v.Deneb.Message == nil ||
v.Deneb.Message.Body == nil {
return nil, ErrDataMissing
}

return v.Deneb.Message.Body.AttesterSlashings, nil
versionedAttesterSlashings := make([]spec.VersionedAttesterSlashing, len(v.Deneb.Message.Body.AttesterSlashings))
for i, attesterSlashing := range v.Deneb.Message.Body.AttesterSlashings {
versionedAttesterSlashings[i] = spec.VersionedAttesterSlashing{
Version: spec.DataVersionDeneb,
Deneb: attesterSlashing,
}
}

return versionedAttesterSlashings, nil
case spec.DataVersionElectra:
if v.Electra == nil ||
v.Electra.Message == nil ||
v.Electra.Message.Body == nil {
return nil, ErrDataMissing
}

return v.Electra.Message.Body.AttesterSlashings, nil
versionedAttesterSlashings := make([]spec.VersionedAttesterSlashing, len(v.Electra.Message.Body.AttesterSlashings))
for i, attesterSlashing := range v.Electra.Message.Body.AttesterSlashings {
versionedAttesterSlashings[i] = spec.VersionedAttesterSlashing{
Version: spec.DataVersionElectra,
Electra: attesterSlashing,
}
}

return versionedAttesterSlashings, nil
default:
return nil, ErrUnsupportedVersion
}
Expand Down
229 changes: 229 additions & 0 deletions spec/versionedattesterslashing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
// Copyright © 2024 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 spec

import (
"errors"

"github.com/attestantio/go-eth2-client/spec/electra"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

// VersionedAttesterSlashing contains a versioned attestation.
type VersionedAttesterSlashing struct {
Version DataVersion
Phase0 *phase0.AttesterSlashing
Altair *phase0.AttesterSlashing
Bellatrix *phase0.AttesterSlashing
Capella *phase0.AttesterSlashing
Deneb *phase0.AttesterSlashing
Electra *electra.AttesterSlashing
}

// IsEmpty returns true if there is no block.
func (v *VersionedAttesterSlashing) IsEmpty() bool {
return v.Phase0 == nil && v.Altair == nil && v.Bellatrix == nil && v.Capella == nil && v.Deneb == nil && v.Electra == nil
}

// Attestation1 returns the first indexed attestation.
func (v *VersionedAttesterSlashing) Attestation1() (*VersionedIndexedAttestation, error) {
switch v.Version {
case DataVersionPhase0:
if v.Phase0 == nil {
return nil, errors.New("no Phase0 indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Phase0: v.Phase0.Attestation1,
}

return &versionedIndexedAttestation, nil
case DataVersionAltair:
if v.Altair == nil {
return nil, errors.New("no Altair indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Altair: v.Altair.Attestation1,
}

return &versionedIndexedAttestation, nil
case DataVersionBellatrix:
if v.Bellatrix == nil {
return nil, errors.New("no Bellatrix indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Bellatrix: v.Bellatrix.Attestation1,
}

return &versionedIndexedAttestation, nil
case DataVersionCapella:
if v.Capella == nil {
return nil, errors.New("no Capella indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionCapella,
Capella: v.Capella.Attestation1,
}

return &versionedIndexedAttestation, nil
case DataVersionDeneb:
if v.Deneb == nil {
return nil, errors.New("no Deneb indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionDeneb,
Deneb: v.Deneb.Attestation1,
}

return &versionedIndexedAttestation, nil
case DataVersionElectra:
if v.Electra == nil {
return nil, errors.New("no Electra indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionElectra,
Electra: v.Electra.Attestation1,
}

return &versionedIndexedAttestation, nil
default:
return nil, errors.New("unknown version")
}
}

// Attestation2 returns the second indexed attestation.
func (v *VersionedAttesterSlashing) Attestation2() (*VersionedIndexedAttestation, error) {
switch v.Version {
case DataVersionPhase0:
if v.Phase0 == nil {
return nil, errors.New("no Phase0 indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Phase0: v.Phase0.Attestation2,
}

return &versionedIndexedAttestation, nil
case DataVersionAltair:
if v.Altair == nil {
return nil, errors.New("no Altair indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Altair: v.Altair.Attestation2,
}

return &versionedIndexedAttestation, nil
case DataVersionBellatrix:
if v.Bellatrix == nil {
return nil, errors.New("no Bellatrix indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionPhase0,
Bellatrix: v.Bellatrix.Attestation2,
}

return &versionedIndexedAttestation, nil
case DataVersionCapella:
if v.Capella == nil {
return nil, errors.New("no Capella indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionCapella,
Capella: v.Capella.Attestation2,
}

return &versionedIndexedAttestation, nil
case DataVersionDeneb:
if v.Deneb == nil {
return nil, errors.New("no Deneb indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionDeneb,
Deneb: v.Deneb.Attestation2,
}

return &versionedIndexedAttestation, nil
case DataVersionElectra:
if v.Electra == nil {
return nil, errors.New("no Electra indexed attestation")
}

versionedIndexedAttestation := VersionedIndexedAttestation{
Version: DataVersionElectra,
Electra: v.Electra.Attestation2,
}

return &versionedIndexedAttestation, nil
default:
return nil, errors.New("unknown version")
}
}

// String returns a string version of the structure.
func (v *VersionedAttesterSlashing) String() string {
switch v.Version {
case DataVersionPhase0:
if v.Phase0 == nil {
return ""
}

return v.Phase0.String()
case DataVersionAltair:
if v.Altair == nil {
return ""
}

return v.Altair.String()
case DataVersionBellatrix:
if v.Bellatrix == nil {
return ""
}

return v.Bellatrix.String()
case DataVersionCapella:
if v.Capella == nil {
return ""
}

return v.Capella.String()
case DataVersionDeneb:
if v.Deneb == nil {
return ""
}

return v.Deneb.String()
case DataVersionElectra:
if v.Electra == nil {
return ""
}

return v.Electra.String()
default:
return "unknown version"
}
}
Loading

0 comments on commit 7c48f0b

Please sign in to comment.