Skip to content

Commit

Permalink
Merge pull request #30 from Layr-Labs/versionedBeaconstate
Browse files Browse the repository at this point in the history
Make Interfaces Hardfork agnostic
  • Loading branch information
Sidu28 authored Feb 10, 2024
2 parents 3e94693 + 55251a3 commit 7a3bb6f
Show file tree
Hide file tree
Showing 26 changed files with 4,160 additions and 489 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
run: unzip data/deneb_goerli_slot_7431952.json.zip -d ./data
- name: Unzip the File 3
run: unzip data/deneb_goerli_slot_7421952.json.zip -d ./data
- name: Unzip the File 4
run: unzip data/goerli_slot_6409723.json.zip -d ./data

- name: Set up Go 1.x
uses: actions/setup-go@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ data/deneb_goerli_slot_7416760.json
data/deneb_goerli_slot_7413760.json
data/deneb_goerli_slot_7421952.json
data/deneb_goerli_slot_7431952.json
data/goerli_slot_6409723.json

generation/generation

Expand Down
1 change: 1 addition & 0 deletions BeaconChainProofs.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"bytes32","name":"latestBlockRoot","type":"bytes32"},{"internalType":"bytes32","name":"beaconStateRoot","type":"bytes32"},{"internalType":"bytes","name":"stateRootProof","type":"bytes"}],"name":"verifyStateRootAgainstLatestBlockRoot","outputs":[],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"beaconStateRoot","type":"bytes32"},{"internalType":"bytes32[]","name":"validatorFields","type":"bytes32[]"},{"internalType":"bytes","name":"validatorFieldsProof","type":"bytes"},{"internalType":"uint40","name":"validatorIndex","type":"uint40"}],"name":"verifyValidatorFields","outputs":[],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"beaconStateRoot","type":"bytes32"},{"internalType":"bytes32[]","name":"withdrawalFields","type":"bytes32[]"},{"components":[{"internalType":"bytes","name":"withdrawalProof","type":"bytes"},{"internalType":"bytes","name":"slotProof","type":"bytes"},{"internalType":"bytes","name":"executionPayloadProof","type":"bytes"},{"internalType":"bytes","name":"timestampProof","type":"bytes"},{"internalType":"bytes","name":"historicalSummaryBlockRootProof","type":"bytes"},{"internalType":"uint64","name":"blockRootIndex","type":"uint64"},{"internalType":"uint64","name":"historicalSummaryIndex","type":"uint64"},{"internalType":"uint64","name":"withdrawalIndex","type":"uint64"},{"internalType":"bytes32","name":"blockRoot","type":"bytes32"},{"internalType":"bytes32","name":"slotRoot","type":"bytes32"},{"internalType":"bytes32","name":"timestampRoot","type":"bytes32"},{"internalType":"bytes32","name":"executionPayloadRoot","type":"bytes32"}],"internalType":"struct BeaconChainProofsContract.WithdrawalProof","name":"withdrawalProof","type":"tuple"},{"internalType":"uint64","name":"denebForkTimestamp","type":"uint64"}],"name":"verifyWithdrawal","outputs":[],"stateMutability":"view","type":"function"}]
111 changes: 111 additions & 0 deletions beacon/versioned_beacon_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package beacon

import (
"errors"

"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) {
switch state.Version {
case spec.DataVersionCapella:
return state.Capella.HistoricalSummaries, nil
case spec.DataVersionDeneb:
return state.Deneb.HistoricalSummaries, nil
default:
return nil, errors.New("unsupported beacon state version")
}
}

func GetGenesisTime(state *spec.VersionedBeaconState) (uint64, error) {
switch state.Version {
case spec.DataVersionCapella:
return state.Capella.GenesisTime, nil
case spec.DataVersionDeneb:
return state.Deneb.GenesisTime, nil
default:
return 0, errors.New("unsupported beacon state version")
}
}

func GetBlockRoots(beaconState spec.VersionedBeaconState) ([]phase0.Root, error) {
var blockRoots []phase0.Root

switch beaconState.Version {
case spec.DataVersionDeneb:
blockRoots = beaconState.Deneb.BlockRoots
case spec.DataVersionCapella:
blockRoots = beaconState.Capella.BlockRoots
default:
return nil, errors.New("unsupported beacon state version")
}
return blockRoots, nil
}
func CreateVersionedSignedBlock(block interface{}) (spec.VersionedSignedBeaconBlock, error) {
var versionedBlock spec.VersionedSignedBeaconBlock

switch s := block.(type) {
case deneb.BeaconBlock:
var signedBlock deneb.SignedBeaconBlock
signedBlock.Message = &s
versionedBlock.Deneb = &signedBlock
versionedBlock.Version = spec.DataVersionDeneb
case capella.BeaconBlock:
var signedBlock capella.SignedBeaconBlock
signedBlock.Message = &s
versionedBlock.Capella = &signedBlock
versionedBlock.Version = spec.DataVersionCapella
default:
return versionedBlock, errors.New("unsupported beacon block version")
}
return versionedBlock, nil
}

func CreateVersionedState(state interface{}) (spec.VersionedBeaconState, error) {
var versionedState spec.VersionedBeaconState

switch s := state.(type) {
case *deneb.BeaconState:
versionedState.Deneb = s
versionedState.Version = spec.DataVersionDeneb
case *capella.BeaconState:
versionedState.Capella = s
versionedState.Version = spec.DataVersionCapella
default:
return versionedState, errors.New("unsupported beacon state version")
}
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
}
Loading

0 comments on commit 7a3bb6f

Please sign in to comment.