forked from Layr-Labs/eigenpod-proofs-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigen_pod_proofs.go
177 lines (151 loc) · 5.59 KB
/
eigen_pod_proofs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package eigenpodproofs
import (
"errors"
"time"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
expirable "github.com/hashicorp/golang-lru/v2/expirable"
beacon "github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
)
const (
BEACON_STATE_ROOT_PREFIX = "BEACON_STATE_ROOT_"
BEACON_STATE_TOP_LEVEL_ROOTS_PREFIX = "BEACON_STATE_TOP_LEVEL_ROOTS_"
VALIDATOR_TREE_PREFIX = "VALIDATOR_TREE_"
MAX_ORACLE_STATE_CACHE_SIZE = 2000000
)
type EigenPodProofs struct {
chainID uint64
oracleStateRootCache *expirable.LRU[uint64, phase0.Root]
oracleStateTopLevelRootsCache *expirable.LRU[uint64, *beacon.BeaconStateTopLevelRoots]
oracleStateValidatorTreeCache *expirable.LRU[uint64, [][]phase0.Root]
oracleStateCacheExpirySeconds int
}
func NewEigenPodProofs(chainID uint64, oracleStateCacheExpirySeconds int) (*EigenPodProofs, error) {
if chainID != 1 && chainID != 5 && chainID != 17000 {
return nil, errors.New("chainID not supported")
}
oracleStateRootCache := expirable.NewLRU[uint64, phase0.Root](MAX_ORACLE_STATE_CACHE_SIZE, nil, time.Duration(oracleStateCacheExpirySeconds)*time.Second)
oracleStateTopLevelRootsCache := expirable.NewLRU[uint64, *beacon.BeaconStateTopLevelRoots](MAX_ORACLE_STATE_CACHE_SIZE, nil, time.Duration(oracleStateCacheExpirySeconds)*time.Second)
oracleStateValidatorTreeCache := expirable.NewLRU[uint64, [][]phase0.Root](MAX_ORACLE_STATE_CACHE_SIZE, nil, time.Duration(oracleStateCacheExpirySeconds)*time.Second)
return &EigenPodProofs{
chainID: chainID,
oracleStateRootCache: oracleStateRootCache,
oracleStateTopLevelRootsCache: oracleStateTopLevelRootsCache,
oracleStateValidatorTreeCache: oracleStateValidatorTreeCache,
oracleStateCacheExpirySeconds: oracleStateCacheExpirySeconds,
}, nil
}
func (epp *EigenPodProofs) ComputeBeaconStateRoot(beaconState *deneb.BeaconState) (phase0.Root, error) {
beaconStateRoot, err := epp.loadOrComputeBeaconStateRoot(
beaconState.Slot,
func() (phase0.Root, error) {
stateRoot, err := beaconState.HashTreeRoot()
if err != nil {
return phase0.Root{}, err
}
return stateRoot, nil
},
)
if err != nil {
return phase0.Root{}, err
}
return beaconStateRoot, nil
}
func (epp *EigenPodProofs) ComputeBeaconStateTopLevelRoots(beaconState *spec.VersionedBeaconState) (*beacon.BeaconStateTopLevelRoots, error) {
//get the versioned beacon state's slot
slot, err := beaconState.Slot()
if err != nil {
return nil, err
}
beaconStateTopLevelRoots, err := epp.loadOrComputeBeaconStateTopLevelRoots(
slot,
func() (*beacon.BeaconStateTopLevelRoots, error) {
beaconStateTopLevelRoots, err := epp.ComputeVersionedBeaconStateTopLevelRoots(beaconState)
if err != nil {
return nil, err
}
return beaconStateTopLevelRoots, nil
},
)
if err != nil {
return nil, err
}
return beaconStateTopLevelRoots, err
}
func (epp *EigenPodProofs) ComputeVersionedBeaconStateTopLevelRoots(beaconState *spec.VersionedBeaconState) (*beacon.BeaconStateTopLevelRoots, error) {
switch beaconState.Version {
case spec.DataVersionDeneb:
return beacon.ComputeBeaconStateTopLevelRootsDeneb(beaconState.Deneb)
case spec.DataVersionCapella:
return beacon.ComputeBeaconStateTopLevelRootsCapella(beaconState.Capella)
default:
return nil, errors.New("unsupported beacon state version")
}
}
func (epp *EigenPodProofs) ComputeValidatorTree(slot phase0.Slot, validators []*phase0.Validator) ([][]phase0.Root, error) {
validatorTree, err := epp.loadOrComputeValidatorTree(
slot,
func() ([][]phase0.Root, error) {
// compute the validator tree leaves
validatorLeaves, err := beacon.ComputeValidatorTreeLeaves(validators)
if err != nil {
return nil, err
}
// compute the validator tree
validatorTree, err := common.ComputeMerkleTreeFromLeaves(validatorLeaves, beacon.ValidatorListMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
// cache the validator tree
return validatorTree, nil
},
)
if err != nil {
return nil, err
}
return validatorTree, nil
}
func (epp *EigenPodProofs) loadOrComputeBeaconStateRoot(slot phase0.Slot, getData func() (phase0.Root, error)) (phase0.Root, error) {
root, found := epp.oracleStateRootCache.Get(uint64(slot))
if found {
return root, nil
}
// compute the data
root, err := getData()
if err != nil {
return phase0.Root{}, err
}
// cache the beacon state root
epp.oracleStateRootCache.Add(uint64(slot), root)
return root, nil
}
func (epp *EigenPodProofs) loadOrComputeBeaconStateTopLevelRoots(slot phase0.Slot, getData func() (*beacon.BeaconStateTopLevelRoots, error)) (*beacon.BeaconStateTopLevelRoots, error) {
topLevelRoots, found := epp.oracleStateTopLevelRootsCache.Get(uint64(slot))
if found {
return topLevelRoots, nil
}
// compute the data
topLevelRoots, err := getData()
if err != nil {
return nil, err
}
// cache the beacon state root
epp.oracleStateTopLevelRootsCache.Add(uint64(slot), topLevelRoots)
return topLevelRoots, nil
}
func (epp *EigenPodProofs) loadOrComputeValidatorTree(slot phase0.Slot, getData func() ([][]phase0.Root, error)) ([][]phase0.Root, error) {
validatorTree, found := epp.oracleStateValidatorTreeCache.Get(uint64(slot))
if found {
return validatorTree, nil
}
// compute the data
validatorTree, err := getData()
if err != nil {
return nil, err
}
// cache the beacon state root
epp.oracleStateValidatorTreeCache.Add(uint64(slot), validatorTree)
return validatorTree, nil
}