forked from Layr-Labs/eigenpod-proofs-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprove_validator.go
110 lines (90 loc) · 4.35 KB
/
prove_validator.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
package eigenpodproofs
import (
"math/big"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/phase0"
beacon "github.com/Layr-Labs/eigenpod-proofs-generation/beacon"
"github.com/Layr-Labs/eigenpod-proofs-generation/common"
)
type VerifyValidatorFieldsCallParams struct {
OracleTimestamp uint64 `json:"oracleTimestamp"`
StateRootProof *StateRootProof `json:"stateRootProof"`
ValidatorIndices []uint64 `json:"validatorIndices"`
ValidatorFieldsProofs []common.Proof `json:"validatorFieldsProofs"`
ValidatorFields [][]Bytes32 `json:"validatorFields"`
}
// We define these two aliases here, since calls to verifyWithdrawalCredentials and verifyBalanceUpdates use the same inputs
type VerifyWithdrawalCredentialsCallParams = VerifyValidatorFieldsCallParams
type VerifyBalanceUpdatesCallParams = VerifyValidatorFieldsCallParams
func (epp *EigenPodProofs) ProveValidatorContainers(oracleBlockHeader *phase0.BeaconBlockHeader, oracleBeaconState *spec.VersionedBeaconState, validatorIndices []uint64) (*VerifyValidatorFieldsCallParams, error) {
oracleBeaconStateSlot, err := oracleBeaconState.Slot()
if err != nil {
return nil, err
}
oracleBeaconStateValidators, err := oracleBeaconState.Validators()
if err != nil {
return nil, err
}
VerifyValidatorFieldsCallParams := &VerifyValidatorFieldsCallParams{}
VerifyValidatorFieldsCallParams.StateRootProof = &StateRootProof{}
// Get beacon state top level roots
beaconStateTopLevelRoots, err := epp.ComputeBeaconStateTopLevelRoots(oracleBeaconState)
if err != nil {
return nil, err
}
// Get beacon state root.
VerifyValidatorFieldsCallParams.StateRootProof.BeaconStateRoot = oracleBlockHeader.StateRoot
if err != nil {
return nil, err
}
VerifyValidatorFieldsCallParams.StateRootProof.StateRootProof, err = beacon.ProveStateRootAgainstBlockHeader(oracleBlockHeader)
if err != nil {
return nil, err
}
VerifyValidatorFieldsCallParams.OracleTimestamp, err = GetSlotTimestamp(oracleBeaconState, oracleBlockHeader)
if err != nil {
return nil, err
}
VerifyValidatorFieldsCallParams.ValidatorIndices = make([]uint64, len(validatorIndices))
VerifyValidatorFieldsCallParams.ValidatorFieldsProofs = make([]common.Proof, len(validatorIndices))
VerifyValidatorFieldsCallParams.ValidatorFields = make([][]Bytes32, len(validatorIndices))
for i, validatorIndex := range validatorIndices {
VerifyValidatorFieldsCallParams.ValidatorIndices[i] = validatorIndex
// prove the validator fields against the beacon state
VerifyValidatorFieldsCallParams.ValidatorFieldsProofs[i], err = epp.ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots, oracleBeaconStateSlot, oracleBeaconStateValidators, validatorIndex)
if err != nil {
return nil, err
}
VerifyValidatorFieldsCallParams.ValidatorFields[i] = ConvertValidatorToValidatorFields(oracleBeaconStateValidators[validatorIndex])
}
return VerifyValidatorFieldsCallParams, nil
}
func (epp *EigenPodProofs) ProveValidatorAgainstBeaconState(beaconStateTopLevelRoots *beacon.BeaconStateTopLevelRoots, oracleBeaconStateSlot phase0.Slot, oracleBeaconStateValidators []*phase0.Validator, validatorIndex uint64) (common.Proof, error) {
// prove the validator list against the beacon state
validatorListProof, err := beacon.ProveBeaconTopLevelRootAgainstBeaconState(beaconStateTopLevelRoots, beacon.ValidatorListIndex)
if err != nil {
return nil, err
}
// prove the validator root against the validator list root
validatorProof, err := epp.ProveValidatorAgainstValidatorList(oracleBeaconStateSlot, oracleBeaconStateValidators, validatorIndex)
if err != nil {
return nil, err
}
proof := append(validatorProof, validatorListProof...)
return proof, nil
}
func (epp *EigenPodProofs) ProveValidatorAgainstValidatorList(slot phase0.Slot, validators []*phase0.Validator, validatorIndex uint64) (common.Proof, error) {
validatorTree, err := epp.ComputeValidatorTree(slot, validators)
if err != nil {
return nil, err
}
proof, err := common.ComputeMerkleProofFromTree(validatorTree, validatorIndex, beacon.ValidatorListMerkleSubtreeNumLayers)
if err != nil {
return nil, err
}
//append the length of the validator array to the proof
//convert big endian to little endian
validatorListLenLE := BigToLittleEndian(big.NewInt(int64(len(validators))))
proof = append(proof, validatorListLenLE)
return proof, nil
}