Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate pos L1 duration #2455

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func CallAddValidator(
}
}
if duration == 0 {
duration, err = PromptDuration(time.Now(), network)
duration, err = PromptDuration(time.Now(), network, pos)
felipemadero marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil
}
Expand Down Expand Up @@ -651,17 +651,19 @@ func CallAddValidatorNonSOV(
return err
}

func PromptDuration(start time.Time, network models.Network) (time.Duration, error) {
func PromptDuration(start time.Time, network models.Network, isPos bool) (time.Duration, error) {
for {
txt := "How long should this validator be validating? Enter a duration, e.g. 8760h. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\""
var d time.Duration
var err error
switch network.Kind {
case models.Fuji:
switch {
case network.Kind == models.Fuji:
d, err = app.Prompt.CaptureFujiDuration(txt)
case models.Mainnet:
case network.Kind == models.Mainnet && isPos:
d, err = app.Prompt.CaptureMainnetL1Duration(txt)
case network.Kind == models.Mainnet && !isPos:
d, err = app.Prompt.CaptureMainnetDuration(txt)
case models.EtnaDevnet:
case network.Kind == models.EtnaDevnet:
d, err = app.Prompt.CaptureEtnaDuration(txt)
default:
d, err = app.Prompt.CaptureDuration(txt)
Expand Down Expand Up @@ -756,7 +758,7 @@ func getTimeParameters(network models.Network, nodeID ids.NodeID, isValidator bo
case defaultDurationOption:
useDefaultDuration = true
default:
duration, err = PromptDuration(start, network)
duration, err = PromptDuration(start, network, false) // notSoV
if err != nil {
return time.Time{}, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/nodecmd/validate_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func GetTimeParametersPrimaryNetwork(network models.Network, nodeIndex int, vali
}
default:
useCustomDuration = true
duration, err = blockchaincmd.PromptDuration(start, network)
duration, err = blockchaincmd.PromptDuration(start, network, false) // not L1
if err != nil {
return time.Time{}, 0, err
}
Expand Down
28 changes: 28 additions & 0 deletions internal/mocks/prompter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/prompts/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Prompter interface {
CaptureEtnaDuration(promptStr string) (time.Duration, error)
CaptureFujiDuration(promptStr string) (time.Duration, error)
CaptureMainnetDuration(promptStr string) (time.Duration, error)
CaptureMainnetL1Duration(promptStr string) (time.Duration, error)
CaptureDate(promptStr string) (time.Time, error)
CaptureNodeID(promptStr string) (ids.NodeID, error)
CaptureID(promptStr string) (ids.ID, error)
Expand Down Expand Up @@ -263,6 +264,20 @@ func (*realPrompter) CaptureMainnetDuration(promptStr string) (time.Duration, er
return time.ParseDuration(durationStr)
}

func (*realPrompter) CaptureMainnetL1Duration(promptStr string) (time.Duration, error) {
felipemadero marked this conversation as resolved.
Show resolved Hide resolved
prompt := promptui.Prompt{
Label: promptStr,
Validate: validateMainnetL1StakingDuration,
}

durationStr, err := prompt.Run()
if err != nil {
return 0, err
}

return time.ParseDuration(durationStr)
}

func (*realPrompter) CaptureDate(promptStr string) (time.Time, error) {
prompt := promptui.Prompt{
Label: promptStr,
Expand Down
15 changes: 15 additions & 0 deletions pkg/prompts/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func validateMainnetStakingDuration(input string) error {
return nil
}

func validateMainnetL1StakingDuration(input string) error {
const minL1StakingDuration = 24 * time.Hour
felipemadero marked this conversation as resolved.
Show resolved Hide resolved
d, err := time.ParseDuration(input)
if err != nil {
return err
}
if d > genesis.MainnetParams.MaxStakeDuration {
return fmt.Errorf("exceeds maximum staking duration of %s", ux.FormatDuration(genesis.MainnetParams.MaxStakeDuration))
}
if d < minL1StakingDuration {
return fmt.Errorf("below the minimum staking duration of %s", ux.FormatDuration(genesis.MainnetParams.MinStakeDuration))
felipemadero marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

func validateFujiStakingDuration(input string) error {
d, err := time.ParseDuration(input)
if err != nil {
Expand Down
Loading