-
Notifications
You must be signed in to change notification settings - Fork 10
/
worker.go
245 lines (200 loc) · 5.98 KB
/
worker.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package fetch
import (
"context"
"errors"
"fmt"
core_types "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/bft/types"
)
// workerInfo is the work context for the fetch routine
type workerInfo struct {
resCh chan<- *workerResponse // response channel
chunkRange chunkRange // data range
}
// workerResponse is the routine response
type workerResponse struct {
error error // encountered error, if any
chunk *chunk // the fetched chunk
chunkRange chunkRange // the fetched chunk range
}
// handleChunk fetches the chunk from the client
func handleChunk(
ctx context.Context,
client Client,
info *workerInfo,
) {
extractChunk := func() (*chunk, error) {
errs := make([]error, 0)
// Get block data from the node
blocks, err := getBlocksFromBatch(info.chunkRange, client)
errs = append(errs, err)
results, err := getTxResultFromBatch(blocks, client)
errs = append(errs, err)
return &chunk{
blocks: blocks,
results: results,
}, errors.Join(errs...)
}
c, err := extractChunk()
response := &workerResponse{
error: err,
chunk: c,
chunkRange: info.chunkRange,
}
select {
case <-ctx.Done():
case info.resCh <- response:
}
}
// getBlocksFromBatch gets the blocks using batch requests.
// In case of encountering an error during fetching (remote temporarily closed, batch error...),
// the fetch is attempted again using sequential block fetches
func getBlocksFromBatch(chunkRange chunkRange, client Client) ([]*types.Block, error) {
var (
batch = client.CreateBatch()
fetchedBlocks = make([]*types.Block, 0)
)
// Add block requests to the batch
for blockNum := chunkRange.from; blockNum <= chunkRange.to; blockNum++ {
if err := batch.AddBlockRequest(blockNum); err != nil {
return nil, fmt.Errorf(
"unable to add block request for block %d, %w",
blockNum,
err,
)
}
}
// Get the block results
blocksRaw, err := batch.Execute(context.Background())
if err != nil {
// Try to fetch sequentially
return getBlocksSequentially(chunkRange, client)
}
// Extract the blocks
for _, blockRaw := range blocksRaw {
block, ok := blockRaw.(*core_types.ResultBlock)
if !ok {
return nil, errors.New("unable to cast batch result into ResultBlock")
}
// Save block
fetchedBlocks = append(fetchedBlocks, block.Block)
}
return fetchedBlocks, nil
}
// getBlocksSequentially attempts to fetch blocks from the client, using sequential requests
func getBlocksSequentially(chunkRange chunkRange, client Client) ([]*types.Block, error) {
var (
errs = make([]error, 0)
blocks = make([]*types.Block, 0)
)
for blockNum := chunkRange.from; blockNum <= chunkRange.to; blockNum++ {
// Get block info from the chain
block, err := client.GetBlock(blockNum)
if err != nil {
errs = append(errs, fmt.Errorf("unable to get block %d, %w", blockNum, err))
continue
}
blocks = append(blocks, block.Block)
}
return blocks, errors.Join(errs...)
}
// getTxResultFromBatch gets the tx results using batch requests.
// In case of encountering an error during fetching (remote temporarily closed, batch error...),
// the fetch is attempted again using sequential tx result fetches
func getTxResultFromBatch(blocks []*types.Block, client Client) ([][]*types.TxResult, error) {
var (
batch = client.CreateBatch()
fetchedResults = make([][]*types.TxResult, len(blocks))
)
// Create the results request batch
for _, block := range blocks {
if block.NumTxs == 0 {
// No need to request results
// for an empty block
continue
}
// Add the request to the batch
if err := batch.AddBlockResultsRequest(uint64(block.Height)); err != nil {
return nil, fmt.Errorf(
"unable to add block results request for block %d, %w",
block.Height,
err,
)
}
}
// Check if there is anything to execute
if batch.Count() == 0 {
// Batch is empty, nothing to fetch
return fetchedResults, nil
}
// Get the block results
blockResultsRaw, err := batch.Execute(context.Background())
if err != nil {
// Try to fetch sequentially
return getTxResultsSequentially(blocks, client)
}
indexOfBlockHeight := make(map[int64]int, len(blocks))
for index, block := range blocks {
indexOfBlockHeight[block.Height] = index
}
// Extract the results
for resultsIndex, resultsRaw := range blockResultsRaw {
results, ok := resultsRaw.(*core_types.ResultBlockResults)
if !ok {
return nil, errors.New("unable to cast batch result into ResultBlockResults")
}
height := results.Height
deliverTxs := results.Results.DeliverTxs
blockIndex := indexOfBlockHeight[height]
txResults := make([]*types.TxResult, blocks[blockIndex].NumTxs)
for txIndex, tx := range blocks[blockIndex].Txs {
result := &types.TxResult{
Height: height,
Index: uint32(txIndex),
Tx: tx,
Response: deliverTxs[txIndex],
}
txResults[txIndex] = result
}
fetchedResults[resultsIndex] = txResults
}
return fetchedResults, nil
}
// getTxResultsSequentially attempts to fetch tx results from the client, using sequential requests
func getTxResultsSequentially(blocks []*types.Block, client Client) ([][]*types.TxResult, error) {
var (
errs = make([]error, 0)
results = make([][]*types.TxResult, len(blocks))
)
for index, block := range blocks {
if block.NumTxs == 0 {
continue
}
// Get the transaction execution results
blockResults, err := client.GetBlockResults(uint64(block.Height))
if err != nil {
errs = append(
errs,
fmt.Errorf(
"unable to get block results for block %d, %w",
block.Height,
err,
),
)
continue
}
// Save the transaction result
txResults := make([]*types.TxResult, block.NumTxs)
for index, tx := range block.Txs {
result := &types.TxResult{
Height: block.Height,
Index: uint32(index),
Tx: tx,
Response: blockResults.Results.DeliverTxs[index],
}
txResults[index] = result
}
results[index] = txResults
}
return results, errors.Join(errs...)
}