-
Notifications
You must be signed in to change notification settings - Fork 178
/
read_protocol_state_blocks.go
214 lines (182 loc) · 5.48 KB
/
read_protocol_state_blocks.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
package common
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"strings"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
storageCommands "github.com/onflow/flow-go/admin/commands/storage"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
var _ commands.AdminCommand = (*ReadProtocolStateBlocksCommand)(nil)
type requestType int
const (
ID requestType = iota
Height
Final
Sealed
)
type requestData struct {
requestType requestType
blockID flow.Identifier
blockHeight uint64
numBlocksToQuery uint
}
type ReadProtocolStateBlocksCommand struct {
state protocol.State
blocks storage.Blocks
}
func (r *ReadProtocolStateBlocksCommand) getBlockByHeight(height uint64) (*flow.Block, error) {
header, err := r.state.AtHeight(height).Head()
if err != nil {
return nil, fmt.Errorf("could not get header by height: %v, %w", height, err)
}
block, err := r.getBlockByHeader(header)
if err != nil {
return nil, fmt.Errorf("could not get block by header: %w", err)
}
return block, nil
}
func (r *ReadProtocolStateBlocksCommand) getFinal() (*flow.Block, error) {
header, err := r.state.Final().Head()
if err != nil {
return nil, fmt.Errorf("could not get finalized, %w", err)
}
block, err := r.getBlockByHeader(header)
if err != nil {
return nil, fmt.Errorf("could not get block by header: %w", err)
}
return block, nil
}
func (r *ReadProtocolStateBlocksCommand) getSealed() (*flow.Block, error) {
header, err := r.state.Sealed().Head()
if err != nil {
return nil, fmt.Errorf("could not get sealed block, %w", err)
}
block, err := r.getBlockByHeader(header)
if err != nil {
return nil, fmt.Errorf("could not get block by header: %w", err)
}
return block, nil
}
func (r *ReadProtocolStateBlocksCommand) getBlockByID(blockID flow.Identifier) (*flow.Block, error) {
header, err := r.state.AtBlockID(blockID).Head()
if err != nil {
return nil, fmt.Errorf("could not get header by blockID: %v, %w", blockID, err)
}
block, err := r.getBlockByHeader(header)
if err != nil {
return nil, fmt.Errorf("could not get block by header: %w", err)
}
return block, nil
}
func (r *ReadProtocolStateBlocksCommand) getBlockByHeader(header *flow.Header) (*flow.Block, error) {
blockID := header.ID()
block, err := r.blocks.ByID(blockID)
if err != nil {
return nil, fmt.Errorf("could not get block by ID %v: %w", blockID, err)
}
return block, nil
}
func (r *ReadProtocolStateBlocksCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) {
data := req.ValidatorData.(*requestData)
var result []*flow.Block
var block *flow.Block
var err error
switch data.requestType {
case ID:
block, err = r.getBlockByID(data.blockID)
case Height:
block, err = r.getBlockByHeight(data.blockHeight)
case Final:
block, err = r.getFinal()
case Sealed:
block, err = r.getSealed()
}
if err != nil {
return nil, err
}
result = append(result, block)
firstHeight := int64(block.Header.Height)
for height := firstHeight - 1; height >= 0 && height > firstHeight-int64(data.numBlocksToQuery); height-- {
block, err = r.getBlockByHeight(uint64(height))
if err != nil {
return nil, err
}
result = append(result, block)
}
var resultList []interface{}
bytes, err := json.Marshal(result)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &resultList)
return resultList, err
}
// Validator validates the request.
// Returns admin.InvalidAdminReqError for invalid/malformed requests.
func (r *ReadProtocolStateBlocksCommand) Validator(req *admin.CommandRequest) error {
input, ok := req.Data.(map[string]interface{})
if !ok {
return admin.NewInvalidAdminReqFormatError("expected map[string]any")
}
inBlock, ok := input["block"]
if !ok {
return admin.NewInvalidAdminReqErrorf("missing 'block' field")
}
data := &requestData{}
switch block := inBlock.(type) {
case string:
block = strings.ToLower(strings.TrimSpace(block))
if block == storageCommands.FINAL {
data.requestType = Final
} else if block == storageCommands.SEALED {
data.requestType = Sealed
} else if len(block) == 2*flow.IdentifierLen {
b, err := hex.DecodeString(block)
if err != nil {
return admin.NewInvalidAdminReqParameterError("block", "block ID must be 64-char hex string", inBlock)
}
data.requestType = ID
data.blockID = flow.HashToID(b)
} else {
return admin.NewInvalidAdminReqParameterError("block", "must be 'final', 'sealed', or block ID hex", inBlock)
}
case float64:
if block < 0 || math.Trunc(block) != block {
return admin.NewInvalidAdminReqParameterError("block", "block height must be >=0 and integral", inBlock)
}
data.requestType = Height
data.blockHeight = uint64(block)
default:
return admin.NewInvalidAdminReqParameterError("block", "must be string or number", inBlock)
}
if inN, ok := input["n"]; ok {
n, ok := inN.(float64)
if !ok {
return admin.NewInvalidAdminReqParameterError("n", "must be number", inN)
}
if math.Trunc(n) != n {
return admin.NewInvalidAdminReqParameterError("n", "must be integral", inN)
}
if n < 1 {
return admin.NewInvalidAdminReqParameterError("n", "must be >=1", inN)
}
data.numBlocksToQuery = uint(n)
} else {
data.numBlocksToQuery = 1
}
req.ValidatorData = data
return nil
}
func NewReadProtocolStateBlocksCommand(state protocol.State, storage storage.Blocks) commands.AdminCommand {
return &ReadProtocolStateBlocksCommand{
state,
storage,
}
}