-
Notifications
You must be signed in to change notification settings - Fork 671
/
batched_vm.go
176 lines (155 loc) · 4.71 KB
/
batched_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"context"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils/wrappers"
statelessblock "github.com/ava-labs/avalanchego/vms/proposervm/block"
)
var _ block.BatchedChainVM = (*VM)(nil)
func (vm *VM) GetAncestors(
ctx context.Context,
blkID ids.ID,
maxBlocksNum int,
maxBlocksSize int,
maxBlocksRetrievalTime time.Duration,
) ([][]byte, error) {
if vm.batchedVM == nil {
return nil, block.ErrRemoteVMNotImplemented
}
res := make([][]byte, 0, maxBlocksNum)
currentByteLength := 0
startTime := vm.Clock.Time()
// hereinafter loop over proposerVM cache and DB, possibly till snowman++
// fork is hit
for {
blk, err := vm.getStatelessBlk(blkID)
if err != nil {
// maybe we have hit the proposerVM fork here?
break
}
blkBytes := blk.Bytes()
// Ensure response size isn't too large. Include wrappers.IntLen because
// the size of the message is included with each container, and the size
// is repr. by an int.
currentByteLength += wrappers.IntLen + len(blkBytes)
elapsedTime := vm.Clock.Time().Sub(startTime)
if len(res) > 0 && (currentByteLength >= maxBlocksSize || maxBlocksRetrievalTime <= elapsedTime) {
return res, nil // reached maximum size or ran out of time
}
res = append(res, blkBytes)
blkID = blk.ParentID()
if len(res) >= maxBlocksNum {
return res, nil
}
}
// snowman++ fork may have been hit.
preMaxBlocksNum := maxBlocksNum - len(res)
preMaxBlocksSize := maxBlocksSize - currentByteLength
preMaxBlocksRetrivalTime := maxBlocksRetrievalTime - time.Since(startTime)
innerBytes, err := vm.batchedVM.GetAncestors(
ctx,
blkID,
preMaxBlocksNum,
preMaxBlocksSize,
preMaxBlocksRetrivalTime,
)
if err != nil {
if len(res) == 0 {
return nil, err
}
return res, nil // return what we have
}
res = append(res, innerBytes...)
return res, nil
}
func (vm *VM) BatchedParseBlock(ctx context.Context, blks [][]byte) ([]snowman.Block, error) {
if vm.batchedVM == nil {
return nil, block.ErrRemoteVMNotImplemented
}
type partialData struct {
index int
block statelessblock.Block
}
var (
blocksIndex int
blocks = make([]snowman.Block, len(blks))
innerBlocksIndex int
statelessBlockDescs = make([]partialData, 0, len(blks))
innerBlockBytes = make([][]byte, 0, len(blks))
)
for ; blocksIndex < len(blks); blocksIndex++ {
blkBytes := blks[blocksIndex]
statelessBlock, err := statelessblock.Parse(blkBytes)
if err != nil {
break
}
blkID := statelessBlock.ID()
block, exists := vm.verifiedBlocks[blkID]
if exists {
blocks[blocksIndex] = block
continue
}
statelessBlockDescs = append(statelessBlockDescs, partialData{
index: blocksIndex,
block: statelessBlock,
})
innerBlockBytes = append(innerBlockBytes, statelessBlock.Block())
}
innerBlockBytes = append(innerBlockBytes, blks[blocksIndex:]...)
// parse all inner blocks at once
innerBlks, err := vm.batchedVM.BatchedParseBlock(ctx, innerBlockBytes)
if err != nil {
return nil, err
}
for ; innerBlocksIndex < len(statelessBlockDescs); innerBlocksIndex++ {
statelessBlockDesc := statelessBlockDescs[innerBlocksIndex]
statelessBlk := statelessBlockDesc.block
blkID := statelessBlk.ID()
_, status, err := vm.State.GetBlock(blkID)
if err == database.ErrNotFound {
status = choices.Processing
} else if err != nil {
return nil, err
}
if statelessSignedBlock, ok := statelessBlk.(statelessblock.SignedBlock); ok {
blocks[statelessBlockDesc.index] = &postForkBlock{
SignedBlock: statelessSignedBlock,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlks[innerBlocksIndex],
status: status,
},
}
} else {
blocks[statelessBlockDesc.index] = &postForkOption{
Block: statelessBlk,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlks[innerBlocksIndex],
status: status,
},
}
}
}
for ; blocksIndex < len(blocks); blocksIndex, innerBlocksIndex = blocksIndex+1, innerBlocksIndex+1 {
blocks[blocksIndex] = &preForkBlock{
Block: innerBlks[innerBlocksIndex],
vm: vm,
}
}
return blocks, nil
}
func (vm *VM) getStatelessBlk(blkID ids.ID) (statelessblock.Block, error) {
if currentBlk, exists := vm.verifiedBlocks[blkID]; exists {
return currentBlk.getStatelessBlk(), nil
}
statelessBlock, _, err := vm.State.GetBlock(blkID)
return statelessBlock, err
}