feat: add invariant check command (#1009)

* add genesis file invariant assertion command

* fix: use correct cmd function name

* update migration guide to add invariant check
This commit is contained in:
Kevin Davis 2021-08-26 17:53:46 -05:00 committed by GitHub
parent ae70dce2b1
commit 8d0bb5a5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 0 deletions

View File

@ -72,6 +72,12 @@ func NewTestApp() TestApp {
return TestApp{App: *app} return TestApp{App: *app}
} }
func NewTestAppFromSealed() TestApp {
db := tmdb.NewMemDB()
app := NewApp(log.NewNopLogger(), db, nil, AppOptions{})
return TestApp{App: *app}
}
// nolint // nolint
func (tApp TestApp) GetAccountKeeper() auth.AccountKeeper { return tApp.accountKeeper } func (tApp TestApp) GetAccountKeeper() auth.AccountKeeper { return tApp.accountKeeper }
func (tApp TestApp) GetBankKeeper() bank.Keeper { return tApp.bankKeeper } func (tApp TestApp) GetBankKeeper() bank.Keeper { return tApp.bankKeeper }

View File

@ -57,6 +57,7 @@ func main() {
genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome), genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, app.DefaultNodeHome), genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, app.DefaultNodeHome),
migrate.MigrateGenesisCmd(ctx, cdc), migrate.MigrateGenesisCmd(ctx, cdc),
migrate.AssertInvariantsCmd(ctx, cdc),
genutilcli.GenTxCmd( genutilcli.GenTxCmd(
ctx, ctx,
cdc, cdc,

View File

@ -7,9 +7,11 @@ import (
"github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/cobra" "github.com/spf13/cobra"
tmtypes "github.com/tendermint/tendermint/types" tmtypes "github.com/tendermint/tendermint/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/migrate/v0_15" "github.com/kava-labs/kava/migrate/v0_15"
) )
@ -48,3 +50,38 @@ func MigrateGenesisCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
return cmd return cmd
} }
func AssertInvariantsCmd(_ *server.Context, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "assert-invariants [genesis-file]",
Short: "Validates that the input genesis file is valid and invariants pass",
Long: "Reads the input genesis file into a genesis document, checks that the state is valid and asserts that all invariants pass.",
Example: fmt.Sprintf(`%s assert-invariants /path/to/genesis.json`, version.ServerName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
importGenesis := args[0]
genDoc, err := tmtypes.GenesisDocFromFile(importGenesis)
if err != nil {
return fmt.Errorf("failed to read genesis document from file %s: %w", importGenesis, err)
}
tApp := app.NewTestAppFromSealed()
var newAppState genutil.AppMap
cdc := app.MakeCodec()
err = cdc.UnmarshalJSON(genDoc.AppState, &newAppState)
if err != nil {
return fmt.Errorf("failed to marchal app state from genesis doc: %s: %w", importGenesis, err)
}
err = app.ModuleBasics.ValidateGenesis(newAppState)
if err != nil {
return fmt.Errorf("genesis doc did not pass validate genesis: %s: %w", importGenesis, err)
}
tApp.InitializeFromGenesisStatesWithTimeAndChainID(genDoc.GenesisTime, genDoc.ChainID, app.GenesisState(newAppState))
fmt.Printf("successfully asserted all invariants for %s\n", importGenesis)
return nil
},
}
return cmd
}

View File

@ -79,6 +79,7 @@ kvd export --for-zero-height --height 1878509 > export-genesis.json
# Verify output of genesis migration # Verify output of genesis migration
kvd validate-genesis genesis.json # should say it's valid kvd validate-genesis genesis.json # should say it's valid
kvd assert-invariants genesis.json # should say invariants pass
jq -S -c -M '' genesis.json | shasum -a 256 jq -S -c -M '' genesis.json | shasum -a 256
# PLACEHOLDER # PLACEHOLDER