-
Notifications
You must be signed in to change notification settings - Fork 175
/
stop_at_height.go
97 lines (79 loc) · 2.59 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
89
90
91
92
93
94
95
96
97
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/stop"
)
var _ commands.AdminCommand = (*StopAtHeightCommand)(nil)
// StopAtHeightCommand will send a signal to engine to stop/crash EN
// at given height
type StopAtHeightCommand struct {
stopControl *stop.StopControl
}
// NewStopAtHeightCommand creates a new StopAtHeightCommand object
func NewStopAtHeightCommand(sah *stop.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)
oldParams := s.stopControl.GetStopParameters()
newParams := stop.StopParameters{
StopBeforeHeight: sah.height,
ShouldCrash: sah.crash,
}
err := s.stopControl.SetStopParameters(newParams)
if err != nil {
return nil, err
}
log.Info().
Interface("newParams", newParams).
Interface("oldParams", oldParams).
Msgf("admintool: New En stop parameters set")
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
}