-
Notifications
You must be signed in to change notification settings - Fork 665
/
block.go
120 lines (96 loc) · 3.04 KB
/
block.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tracedvm
import (
"context"
"errors"
"fmt"
"go.opentelemetry.io/otel/attribute"
oteltrace "go.opentelemetry.io/otel/trace"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
)
var (
_ snowman.Block = (*tracedBlock)(nil)
_ snowman.OracleBlock = (*tracedBlock)(nil)
_ block.WithVerifyContext = (*tracedBlock)(nil)
errExpectedBlockWithVerifyContext = errors.New("expected block.WithVerifyContext")
)
type tracedBlock struct {
snowman.Block
vm *blockVM
}
func (b *tracedBlock) Verify(ctx context.Context) error {
ctx, span := b.vm.tracer.Start(ctx, b.vm.verifyTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
))
defer span.End()
return b.Block.Verify(ctx)
}
func (b *tracedBlock) Accept(ctx context.Context) error {
ctx, span := b.vm.tracer.Start(ctx, b.vm.acceptTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
))
defer span.End()
return b.Block.Accept(ctx)
}
func (b *tracedBlock) Reject(ctx context.Context) error {
ctx, span := b.vm.tracer.Start(ctx, b.vm.rejectTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
))
defer span.End()
return b.Block.Reject(ctx)
}
func (b *tracedBlock) Options(ctx context.Context) ([2]snowman.Block, error) {
oracleBlock, ok := b.Block.(snowman.OracleBlock)
if !ok {
return [2]snowman.Block{}, snowman.ErrNotOracle
}
ctx, span := b.vm.tracer.Start(ctx, b.vm.optionsTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
))
defer span.End()
blks, err := oracleBlock.Options(ctx)
if err != nil {
return [2]snowman.Block{}, err
}
return [2]snowman.Block{
&tracedBlock{
Block: blks[0],
vm: b.vm,
},
&tracedBlock{
Block: blks[1],
vm: b.vm,
},
}, nil
}
func (b *tracedBlock) ShouldVerifyWithContext(ctx context.Context) (bool, error) {
blkWithCtx, ok := b.Block.(block.WithVerifyContext)
if !ok {
return false, nil
}
ctx, span := b.vm.tracer.Start(ctx, b.vm.shouldVerifyWithContextTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
))
defer span.End()
return blkWithCtx.ShouldVerifyWithContext(ctx)
}
func (b *tracedBlock) VerifyWithContext(ctx context.Context, blockCtx *block.Context) error {
blkWithCtx, ok := b.Block.(block.WithVerifyContext)
if !ok {
return fmt.Errorf("%w but got %T", errExpectedBlockWithVerifyContext, b.Block)
}
ctx, span := b.vm.tracer.Start(ctx, b.vm.verifyWithContextTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", b.ID()),
attribute.Int64("height", int64(b.Height())),
attribute.Int64("pChainHeight", int64(blockCtx.PChainHeight)),
))
defer span.End()
return blkWithCtx.VerifyWithContext(ctx, blockCtx)
}