-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstructs_test.go
376 lines (325 loc) · 9.17 KB
/
structs_test.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package spectests
import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"
ssz "github.com/ferranbt/fastssz"
"github.com/golang/snappy"
"github.com/prysmaticlabs/gohashtree"
"gopkg.in/yaml.v2"
)
type codec interface {
ssz.Marshaler
ssz.Unmarshaler
ssz.HashRoot
}
type fork string
const (
phase0 fork = "phase0"
altair fork = "altair"
bellatrix fork = "bellatrix"
capella fork = "capella"
deneb fork = "deneb"
)
type testCallback func(fork fork) codec
var codecs = map[string]testCallback{
"AttestationData": func(fork fork) codec { return new(AttestationData) },
"Checkpoint": func(fork fork) codec { return new(Checkpoint) },
"AggregateAndProof": func(fork fork) codec { return new(AggregateAndProof) },
"Attestation": func(fork fork) codec { return new(Attestation) },
"AttesterSlashing": func(fork fork) codec { return new(AttesterSlashing) },
"BeaconState": func(fork fork) codec {
if fork == phase0 {
return new(BeaconState)
} else if fork == altair {
return new(BeaconStateAltair)
} else if fork == bellatrix {
return new(BeaconStateBellatrix)
} else if fork == capella {
return new(BeaconStateCapella)
}
return nil
},
"BeaconBlock": func(fork fork) codec {
if fork == phase0 {
return new(BeaconBlock)
} else if fork == capella {
return new(BeaconBlockCapella)
}
return nil
},
"BeaconBlockBody": func(fork fork) codec {
if fork == phase0 {
return new(BeaconBlockBodyPhase0)
} else if fork == altair {
return new(BeaconBlockBodyAltair)
} else if fork == bellatrix {
return new(BeaconBlockBodyBellatrix)
} else if fork == capella {
return new(BeaconBlockBodyCapella)
}
return nil
},
"BeaconBlockHeader": func(fork fork) codec { return new(BeaconBlockHeader) },
"Deposit": func(fork fork) codec { return new(Deposit) },
"DepositData": func(fork fork) codec { return new(DepositData) },
"DepositMessage": func(fork fork) codec { return new(DepositMessage) },
"Eth1Block": func(fork fork) codec { return new(Eth1Block) },
"Eth1Data": func(fork fork) codec { return new(Eth1Data) },
"Fork": func(fork fork) codec { return new(Fork) },
"HistoricalBatch": func(fork fork) codec { return new(HistoricalBatch) },
"IndexedAttestation": func(fork fork) codec { return new(IndexedAttestation) },
"PendingAttestation": func(fork fork) codec { return new(PendingAttestation) },
"ProposerSlashing": func(fork fork) codec { return new(ProposerSlashing) },
"SignedBeaconBlock": func(fork fork) codec {
if fork == phase0 {
return new(SignedBeaconBlock)
} else if fork == capella {
return new(SignedBeaconBlockCapella)
}
return nil
},
"SignedBeaconBlockHeader": func(fork fork) codec { return new(SignedBeaconBlockHeader) },
"SignedVoluntaryExit": func(fork fork) codec { return new(SignedVoluntaryExit) },
"SigningRoot": func(fork fork) codec { return new(SigningRoot) },
"Validator": func(fork fork) codec { return new(Validator) },
"VoluntaryExit": func(fork fork) codec { return new(VoluntaryExit) },
"ErrorResponse": func(fork fork) codec { return new(ErrorResponse) },
"SyncCommittee": func(fork fork) codec {
return new(SyncCommittee)
},
"SyncAggregate": func(fork fork) codec {
return new(SyncAggregate)
},
"ExecutionPayload": func(fork fork) codec {
if fork == deneb {
return new(ExecutionPayloadDeneb)
} else if fork == capella {
return new(ExecutionPayloadCapella)
}
return new(ExecutionPayload)
},
"ExecutionPayloadHeader": func(fork fork) codec {
if fork == deneb {
return new(ExecutionPayloadHeaderDeneb)
} else if fork == capella {
return new(ExecutionPayloadHeaderCapella)
}
return new(ExecutionPayloadHeader)
},
"BLSToExecutionChange": func(f fork) codec { return new(BLSToExecutionChange) },
"HistoricalSummary": func(f fork) codec { return new(HistoricalSummary) },
"SignedBLSToExecutionChange": func(f fork) codec { return new(SignedBLSToExecutionChange) },
"Withdrawal": func(f fork) codec { return new(Withdrawal) },
}
func testSpecFork(t *testing.T, fork fork) {
files := readDir(t, filepath.Join(testsPath, "/mainnet/"+string(fork)+"/ssz_static"))
for _, f := range files {
spl := strings.Split(f, "/")
name := spl[len(spl)-1]
base, ok := codecs[name]
if !ok {
continue
}
t.Run(name, func(t *testing.T) {
files := readDir(t, filepath.Join(f, "ssz_random"))
for _, f := range files {
checkSSZEncoding(t, fork, f, name, base)
}
})
}
}
func TestSpec_Phase0(t *testing.T) {
testSpecFork(t, phase0)
}
func TestSpec_Altair(t *testing.T) {
testSpecFork(t, altair)
}
func TestSpec_Bellatrix(t *testing.T) {
testSpecFork(t, bellatrix)
}
func TestSpec_Capella(t *testing.T) {
testSpecFork(t, capella)
}
func TestSpec_Deneb(t *testing.T) {
testSpecFork(t, deneb)
}
func checkSSZEncoding(t *testing.T, fork fork, fileName, structName string, base testCallback) {
obj := base(fork)
if obj == nil {
// skip
return
}
output := readValidGenericSSZ(t, fileName, &obj)
fatal := func(errHeader string, err error) {
t.Fatalf("%s spec file=%s, struct=%s, err=%v", errHeader, fileName, structName, err)
}
// Marshal
res, err := obj.MarshalSSZTo(nil)
if err != nil {
fatal("marshalSSZ", err)
}
if !bytes.Equal(res, output.ssz) {
fatal("marshalSSZ_equal", fmt.Errorf("bad marshal"))
}
// Unmarshal
obj2 := base(fork)
if err := obj2.UnmarshalSSZ(output.ssz); err != nil {
fatal("UnmarshalSSZ", err)
}
if !deepEqual(obj, obj2) {
fatal("UnmarshalSSZ_equal", fmt.Errorf("bad unmarshal"))
}
// Root
root, err := obj.HashTreeRoot()
if err != nil {
fatal("HashTreeRoot", err)
}
if !bytes.Equal(root[:], output.root) {
fatal("HashTreeRoot_equal", fmt.Errorf("bad root"))
}
// Root with gohashtree
hh := ssz.NewHasherWithHashFn(gohashtree.HashByteSlice)
if err := obj.HashTreeRootWith(hh); err != nil {
fatal("GohashtreeRoot", err)
}
gohashTreeRoot, err := hh.HashRoot()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(gohashTreeRoot[:], output.root) {
fatal("GohashtreeRoot_equal", fmt.Errorf("bad root"))
}
// Proof
node, err := obj.GetTree()
if err != nil {
fatal("Tree", err)
}
nodeRoot := node.Hash()
if !bytes.Equal(nodeRoot, root[:]) {
fatal("Tree_equal", fmt.Errorf("bad node"))
}
}
const benchmarkTestCase = "../eth2.0-spec-tests/tests/mainnet/phase0/ssz_static/BeaconBlock/ssz_random/case_4"
func BenchmarkMarshal_Fast(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.MarshalSSZ()
}
}
func BenchmarkMarshal_SuperFast(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
buf := make([]byte, 0)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, _ = obj.MarshalSSZTo(buf[:0])
}
}
func BenchmarkUnMarshal_Fast(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
dst, err := obj.MarshalSSZ()
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj2 := new(BeaconBlock)
if err := obj2.UnmarshalSSZ(dst); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkHashTreeRoot_Fast(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
b.ReportAllocs()
b.ResetTimer()
hh := ssz.NewHasher()
for i := 0; i < b.N; i++ {
obj.HashTreeRootWith(hh)
hh.Reset()
}
}
func BenchmarkHashTreeRoot_SuperFast(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
b.ReportAllocs()
b.ResetTimer()
hh := ssz.NewHasherWithHashFn(gohashtree.HashByteSlice)
for i := 0; i < b.N; i++ {
obj.HashTreeRootWith(hh)
hh.Reset()
}
}
func BenchmarkProof_Tree(b *testing.B) {
obj := new(BeaconBlock)
readValidGenericSSZ(nil, benchmarkTestCase, obj)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.GetTree()
}
}
const (
testsPath = "../eth2.0-spec-tests/tests"
serializedFile = "serialized.ssz_snappy"
valueFile = "value.yaml"
rootsFile = "roots.yaml"
)
func readDir(t *testing.T, path string) []string {
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatal(err)
}
res := []string{}
for _, f := range files {
res = append(res, filepath.Join(path, f.Name()))
}
return res
}
type output struct {
root []byte
ssz []byte
}
func readValidGenericSSZ(t *testing.T, path string, obj interface{}) *output {
serializedSnappy, err := ioutil.ReadFile(filepath.Join(path, serializedFile))
if err != nil {
t.Fatal(err)
}
serialized, err := snappy.Decode(nil, serializedSnappy)
if err != nil {
t.Fatal(err)
}
raw, err := ioutil.ReadFile(filepath.Join(path, valueFile))
if err != nil {
t.Fatal(err)
}
raw2, err := ioutil.ReadFile(filepath.Join(path, rootsFile))
if err != nil {
t.Fatal(err)
}
// Decode ssz root
var out map[string]string
if err := yaml.Unmarshal(raw2, &out); err != nil {
t.Fatal(err)
}
root, err := hex.DecodeString(out["root"][2:])
if err != nil {
t.Fatal(err)
}
if err := ssz.UnmarshalSSZTest(raw, obj); err != nil {
t.Fatal(err)
}
return &output{root: root, ssz: serialized}
}