-
Notifications
You must be signed in to change notification settings - Fork 212
/
tracer.go
502 lines (426 loc) · 10.1 KB
/
tracer.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package tortoise
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/types/result"
)
type output struct {
Type eventType `json:"t"`
Event json.RawMessage `json:"o"`
}
type tracer struct {
logger *zap.Logger
}
func (t *tracer) On(event traceEvent) {
buf, err := json.Marshal(event)
if err != nil {
panic(err.Error())
}
raw := json.RawMessage(buf)
t.logger.Info("",
zap.Uint16("t", event.Type()),
zap.Any("o", &raw),
)
}
type TraceOpt func(*zap.Config)
func WithOutput(path string) TraceOpt {
return func(cfg *zap.Config) {
cfg.OutputPaths = []string{path}
}
}
func newTracer(opts ...TraceOpt) *tracer {
cfg := zap.NewProductionConfig()
cfg.Sampling = nil
cfg.EncoderConfig.CallerKey = zapcore.OmitKey
cfg.EncoderConfig.MessageKey = zapcore.OmitKey
cfg.EncoderConfig.LevelKey = zapcore.OmitKey
cfg.DisableCaller = true
for _, opt := range opts {
opt(&cfg)
}
logger, err := cfg.Build()
if err != nil {
panic(err.Error())
}
return &tracer{
logger: logger.Named("tracer"),
}
}
type traceRunner struct {
opts []Opt
trt *Tortoise
pending map[types.BallotID]*DecodedBallot
assertOutputs bool
assertErrors bool
}
func RunTrace(path string, breakpoint func(), opts ...Opt) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(bufio.NewReaderSize(f, 1<<20))
enum := newEventEnum()
runner := &traceRunner{
opts: opts,
pending: map[types.BallotID]*DecodedBallot{},
assertOutputs: true,
assertErrors: true,
}
for {
ev, err := enum.Decode(dec)
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
if err := ev.Run(runner); err != nil {
return err
}
if breakpoint != nil {
breakpoint()
}
}
}
type eventType = uint16
const (
traceStart eventType = 1 + iota
traceWeakCoin
traceBeacon
traceAtx
traceBallot
traceDecode
traceStore
traceEncode
traceTally
traceBlock
traceHare
traceActiveset
traceResults
traceUpdates
traceApplied
traceMalfeasence
)
type traceEvent interface {
Type() eventType
New() traceEvent
Run(*traceRunner) error
}
type ConfigTrace struct {
Hdist uint32 `json:"hdist"`
Zdist uint32 `json:"zdist"`
WindowSize uint32 `json:"window"`
MaxExceptions uint32 `json:"exceptions"`
BadBeaconVoteDelayLayers uint32 `json:"delay"`
LayerSize uint32 `json:"layer-size"`
EpochSize uint32 `json:"epoch-size"` // this field is not set in the original config
EffectiveGenesis uint32 `json:"effective-genesis"`
}
func (c *ConfigTrace) Type() eventType {
return traceStart
}
func (c *ConfigTrace) New() traceEvent {
return &ConfigTrace{}
}
func (c *ConfigTrace) Run(r *traceRunner) error {
types.SetLayersPerEpoch(c.EpochSize)
types.SetEffectiveGenesis(c.EffectiveGenesis)
trt, err := New(append(r.opts, WithConfig(Config{
Hdist: c.Hdist,
Zdist: c.Zdist,
WindowSize: c.WindowSize,
MaxExceptions: int(c.MaxExceptions),
BadBeaconVoteDelayLayers: c.BadBeaconVoteDelayLayers,
LayerSize: c.LayerSize,
}))...)
if err != nil {
return err
}
r.trt = trt
return nil
}
type AtxTrace struct {
Header *types.AtxTortoiseData `json:",inline"`
}
func (a *AtxTrace) Type() eventType {
return traceAtx
}
func (a *AtxTrace) New() traceEvent {
return &AtxTrace{}
}
func (a *AtxTrace) Run(r *traceRunner) error {
r.trt.OnAtx(a.Header)
return nil
}
type WeakCoinTrace struct {
Layer types.LayerID `json:"lid"`
Coin bool `json:"coin"`
}
func (w *WeakCoinTrace) Type() eventType {
return traceWeakCoin
}
func (w *WeakCoinTrace) New() traceEvent {
return &WeakCoinTrace{}
}
func (w *WeakCoinTrace) Run(r *traceRunner) error {
r.trt.OnWeakCoin(w.Layer, w.Coin)
return nil
}
type BeaconTrace struct {
Epoch types.EpochID `json:"epoch"`
Beacon types.Beacon `json:"beacon"`
}
func (b *BeaconTrace) Type() eventType {
return traceBeacon
}
func (b *BeaconTrace) New() traceEvent {
return &BeaconTrace{}
}
func (b *BeaconTrace) Run(r *traceRunner) error {
r.trt.OnBeacon(b.Epoch, b.Beacon)
return nil
}
type BallotTrace struct {
Ballot *types.BallotTortoiseData `json:",inline"`
}
func (b *BallotTrace) Type() eventType {
return traceBallot
}
func (b *BallotTrace) New() traceEvent {
return &BallotTrace{}
}
func (b *BallotTrace) Run(r *traceRunner) error {
r.trt.OnBallot(b.Ballot)
return nil
}
type DecodeBallotTrace struct {
Ballot *types.BallotTortoiseData `json:",inline"`
Error string `json:"e"`
// TODO(dshulyak) want to assert decoding results somehow
}
func (d *DecodeBallotTrace) Type() eventType {
return traceDecode
}
func (d *DecodeBallotTrace) New() traceEvent {
return &DecodeBallotTrace{}
}
func (b *DecodeBallotTrace) Run(r *traceRunner) error {
decoded, err := r.trt.DecodeBallot(b.Ballot)
if r.assertErrors {
if err := assertErrors(err, b.Error); err != nil {
return err
}
}
if err == nil {
r.pending[decoded.ID] = decoded
}
return nil
}
type StoreBallotTrace struct {
ID types.BallotID `json:"id"`
Malicious bool `json:"mal"`
Error string `json:"e,omitempty"`
}
func (s *StoreBallotTrace) Type() eventType {
return traceStore
}
func (s *StoreBallotTrace) New() traceEvent {
return &StoreBallotTrace{}
}
func (s *StoreBallotTrace) Run(r *traceRunner) error {
pending, exist := r.pending[s.ID]
if !exist {
return fmt.Errorf("id %v should be pending", s.ID)
}
if s.Malicious {
pending.SetMalicious()
}
delete(r.pending, s.ID)
err := r.trt.StoreBallot(pending)
if r.assertErrors {
if err := assertErrors(err, s.Error); err != nil {
return err
}
}
return nil
}
type EncodeVotesTrace struct {
Layer types.LayerID `json:"lid"`
Opinion *types.Opinion `json:"opinion"`
Error string `json:"e"`
}
func (e *EncodeVotesTrace) Type() eventType {
return traceEncode
}
func (e *EncodeVotesTrace) New() traceEvent {
return &EncodeVotesTrace{}
}
func (e *EncodeVotesTrace) Run(r *traceRunner) error {
opinion, err := r.trt.EncodeVotes(context.Background(), EncodeVotesWithCurrent(e.Layer))
if r.assertErrors {
if err := assertErrors(err, e.Error); err != nil {
return err
}
}
if err == nil {
if diff := cmp.Diff(opinion, e.Opinion); len(diff) > 0 && r.assertOutputs {
return errors.New(diff)
}
}
return nil
}
type TallyTrace struct {
Layer types.LayerID `json:"lid"`
}
func (t *TallyTrace) Type() eventType {
return traceTally
}
func (t *TallyTrace) New() traceEvent {
return &TallyTrace{}
}
func (t *TallyTrace) Run(r *traceRunner) error {
r.trt.TallyVotes(context.Background(), t.Layer)
return nil
}
type HareTrace struct {
Layer types.LayerID `json:"lid"`
Vote types.BlockID `json:"vote"`
}
func (h *HareTrace) Type() eventType {
return traceHare
}
func (h *HareTrace) New() traceEvent {
return &HareTrace{}
}
func (h *HareTrace) Run(r *traceRunner) error {
r.trt.OnHareOutput(h.Layer, h.Vote)
return nil
}
type UpdatesTrace struct {
From types.LayerID `json:"from"`
To types.LayerID `json:"to"`
Error string `json:"e"`
Results []result.Layer `json:"results"`
}
func (u *UpdatesTrace) Type() eventType {
return traceUpdates
}
func (u *UpdatesTrace) New() traceEvent {
return &UpdatesTrace{}
}
func (u *UpdatesTrace) Run(r *traceRunner) error {
rst := r.trt.Updates()
if diff := cmp.Diff(rst, u.Results, cmpopts.EquateEmpty()); len(diff) > 0 && r.assertOutputs {
return errors.New(diff)
}
return nil
}
type AppliedTrace struct {
Layer types.LayerID `json:"layer"`
Opinion types.Hash32 `json:"opinion"`
Result bool `json:"rst"`
}
func (a *AppliedTrace) Type() eventType {
return traceApplied
}
func (a *AppliedTrace) New() traceEvent {
return &AppliedTrace{}
}
func (a *AppliedTrace) Run(r *traceRunner) error {
rst := r.trt.OnApplied(a.Layer, a.Opinion)
if rst != a.Result {
return fmt.Errorf("on applied: expected %v got %v", a.Result, rst)
}
return nil
}
type BlockTrace struct {
Header types.BlockHeader `json:",inline"`
Valid bool `json:"v"`
}
func (b *BlockTrace) Type() eventType {
return traceBlock
}
func (b *BlockTrace) New() traceEvent {
return &BlockTrace{}
}
func (b *BlockTrace) Run(r *traceRunner) error {
if b.Valid {
r.trt.OnValidBlock(b.Header)
} else {
r.trt.OnBlock(b.Header)
}
return nil
}
type MalfeasanceTrace struct {
ID types.NodeID `json:"id"`
}
func (m *MalfeasanceTrace) Type() eventType {
return traceMalfeasence
}
func (m *MalfeasanceTrace) New() traceEvent {
return &MalfeasanceTrace{}
}
func (m *MalfeasanceTrace) Run(r *traceRunner) error {
r.trt.OnMalfeasance(m.ID)
return nil
}
func assertErrors(err error, expect string) error {
msg := ""
if err != nil {
msg = err.Error()
}
if expect != msg {
return fmt.Errorf("%s != %s", expect, msg)
}
return nil
}
func newEventEnum() eventEnum {
enum := eventEnum{types: map[uint16]traceEvent{}}
enum.Register(&ConfigTrace{})
enum.Register(&WeakCoinTrace{})
enum.Register(&BeaconTrace{})
enum.Register(&AtxTrace{})
enum.Register(&BallotTrace{})
enum.Register(&DecodeBallotTrace{})
enum.Register(&StoreBallotTrace{})
enum.Register(&EncodeVotesTrace{})
enum.Register(&TallyTrace{})
enum.Register(&BlockTrace{})
enum.Register(&HareTrace{})
enum.Register(&UpdatesTrace{})
enum.Register(&AppliedTrace{})
enum.Register(&MalfeasanceTrace{})
return enum
}
type eventEnum struct {
types map[eventType]traceEvent
}
func (e *eventEnum) Register(ev traceEvent) {
e.types[ev.Type()] = ev
}
func (e *eventEnum) Decode(dec *json.Decoder) (traceEvent, error) {
var event output
if err := dec.Decode(&event); err != nil {
return nil, err
}
ev := e.types[event.Type]
if ev == nil {
return nil, fmt.Errorf("type %d is not registered", event.Type)
}
obj := ev.New()
if err := json.Unmarshal(event.Event, obj); err != nil {
return nil, err
}
return obj, nil
}