forked from prysmaticlabs/prysm
/
attestations.go
277 lines (239 loc) · 8.32 KB
/
attestations.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package stateutil
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
// EpochAttestationsRoot computes the HashTreeRoot Merkleization of
// a list of pending attestation values according to the eth2
// Simple Serialize specification.
func EpochAttestationsRoot(atts []*pb.PendingAttestation) ([32]byte, error) {
if featureconfig.Get().EnableSSZCache {
return cachedHasher.epochAttestationsRoot(atts)
}
return nocachedHasher.epochAttestationsRoot(atts)
}
// PendingAttestationRoot describes a method from which the hash tree root
// of a pending attestation is returned.
func PendingAttestationRoot(hasher HashFn, att *pb.PendingAttestation) ([32]byte, error) {
fieldRoots := [][32]byte{}
if att != nil {
// Bitfield.
aggregationRoot, err := bitlistRoot(hasher, att.AggregationBits, params.BeaconConfig().MaxValidatorsPerCommittee)
if err != nil {
return [32]byte{}, err
}
// Attestation data.
attDataRoot, err := attestationDataRoot(hasher, att.Data)
if err != nil {
return [32]byte{}, err
}
inclusionBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(inclusionBuf, att.InclusionDelay)
// Inclusion delay.
inclusionRoot := bytesutil.ToBytes32(inclusionBuf)
proposerBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(proposerBuf, att.ProposerIndex)
// Proposer index.
proposerRoot := bytesutil.ToBytes32(proposerBuf)
fieldRoots = [][32]byte{aggregationRoot, attDataRoot, inclusionRoot, proposerRoot}
}
return bitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func marshalAttestationData(data *ethpb.AttestationData) []byte {
enc := make([]byte, 128)
if data != nil {
// Slot.
slotBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(slotBuf, data.Slot)
copy(enc[0:8], slotBuf)
// Committee index.
indexBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(indexBuf, data.CommitteeIndex)
copy(enc[8:16], indexBuf)
copy(enc[16:48], data.BeaconBlockRoot)
// Source epoch and root.
if data.Source != nil {
sourceEpochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(sourceEpochBuf, data.Source.Epoch)
copy(enc[48:56], sourceEpochBuf)
copy(enc[56:88], data.Source.Root)
}
// Target.
if data.Target != nil {
targetEpochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(targetEpochBuf, data.Target.Epoch)
copy(enc[88:96], targetEpochBuf)
copy(enc[96:128], data.Target.Root)
}
}
return enc
}
func attestationRoot(hasher HashFn, att *ethpb.Attestation) ([32]byte, error) {
fieldRoots := make([][32]byte, 3)
// Bitfield.
aggregationRoot, err := bitlistRoot(hasher, att.AggregationBits, params.BeaconConfig().MaxValidatorsPerCommittee)
if err != nil {
return [32]byte{}, err
}
fieldRoots[0] = aggregationRoot
dataRoot, err := attestationDataRoot(hasher, att.Data)
if err != nil {
return [32]byte{}, err
}
fieldRoots[1] = dataRoot
signatureBuf := bytesutil.ToBytes96(att.Signature)
packedSig, err := pack([][]byte{signatureBuf[:]})
if err != nil {
return [32]byte{}, err
}
sigRoot, err := bitwiseMerkleize(hasher, packedSig, uint64(len(packedSig)), uint64(len(packedSig)))
if err != nil {
return [32]byte{}, err
}
fieldRoots[2] = sigRoot
return bitwiseMerkleizeArrays(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func blockAttestationRoot(atts []*ethpb.Attestation) ([32]byte, error) {
hasher := hashutil.CustomSHA256Hasher()
roots := make([][]byte, len(atts))
for i := 0; i < len(atts); i++ {
pendingRoot, err := attestationRoot(hasher, atts[i])
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
}
roots[i] = pendingRoot[:]
}
attsRootsRoot, err := bitwiseMerkleize(
hasher,
roots,
uint64(len(roots)),
params.BeaconConfig().MaxAttestations,
)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute block attestations merkleization")
}
attsLenBuf := new(bytes.Buffer)
if err := binary.Write(attsLenBuf, binary.LittleEndian, uint64(len(atts))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal epoch attestations length")
}
// We need to mix in the length of the slice.
attsLenRoot := make([]byte, 32)
copy(attsLenRoot, attsLenBuf.Bytes())
res := mixInLength(attsRootsRoot, attsLenRoot)
return res, nil
}
func attestationDataRoot(hasher HashFn, data *ethpb.AttestationData) ([32]byte, error) {
fieldRoots := make([][]byte, 5)
if data != nil {
// Slot.
slotBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(slotBuf, data.Slot)
slotRoot := bytesutil.ToBytes32(slotBuf)
fieldRoots[0] = slotRoot[:]
// CommitteeIndex.
indexBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(indexBuf, data.CommitteeIndex)
interRoot := bytesutil.ToBytes32(indexBuf)
fieldRoots[1] = interRoot[:]
// Beacon block root.
blockRoot := bytesutil.ToBytes32(data.BeaconBlockRoot)
fieldRoots[2] = blockRoot[:]
// Source
sourceRoot, err := CheckpointRoot(hasher, data.Source)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute source checkpoint merkleization")
}
fieldRoots[3] = sourceRoot[:]
// Target
targetRoot, err := CheckpointRoot(hasher, data.Target)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute target checkpoint merkleization")
}
fieldRoots[4] = targetRoot[:]
}
return bitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func (h *stateRootHasher) pendingAttestationRoot(hasher HashFn, att *pb.PendingAttestation) ([32]byte, error) {
// Marshal attestation to determine if it exists in the cache.
enc := make([]byte, 2192)
fieldRoots := make([][]byte, 4)
if att != nil {
copy(enc[0:2048], att.AggregationBits)
inclusionBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(inclusionBuf, att.InclusionDelay)
copy(enc[2048:2056], inclusionBuf)
attDataBuf := marshalAttestationData(att.Data)
copy(enc[2056:2184], attDataBuf)
proposerBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(proposerBuf, att.ProposerIndex)
copy(enc[2184:2192], proposerBuf)
// Check if it exists in cache:
if h.rootsCache != nil {
if found, ok := h.rootsCache.Get(string(enc)); found != nil && ok {
return found.([32]byte), nil
}
}
// Bitfield.
aggregationRoot, err := bitlistRoot(hasher, att.AggregationBits, 2048)
if err != nil {
return [32]byte{}, err
}
fieldRoots[0] = aggregationRoot[:]
// Attestation data.
attDataRoot, err := attestationDataRoot(hasher, att.Data)
if err != nil {
return [32]byte{}, err
}
fieldRoots[1] = attDataRoot[:]
// Inclusion delay.
inclusionRoot := bytesutil.ToBytes32(inclusionBuf)
fieldRoots[2] = inclusionRoot[:]
// Proposer index.
proposerRoot := bytesutil.ToBytes32(proposerBuf)
fieldRoots[3] = proposerRoot[:]
}
res, err := bitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
if err != nil {
return [32]byte{}, err
}
if h.rootsCache != nil {
h.rootsCache.Set(string(enc), res, 32)
}
return res, nil
}
func (h *stateRootHasher) epochAttestationsRoot(atts []*pb.PendingAttestation) ([32]byte, error) {
hasher := hashutil.CustomSHA256Hasher()
roots := make([][]byte, len(atts))
for i := 0; i < len(atts); i++ {
pendingRoot, err := h.pendingAttestationRoot(hasher, atts[i])
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not attestation merkleization")
}
roots[i] = pendingRoot[:]
}
attsRootsRoot, err := bitwiseMerkleize(
hasher,
roots,
uint64(len(roots)),
params.BeaconConfig().MaxAttestations*params.BeaconConfig().SlotsPerEpoch,
)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute epoch attestations merkleization")
}
attsLenBuf := new(bytes.Buffer)
if err := binary.Write(attsLenBuf, binary.LittleEndian, uint64(len(atts))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal epoch attestations length")
}
// We need to mix in the length of the slice.
attsLenRoot := make([]byte, 32)
copy(attsLenRoot, attsLenBuf.Bytes())
res := mixInLength(attsRootsRoot, attsLenRoot)
return res, nil
}