/
attester_test.go
357 lines (322 loc) · 10.3 KB
/
attester_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
package validator
import (
"context"
"strings"
"sync"
"testing"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
mockp2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
// Use minimal config to reduce test setup time.
params.OverrideBeaconConfig(params.MinimalSpecConfig())
}
func TestProposeAttestation_OK(t *testing.T) {
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
ctx := context.Background()
attesterServer := &Server{
HeadFetcher: &mock.ChainService{},
P2P: &mockp2p.MockBroadcaster{},
BeaconDB: db,
AttestationCache: cache.NewAttestationCache(),
AttPool: attestations.NewPool(),
}
head := ðpb.SignedBeaconBlock{
Block: ðpb.BeaconBlock{
Slot: 999,
ParentRoot: []byte{'a'},
},
}
if err := db.SaveBlock(ctx, head); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(head.Block)
if err != nil {
t.Fatal(err)
}
validators := make([]*ethpb.Validator, params.BeaconConfig().MinGenesisActiveValidatorCount/16)
for i := 0; i < len(validators); i++ {
validators[i] = ðpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
}
}
state := &pbp2p.BeaconState{
Slot: params.BeaconConfig().SlotsPerEpoch + 1,
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
if err := db.SaveState(ctx, state, root); err != nil {
t.Fatal(err)
}
if err := db.SaveHeadBlockRoot(ctx, root); err != nil {
t.Fatal(err)
}
sk := bls.RandKey()
sig := sk.Sign([]byte("dummy_test_data"), 0 /*domain*/)
req := ðpb.Attestation{
Signature: sig.Marshal(),
Data: ðpb.AttestationData{
BeaconBlockRoot: root[:],
Source: ðpb.Checkpoint{},
Target: ðpb.Checkpoint{},
},
}
if _, err := attesterServer.ProposeAttestation(context.Background(), req); err != nil {
t.Errorf("Could not attest head correctly: %v", err)
}
}
func TestProposeAttestation_IncorrectSignature(t *testing.T) {
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
attesterServer := &Server{
HeadFetcher: &mock.ChainService{},
P2P: &mockp2p.MockBroadcaster{},
BeaconDB: db,
AttestationCache: cache.NewAttestationCache(),
AttPool: attestations.NewPool(),
}
req := ðpb.Attestation{
Data: ðpb.AttestationData{
Source: ðpb.Checkpoint{},
Target: ðpb.Checkpoint{},
},
}
wanted := "Incorrect attestation signature"
if _, err := attesterServer.ProposeAttestation(context.Background(), req); !strings.Contains(err.Error(), wanted) {
t.Errorf("Did not get wanted error")
}
}
func TestGetAttestationData_OK(t *testing.T) {
ctx := context.Background()
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
block := ðpb.BeaconBlock{
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
}
targetBlock := ðpb.BeaconBlock{
Slot: 1 * params.BeaconConfig().SlotsPerEpoch,
}
justifiedBlock := ðpb.BeaconBlock{
Slot: 2 * params.BeaconConfig().SlotsPerEpoch,
}
blockRoot, err := ssz.HashTreeRoot(block)
if err != nil {
t.Fatalf("Could not hash beacon block: %v", err)
}
justifiedRoot, err := ssz.HashTreeRoot(justifiedBlock)
if err != nil {
t.Fatalf("Could not get signing root for justified block: %v", err)
}
targetRoot, err := ssz.HashTreeRoot(targetBlock)
if err != nil {
t.Fatalf("Could not get signing root for target block: %v", err)
}
beaconState := &pbp2p.BeaconState{
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot),
CurrentJustifiedCheckpoint: ðpb.Checkpoint{
Epoch: 2,
Root: justifiedRoot[:],
},
}
beaconState.BlockRoots[1] = blockRoot[:]
beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:]
beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:]
attesterServer := &Server{
BeaconDB: db,
P2P: &mockp2p.MockBroadcaster{},
SyncChecker: &mockSync.Sync{IsSyncing: false},
AttestationCache: cache.NewAttestationCache(),
HeadFetcher: &mock.ChainService{State: beaconState, Root: blockRoot[:]},
}
if err := attesterServer.BeaconDB.SaveState(ctx, beaconState, blockRoot); err != nil {
t.Fatal(err)
}
if err := attesterServer.BeaconDB.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil {
t.Fatal(err)
}
if err := attesterServer.BeaconDB.SaveHeadBlockRoot(ctx, blockRoot); err != nil {
t.Fatal(err)
}
req := ðpb.AttestationDataRequest{
CommitteeIndex: 0,
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
}
res, err := attesterServer.GetAttestationData(context.Background(), req)
if err != nil {
t.Fatalf("Could not get attestation info at slot: %v", err)
}
expectedInfo := ðpb.AttestationData{
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
BeaconBlockRoot: blockRoot[:],
Source: ðpb.Checkpoint{
Epoch: 2,
Root: justifiedRoot[:],
},
Target: ðpb.Checkpoint{
Epoch: 3,
},
}
if !proto.Equal(res, expectedInfo) {
t.Errorf("Expected attestation info to match, received %v, wanted %v", res, expectedInfo)
}
}
func TestGetAttestationData_SyncNotReady(t *testing.T) {
as := &Server{
SyncChecker: &mockSync.Sync{IsSyncing: true},
}
_, err := as.GetAttestationData(context.Background(), ðpb.AttestationDataRequest{})
if strings.Contains(err.Error(), "syncing to latest head") {
t.Error("Did not get wanted error")
}
}
func TestAttestationDataAtSlot_handlesFarAwayJustifiedEpoch(t *testing.T) {
// Scenario:
//
// State slot = 10000
// Last justified slot = epoch start of 1500
// HistoricalRootsLimit = 8192
//
// More background: https://github.com/prysmaticlabs/prysm/issues/2153
// This test breaks if it doesnt use mainnet config
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
ctx := context.Background()
params.OverrideBeaconConfig(params.MainnetConfig())
defer params.OverrideBeaconConfig(params.MinimalSpecConfig())
// Ensure HistoricalRootsLimit matches scenario
cfg := params.BeaconConfig()
cfg.HistoricalRootsLimit = 8192
params.OverrideBeaconConfig(cfg)
block := ðpb.BeaconBlock{
Slot: 10000,
}
epochBoundaryBlock := ðpb.BeaconBlock{
Slot: helpers.StartSlot(helpers.SlotToEpoch(10000)),
}
justifiedBlock := ðpb.BeaconBlock{
Slot: helpers.StartSlot(helpers.SlotToEpoch(1500)) - 2, // Imagine two skip block
}
blockRoot, err := ssz.HashTreeRoot(block)
if err != nil {
t.Fatalf("Could not hash beacon block: %v", err)
}
justifiedBlockRoot, err := ssz.HashTreeRoot(justifiedBlock)
if err != nil {
t.Fatalf("Could not hash justified block: %v", err)
}
epochBoundaryRoot, err := ssz.HashTreeRoot(epochBoundaryBlock)
if err != nil {
t.Fatalf("Could not hash justified block: %v", err)
}
beaconState := &pbp2p.BeaconState{
Slot: 10000,
BlockRoots: make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot),
CurrentJustifiedCheckpoint: ðpb.Checkpoint{
Epoch: helpers.SlotToEpoch(1500),
Root: justifiedBlockRoot[:],
},
}
beaconState.BlockRoots[1] = blockRoot[:]
beaconState.BlockRoots[1*params.BeaconConfig().SlotsPerEpoch] = epochBoundaryRoot[:]
beaconState.BlockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedBlockRoot[:]
attesterServer := &Server{
BeaconDB: db,
P2P: &mockp2p.MockBroadcaster{},
AttestationCache: cache.NewAttestationCache(),
HeadFetcher: &mock.ChainService{State: beaconState, Root: blockRoot[:]},
SyncChecker: &mockSync.Sync{IsSyncing: false},
}
if err := attesterServer.BeaconDB.SaveState(ctx, beaconState, blockRoot); err != nil {
t.Fatal(err)
}
if err := attesterServer.BeaconDB.SaveBlock(ctx, ðpb.SignedBeaconBlock{Block: block}); err != nil {
t.Fatal(err)
}
if err := attesterServer.BeaconDB.SaveHeadBlockRoot(ctx, blockRoot); err != nil {
t.Fatal(err)
}
req := ðpb.AttestationDataRequest{
CommitteeIndex: 0,
Slot: 10000,
}
res, err := attesterServer.GetAttestationData(context.Background(), req)
if err != nil {
t.Fatalf("Could not get attestation info at slot: %v", err)
}
expectedInfo := ðpb.AttestationData{
Slot: req.Slot,
BeaconBlockRoot: blockRoot[:],
Source: ðpb.Checkpoint{
Epoch: helpers.SlotToEpoch(1500),
Root: justifiedBlockRoot[:],
},
Target: ðpb.Checkpoint{
Epoch: 312,
},
}
if !proto.Equal(res, expectedInfo) {
t.Errorf("Expected attestation info to match, received %v, wanted %v", res, expectedInfo)
}
}
func TestAttestationDataSlot_handlesInProgressRequest(t *testing.T) {
// Cache toggled by feature flag for now. See https://github.com/prysmaticlabs/prysm/issues/3106.
featureconfig.Init(&featureconfig.Flags{
EnableAttestationCache: true,
})
defer func() {
featureconfig.Init(nil)
}()
ctx := context.Background()
server := &Server{
AttestationCache: cache.NewAttestationCache(),
SyncChecker: &mockSync.Sync{IsSyncing: false},
}
req := ðpb.AttestationDataRequest{
CommitteeIndex: 1,
Slot: 2,
}
res := ðpb.AttestationData{
Target: ðpb.Checkpoint{Epoch: 55},
}
if err := server.AttestationCache.MarkInProgress(req); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
response, err := server.GetAttestationData(ctx, req)
if err != nil {
t.Error(err)
}
if !proto.Equal(res, response) {
t.Error("Expected equal responses from cache")
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := server.AttestationCache.Put(ctx, req, res); err != nil {
t.Error(err)
}
if err := server.AttestationCache.MarkNotInProgress(req); err != nil {
t.Error(err)
}
}()
wg.Wait()
}