-
Notifications
You must be signed in to change notification settings - Fork 179
/
read_results.go
120 lines (102 loc) · 3.27 KB
/
read_results.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
package storage
import (
"context"
"fmt"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
var _ commands.AdminCommand = (*ReadResultsCommand)(nil)
type readResultsRequestType int
const (
readResultsRequestByID readResultsRequestType = iota
readResultsRequestByBlock
)
type readResultsRequest struct {
requestType readResultsRequestType
value interface{}
numResultsToQuery uint64
}
type ReadResultsCommand struct {
state protocol.State
results storage.ExecutionResults
}
func (r *ReadResultsCommand) Handler(ctx context.Context, req *admin.CommandRequest) (interface{}, error) {
data := req.ValidatorData.(*readResultsRequest)
var results []*flow.ExecutionResult
var resultID flow.Identifier
n := data.numResultsToQuery
switch data.requestType {
case readResultsRequestByID:
resultID = data.value.(flow.Identifier)
case readResultsRequestByBlock:
if header, err := getBlockHeader(r.state, data.value.(*blocksRequest)); err != nil {
return nil, fmt.Errorf("failed to get block header: %w", err)
} else if result, err := r.results.ByBlockID(header.ID()); err != nil {
return nil, fmt.Errorf("failed to get result by block ID: %w", err)
} else {
results = append(results, result)
resultID = result.PreviousResultID
n -= 1
}
}
for i := uint64(0); i < n && resultID != flow.ZeroID; i++ {
result, err := r.results.ByID(resultID)
if err != nil {
return nil, fmt.Errorf("failed to get result by ID: %w", err)
}
results = append(results, result)
resultID = result.PreviousResultID
}
return commands.ConvertToInterfaceList(results)
}
// Validator validates the request.
// Returns admin.InvalidAdminReqError for invalid/malformed requests.
func (r *ReadResultsCommand) Validator(req *admin.CommandRequest) error {
input, ok := req.Data.(map[string]interface{})
if !ok {
return admin.NewInvalidAdminReqFormatError("expected map[string]any")
}
data := &readResultsRequest{}
if resultIn, ok := input["result"]; ok {
errInvalidResultValue := admin.NewInvalidAdminReqParameterError("result", "expected a result ID represented as a 64 character long hex string", resultIn)
result, ok := resultIn.(string)
if !ok {
return errInvalidResultValue
}
resultID, err := flow.HexStringToIdentifier(result)
if err != nil {
return errInvalidResultValue
}
data.requestType = readResultsRequestByID
data.value = resultID
} else if block, ok := input["block"]; ok {
br, err := parseBlocksRequest(block)
if err != nil {
return admin.NewInvalidAdminReqErrorf("invalid 'block' field: %w", err)
}
data.requestType = readResultsRequestByBlock
data.value = br
} else {
return admin.NewInvalidAdminReqErrorf("either \"block\" or \"result\" field is required")
}
if n, ok := input["n"]; ok {
if n, err := parseN(n); err != nil {
return admin.NewInvalidAdminReqErrorf("invalid 'n' field: %w", err)
} else {
data.numResultsToQuery = n
}
} else {
data.numResultsToQuery = 1
}
req.ValidatorData = data
return nil
}
func NewReadResultsCommand(state protocol.State, storage storage.ExecutionResults) commands.AdminCommand {
return &ReadResultsCommand{
state,
storage,
}
}