forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_validator_remove.go
71 lines (57 loc) · 1.68 KB
/
genesis_validator_remove.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
package main
import (
"context"
"errors"
"fmt"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto"
)
var errValidatorNotPresent = errors.New("validator not present in genesis.json")
// newValidatorRemoveCmd creates the genesis validator remove subcommand
func newValidatorRemoveCmd(rootCfg *validatorCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "remove",
ShortUsage: "validator remove [flags]",
ShortHelp: "removes a validator from the genesis.json",
},
commands.NewEmptyConfig(),
func(_ context.Context, _ []string) error {
return execValidatorRemove(rootCfg, io)
},
)
}
func execValidatorRemove(cfg *validatorCfg, io commands.IO) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
// Check the validator address
address, err := crypto.AddressFromString(cfg.address)
if err != nil {
return fmt.Errorf("invalid validator address, %w", err)
}
index := -1
for indx, validator := range genesis.Validators {
if validator.Address == address {
index = indx
break
}
}
if index < 0 {
return errors.New("validator not present in genesis.json")
}
// Drop the validator
genesis.Validators = append(genesis.Validators[:index], genesis.Validators[index+1:]...)
// Save the updated genesis
if err := genesis.SaveAs(cfg.genesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)
}
io.Printfln(
"Validator with address %s removed from genesis file",
cfg.address,
)
return nil
}