-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d932213
commit 600f02e
Showing
5 changed files
with
238 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(validatorCfg *validatorCfg, io *commands.IO) *commands.Command { | ||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "remove", | ||
ShortUsage: "validator remove [flags]", | ||
LongHelp: "Removes a validator from the genesis.json", | ||
}, | ||
commands.NewEmptyConfig(), | ||
func(_ context.Context, _ []string) error { | ||
return execValidatorRemove(validatorCfg, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/bft/types" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
"github.com/gnolang/gno/tm2/pkg/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenesis_Validator_Remove(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("invalid genesis file", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"validator", | ||
"remove", | ||
"--genesis-path", | ||
"dummy-path", | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
assert.ErrorContains(t, cmdErr, "unable to load genesis") | ||
}) | ||
|
||
t.Run("invalid validator address", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempGenesis, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
genesis := getDefaultGenesis() | ||
require.NoError(t, genesis.SaveAs(tempGenesis.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"validator", | ||
"remove", | ||
"--genesis-path", | ||
tempGenesis.Name(), | ||
"--address", | ||
"dummyaddress", | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
assert.ErrorContains(t, cmdErr, "invalid validator address") | ||
}) | ||
|
||
t.Run("validator not found", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempGenesis, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
dummyKeys := []keys.Info{ | ||
getDummyKey(t), | ||
getDummyKey(t), | ||
} | ||
|
||
genesis := getDefaultGenesis() | ||
|
||
// Set an existing validator | ||
genesis.Validators = append(genesis.Validators, types.GenesisValidator{ | ||
Address: dummyKeys[0].GetAddress(), | ||
PubKey: dummyKeys[0].GetPubKey(), | ||
Power: 1, | ||
Name: "example", | ||
}) | ||
|
||
require.NoError(t, genesis.SaveAs(tempGenesis.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"validator", | ||
"remove", | ||
"--genesis-path", | ||
tempGenesis.Name(), | ||
"--address", | ||
dummyKeys[1].GetPubKey().Address().String(), | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
assert.ErrorContains(t, cmdErr, errValidatorNotPresent.Error()) | ||
}) | ||
|
||
t.Run("validator removed", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempGenesis, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
dummyKey := getDummyKey(t) | ||
|
||
genesis := getDefaultGenesis() | ||
|
||
// Set an existing validator | ||
genesis.Validators = append(genesis.Validators, types.GenesisValidator{ | ||
Address: dummyKey.GetAddress(), | ||
PubKey: dummyKey.GetPubKey(), | ||
Power: 1, | ||
Name: "example", | ||
}) | ||
|
||
require.NoError(t, genesis.SaveAs(tempGenesis.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"validator", | ||
"remove", | ||
"--genesis-path", | ||
tempGenesis.Name(), | ||
"--address", | ||
dummyKey.GetPubKey().Address().String(), | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
assert.NoError(t, cmdErr) | ||
}) | ||
} |