Skip to content

Commit

Permalink
added helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidu28 committed Feb 10, 2024
1 parent 5555657 commit 6c2f7b6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions beacon/versioned_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

func GetHistoricalSummaries(state *spec.VersionedBeaconState) ([]*capella.HistoricalSummary, error) {
Expand All @@ -29,6 +30,17 @@ func GetGenesisTime(state *spec.VersionedBeaconState) (uint64, error) {
return 0, errors.New("unsupported beacon state version")
}
}

func GetBlockRoots(beaconState spec.VersionedBeaconState) []phase0.Root {
blockRoots := make([]phase0.Root, 0)
switch beaconState.Version {
case spec.DataVersionDeneb:
blockRoots = beaconState.Deneb.BlockRoots
case spec.DataVersionCapella:
blockRoots = beaconState.Capella.BlockRoots
}
return blockRoots
}
func CreateVersionedSignedBlock(block interface{}) (spec.VersionedSignedBeaconBlock, error) {
var versionedBlock spec.VersionedSignedBeaconBlock

Expand Down Expand Up @@ -64,3 +76,33 @@ func CreateVersionedState(state interface{}) (spec.VersionedBeaconState, error)
}
return versionedState, nil
}

func UnmarshalSSZVersionedBeaconState(data []byte) (*spec.VersionedBeaconState, error) {
beaconState := &spec.VersionedBeaconState{}
// Try to unmarshal using Deneb
err := beaconState.Deneb.UnmarshalSSZ(data)
if err != nil {
// If Deneb fails, try Capella
err = beaconState.Capella.UnmarshalSSZ(data)
if err != nil {
return nil, err
}
}

return beaconState, nil
}

func MarshalSSZVersionedBeaconState(beaconState spec.VersionedBeaconState) ([]byte, error) {
var data []byte
// Try to marshal using Deneb
data, err := beaconState.Deneb.MarshalSSZ()
if err != nil {
// If Deneb fails, try Capella
data, err = beaconState.Capella.MarshalSSZ()
if err != nil {
return nil, err
}
}

return data, nil
}

0 comments on commit 6c2f7b6

Please sign in to comment.