Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: versioned pectra compatability #169

Merged
merged 10 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ jobs:

- name: Download blockchain data
run: |
# Download Deneb state
curl -o data/deneb_holesky_beacon_state_2227472.ssz.zip https://dviu8zszosyat.cloudfront.net/deneb_holesky_beacon_state_2227472.ssz.zip
(cd data && unzip deneb_holesky_beacon_state_2227472.ssz.zip)
unzip -j data/deneb_holesky_beacon_state_2227472.ssz.zip -d data/

# Download Electra state
curl -o data/electra_mekong_beacon_state_654719.ssz.zip https://d1w8rcimizlk6a.cloudfront.net/electra_mekong_beacon_state_654719.ssz.zip
unzip -j data/electra_mekong_beacon_state_654719.ssz.zip -d data/

- name: Run tests
env:
RPC_URL: ${{ secrets.RPC_URL }}
run: |
go test -v ./...
goreleaser:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ test

**/.DS_Store

*.ssz
*.ssz

.env
80 changes: 76 additions & 4 deletions beacon/beacon_state_top_level_roots.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package beacon

import (
"errors"
"reflect"

"github.com/Layr-Labs/eigenpod-proofs-generation/common"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/phase0"
)

type BeaconStateTopLevelRoots struct {
type VersionedBeaconStateTopLevelRoots struct {
Version spec.DataVersion
Deneb *BeaconStateTopLevelRootsDeneb
Electra *BeaconStateTopLevelRootsElectra
}

func (v *VersionedBeaconStateTopLevelRoots) GetBalancesRoot() (*phase0.Root, error) {
switch v.Version {
case spec.DataVersionDeneb:
return v.Deneb.BalancesRoot, nil
case spec.DataVersionElectra:
return v.Electra.BalancesRoot, nil
default:
return nil, errors.New("unsupported beacon state version")
}
}

type BeaconStateTopLevelRootsDeneb struct {
GenesisTimeRoot *phase0.Root
GenesisValidatorsRoot *phase0.Root
SlotRoot *phase0.Root
Expand Down Expand Up @@ -38,18 +57,71 @@ type BeaconStateTopLevelRoots struct {
HistoricalSummariesRoot *phase0.Root
}

func ProveBeaconTopLevelRootAgainstBeaconState(beaconTopLevelRoots *BeaconStateTopLevelRoots, index uint64) (common.Proof, error) {
v := reflect.ValueOf(*beaconTopLevelRoots)
type BeaconStateTopLevelRootsElectra struct {
GenesisTimeRoot *phase0.Root
GenesisValidatorsRoot *phase0.Root
SlotRoot *phase0.Root
ForkRoot *phase0.Root
LatestBlockHeaderRoot *phase0.Root
BlockRootsRoot *phase0.Root
StateRootsRoot *phase0.Root
HistoricalRootsRoot *phase0.Root
ETH1DataRoot *phase0.Root
ETH1DataVotesRoot *phase0.Root
ETH1DepositIndexRoot *phase0.Root
ValidatorsRoot *phase0.Root
BalancesRoot *phase0.Root
RANDAOMixesRoot *phase0.Root
SlashingsRoot *phase0.Root
PreviousEpochParticipationRoot *phase0.Root
CurrentEpochParticipationRoot *phase0.Root
JustificationBitsRoot *phase0.Root
PreviousJustifiedCheckpointRoot *phase0.Root
CurrentJustifiedCheckpointRoot *phase0.Root
FinalizedCheckpointRoot *phase0.Root
InactivityScoresRoot *phase0.Root
CurrentSyncCommitteeRoot *phase0.Root
NextSyncCommitteeRoot *phase0.Root
LatestExecutionPayloadHeaderRoot *phase0.Root
NextWithdrawalIndexRoot *phase0.Root
NextWithdrawalValidatorIndexRoot *phase0.Root
HistoricalSummariesRoot *phase0.Root
DepositRequestsStartIndexRoot *phase0.Root
DepositBalanceToConsumeRoot *phase0.Root
ExitBalanceToConsumeRoot *phase0.Root
EarliestExitEpochRoot *phase0.Root
ConsolidationBalanceToConsumeRoot *phase0.Root
EarliestConsolidationEpochRoot *phase0.Root
PendingDepositsRoot *phase0.Root
PendingPartialWithdrawalsRoot *phase0.Root
PendingConsolidationsRoot *phase0.Root
}

func ProveBeaconTopLevelRootAgainstBeaconState(beaconTopLevelRoots *VersionedBeaconStateTopLevelRoots, index uint64) (common.Proof, error) {
var v reflect.Value
var treeHeight uint64
switch beaconTopLevelRoots.Version {
case spec.DataVersionDeneb:
v = reflect.ValueOf(*beaconTopLevelRoots.Deneb)
treeHeight = BEACON_STATE_TREE_HEIGHT_DENEB
case spec.DataVersionElectra:
v = reflect.ValueOf(*beaconTopLevelRoots.Electra)
treeHeight = BEACON_STATE_TREE_HEIGHT_ELECTRA
default:
return nil, errors.New("unsupported beacon state version")
}

beaconTopLevelRootsList := make([]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
r := v.Field(i).Interface()
typedR := r.(*phase0.Root)
beaconTopLevelRootsList[i] = *typedR
}

roots := make([]phase0.Root, len(beaconTopLevelRootsList))
for i, v := range beaconTopLevelRootsList {
roots[i] = v.(phase0.Root)
}

return common.GetProof(roots, index, BEACON_STATE_TREE_HEIGHT)
return common.GetProof(roots, index, treeHeight)
}
9 changes: 5 additions & 4 deletions beacon/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package beacon
const (
BEACON_BLOCK_HEADER_NUM_FIELDS = uint64(5)

BEACON_BLOCK_HEADER_TREE_HEIGHT = uint64(3)
BEACON_STATE_TREE_HEIGHT = uint64(5)
BALANCE_TREE_HEIGHT = uint64(38)
VALIDATOR_TREE_HEIGHT = uint64(40)
BEACON_BLOCK_HEADER_TREE_HEIGHT = uint64(3)
BEACON_STATE_TREE_HEIGHT_DENEB = uint64(5)
BEACON_STATE_TREE_HEIGHT_ELECTRA = uint64(6)
BALANCE_TREE_HEIGHT = uint64(38)
VALIDATOR_TREE_HEIGHT = uint64(40)

STATE_ROOT_INDEX = uint64(3)

Expand Down
10 changes: 7 additions & 3 deletions beacon/deneb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package beacon

import (
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/altair"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
)

// taken from https://github.com/attestantio/go-eth2-client/blob/21f7dd480fed933d8e0b1c88cee67da721c80eb2/spec/deneb/beaconstate_ssz.go#L640
func ComputeBeaconStateTopLevelRootsDeneb(b *deneb.BeaconState) (*BeaconStateTopLevelRoots, error) {
func ComputeBeaconStateTopLevelRootsDeneb(b *deneb.BeaconState) (*VersionedBeaconStateTopLevelRoots, error) {
var err error
beaconStateTopLevelRoots := &BeaconStateTopLevelRoots{}
beaconStateTopLevelRoots := &BeaconStateTopLevelRootsDeneb{}

hh := ssz.NewHasher()

Expand Down Expand Up @@ -409,5 +410,8 @@ func ComputeBeaconStateTopLevelRootsDeneb(b *deneb.BeaconState) (*BeaconStateTop
hh.Reset()
}

return beaconStateTopLevelRoots, nil
return &VersionedBeaconStateTopLevelRoots{
Version: spec.DataVersionDeneb,
Deneb: beaconStateTopLevelRoots,
}, nil
}
Loading