/
runner.go
334 lines (303 loc) · 11.8 KB
/
runner.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
package forkchoice
import (
"context"
"fmt"
"os"
"path"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/golang/snappy"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/transition"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
state_native "github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/runtime/version"
"github.com/prysmaticlabs/prysm/v4/testing/require"
"github.com/prysmaticlabs/prysm/v4/testing/spectest/utils"
"github.com/prysmaticlabs/prysm/v4/testing/util"
)
func init() {
transition.SkipSlotCache.Disable()
}
// Run executes "forkchoice" and "sync" test.
func Run(t *testing.T, config string, fork int) {
runTest(t, config, fork, "fork_choice")
if fork >= version.Bellatrix {
runTest(t, config, fork, "sync")
}
}
func runTest(t *testing.T, config string, fork int, basePath string) {
require.NoError(t, utils.SetConfig(t, config))
testFolders, _ := utils.TestFolders(t, config, version.String(fork), basePath)
if len(testFolders) == 0 {
t.Fatalf("No test folders found for %s/%s/%s", config, version.String(fork), basePath)
}
for _, folder := range testFolders {
folderPath := path.Join(basePath, folder.Name(), "pyspec_tests")
testFolders, testsFolderPath := utils.TestFolders(t, config, version.String(fork), folderPath)
if len(testFolders) == 0 {
t.Fatalf("No test folders found for %s/%s/%s", config, version.String(fork), folderPath)
}
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
preStepsFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "steps.yaml")
require.NoError(t, err)
var steps []Step
require.NoError(t, utils.UnmarshalYaml(preStepsFile, &steps))
preBeaconStateFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "anchor_state.ssz_snappy")
require.NoError(t, err)
preBeaconStateSSZ, err := snappy.Decode(nil /* dst */, preBeaconStateFile)
require.NoError(t, err)
blockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "anchor_block.ssz_snappy")
require.NoError(t, err)
blockSSZ, err := snappy.Decode(nil /* dst */, blockFile)
require.NoError(t, err)
var beaconState state.BeaconState
var beaconBlock interfaces.ReadOnlySignedBeaconBlock
switch fork {
case version.Phase0:
beaconState = unmarshalPhase0State(t, preBeaconStateSSZ)
beaconBlock = unmarshalPhase0Block(t, blockSSZ)
case version.Altair:
beaconState = unmarshalAltairState(t, preBeaconStateSSZ)
beaconBlock = unmarshalAltairBlock(t, blockSSZ)
case version.Bellatrix:
beaconState = unmarshalBellatrixState(t, preBeaconStateSSZ)
beaconBlock = unmarshalBellatrixBlock(t, blockSSZ)
case version.Capella:
beaconState = unmarshalCapellaState(t, preBeaconStateSSZ)
beaconBlock = unmarshalCapellaBlock(t, blockSSZ)
case version.Deneb:
beaconState = unmarshalDenebState(t, preBeaconStateSSZ)
beaconBlock = unmarshalDenebBlock(t, blockSSZ)
default:
t.Fatalf("unknown fork version: %v", fork)
}
builder := NewBuilder(t, beaconState, beaconBlock)
for _, step := range steps {
if step.Tick != nil {
builder.Tick(t, int64(*step.Tick))
}
var beaconBlock interfaces.ReadOnlySignedBeaconBlock
if step.Block != nil {
blockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fmt.Sprint(*step.Block, ".ssz_snappy"))
require.NoError(t, err)
blockSSZ, err := snappy.Decode(nil /* dst */, blockFile)
require.NoError(t, err)
switch fork {
case version.Phase0:
beaconBlock = unmarshalSignedPhase0Block(t, blockSSZ)
case version.Altair:
beaconBlock = unmarshalSignedAltairBlock(t, blockSSZ)
case version.Bellatrix:
beaconBlock = unmarshalSignedBellatrixBlock(t, blockSSZ)
case version.Capella:
beaconBlock = unmarshalSignedCapellaBlock(t, blockSSZ)
case version.Deneb:
beaconBlock = unmarshalSignedDenebBlock(t, blockSSZ)
default:
t.Fatalf("unknown fork version: %v", fork)
}
}
runBlobStep(t, step.Blobs, beaconBlock, fork, folder, testsFolderPath, step.Proofs, builder)
if beaconBlock != nil {
if step.Valid != nil && !*step.Valid {
builder.InvalidBlock(t, beaconBlock)
} else {
builder.ValidBlock(t, beaconBlock)
}
}
if step.AttesterSlashing != nil {
slashingFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fmt.Sprint(*step.AttesterSlashing, ".ssz_snappy"))
require.NoError(t, err)
slashingSSZ, err := snappy.Decode(nil /* dst */, slashingFile)
require.NoError(t, err)
slashing := ðpb.AttesterSlashing{}
require.NoError(t, slashing.UnmarshalSSZ(slashingSSZ), "Failed to unmarshal")
builder.AttesterSlashing(slashing)
}
if step.Attestation != nil {
attFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fmt.Sprint(*step.Attestation, ".ssz_snappy"))
require.NoError(t, err)
attSSZ, err := snappy.Decode(nil /* dst */, attFile)
require.NoError(t, err)
att := ðpb.Attestation{}
require.NoError(t, att.UnmarshalSSZ(attSSZ), "Failed to unmarshal")
builder.Attestation(t, att)
}
if step.PayloadStatus != nil {
require.NoError(t, builder.SetPayloadStatus(step.PayloadStatus))
}
if step.PowBlock != nil {
powBlockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fmt.Sprint(*step.PowBlock, ".ssz_snappy"))
require.NoError(t, err)
p, err := snappy.Decode(nil /* dst */, powBlockFile)
require.NoError(t, err)
pb := ðpb.PowBlock{}
require.NoError(t, pb.UnmarshalSSZ(p), "Failed to unmarshal")
builder.PoWBlock(pb)
}
builder.Check(t, step.Check)
}
})
}
}
}
func unmarshalPhase0State(t *testing.T, raw []byte) state.BeaconState {
base := ðpb.BeaconState{}
require.NoError(t, base.UnmarshalSSZ(raw))
st, err := state_native.InitializeFromProtoPhase0(base)
require.NoError(t, err)
return st
}
func unmarshalPhase0Block(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.BeaconBlock{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: base, Signature: make([]byte, fieldparams.BLSSignatureLength)})
require.NoError(t, err)
return blk
}
func unmarshalSignedPhase0Block(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.SignedBeaconBlock{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(base)
require.NoError(t, err)
return blk
}
func unmarshalAltairState(t *testing.T, raw []byte) state.BeaconState {
base := ðpb.BeaconStateAltair{}
require.NoError(t, base.UnmarshalSSZ(raw))
st, err := state_native.InitializeFromProtoAltair(base)
require.NoError(t, err)
return st
}
func unmarshalAltairBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.BeaconBlockAltair{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: base, Signature: make([]byte, fieldparams.BLSSignatureLength)})
require.NoError(t, err)
return blk
}
func unmarshalSignedAltairBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.SignedBeaconBlockAltair{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(base)
require.NoError(t, err)
return blk
}
func unmarshalBellatrixState(t *testing.T, raw []byte) state.BeaconState {
base := ðpb.BeaconStateBellatrix{}
require.NoError(t, base.UnmarshalSSZ(raw))
st, err := state_native.InitializeFromProtoBellatrix(base)
require.NoError(t, err)
return st
}
func unmarshalBellatrixBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.BeaconBlockBellatrix{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: base, Signature: make([]byte, fieldparams.BLSSignatureLength)})
require.NoError(t, err)
return blk
}
func unmarshalSignedBellatrixBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.SignedBeaconBlockBellatrix{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(base)
require.NoError(t, err)
return blk
}
func unmarshalCapellaState(t *testing.T, raw []byte) state.BeaconState {
base := ðpb.BeaconStateCapella{}
require.NoError(t, base.UnmarshalSSZ(raw))
st, err := state_native.InitializeFromProtoCapella(base)
require.NoError(t, err)
return st
}
func unmarshalCapellaBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.BeaconBlockCapella{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockCapella{Block: base, Signature: make([]byte, fieldparams.BLSSignatureLength)})
require.NoError(t, err)
return blk
}
func unmarshalSignedCapellaBlock(t *testing.T, raw []byte) interfaces.ReadOnlySignedBeaconBlock {
base := ðpb.SignedBeaconBlockCapella{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(base)
require.NoError(t, err)
return blk
}
func unmarshalDenebState(t *testing.T, raw []byte) state.BeaconState {
base := ðpb.BeaconStateDeneb{}
require.NoError(t, base.UnmarshalSSZ(raw))
st, err := state_native.InitializeFromProtoDeneb(base)
require.NoError(t, err)
return st
}
func unmarshalDenebBlock(t *testing.T, raw []byte) interfaces.SignedBeaconBlock {
base := ðpb.BeaconBlockDeneb{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockDeneb{Block: base, Signature: make([]byte, fieldparams.BLSSignatureLength)})
require.NoError(t, err)
return blk
}
func unmarshalSignedDenebBlock(t *testing.T, raw []byte) interfaces.SignedBeaconBlock {
base := ðpb.SignedBeaconBlockDeneb{}
require.NoError(t, base.UnmarshalSSZ(raw))
blk, err := blocks.NewSignedBeaconBlock(base)
require.NoError(t, err)
return blk
}
func runBlobStep(t *testing.T,
blobs *string,
beaconBlock interfaces.ReadOnlySignedBeaconBlock,
fork int,
folder os.DirEntry,
testsFolderPath string,
proofs []*string,
builder *Builder,
) {
if blobs != nil && *blobs != "null" {
require.NotNil(t, beaconBlock)
require.Equal(t, true, fork >= version.Deneb)
block := beaconBlock.Block()
root, err := block.HashTreeRoot()
require.NoError(t, err)
parentRoot := block.ParentRoot()
kzgs, err := block.Body().BlobKzgCommitments()
require.NoError(t, err)
blobsFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fmt.Sprint(*blobs, ".ssz_snappy"))
require.NoError(t, err)
blobsSSZ, err := snappy.Decode(nil /* dst */, blobsFile)
require.NoError(t, err)
for index := uint64(0); index*fieldparams.BlobLength < uint64(len(blobsSSZ)); index++ {
var proof []byte
if index < uint64(len(proofs)) {
proofPTR := proofs[index]
require.NotNil(t, proofPTR)
proof, err = hexutil.Decode(*proofPTR)
require.NoError(t, err)
}
var kzg []byte
if uint64(len(kzgs)) < index {
kzg = kzgs[index]
}
blob := [fieldparams.BlobLength]byte{}
copy(blob[:], blobsSSZ[index*fieldparams.BlobLength:])
sidecar := ðpb.DeprecatedBlobSidecar{
BlockRoot: root[:],
Index: index,
Slot: block.Slot(),
BlockParentRoot: parentRoot[:],
ProposerIndex: block.ProposerIndex(),
Blob: blob[:],
KzgCommitment: kzg,
KzgProof: proof,
}
require.NoError(t, builder.service.ReceiveBlob(context.Background(), sidecar))
}
}
}