From bed13911f32c1c396049b5de90b8f1cb68a08447 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 21 Dec 2023 19:35:13 -0700 Subject: [PATCH 1/3] refactor: auto-set timeout-commit to 4s --- .vscode/launch.json | 12 +++++++ CHANGELOG.md | 4 +++ cmd/osmosisd/cmd/init.go | 4 +++ cmd/osmosisd/cmd/root.go | 68 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 437578dee06..c78d6234f23 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,18 @@ { "version": "0.2.0", "configurations": [ + { + "name": "Launch file", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "cmd/osmosisd/main.go", + "args": [ + "init", + "test", + ], + }, + { // Note: Osmosisd must already be running // Binary must be built with debug flags. diff --git a/CHANGELOG.md b/CHANGELOG.md index fe7d4602743..5d988e1d461 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Config + +* [#7180](https://github.com/osmosis-labs/osmosis/pull/7180) Change `consensus.timeout-commit` from 5s to 4s. Overwrites the existing value on start-up. Default is set to 4s. + ### API * [#6991](https://github.com/osmosis-labs/osmosis/pull/6991) Fix: total liquidity poolmanager grpc gateway query diff --git a/cmd/osmosisd/cmd/init.go b/cmd/osmosisd/cmd/init.go index fa3f8b70434..2861a23f7c1 100644 --- a/cmd/osmosisd/cmd/init.go +++ b/cmd/osmosisd/cmd/init.go @@ -108,6 +108,10 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command { config.Mempool.Size = 10000 config.StateSync.TrustPeriod = 112 * time.Hour + // The original default is 5s and is set in Cosmos SDK. + // We lower it to 4s for faster block times. + config.Consensus.TimeoutCommit = 4 * time.Second + config.SetRoot(clientCtx.HomeDir) chainID, _ := cmd.Flags().GetString(flags.FlagChainID) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 3e6154c87cb..a0f4c8850f3 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -12,6 +12,7 @@ import ( "path/filepath" "regexp" "strings" + "time" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" "github.com/prometheus/client_golang/prometheus" @@ -32,6 +33,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/client/debug" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/keys" @@ -59,8 +61,6 @@ import ( "github.com/joho/godotenv" - "github.com/cosmos/cosmos-sdk/client/config" - osmosis "github.com/osmosis-labs/osmosis/v21/app" ) @@ -301,6 +301,22 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return err } + serverCtx := server.GetServerContextFromCmd(cmd) + + // configure the viper instance to parse home flags + if err := serverCtx.Viper.BindPFlags(cmd.Flags()); err != nil { + return err + } + if err := serverCtx.Viper.BindPFlags(cmd.PersistentFlags()); err != nil { + return err + } + + // overwrite config.toml values + cometConfig, err := overwriteConfigTomlValues(serverCtx) + if err != nil { + return err + } + initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags()) if err != nil { return err @@ -389,7 +405,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { } }) } - return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, tmcfg.DefaultConfig()) + return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, cometConfig) }, SilenceUsage: true, } @@ -401,6 +417,52 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return rootCmd, encodingConfig } +// overwrites config.toml values if it exists, otherwise it writes the default config.toml +func overwriteConfigTomlValues(serverCtx *server.Context) (*tmcfg.Config, error) { + // Get paths to config.toml and config parent directory + rootDir := serverCtx.Viper.GetString(flags.FlagHome) + configParentDirPath := filepath.Join(rootDir, "config") + configFilePath := filepath.Join(configParentDirPath, "config.toml") + + // Initialize default config + tmcConfig := tmcfg.DefaultConfig() + + _, err := os.Stat(configFilePath) + if err != nil { + if !os.IsNotExist(err) { + return nil, fmt.Errorf("failed to read in %s: %w", configFilePath, err) + } + + // If does not exist, write default config.toml to update + + // We modify the default config.toml to have faster block times + // It will be written by server.InterceptConfigsPreRunHandler + tmcConfig.Consensus.TimeoutCommit = 4 * time.Second + } else { + // config.toml exists + + serverCtx.Viper.SetConfigType("toml") + serverCtx.Viper.SetConfigName("config") + serverCtx.Viper.AddConfigPath(configParentDirPath) + + // We read it in and modify the consensus timeout commit + // and write it back. + if err := serverCtx.Viper.ReadInConfig(); err != nil { + return nil, fmt.Errorf("failed to read in %s: %w", configFilePath, err) + } + + // The original default is 5s and is set in Cosmos SDK. + // We lower it to 4s for faster block times. + serverCtx.Config.Consensus.TimeoutCommit = 4 * time.Second + + // It will be re-read in server.InterceptConfigsPreRunHandler + tmcfg.WriteConfigFile(configFilePath, serverCtx.Config) + + tmcConfig = serverCtx.Config + } + return tmcConfig, nil +} + func getHomeEnvironment() string { envPath := filepath.Join(osmosis.DefaultNodeHome, ".env") From e2ed4f11b873f3370a47b028f5d980891e35ce11 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 21 Dec 2023 19:37:40 -0700 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d988e1d461..68b93a33b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Config -* [#7180](https://github.com/osmosis-labs/osmosis/pull/7180) Change `consensus.timeout-commit` from 5s to 4s. Overwrites the existing value on start-up. Default is set to 4s. +* [#7180](https://github.com/osmosis-labs/osmosis/pull/7180) Change `consensus.timeout-commit` from 5s to 4s in `config.toml`. Overwrites the existing value on start-up. Default is set to 4s. ### API From aca424ea03aa02a0f61aadc592e2a4d610ad0a5e Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 22 Dec 2023 09:56:43 -0600 Subject: [PATCH 3/3] Add panic catching + comment --- cmd/osmosisd/cmd/root.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index a0f4c8850f3..73b6a8ddf79 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -429,12 +429,12 @@ func overwriteConfigTomlValues(serverCtx *server.Context) (*tmcfg.Config, error) _, err := os.Stat(configFilePath) if err != nil { + // something besides a does not exist error if !os.IsNotExist(err) { return nil, fmt.Errorf("failed to read in %s: %w", configFilePath, err) } - // If does not exist, write default config.toml to update - + // It does not exist, so we update the default config.toml to update // We modify the default config.toml to have faster block times // It will be written by server.InterceptConfigsPreRunHandler tmcConfig.Consensus.TimeoutCommit = 4 * time.Second @@ -453,12 +453,19 @@ func overwriteConfigTomlValues(serverCtx *server.Context) (*tmcfg.Config, error) // The original default is 5s and is set in Cosmos SDK. // We lower it to 4s for faster block times. - serverCtx.Config.Consensus.TimeoutCommit = 4 * time.Second + if serverCtx.Config.Consensus.TimeoutCommit == 5*time.Second { + serverCtx.Config.Consensus.TimeoutCommit = 4 * time.Second + } + tmcConfig = serverCtx.Config + defer func() { + if err := recover(); err != nil { + fmt.Printf("failed to write to %s: %s\n", configFilePath, err) + } + }() // It will be re-read in server.InterceptConfigsPreRunHandler + // this may panic for permissions issues. So we catch the panic. tmcfg.WriteConfigFile(configFilePath, serverCtx.Config) - - tmcConfig = serverCtx.Config } return tmcConfig, nil }