-
Notifications
You must be signed in to change notification settings - Fork 660
/
block.go
112 lines (96 loc) · 2.69 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metervm
import (
"context"
"errors"
"fmt"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
)
var (
_ snowman.Block = (*meterBlock)(nil)
_ snowman.OracleBlock = (*meterBlock)(nil)
_ block.WithVerifyContext = (*meterBlock)(nil)
errExpectedBlockWithVerifyContext = errors.New("expected block.WithVerifyContext")
)
type meterBlock struct {
snowman.Block
vm *blockVM
}
func (mb *meterBlock) Verify(ctx context.Context) error {
start := mb.vm.clock.Time()
err := mb.Block.Verify(ctx)
end := mb.vm.clock.Time()
duration := float64(end.Sub(start))
if err != nil {
mb.vm.blockMetrics.verifyErr.Observe(duration)
} else {
mb.vm.verify.Observe(duration)
}
return err
}
func (mb *meterBlock) Accept(ctx context.Context) error {
start := mb.vm.clock.Time()
err := mb.Block.Accept(ctx)
end := mb.vm.clock.Time()
duration := float64(end.Sub(start))
mb.vm.blockMetrics.accept.Observe(duration)
return err
}
func (mb *meterBlock) Reject(ctx context.Context) error {
start := mb.vm.clock.Time()
err := mb.Block.Reject(ctx)
end := mb.vm.clock.Time()
duration := float64(end.Sub(start))
mb.vm.blockMetrics.reject.Observe(duration)
return err
}
func (mb *meterBlock) Options(ctx context.Context) ([2]snowman.Block, error) {
oracleBlock, ok := mb.Block.(snowman.OracleBlock)
if !ok {
return [2]snowman.Block{}, snowman.ErrNotOracle
}
blks, err := oracleBlock.Options(ctx)
if err != nil {
return [2]snowman.Block{}, err
}
return [2]snowman.Block{
&meterBlock{
Block: blks[0],
vm: mb.vm,
},
&meterBlock{
Block: blks[1],
vm: mb.vm,
},
}, nil
}
func (mb *meterBlock) ShouldVerifyWithContext(ctx context.Context) (bool, error) {
blkWithCtx, ok := mb.Block.(block.WithVerifyContext)
if !ok {
return false, nil
}
start := mb.vm.clock.Time()
shouldVerify, err := blkWithCtx.ShouldVerifyWithContext(ctx)
end := mb.vm.clock.Time()
duration := float64(end.Sub(start))
mb.vm.blockMetrics.shouldVerifyWithContext.Observe(duration)
return shouldVerify, err
}
func (mb *meterBlock) VerifyWithContext(ctx context.Context, blockCtx *block.Context) error {
blkWithCtx, ok := mb.Block.(block.WithVerifyContext)
if !ok {
return fmt.Errorf("%w but got %T", errExpectedBlockWithVerifyContext, mb.Block)
}
start := mb.vm.clock.Time()
err := blkWithCtx.VerifyWithContext(ctx, blockCtx)
end := mb.vm.clock.Time()
duration := float64(end.Sub(start))
if err != nil {
mb.vm.blockMetrics.verifyWithContextErr.Observe(duration)
} else {
mb.vm.verifyWithContext.Observe(duration)
}
return err
}