-
Notifications
You must be signed in to change notification settings - Fork 179
/
read_blocks.go
93 lines (76 loc) · 2.23 KB
/
read_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
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 = (*ReadBlocksCommand)(nil)
type readBlocksRequest struct {
blocksRequest *blocksRequest
numBlocksToQuery uint64
}
type ReadBlocksCommand struct {
state protocol.State
blocks storage.Blocks
}
func (r *ReadBlocksCommand) Handler(ctx context.Context, req *admin.CommandRequest) (interface{}, error) {
data := req.ValidatorData.(*readBlocksRequest)
var result []*flow.Block
var blockID flow.Identifier
if header, err := getBlockHeader(r.state, data.blocksRequest); err != nil {
return nil, fmt.Errorf("failed to get block header: %w", err)
} else {
blockID = header.ID()
}
for i := uint64(0); i < data.numBlocksToQuery; i++ {
block, err := r.blocks.ByID(blockID)
if err != nil {
return nil, fmt.Errorf("failed to get block by ID: %w", err)
}
result = append(result, block)
if block.Header.Height == 0 {
break
}
blockID = block.Header.ParentID
}
return commands.ConvertToInterfaceList(result)
}
// Validator validates the request.
// Returns admin.InvalidAdminReqError for invalid/malformed requests.
func (r *ReadBlocksCommand) Validator(req *admin.CommandRequest) error {
input, ok := req.Data.(map[string]interface{})
if !ok {
return admin.NewInvalidAdminReqFormatError("expected map[string]any")
}
block, ok := input["block"]
if !ok {
return admin.NewInvalidAdminReqErrorf("the \"block\" field is required")
}
data := &readBlocksRequest{}
if blocksRequest, err := parseBlocksRequest(block); err != nil {
return admin.NewInvalidAdminReqErrorf("invalid 'block' field: %w", err)
} else {
data.blocksRequest = blocksRequest
}
if n, ok := input["n"]; ok {
if n, err := parseN(n); err != nil {
return admin.NewInvalidAdminReqErrorf("invalid 'n' field: %w", err)
} else {
data.numBlocksToQuery = n
}
} else {
data.numBlocksToQuery = 1
}
req.ValidatorData = data
return nil
}
func NewReadBlocksCommand(state protocol.State, storage storage.Blocks) commands.AdminCommand {
return &ReadBlocksCommand{
state,
storage,
}
}