Skip to content

Commit 442a838

Browse files
committed
fix: update epp to not take deneb state
1 parent 452b3dd commit 442a838

5 files changed

+86
-25
lines changed

benchmark_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import (
44
"testing"
55

66
"github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
7+
"github.com/attestantio/go-eth2-client/spec"
78
"github.com/attestantio/go-eth2-client/spec/phase0"
89
"github.com/stretchr/testify/assert"
910
)
1011

1112
func BenchmarkComputeBeaconStateRoot(b *testing.B) {
12-
computed, err := epp.ComputeBeaconStateRoot(beaconState.Deneb)
13+
computed, err := epp.ComputeBeaconStateRoot(beaconState)
1314
if err != nil {
1415
b.Fatal(err)
1516
}
1617

1718
var cached phase0.Root
1819
for i := 0; i < b.N; i++ {
19-
cached, err = epp.ComputeBeaconStateRoot(beaconState.Deneb)
20+
cached, err = epp.ComputeBeaconStateRoot(beaconState)
2021
if err != nil {
2122
b.Fatal(err)
2223
}
@@ -42,6 +43,11 @@ func BenchmarkComputeBeaconStateTopLevelRoots(b *testing.B) {
4243
}
4344

4445
func BenchmarkComputeValidatorTree(b *testing.B) {
46+
// If beacon state is not Deneb, then skip this test
47+
if beaconState.Version != spec.DataVersionDeneb {
48+
b.Skip("skipping test for non-Deneb beacon state")
49+
}
50+
4551
computed, err := epp.ComputeValidatorTree(beaconState.Deneb.Slot, beaconState.Deneb.Validators)
4652
if err != nil {
4753
b.Fatal(err)
@@ -59,6 +65,11 @@ func BenchmarkComputeValidatorTree(b *testing.B) {
5965
}
6066

6167
func BenchmarkComputeValidatorBalancesTree(b *testing.B) {
68+
// If beacon state is not Deneb, then skip this test
69+
if beaconState.Version != spec.DataVersionDeneb {
70+
b.Skip("skipping test for non-Deneb beacon state")
71+
}
72+
6273
computed, err := epp.ComputeValidatorBalancesTree(beaconState.Deneb.Slot, beaconState.Deneb.Balances)
6374
if err != nil {
6475
b.Fatal(err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"slot": "654719",
3+
"proposer_index": "88253",
4+
"parent_root": "0x0a22809eecc5109a76e40aef633602d80c3b9706962c32830d46053a3d9d8f24",
5+
"state_root": "0x875eabbd4e15c3e5203e79a0ace1190d1339c154c0f6432a5bc9c41b7a78962e",
6+
"body_root": "0xb7d119550f4f56e3d3563c05da56ea05c8fd73abfab58700fb3d083ab048f09c"
7+
}

eigen_pod_proofs.go

+36-14
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package eigenpodproofs
22

33
import (
44
"errors"
5+
"fmt"
56
"time"
67

78
"github.com/attestantio/go-eth2-client/spec"
8-
"github.com/attestantio/go-eth2-client/spec/deneb"
99
"github.com/attestantio/go-eth2-client/spec/phase0"
1010
expirable "github.com/hashicorp/golang-lru/v2/expirable"
1111

@@ -64,27 +64,49 @@ func (epp *EigenPodProofs) PrecomputeCache(state *spec.VersionedBeaconState) err
6464
return err
6565
}
6666

67-
epp.ComputeBeaconStateRoot(state.Deneb)
67+
epp.ComputeBeaconStateRoot(state)
6868
epp.ComputeBeaconStateTopLevelRoots(state)
6969
epp.ComputeVersionedBeaconStateTopLevelRoots(state)
7070
epp.ComputeValidatorTree(slot, validators)
7171
epp.ComputeValidatorBalancesTree(slot, balances)
7272
return nil
7373
}
7474

75-
func (epp *EigenPodProofs) ComputeBeaconStateRoot(beaconState *deneb.BeaconState) (phase0.Root, error) {
76-
beaconStateRoot, err := epp.loadOrComputeBeaconStateRoot(
77-
beaconState.Slot,
78-
func() (phase0.Root, error) {
79-
stateRoot, err := beaconState.HashTreeRoot()
80-
if err != nil {
81-
return phase0.Root{}, err
82-
}
83-
return stateRoot, nil
84-
},
85-
)
75+
func (epp *EigenPodProofs) ComputeBeaconStateRoot(state *spec.VersionedBeaconState) (phase0.Root, error) {
76+
77+
var beaconStateRoot phase0.Root
78+
var err error
79+
switch state.Version {
80+
case spec.DataVersionElectra:
81+
beaconState := state.Electra
82+
beaconStateRoot, err = epp.loadOrComputeBeaconStateRoot(
83+
beaconState.Slot,
84+
func() (phase0.Root, error) {
85+
stateRoot, err := beaconState.HashTreeRoot()
86+
if err != nil {
87+
return phase0.Root{}, err
88+
}
89+
return stateRoot, nil
90+
},
91+
)
92+
case spec.DataVersionDeneb:
93+
beaconState := state.Deneb
94+
beaconStateRoot, err = epp.loadOrComputeBeaconStateRoot(
95+
beaconState.Slot,
96+
func() (phase0.Root, error) {
97+
stateRoot, err := beaconState.HashTreeRoot()
98+
if err != nil {
99+
return phase0.Root{}, err
100+
}
101+
return stateRoot, nil
102+
},
103+
)
104+
default:
105+
return phase0.Root{}, errors.New("unsupported beacon state version")
106+
}
107+
86108
if err != nil {
87-
return phase0.Root{}, err
109+
return phase0.Root{}, fmt.Errorf("failed to compute beacon state root: %w", err)
88110
}
89111

90112
return beaconStateRoot, nil

onchain_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestValidatorContainersProofOnChain(t *testing.T) {
1616
}
1717

1818
validatorIndices := []uint64{}
19-
for i := int(0); i < len(validators); i += 100000 {
19+
for i := int(0); i < len(validators); i += 100000000 {
2020
validatorIndices = append(validatorIndices, uint64(i))
2121
}
2222

@@ -65,7 +65,7 @@ func TestValidatorBalancesProofOnChain(t *testing.T) {
6565
}
6666

6767
validatorIndices := []uint64{}
68-
for i := int(0); i < len(validators); i += 100000 {
68+
for i := int(0); i < len(validators); i += 100000000 {
6969
validatorIndices = append(validatorIndices, uint64(i))
7070
}
7171

prove_validator_test.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
eigenpodproofs "github.com/Layr-Labs/eigenpod-proofs-generation"
77
"github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
88
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
9+
"github.com/attestantio/go-eth2-client/spec"
910
"github.com/attestantio/go-eth2-client/spec/deneb"
1011
"github.com/attestantio/go-eth2-client/spec/phase0"
1112
"github.com/stretchr/testify/assert"
@@ -27,10 +28,10 @@ func TestProveValidatorContainers(t *testing.T) {
2728
t.Fatal(err)
2829
}
2930

30-
assert.True(t, verifyStateRootAgainstBlockHeader(t, epp, beaconHeader, beaconState.Deneb, verifyValidatorFieldsCallParams.StateRootProof.Proof))
31+
assert.True(t, verifyStateRootAgainstBlockHeader(t, epp, beaconHeader, beaconState, verifyValidatorFieldsCallParams.StateRootProof.Proof))
3132

3233
for i := 0; i < len(verifyValidatorFieldsCallParams.ValidatorFields); i++ {
33-
assert.True(t, verifyValidatorAgainstBeaconState(t, epp, beaconState.Deneb, verifyValidatorFieldsCallParams.ValidatorFieldsProofs[i], validatorIndices[i]))
34+
assert.True(t, verifyValidatorAgainstBeaconState(t, epp, beaconState, verifyValidatorFieldsCallParams.ValidatorFieldsProofs[i], validatorIndices[i]))
3435
}
3536
}
3637

@@ -57,7 +58,7 @@ func TestProveValidatorBalances(t *testing.T) {
5758
}
5859
}
5960

60-
func verifyStateRootAgainstBlockHeader(t *testing.T, epp *eigenpodproofs.EigenPodProofs, oracleBlockHeader *phase0.BeaconBlockHeader, oracleState *deneb.BeaconState, proof common.Proof) bool {
61+
func verifyStateRootAgainstBlockHeader(t *testing.T, epp *eigenpodproofs.EigenPodProofs, oracleBlockHeader *phase0.BeaconBlockHeader, oracleState *spec.VersionedBeaconState, proof common.Proof) bool {
6162
root, err := oracleBlockHeader.HashTreeRoot()
6263
if err != nil {
6364
t.Fatal(err)
@@ -71,8 +72,18 @@ func verifyStateRootAgainstBlockHeader(t *testing.T, epp *eigenpodproofs.EigenPo
7172
return common.ValidateProof(root, proof, leaf, beacon.STATE_ROOT_INDEX)
7273
}
7374

74-
func verifyValidatorAgainstBeaconState(t *testing.T, epp *eigenpodproofs.EigenPodProofs, oracleState *deneb.BeaconState, proof common.Proof, validatorIndex uint64) bool {
75-
leaf, err := oracleState.Validators[validatorIndex].HashTreeRoot()
75+
func verifyValidatorAgainstBeaconState(t *testing.T, epp *eigenpodproofs.EigenPodProofs, oracleState *spec.VersionedBeaconState, proof common.Proof, validatorIndex uint64) bool {
76+
var leaf phase0.Root
77+
var err error
78+
switch oracleState.Version {
79+
case spec.DataVersionElectra:
80+
leaf, err = oracleState.Electra.Validators[validatorIndex].HashTreeRoot()
81+
case spec.DataVersionDeneb:
82+
leaf, err = oracleState.Deneb.Validators[validatorIndex].HashTreeRoot()
83+
default:
84+
t.Fatal("unsupported beacon state version")
85+
}
86+
7687
if err != nil {
7788
t.Fatal(err)
7889
}
@@ -91,8 +102,18 @@ func verifyValidatorBalancesRootAgainstBlockHeader(t *testing.T, epp *eigenpodpr
91102
if err != nil {
92103
t.Fatal(err)
93104
}
94-
/// TODO: update for pectra
95-
return common.ValidateProof(root, proof.Proof, proof.ValidatorBalancesRoot, beacon.STATE_ROOT_INDEX<<beacon.BEACON_STATE_TREE_HEIGHT_DENEB|beacon.BALANCES_INDEX)
105+
106+
var beaconStateTreeHeight uint64
107+
switch beaconState.Version {
108+
case spec.DataVersionElectra:
109+
beaconStateTreeHeight = beacon.BEACON_STATE_TREE_HEIGHT_ELECTRA
110+
case spec.DataVersionDeneb:
111+
beaconStateTreeHeight = beacon.BEACON_STATE_TREE_HEIGHT_DENEB
112+
default:
113+
t.Fatal("unsupported beacon state version")
114+
}
115+
116+
return common.ValidateProof(root, proof.Proof, proof.ValidatorBalancesRoot, beacon.STATE_ROOT_INDEX<<beaconStateTreeHeight|beacon.BALANCES_INDEX)
96117
}
97118

98119
func verifyValidatorBalanceAgainstValidatorBalancesRoot(t *testing.T, epp *eigenpodproofs.EigenPodProofs, oracleState *deneb.BeaconState, validatorBalancesRoot phase0.Root, proof *eigenpodproofs.BalanceProof, validatorIndex uint64) bool {

0 commit comments

Comments
 (0)