-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description This PR adds the `genesis verify` subcommand. Closes #1229 <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details>
- Loading branch information
1 parent
5033232
commit c128bf6
Showing
6 changed files
with
381 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/gno.land/pkg/gnoland" | ||
"github.com/gnolang/gno/tm2/pkg/bft/types" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/std" | ||
) | ||
|
||
type verifyCfg struct { | ||
genesisPath string | ||
} | ||
|
||
// newVerifyCmd creates the genesis verify subcommand | ||
func newVerifyCmd(io *commands.IO) *commands.Command { | ||
cfg := &verifyCfg{} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "verify", | ||
ShortUsage: "verify [flags]", | ||
LongHelp: "Verifies a node's genesis.json", | ||
}, | ||
cfg, | ||
func(_ context.Context, _ []string) error { | ||
return execVerify(cfg, io) | ||
}, | ||
) | ||
} | ||
|
||
func (c *verifyCfg) RegisterFlags(fs *flag.FlagSet) { | ||
fs.StringVar( | ||
&c.genesisPath, | ||
"genesis-path", | ||
"./genesis.json", | ||
"the path to the genesis.json", | ||
) | ||
} | ||
|
||
func execVerify(cfg *verifyCfg, 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) | ||
} | ||
|
||
// Verify it | ||
if validateErr := genesis.Validate(); validateErr != nil { | ||
return fmt.Errorf("unable to verify genesis, %w", validateErr) | ||
} | ||
|
||
// Validate the genesis state | ||
state := genesis.AppState.(gnoland.GnoGenesisState) | ||
|
||
// Validate the initial transactions | ||
for _, tx := range state.Txs { | ||
if validateErr := tx.ValidateBasic(); validateErr != nil { | ||
return fmt.Errorf("invalid transacton, %w", validateErr) | ||
} | ||
} | ||
|
||
// Validate the initial balances | ||
for _, balance := range state.Balances { | ||
if _, parseErr := std.ParseCoins(balance); parseErr != nil { | ||
return fmt.Errorf("invalid balance %s, %w", balance, parseErr) | ||
} | ||
} | ||
|
||
io.Printfln("Genesis at %s is valid", cfg.genesisPath) | ||
|
||
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,124 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gnolang/gno/gno.land/pkg/gnoland" | ||
"github.com/gnolang/gno/tm2/pkg/bft/types" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/mock" | ||
"github.com/gnolang/gno/tm2/pkg/std" | ||
"github.com/gnolang/gno/tm2/pkg/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenesis_Verify(t *testing.T) { | ||
t.Parallel() | ||
|
||
getValidTestGenesis := func() *types.GenesisDoc { | ||
key := mock.GenPrivKey().PubKey() | ||
|
||
return &types.GenesisDoc{ | ||
GenesisTime: time.Now(), | ||
ChainID: "valid-chain-id", | ||
ConsensusParams: types.DefaultConsensusParams(), | ||
Validators: []types.GenesisValidator{ | ||
{ | ||
Address: key.Address(), | ||
PubKey: key, | ||
Power: 1, | ||
Name: "valid validator", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
t.Run("invalid txs", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempFile, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
g := getValidTestGenesis() | ||
|
||
g.AppState = gnoland.GnoGenesisState{ | ||
Balances: []string{}, | ||
Txs: []std.Tx{ | ||
{}, | ||
}, | ||
} | ||
|
||
require.NoError(t, g.SaveAs(tempFile.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"verify", | ||
"--genesis-path", | ||
tempFile.Name(), | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
require.Error(t, cmdErr) | ||
}) | ||
|
||
t.Run("invalid balances", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempFile, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
g := getValidTestGenesis() | ||
|
||
g.AppState = gnoland.GnoGenesisState{ | ||
Balances: []string{ | ||
"dummybalance", | ||
}, | ||
Txs: []std.Tx{}, | ||
} | ||
|
||
require.NoError(t, g.SaveAs(tempFile.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"verify", | ||
"--genesis-path", | ||
tempFile.Name(), | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
require.Error(t, cmdErr) | ||
}) | ||
|
||
t.Run("valid genesis", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempFile, cleanup := testutils.NewTestFile(t) | ||
t.Cleanup(cleanup) | ||
|
||
g := getValidTestGenesis() | ||
g.AppState = gnoland.GnoGenesisState{ | ||
Balances: []string{}, | ||
Txs: []std.Tx{}, | ||
} | ||
|
||
require.NoError(t, g.SaveAs(tempFile.Name())) | ||
|
||
// Create the command | ||
cmd := newRootCmd(commands.NewTestIO()) | ||
args := []string{ | ||
"verify", | ||
"--genesis-path", | ||
tempFile.Name(), | ||
} | ||
|
||
// Run the command | ||
cmdErr := cmd.ParseAndRun(context.Background(), args) | ||
require.NoError(t, cmdErr) | ||
}) | ||
} |
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
Oops, something went wrong.