Skip to content

Commit ef27d87

Browse files
committed
feat: versioned pectra compatability
1 parent 65a414e commit ef27d87

File tree

11 files changed

+654
-61
lines changed

11 files changed

+654
-61
lines changed

beacon/beacon_state_top_level_roots.go

Lines changed: 73 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,68 @@ 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+
switch beaconTopLevelRoots.Version {
103+
case spec.DataVersionDeneb:
104+
v = reflect.ValueOf(*beaconTopLevelRoots.Deneb)
105+
case spec.DataVersionElectra:
106+
v = reflect.ValueOf(*beaconTopLevelRoots.Electra)
107+
default:
108+
return nil, errors.New("unsupported beacon state version")
109+
}
110+
43111
beaconTopLevelRootsList := make([]interface{}, v.NumField())
44112
for i := 0; i < v.NumField(); i++ {
45113
r := v.Field(i).Interface()
46114
typedR := r.(*phase0.Root)
47115
beaconTopLevelRootsList[i] = *typedR
48116
}
117+
49118
roots := make([]phase0.Root, len(beaconTopLevelRootsList))
50119
for i, v := range beaconTopLevelRootsList {
51120
roots[i] = v.(phase0.Root)
52121
}
53122

54-
return common.GetProof(roots, index, BEACON_STATE_TREE_HEIGHT)
123+
return common.GetProof(roots, index, BEACON_STATE_TREE_HEIGHT_DENEB)
55124
}

beacon/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const (
44
BEACON_BLOCK_HEADER_NUM_FIELDS = uint64(5)
55

66
BEACON_BLOCK_HEADER_TREE_HEIGHT = uint64(3)
7-
BEACON_STATE_TREE_HEIGHT = uint64(5)
7+
BEACON_STATE_TREE_HEIGHT_DENEB = uint64(5)
8+
BEACON_STATE_TREE_HEIGHT_PECTRA = uint64(6)
89
BALANCE_TREE_HEIGHT = uint64(38)
910
VALIDATOR_TREE_HEIGHT = uint64(40)
1011

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)