-
Notifications
You must be signed in to change notification settings - Fork 670
/
block_vm.go
148 lines (133 loc) · 3.62 KB
/
block_vm.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metervm
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)
var (
_ block.ChainVM = (*blockVM)(nil)
_ block.BatchedChainVM = (*blockVM)(nil)
_ block.HeightIndexedChainVM = (*blockVM)(nil)
_ block.StateSyncableVM = (*blockVM)(nil)
)
type blockVM struct {
block.ChainVM
bVM block.BatchedChainVM
hVM block.HeightIndexedChainVM
ssVM block.StateSyncableVM
blockMetrics
clock mockable.Clock
}
func NewBlockVM(vm block.ChainVM) block.ChainVM {
bVM, _ := vm.(block.BatchedChainVM)
hVM, _ := vm.(block.HeightIndexedChainVM)
ssVM, _ := vm.(block.StateSyncableVM)
return &blockVM{
ChainVM: vm,
bVM: bVM,
hVM: hVM,
ssVM: ssVM,
}
}
func (vm *blockVM) Initialize(
ctx *snow.Context,
db manager.Manager,
genesisBytes,
upgradeBytes,
configBytes []byte,
toEngine chan<- common.Message,
fxs []*common.Fx,
appSender common.AppSender,
) error {
registerer := prometheus.NewRegistry()
err := vm.blockMetrics.Initialize(
vm.bVM != nil,
vm.hVM != nil,
vm.ssVM != nil,
"",
registerer,
)
if err != nil {
return err
}
optionalGatherer := metrics.NewOptionalGatherer()
multiGatherer := metrics.NewMultiGatherer()
if err := multiGatherer.Register("metervm", registerer); err != nil {
return err
}
if err := multiGatherer.Register("", optionalGatherer); err != nil {
return err
}
if err := ctx.Metrics.Register(multiGatherer); err != nil {
return err
}
ctx.Metrics = optionalGatherer
return vm.ChainVM.Initialize(ctx, db, genesisBytes, upgradeBytes, configBytes, toEngine, fxs, appSender)
}
func (vm *blockVM) BuildBlock() (snowman.Block, error) {
start := vm.clock.Time()
blk, err := vm.ChainVM.BuildBlock()
end := vm.clock.Time()
duration := float64(end.Sub(start))
if err != nil {
vm.blockMetrics.buildBlockErr.Observe(duration)
return nil, err
}
vm.blockMetrics.buildBlock.Observe(duration)
return &meterBlock{
Block: blk,
vm: vm,
}, nil
}
func (vm *blockVM) ParseBlock(b []byte) (snowman.Block, error) {
start := vm.clock.Time()
blk, err := vm.ChainVM.ParseBlock(b)
end := vm.clock.Time()
duration := float64(end.Sub(start))
if err != nil {
vm.blockMetrics.parseBlockErr.Observe(duration)
return nil, err
}
vm.blockMetrics.parseBlock.Observe(duration)
return &meterBlock{
Block: blk,
vm: vm,
}, nil
}
func (vm *blockVM) GetBlock(id ids.ID) (snowman.Block, error) {
start := vm.clock.Time()
blk, err := vm.ChainVM.GetBlock(id)
end := vm.clock.Time()
duration := float64(end.Sub(start))
if err != nil {
vm.blockMetrics.getBlockErr.Observe(duration)
return nil, err
}
vm.blockMetrics.getBlock.Observe(duration)
return &meterBlock{
Block: blk,
vm: vm,
}, nil
}
func (vm *blockVM) SetPreference(id ids.ID) error {
start := vm.clock.Time()
err := vm.ChainVM.SetPreference(id)
end := vm.clock.Time()
vm.blockMetrics.setPreference.Observe(float64(end.Sub(start)))
return err
}
func (vm *blockVM) LastAccepted() (ids.ID, error) {
start := vm.clock.Time()
lastAcceptedID, err := vm.ChainVM.LastAccepted()
end := vm.clock.Time()
vm.blockMetrics.lastAccepted.Observe(float64(end.Sub(start)))
return lastAcceptedID, err
}