forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
block_vm.go
184 lines (162 loc) · 5.58 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
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tracedvm
import (
"context"
"go.opentelemetry.io/otel/attribute"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/snow/consensus/snowman"
"github.com/MetalBlockchain/metalgo/snow/engine/common"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
"github.com/MetalBlockchain/metalgo/trace"
oteltrace "go.opentelemetry.io/otel/trace"
)
var (
_ block.ChainVM = (*blockVM)(nil)
_ block.BuildBlockWithContextChainVM = (*blockVM)(nil)
_ block.BatchedChainVM = (*blockVM)(nil)
_ block.StateSyncableVM = (*blockVM)(nil)
)
type blockVM struct {
block.ChainVM
buildBlockVM block.BuildBlockWithContextChainVM
batchedVM block.BatchedChainVM
ssVM block.StateSyncableVM
// ChainVM tags
initializeTag string
buildBlockTag string
parseBlockTag string
getBlockTag string
setPreferenceTag string
lastAcceptedTag string
verifyTag string
acceptTag string
rejectTag string
optionsTag string
shouldVerifyWithContextTag string
verifyWithContextTag string
// BuildBlockWithContextChainVM tags
buildBlockWithContextTag string
// BatchedChainVM tags
getAncestorsTag string
batchedParseBlockTag string
// HeightIndexedChainVM tags
getBlockIDAtHeightTag string
// StateSyncableVM tags
stateSyncEnabledTag string
getOngoingSyncStateSummaryTag string
getLastStateSummaryTag string
parseStateSummaryTag string
getStateSummaryTag string
tracer trace.Tracer
}
func NewBlockVM(vm block.ChainVM, name string, tracer trace.Tracer) block.ChainVM {
buildBlockVM, _ := vm.(block.BuildBlockWithContextChainVM)
batchedVM, _ := vm.(block.BatchedChainVM)
ssVM, _ := vm.(block.StateSyncableVM)
return &blockVM{
ChainVM: vm,
buildBlockVM: buildBlockVM,
batchedVM: batchedVM,
ssVM: ssVM,
initializeTag: name + ".initialize",
buildBlockTag: name + ".buildBlock",
parseBlockTag: name + ".parseBlock",
getBlockTag: name + ".getBlock",
setPreferenceTag: name + ".setPreference",
lastAcceptedTag: name + ".lastAccepted",
verifyTag: name + ".verify",
acceptTag: name + ".accept",
rejectTag: name + ".reject",
optionsTag: name + ".options",
shouldVerifyWithContextTag: name + ".shouldVerifyWithContext",
verifyWithContextTag: name + ".verifyWithContext",
buildBlockWithContextTag: name + ".buildBlockWithContext",
getAncestorsTag: name + ".getAncestors",
batchedParseBlockTag: name + ".batchedParseBlock",
getBlockIDAtHeightTag: name + ".getBlockIDAtHeight",
stateSyncEnabledTag: name + ".stateSyncEnabled",
getOngoingSyncStateSummaryTag: name + ".getOngoingSyncStateSummary",
getLastStateSummaryTag: name + ".getLastStateSummary",
parseStateSummaryTag: name + ".parseStateSummary",
getStateSummaryTag: name + ".getStateSummary",
tracer: tracer,
}
}
func (vm *blockVM) Initialize(
ctx context.Context,
chainCtx *snow.Context,
db database.Database,
genesisBytes,
upgradeBytes,
configBytes []byte,
toEngine chan<- common.Message,
fxs []*common.Fx,
appSender common.AppSender,
) error {
ctx, span := vm.tracer.Start(ctx, vm.initializeTag)
defer span.End()
return vm.ChainVM.Initialize(
ctx,
chainCtx,
db,
genesisBytes,
upgradeBytes,
configBytes,
toEngine,
fxs,
appSender,
)
}
func (vm *blockVM) BuildBlock(ctx context.Context) (snowman.Block, error) {
ctx, span := vm.tracer.Start(ctx, vm.buildBlockTag)
defer span.End()
blk, err := vm.ChainVM.BuildBlock(ctx)
return &tracedBlock{
Block: blk,
vm: vm,
}, err
}
func (vm *blockVM) ParseBlock(ctx context.Context, block []byte) (snowman.Block, error) {
ctx, span := vm.tracer.Start(ctx, vm.parseBlockTag, oteltrace.WithAttributes(
attribute.Int("blockLen", len(block)),
))
defer span.End()
blk, err := vm.ChainVM.ParseBlock(ctx, block)
return &tracedBlock{
Block: blk,
vm: vm,
}, err
}
func (vm *blockVM) GetBlock(ctx context.Context, blkID ids.ID) (snowman.Block, error) {
ctx, span := vm.tracer.Start(ctx, vm.getBlockTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", blkID),
))
defer span.End()
blk, err := vm.ChainVM.GetBlock(ctx, blkID)
return &tracedBlock{
Block: blk,
vm: vm,
}, err
}
func (vm *blockVM) SetPreference(ctx context.Context, blkID ids.ID) error {
ctx, span := vm.tracer.Start(ctx, vm.setPreferenceTag, oteltrace.WithAttributes(
attribute.Stringer("blkID", blkID),
))
defer span.End()
return vm.ChainVM.SetPreference(ctx, blkID)
}
func (vm *blockVM) LastAccepted(ctx context.Context) (ids.ID, error) {
ctx, span := vm.tracer.Start(ctx, vm.lastAcceptedTag)
defer span.End()
return vm.ChainVM.LastAccepted(ctx)
}
func (vm *blockVM) GetBlockIDAtHeight(ctx context.Context, height uint64) (ids.ID, error) {
ctx, span := vm.tracer.Start(ctx, vm.getBlockIDAtHeightTag, oteltrace.WithAttributes(
attribute.Int64("height", int64(height)),
))
defer span.End()
return vm.ChainVM.GetBlockIDAtHeight(ctx, height)
}