Skip to content

Commit 6f4bada

Browse files
authored
Merge pull request #169 from Layr-Labs/yash/pectra-testnet-compatability
feat: versioned pectra compatability
2 parents 4089d42 + 8f8988c commit 6f4bada

17 files changed

+1644
-244
lines changed

.github/workflows/pipeline.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,17 @@ jobs:
5858
5959
- name: Download blockchain data
6060
run: |
61+
# Download Deneb state
6162
curl -o data/deneb_holesky_beacon_state_2227472.ssz.zip https://dviu8zszosyat.cloudfront.net/deneb_holesky_beacon_state_2227472.ssz.zip
62-
(cd data && unzip deneb_holesky_beacon_state_2227472.ssz.zip)
63+
unzip -j data/deneb_holesky_beacon_state_2227472.ssz.zip -d data/
64+
65+
# Download Electra state
66+
curl -o data/electra_mekong_beacon_state_654719.ssz.zip https://d1w8rcimizlk6a.cloudfront.net/electra_mekong_beacon_state_654719.ssz.zip
67+
unzip -j data/electra_mekong_beacon_state_654719.ssz.zip -d data/
6368
6469
- name: Run tests
70+
env:
71+
RPC_URL: ${{ secrets.RPC_URL }}
6572
run: |
6673
go test -v ./...
6774
goreleaser:

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ test
1919

2020
**/.DS_Store
2121

22-
*.ssz
22+
*.ssz
23+
24+
.env

beacon/beacon_state_top_level_roots.go

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package beacon
22

33
import (
4+
"errors"
45
"reflect"
56

67
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
8+
"github.com/attestantio/go-eth2-client/spec"
79
"github.com/attestantio/go-eth2-client/spec/phase0"
810
)
911

