-
Notifications
You must be signed in to change notification settings - Fork 176
/
stop_at_height.go
88 lines (71 loc) · 2.49 KB
/
stop_at_height.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
package execution
import (
"context"
"github.com/rs/zerolog/log"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
"github.com/onflow/flow-go/engine/execution/ingestion"
)
var _ commands.AdminCommand = (*StopAtHeightCommand)(nil)
// StopAtHeightCommand will send a signal to engine to stop/crash EN
// at given height
type StopAtHeightCommand struct {
stopControl *ingestion.StopControl
}
// NewStopAtHeightCommand creates a new StopAtHeightCommand object
func NewStopAtHeightCommand(sah *ingestion.StopControl) *StopAtHeightCommand {
return &StopAtHeightCommand{
stopControl: sah,
}
}
type StopAtHeightReq struct {
height uint64
crash bool
}
// Handler method sets the stop height parameters.
// Errors only if setting of stop height parameters fails.
// Returns "ok" if successful.
func (s *StopAtHeightCommand) Handler(_ context.Context, req *admin.CommandRequest) (interface{}, error) {
sah := req.ValidatorData.(StopAtHeightReq)
oldHeight, oldCrash, err := s.stopControl.SetStopHeight(sah.height, sah.crash)
if err != nil {
return nil, err
}
log.Info().Msgf("admintool: EN will stop at height %d and crash: %t, previous values: %d %t", sah.height, sah.crash, oldHeight, oldCrash)
return "ok", nil
}
// Validator checks the inputs for StopAtHeight command.
// It expects the following fields in the Data field of the req object:
// - height in a numeric format
// - crash, a boolean
//
// Additionally, height must be a positive integer. If a float value is provided, only the integer part is used.
// The following sentinel errors are expected during normal operations:
// * `admin.InvalidAdminReqError` if any required field is missing or in a wrong format
func (s *StopAtHeightCommand) Validator(req *admin.CommandRequest) error {
input, ok := req.Data.(map[string]interface{})
if !ok {
return admin.NewInvalidAdminReqFormatError("expected map[string]any")
}
result, ok := input["height"]
if !ok {
return admin.NewInvalidAdminReqErrorf("missing required field: 'height'")
}
height, ok := result.(float64)
if !ok || height <= 0 {
return admin.NewInvalidAdminReqParameterError("height", "must be number >=0", result)
}
result, ok = input["crash"]
if !ok {
return admin.NewInvalidAdminReqErrorf("missing required field: 'crash'")
}
crash, ok := result.(bool)
if !ok {
return admin.NewInvalidAdminReqParameterError("crash", "must be bool", result)
}
req.ValidatorData = StopAtHeightReq{
height: uint64(height),
crash: crash,
}
return nil
}