0g-chain/cmd/kvd/main.go

177 lines
5.3 KiB
Go
Raw Normal View History

2018-05-25 13:46:33 +00:00
package main
import (
"encoding/json"
"fmt"
2018-08-14 22:13:54 +00:00
"io"
2018-05-25 13:46:33 +00:00
"github.com/spf13/cobra"
2018-08-14 22:13:54 +00:00
"github.com/spf13/viper"
2018-05-25 13:46:33 +00:00
2020-04-30 14:23:41 +00:00
dbm "github.com/tendermint/tm-db"
2018-08-14 22:13:54 +00:00
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
2018-06-16 21:34:07 +00:00
tmtypes "github.com/tendermint/tendermint/types"
2018-05-25 13:46:33 +00:00
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2019-07-18 18:05:26 +00:00
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking"
2019-06-20 17:08:58 +00:00
"github.com/kava-labs/kava/app"
v0.8 Migration Scripts (#518) * initial sketch * add module migrations * add migrations for all accout types * test account migration * add tendermint migration and migrate cmd * remove need for errors pkg dependency * add bech32 decoding fork * add suggested params and cmd to write them * add basic upgrade instructions * fix tests * address some migration todos * tidy contrib folder * finalize params values * align cdp init genesis with other modules * add tendermint and distribution test add custom distribution migration to patch bug * add staking migration test * add slashing, evidence tests, refactor auth tests * add full migration test * remove go-amino dependency from go.mod also tidy up unused indirect dependencies * address remaining TODOs * remove commented out code from legacy types * add spot/liquidation markets ids to kava-3 params * Apply suggestions from code review Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * address code review suggestions * add validate genesis to migrate test * refactor add params func * remove commented out code from old types * fix add params * add deputy address * add tests using exported kava-2 state * incorporate new cdp params from master * update params from review Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * add deputy account * add committee permissions for new params Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
2020-06-03 19:35:00 +00:00
kava3 "github.com/kava-labs/kava/contrib/kava-3"
"github.com/kava-labs/kava/migrate"
2018-05-25 13:46:33 +00:00
)
2019-06-20 17:08:58 +00:00
// kvd custom flags
2019-06-07 11:59:19 +00:00
const flagInvCheckPeriod = "inv-check-period"
var invCheckPeriod uint
2018-05-25 13:46:33 +00:00
func main() {
2019-06-07 11:59:19 +00:00
cdc := app.MakeCodec()
2018-08-14 22:13:54 +00:00
2019-06-07 11:59:19 +00:00
config := sdk.GetConfig()
2019-06-25 13:29:56 +00:00
app.SetBech32AddressPrefixes(config)
app.SetBip44CoinType(config)
2019-06-07 11:59:19 +00:00
config.Seal()
2018-05-25 13:46:33 +00:00
2019-06-07 11:59:19 +00:00
ctx := server.NewDefaultContext()
2018-08-14 22:13:54 +00:00
cobra.EnableCommandSorting = false
2018-05-25 13:46:33 +00:00
rootCmd := &cobra.Command{
2019-06-20 17:08:58 +00:00
Use: "kvd",
2019-06-20 17:28:21 +00:00
Short: "Kava Daemon (server)",
PersistentPreRunE: persistentPreRunEFn(ctx),
2018-05-25 13:46:33 +00:00
}
2020-05-07 20:34:05 +00:00
rootCmd.AddCommand(
genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(ctx, cdc, auth.GenesisAccountIterator{}, app.DefaultNodeHome),
v0.8 Migration Scripts (#518) * initial sketch * add module migrations * add migrations for all accout types * test account migration * add tendermint migration and migrate cmd * remove need for errors pkg dependency * add bech32 decoding fork * add suggested params and cmd to write them * add basic upgrade instructions * fix tests * address some migration todos * tidy contrib folder * finalize params values * align cdp init genesis with other modules * add tendermint and distribution test add custom distribution migration to patch bug * add staking migration test * add slashing, evidence tests, refactor auth tests * add full migration test * remove go-amino dependency from go.mod also tidy up unused indirect dependencies * address remaining TODOs * remove commented out code from legacy types * add spot/liquidation markets ids to kava-3 params * Apply suggestions from code review Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * address code review suggestions * add validate genesis to migrate test * refactor add params func * remove commented out code from old types * fix add params * add deputy address * add tests using exported kava-2 state * incorporate new cdp params from master * update params from review Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * add deputy account * add committee permissions for new params Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
2020-06-03 19:35:00 +00:00
migrate.MigrateGenesisCmd(ctx, cdc),
writeParamsAndConfigCmd(cdc),
2020-05-07 20:34:05 +00:00
genutilcli.GenTxCmd(
ctx,
cdc,
app.ModuleBasics,
staking.AppModuleBasic{},
auth.GenesisAccountIterator{},
app.DefaultNodeHome,
app.DefaultCLIHome),
genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics),
AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome),
2020-06-12 01:40:46 +00:00
testnetCmd(ctx, cdc, app.ModuleBasics, auth.GenesisAccountIterator{}),
2020-05-07 20:34:05 +00:00
flags.NewCompletionCmd(rootCmd, true),
)
2019-06-07 11:59:19 +00:00
server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)
// prepare and add flags
2019-07-18 18:05:26 +00:00
executor := cli.PrepareBaseCmd(rootCmd, "KA", app.DefaultNodeHome)
2019-06-07 11:59:19 +00:00
rootCmd.PersistentFlags().UintVar(&invCheckPeriod, flagInvCheckPeriod,
0, "Assert registered invariants every N blocks")
2018-08-14 22:13:54 +00:00
err := executor.Execute()
if err != nil {
panic(err)
}
2018-05-25 13:46:33 +00:00
}
2018-08-14 22:13:54 +00:00
func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application {
2019-09-11 22:42:35 +00:00
var cache sdk.MultiStorePersistentCache
if viper.GetBool(server.FlagInterBlockCache) {
cache = store.NewCommitKVStoreCacheManager()
}
2020-05-07 20:34:05 +00:00
skipUpgradeHeights := make(map[int64]bool)
for _, h := range viper.GetIntSlice(server.FlagUnsafeSkipUpgrades) {
skipUpgradeHeights[int64(h)] = true
}
2019-06-20 17:02:29 +00:00
return app.NewApp(
2020-05-07 20:34:05 +00:00
logger, db, traceStore, true, skipUpgradeHeights, invCheckPeriod,
2019-06-07 11:59:19 +00:00
baseapp.SetPruning(store.NewPruningOptionsFromString(viper.GetString("pruning"))),
baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)),
2019-09-11 22:42:35 +00:00
baseapp.SetHaltHeight(viper.GetUint64(server.FlagHaltHeight)),
baseapp.SetHaltTime(viper.GetUint64(server.FlagHaltTime)),
baseapp.SetInterBlockCache(cache),
2019-06-07 11:59:19 +00:00
)
2018-10-02 22:50:49 +00:00
}
2019-06-07 11:59:19 +00:00
func exportAppStateAndTMValidators(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailWhiteList []string,
) (json.RawMessage, []tmtypes.GenesisValidator, error) {
2018-10-02 22:50:49 +00:00
2019-06-07 11:59:19 +00:00
if height != -1 {
2020-05-07 20:34:05 +00:00
tempApp := app.NewApp(logger, db, traceStore, false, map[int64]bool{}, uint(1))
2019-07-18 18:05:26 +00:00
err := tempApp.LoadHeight(height)
2019-06-07 11:59:19 +00:00
if err != nil {
return nil, nil, err
2018-10-02 22:50:49 +00:00
}
2019-07-18 18:05:26 +00:00
return tempApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
2018-10-03 17:11:13 +00:00
}
2020-05-07 20:34:05 +00:00
tempApp := app.NewApp(logger, db, traceStore, true, map[int64]bool{}, uint(1))
2019-07-18 18:05:26 +00:00
return tempApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
2018-10-03 17:11:13 +00:00
}
// persistentPreRunEFn wraps the sdk function server.PersistentPreRunEFn to error on invaid pruning config.
func persistentPreRunEFn(ctx *server.Context) func(*cobra.Command, []string) error {
originalFunc := server.PersistentPreRunEFn(ctx)
return func(cmd *cobra.Command, args []string) error {
if err := originalFunc(cmd, args); err != nil {
return err
}
// check pruning config for `kvd start`
if cmd.Name() == "start" {
if viper.GetString("pruning") == store.PruningStrategySyncable {
return fmt.Errorf(
"invalid app config: pruning == '%s'. Update config (%s) with pruning set to '%s' or '%s'.",
store.PruningStrategySyncable, viper.ConfigFileUsed(), store.PruningStrategyNothing, store.PruningStrategyEverything,
)
}
}
return nil
}
}
// writeParamsAndConfigCmd patches the write-params cmd to additionally update the app pruning config.
func writeParamsAndConfigCmd(cdc *codec.Codec) *cobra.Command {
cmd := kava3.WriteGenesisParamsCmd(cdc)
originalFunc := cmd.RunE
wrappedFunc := func(cmd *cobra.Command, args []string) error {
if err := originalFunc(cmd, args); err != nil {
return err
}
// fetch the app config from viper
cfg, err := srvconfig.ParseConfig()
if err != nil {
return nil // don't return errors since as failures aren't critical
}
// don't prune any state, ie store everything
cfg.Pruning = store.PruningStrategyNothing
// write updated config
if viper.ConfigFileUsed() == "" {
return nil
}
srvconfig.WriteConfigFile(viper.ConfigFileUsed(), cfg)
return nil
}
cmd.RunE = wrappedFunc
return cmd
}