10-
type BeaconStateTopLevelRoots struct {
12+
type VersionedBeaconStateTopLevelRoots struct {
13+
Version spec.DataVersion
14+
Deneb *BeaconStateTopLevelRootsDeneb
15+
Electra *BeaconStateTopLevelRootsElectra
16+
}
17+
18+
func (v *VersionedBeaconStateTopLevelRoots) GetBalancesRoot() (*phase0.Root, error) {
19+
switch v.Version {
20+
case spec.DataVersionDeneb:
21+
return v.Deneb.BalancesRoot, nil
22+
case spec.DataVersionElectra:
23+
return v.Electra.BalancesRoot, nil
24+
default:
25+
return nil, errors.New("unsupported beacon state version")
26+
}
27+
}
28+
29+
type BeaconStateTopLevelRootsDeneb struct {
1130
GenesisTimeRoot *phase0.Root
1231
GenesisValidatorsRoot *phase0.Root
1332
SlotRoot *phase0.Root
@@ -38,18 +57,71 @@ type BeaconStateTopLevelRoots struct {
3857
HistoricalSummariesRoot *phase0.Root
3958
}
4059

41-
func ProveBeaconTopLevelRootAgainstBeaconState(beaconTopLevelRoots *BeaconStateTopLevelRoots, index uint64) (common.Proof, error) {
42-
v := reflect.ValueOf(*beaconTopLevelRoots)
60+
type BeaconStateTopLevelRootsElectra struct {
61+
GenesisTimeRoot *phase0.Root
62+
GenesisValidatorsRoot *phase0.Root
63+
SlotRoot *phase0.Root
64+
ForkRoot *phase0.Root
65+
LatestBlockHeaderRoot *phase0.Root
66+
BlockRootsRoot *phase0.Root
67+
StateRootsRoot *phase0.Root
68+
HistoricalRootsRoot *phase0.Root
69+
ETH1DataRoot *phase0.Root
70+
ETH1DataVotesRoot *phase0.Root
71+
ETH1DepositIndexRoot *phase0.Root
72+
ValidatorsRoot *phase0.Root
73+
BalancesRoot *phase0.Root
74+
RANDAOMixesRoot *phase0.Root
75+
SlashingsRoot *phase0.Root
76+
PreviousEpochParticipationRoot *phase0.Root
77+
CurrentEpochParticipationRoot *phase0.Root
78+
JustificationBitsRoot *phase0.Root
79+
PreviousJustifiedCheckpointRoot *phase0.Root
80+
CurrentJustifiedCheckpointRoot *phase0.Root
81+
FinalizedCheckpointRoot *phase0.Root
82+
InactivityScoresRoot *phase0.Root
83+
CurrentSyncCommitteeRoot *phase0.Root
84+
NextSyncCommitteeRoot *phase0.Root
85+
LatestExecutionPayloadHeaderRoot *phase0.Root
86+
NextWithdrawalIndexRoot *phase0.Root
87+
NextWithdrawalValidatorIndexRoot *phase0.Root
88+
HistoricalSummariesRoot *phase0.Root
89+
DepositRequestsStartIndexRoot *phase0.Root
90+
DepositBalanceToConsumeRoot *phase0.Root
91+
ExitBalanceToConsumeRoot *phase0.Root
92+
EarliestExitEpochRoot *phase0.Root
93+
ConsolidationBalanceToConsumeRoot *phase0.Root
94+
EarliestConsolidationEpochRoot *phase0.Root
95+
PendingDepositsRoot *phase0.Root
96+
PendingPartialWithdrawalsRoot *phase0.Root
97+
PendingConsolidationsRoot *phase0.Root
98+
}
99+
100+
func ProveBeaconTopLevelRootAgainstBeaconState(beaconTopLevelRoots *VersionedBeaconStateTopLevelRoots, index uint64) (common.Proof, error) {
101+
var v reflect.Value
102+
var treeHeight uint64
103+
switch beaconTopLevelRoots.Version {
104+
case spec.DataVersionDeneb:
105+
v = reflect.ValueOf(*beaconTopLevelRoots.Deneb)
106+
treeHeight = BEACON_STATE_TREE_HEIGHT_DENEB
107+
case spec.DataVersionElectra:
108+
v = reflect.ValueOf(*beaconTopLevelRoots.Electra)
109+
treeHeight = BEACON_STATE_TREE_HEIGHT_ELECTRA
110+
default:
111+
return nil, errors.New("unsupported beacon state version")
112+
}
113+
43114
beaconTopLevelRootsList := make([]interface{}, v.NumField())
44115
for i := 0; i < v.NumField(); i++ {
45116
r := v.Field(i).Interface()
46117
typedR := r.(*phase0.Root)
47118
beaconTopLevelRootsList[i] = *typedR
48119
}
120+
49121
roots := make([]phase0.Root, len(beaconTopLevelRootsList))
50122
for i, v := range beaconTopLevelRootsList {
51123
roots[i] = v.(phase0.Root)
52124
}
53125

54-
return common.GetProof(roots, index, BEACON_STATE_TREE_HEIGHT)
126+
return common.GetProof(roots, index, treeHeight)
55127
}

beacon/constants.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package beacon
33
const (
44
BEACON_BLOCK_HEADER_NUM_FIELDS = uint64(5)
55

6-
BEACON_BLOCK_HEADER_TREE_HEIGHT = uint64(3)
7-
BEACON_STATE_TREE_HEIGHT = uint64(5)
8-
BALANCE_TREE_HEIGHT = uint64(38)
9-
VALIDATOR_TREE_HEIGHT = uint64(40)
6+
BEACON_BLOCK_HEADER_TREE_HEIGHT = uint64(3)
7+
BEACON_STATE_TREE_HEIGHT_DENEB = uint64(5)
8+
BEACON_STATE_TREE_HEIGHT_ELECTRA = uint64(6)
9+
BALANCE_TREE_HEIGHT = uint64(38)
10+
VALIDATOR_TREE_HEIGHT = uint64(40)
1011

1112
STATE_ROOT_INDEX = uint64(3)
1213

beacon/deneb.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package beacon
22

33
import (
44
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
5+
"github.com/attestantio/go-eth2-client/spec"
56
"github.com/attestantio/go-eth2-client/spec/altair"
67
"github.com/attestantio/go-eth2-client/spec/deneb"
78
"github.com/attestantio/go-eth2-client/spec/phase0"
89
ssz "github.com/ferranbt/fastssz"
910
)
1011

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

1617
hh := ssz.NewHasher()
1718

@@ -409,5 +410,8 @@ func ComputeBeaconStateTopLevelRootsDeneb(b *deneb.BeaconState) (*BeaconStateTop
409410
hh.Reset()
410411
}
411412

412-
return beaconStateTopLevelRoots, nil
413+
return &VersionedBeaconStateTopLevelRoots{
414+
Version: spec.DataVersionDeneb,
415+
Deneb: beaconStateTopLevelRoots,
416+
}, nil
413417
}

0 commit comments

Comments
 (0)