-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Layr-Labs/versionedBeaconstate
Make Interfaces Hardfork agnostic
- Loading branch information
Showing
26 changed files
with
4,160 additions
and
489 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
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
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 @@ | ||
[{"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"}] |
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,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 | ||
} |
Oops, something went wrong.