From 3f29363ad6d7cb4ffb14cf12a88acb53a43a689b Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 11:07:33 -0600 Subject: [PATCH 01/20] fix config and app toml overwrites --- cmd/osmosisd/cmd/root.go | 106 ++++++++++++++------------------------- 1 file changed, 39 insertions(+), 67 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index b4b59bdce7d..018aa0012a0 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -12,11 +12,9 @@ import ( "path/filepath" "regexp" "strings" - "time" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" "github.com/prometheus/client_golang/prometheus" - "github.com/spf13/viper" cometbftdb "github.com/cometbft/cometbft-db" @@ -100,6 +98,7 @@ type DenomUnitMap struct { } const ( + // app.toml mempoolConfigName = "osmosis-mempool" arbitrageMinGasFeeConfigName = "arbitrage-min-gas-fee" @@ -108,8 +107,11 @@ const ( maxGasWantedPerTxName = "max-gas-wanted-per-tx" recommendedNewMaxGasWantedPerTxValue = "60000000" - consensusConfigName = "consensus" - timeoutCommitConfigName = "timeout_commit" + // config.toml + consensusConfigName = "consensus" + + timeoutCommitConfigName = "timeout_commit" + recommendedNewTimeoutCommitValue = "2s" ) var ( @@ -422,10 +424,19 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return rootCmd, encodingConfig } -// overwrites config.toml values if it exists, otherwise it writes the default config.toml +// overwriteConfigTomlValues overwrites config.toml values. Returns error if config.toml does not exist +// +// Currently, overwrites: +// - timeout_commit value in config.toml to 2s. +// +// Also overwrites the respective viper config value. +// +// Silently handles and skips any error/panic due to write permission issues. +// No-op otherwise. func overwriteConfigTomlValues(serverCtx *server.Context) error { // Get paths to config.toml and config parent directory rootDir := serverCtx.Viper.GetString(tmcli.HomeFlag) + configParentDirPath := filepath.Join(rootDir, "config") configFilePath := filepath.Join(configParentDirPath, "config.toml") @@ -438,30 +449,8 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { } else { // config.toml exists - // Create a copy since the original viper from serverCtx - // contains app.toml config values that would get overwritten - // by ReadInConfig below. - viperCopy := viper.New() - - viperCopy.SetConfigType("toml") - viperCopy.SetConfigName("config") - viperCopy.AddConfigPath(configParentDirPath) - - // We read it in and modify the consensus timeout commit - // and write it back. - if err := viperCopy.ReadInConfig(); err != nil { - return fmt.Errorf("failed to read in %s: %w", configFilePath, err) - } - - consensusValues := viperCopy.GetStringMapString(consensusConfigName) - timeoutCommitValue, ok := consensusValues[timeoutCommitConfigName] - if !ok { - return fmt.Errorf("failed to get %s.%s from %s: %w", consensusConfigName, timeoutCommitValue, configFilePath, err) - } - - // The original default is 5s and is set in Cosmos SDK. - // We lower it to 2s for faster block times. - serverCtx.Config.Consensus.TimeoutCommit = 2 * time.Second + // Set new values in viper + serverCtx.Viper.Set(consensusConfigName+"."+timeoutCommitConfigName, recommendedNewTimeoutCommitValue) defer func() { if err := recover(); err != nil { @@ -475,27 +464,36 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. tmcfg.WriteConfigFile(configFilePath, serverCtx.Config) + } else { + fmt.Println("config.toml is not writable. Cannot apply update. Please consder manually changing timeout_commit to " + recommendedNewTimeoutCommitValue) } } return nil } -// overwrites app.toml values. Returns error if app.toml does not exist +// overwriteAppTomlValues overwrites app.toml values. Returns error if app.toml does not exist +// +// Currently, overwrites: +// - arbitrage-min-gas-fee value in app.toml to 0.1. +// - max-gas-wanted-per-tx value in app.toml to 60000000. +// +// Also overwrites the respective viper config value. // -// Currently, overwrites arbitrage-min-gas-fee value in app.toml if it is set to 0.005. Similarly, -// overwrites the given viper config value. // Silently handles and skips any error/panic due to write permission issues. // No-op otherwise. func overwriteAppTomlValues(serverCtx *server.Context) error { - // Get paths to config.toml and config parent directory + // Get paths to app.toml and config parent directory rootDir := serverCtx.Viper.GetString(tmcli.HomeFlag) configParentDirPath := filepath.Join(rootDir, "config") - configFilePath := filepath.Join(configParentDirPath, "app.toml") + appFilePath := filepath.Join(configParentDirPath, "app.toml") - fileInfo, err := os.Stat(configFilePath) + fileInfo, err := os.Stat(appFilePath) if err != nil { - return fmt.Errorf("failed to read in %s: %w", configFilePath, err) + // something besides a does not exist error + if !os.IsNotExist(err) { + return fmt.Errorf("failed to read in %s: %w", appFilePath, err) + } } else { // app.toml exists @@ -505,42 +503,16 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { defer func() { if err := recover(); err != nil { - fmt.Printf("failed to write to %s: %s\n", configFilePath, err) + fmt.Printf("failed to write to %s: %s\n", appFilePath, err) } }() // Check if the file is writable if fileInfo.Mode()&os.FileMode(0200) != 0 { - // Read the entire content of the file - content, err := os.ReadFile(configFilePath) - if err != nil { - return err - } - - // Convert the content to a string - fileContent := string(content) - - // Find the index of the search line - index := strings.Index(fileContent, arbitrageMinGasFeeConfigName) - if index == -1 { - return fmt.Errorf("search line not found in the file") - } - - // Find the opening and closing quotes - openQuoteIndex := strings.Index(fileContent[index:], "\"") - openQuoteIndex += index - - closingQuoteIndex := strings.Index(fileContent[openQuoteIndex+1:], "\"") - closingQuoteIndex += openQuoteIndex + 1 - - // Replace the old value with the new value - newFileContent := fileContent[:openQuoteIndex+1] + recommendedNewArbitrageMinGasFeeValue + fileContent[closingQuoteIndex:] - - // Write the modified content back to the file - err = os.WriteFile(configFilePath, []byte(newFileContent), 0644) - if err != nil { - return err - } + // It will be re-read in server.InterceptConfigsPreRunHandler + // this may panic for permissions issues. So we catch the panic. + // Note that this exits with a non-zero exit code if fails to write the file. + serverconfig.WriteConfigFile(appFilePath, serverCtx.Config) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } From ccc56751ab2bfeb28306e65d146aa51176c2f4e4 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 11:12:21 -0600 Subject: [PATCH 02/20] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4c82cec0c..a0e4d968e98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#8006](https://github.com/osmosis-labs/osmosis/pull/8006), [#8014](https://github.com/osmosis-labs/osmosis/pull/8014) Speedup many BigDec operations * [#8030](https://github.com/osmosis-labs/osmosis/pull/8030) Delete legacy behavior where lockups could not unbond at very small block heights on a testnet +* [#8118](https://github.com/osmosis-labs/osmosis/pull/8118) Fix: Don't overwrite config.toml if config is not found. ## v24.0.1 From d41515d18f3b8ab6ff14778c2654adaa16e2f4e2 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 12:34:46 -0600 Subject: [PATCH 03/20] more fixes --- cmd/osmosisd/cmd/root.go | 165 ++++++++++++++++++++++++--------------- 1 file changed, 104 insertions(+), 61 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 018aa0012a0..39fd44f56b3 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( // "fmt" + b "bytes" "embed" "encoding/json" "fmt" @@ -11,7 +12,9 @@ import ( "os" "path/filepath" "regexp" + "strconv" "strings" + "text/template" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" "github.com/prometheus/client_golang/prometheus" @@ -60,6 +63,8 @@ import ( genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + txfeestypes "github.com/osmosis-labs/osmosis/v24/x/txfees/types" + "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" @@ -97,6 +102,68 @@ type DenomUnitMap struct { Exponent uint64 `json:"exponent"` } +type CustomAppConfig struct { + serverconfig.Config + + OsmosisMempoolConfig txfeestypes.MempoolFeeOptions `mapstructure:"osmosis-mempool"` + + SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` + + Wasm wasmtypes.WasmConfig `mapstructure:"wasm"` +} + +var OsmosisAppTemplate = serverconfig.DefaultConfigTemplate + ` +############################################################################### +### Osmosis Mempool Configuration ### +############################################################################### + +[osmosis-mempool] +# This is the max allowed gas any tx. +# This is only for local mempool purposes, and thus is only ran on check tx. +max-gas-wanted-per-tx = "60000000" + +# This is the minimum gas fee any arbitrage tx should have, denominated in uosmo per gas +# Default value of ".1" then means that a tx with 1 million gas costs (.1 uosmo/gas) * 1_000_000 gas = .1 osmo +arbitrage-min-gas-fee = ".1" + +# This is the minimum gas fee any tx with high gas demand should have, denominated in uosmo per gas +# Default value of ".0025" then means that a tx with 1 million gas costs (.0025 uosmo/gas) * 1_000_000 gas = .0025 osmo +min-gas-price-for-high-gas-tx = ".0025" + +# This parameter enables EIP-1559 like fee market logic in the mempool +adaptive-fee-enabled = "true" + +############################################################################### +### Osmosis Sidecar Query Server Configuration ### +############################################################################### + +[osmosis-sqs] + +# SQS service is disabled by default. +is-enabled = "false" + +# The hostname of the GRPC sqs service +grpc-ingest-address = "{{ .SidecarQueryServerConfig.GRPCIngestAddress }}" +# The maximum size of the GRPC message that can be received by the sqs service in bytes. +grpc-ingest-max-call-size-bytes = "{{ .SidecarQueryServerConfig.GRPCIngestMaxCallSizeBytes }}" + +############################################################################### +### Wasm Configuration ### +############################################################################### +` + wasmtypes.DefaultConfigTemplate() + +var configTemplate *template.Template + +func init() { + var err error + + tmpl := template.New("appConfigFileTemplate") + + if configTemplate, err = tmpl.Parse(OsmosisAppTemplate); err != nil { + panic(err) + } +} + const ( // app.toml mempoolConfigName = "osmosis-mempool" @@ -314,6 +381,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { SetCustomEnvVariablesFromClientToml(initClientCtx) humanReadableDenomsInput, humanReadableDenomsOutput := GetHumanReadableDenomEnvVariables() + var customAppConfigExt CustomAppConfig + rootCmd := &cobra.Command{ Use: "osmosisd", Short: "Start osmosis app", @@ -348,6 +417,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return err } customAppTemplate, customAppConfig := initAppConfig() + customAppConfigExt = customAppConfig // If enabled, CLI input will be parsed and human readable denominations will be automatically converted to ibc denoms. if humanReadableDenomsInput { @@ -419,7 +489,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { genAutoCompleteCmd(rootCmd) - initRootCmd(rootCmd, encodingConfig) + initRootCmd(rootCmd, encodingConfig, customAppConfigExt) return rootCmd, encodingConfig } @@ -481,7 +551,7 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // // Silently handles and skips any error/panic due to write permission issues. // No-op otherwise. -func overwriteAppTomlValues(serverCtx *server.Context) error { +func overwriteAppTomlValues(serverCtx *server.Context, customAppConfig CustomAppConfig) error { // Get paths to app.toml and config parent directory rootDir := serverCtx.Viper.GetString(tmcli.HomeFlag) @@ -501,6 +571,14 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) + // Check if customAppConfig can be asserted to CustomAppConfig + recommendedNewMaxGasWantedPerTxValueUint64, err := strconv.ParseUint(recommendedNewMaxGasWantedPerTxValue, 10, 64) + if err != nil { + return fmt.Errorf("failed to parse %s to uint64: %w", recommendedNewMaxGasWantedPerTxValue, err) + } + customAppConfig.OsmosisMempoolConfig.MaxGasWantedPerTx = recommendedNewMaxGasWantedPerTxValueUint64 + customAppConfig.OsmosisMempoolConfig.MinGasPriceForArbitrageTx = osmomath.MustNewDecFromStr(recommendedNewArbitrageMinGasFeeValue) + defer func() { if err := recover(); err != nil { fmt.Printf("failed to write to %s: %s\n", appFilePath, err) @@ -512,7 +590,7 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // It will be re-read in server.InterceptConfigsPreRunHandler // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. - serverconfig.WriteConfigFile(appFilePath, serverCtx.Config) + WriteConfigFile(appFilePath, customAppConfig) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } @@ -536,21 +614,7 @@ func getHomeEnvironment() string { // initAppConfig helps to override default appConfig template and configs. // return "", nil if no custom configuration is required for the application. -func initAppConfig() (string, interface{}) { - type OsmosisMempoolConfig struct { - ArbitrageMinGasPrice string `mapstructure:"arbitrage-min-gas-fee"` - } - - type CustomAppConfig struct { - serverconfig.Config - - OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` - - SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` - - Wasm wasmtypes.WasmConfig `mapstructure:"wasm"` - } - +func initAppConfig() (string, CustomAppConfig) { // Optionally allow the chain developer to overwrite the SDK's default // server config. srvCfg := serverconfig.DefaultConfig() @@ -561,57 +625,17 @@ func initAppConfig() (string, interface{}) { // 128MB IAVL cache srvCfg.IAVLCacheSize = 781250 - memCfg := OsmosisMempoolConfig{ArbitrageMinGasPrice: "0.1"} + memCfg := txfeestypes.MempoolFeeOptions{MinGasPriceForArbitrageTx: osmomath.MustNewDecFromStr("0.1")} sqsConfig := sqs.DefaultConfig OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsConfig, Wasm: wasmtypes.DefaultWasmConfig()} - OsmosisAppTemplate := serverconfig.DefaultConfigTemplate + ` -############################################################################### -### Osmosis Mempool Configuration ### -############################################################################### - -[osmosis-mempool] -# This is the max allowed gas any tx. -# This is only for local mempool purposes, and thus is only ran on check tx. -max-gas-wanted-per-tx = "60000000" - -# This is the minimum gas fee any arbitrage tx should have, denominated in uosmo per gas -# Default value of ".1" then means that a tx with 1 million gas costs (.1 uosmo/gas) * 1_000_000 gas = .1 osmo -arbitrage-min-gas-fee = ".1" - -# This is the minimum gas fee any tx with high gas demand should have, denominated in uosmo per gas -# Default value of ".0025" then means that a tx with 1 million gas costs (.0025 uosmo/gas) * 1_000_000 gas = .0025 osmo -min-gas-price-for-high-gas-tx = ".0025" - -# This parameter enables EIP-1559 like fee market logic in the mempool -adaptive-fee-enabled = "true" - -############################################################################### -### Osmosis Sidecar Query Server Configuration ### -############################################################################### - -[osmosis-sqs] - -# SQS service is disabled by default. -is-enabled = "false" - -# The hostname of the GRPC sqs service -grpc-ingest-address = "{{ .SidecarQueryServerConfig.GRPCIngestAddress }}" -# The maximum size of the GRPC message that can be received by the sqs service in bytes. -grpc-ingest-max-call-size-bytes = "{{ .SidecarQueryServerConfig.GRPCIngestMaxCallSizeBytes }}" - -############################################################################### -### Wasm Configuration ### -############################################################################### -` + wasmtypes.DefaultConfigTemplate() - return OsmosisAppTemplate, OsmosisAppCfg } // initRootCmd initializes root commands when creating a new root command for simd. -func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { +func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, customAppConfig CustomAppConfig) { cfg := sdk.GetConfig() cfg.Seal() @@ -672,7 +696,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { return err } - err = overwriteAppTomlValues(serverCtx) + err = overwriteAppTomlValues(serverCtx, customAppConfig) if err != nil { return err } @@ -1006,3 +1030,22 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin } return "", fmt.Errorf("denom %s not found in asset map", coinDenom) } + +// WriteConfigFile renders config using the template and writes it to +// configFilePath. +func WriteConfigFile(configFilePath string, config interface{}) { + var buffer b.Buffer + + if err := configTemplate.Execute(&buffer, config); err != nil { + panic(err) + } + + mustWriteFile(configFilePath, buffer.Bytes(), 0o644) +} + +func mustWriteFile(filePath string, contents []byte, mode os.FileMode) { + if err := os.WriteFile(filePath, contents, mode); err != nil { + fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n") + os.Exit(1) + } +} From 4df772ea5e74fa951baa669759f57a23db899692 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 13:23:02 -0600 Subject: [PATCH 04/20] changelog name --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0e4d968e98..8731f8d0683 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#8006](https://github.com/osmosis-labs/osmosis/pull/8006), [#8014](https://github.com/osmosis-labs/osmosis/pull/8014) Speedup many BigDec operations * [#8030](https://github.com/osmosis-labs/osmosis/pull/8030) Delete legacy behavior where lockups could not unbond at very small block heights on a testnet -* [#8118](https://github.com/osmosis-labs/osmosis/pull/8118) Fix: Don't overwrite config.toml if config is not found. +* [#8118](https://github.com/osmosis-labs/osmosis/pull/8118) Config.toml and app.toml overwrite improvements ## v24.0.1 From 8b9ed67bfe5ec47801f73b6f7be91cdc074fd75b Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 19:50:06 -0600 Subject: [PATCH 05/20] push --- cmd/osmosisd/cmd/root.go | 171 +++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 71 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 39fd44f56b3..881c8531138 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -12,7 +12,6 @@ import ( "os" "path/filepath" "regexp" - "strconv" "strings" "text/template" @@ -63,8 +62,6 @@ import ( genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" - txfeestypes "github.com/osmosis-labs/osmosis/v24/x/txfees/types" - "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" @@ -102,67 +99,31 @@ type DenomUnitMap struct { Exponent uint64 `json:"exponent"` } +type OsmosisMempoolConfig struct { + MaxGasWantedPerTx string `mapstructure:"max-gas-wanted-per-tx"` + MinGasPriceForArbitrageTx string `mapstructure:"arbitrage-min-gas-fee"` + MinGasPriceForHighGasTx string `mapstructure:"min-gas-price-for-high-gas-tx"` + Mempool1559Enabled string `mapstructure:"adaptive-fee-enabled"` +} + +var DefaultOsmosisMempoolConfig = OsmosisMempoolConfig{ + MaxGasWantedPerTx: "60000000", + MinGasPriceForArbitrageTx: ".1", + MinGasPriceForHighGasTx: ".0025", + Mempool1559Enabled: "true", +} + type CustomAppConfig struct { serverconfig.Config - OsmosisMempoolConfig txfeestypes.MempoolFeeOptions `mapstructure:"osmosis-mempool"` + OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` - Wasm wasmtypes.WasmConfig `mapstructure:"wasm"` + WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` } -var OsmosisAppTemplate = serverconfig.DefaultConfigTemplate + ` -############################################################################### -### Osmosis Mempool Configuration ### -############################################################################### - -[osmosis-mempool] -# This is the max allowed gas any tx. -# This is only for local mempool purposes, and thus is only ran on check tx. -max-gas-wanted-per-tx = "60000000" - -# This is the minimum gas fee any arbitrage tx should have, denominated in uosmo per gas -# Default value of ".1" then means that a tx with 1 million gas costs (.1 uosmo/gas) * 1_000_000 gas = .1 osmo -arbitrage-min-gas-fee = ".1" - -# This is the minimum gas fee any tx with high gas demand should have, denominated in uosmo per gas -# Default value of ".0025" then means that a tx with 1 million gas costs (.0025 uosmo/gas) * 1_000_000 gas = .0025 osmo -min-gas-price-for-high-gas-tx = ".0025" - -# This parameter enables EIP-1559 like fee market logic in the mempool -adaptive-fee-enabled = "true" - -############################################################################### -### Osmosis Sidecar Query Server Configuration ### -############################################################################### - -[osmosis-sqs] - -# SQS service is disabled by default. -is-enabled = "false" - -# The hostname of the GRPC sqs service -grpc-ingest-address = "{{ .SidecarQueryServerConfig.GRPCIngestAddress }}" -# The maximum size of the GRPC message that can be received by the sqs service in bytes. -grpc-ingest-max-call-size-bytes = "{{ .SidecarQueryServerConfig.GRPCIngestMaxCallSizeBytes }}" - -############################################################################### -### Wasm Configuration ### -############################################################################### -` + wasmtypes.DefaultConfigTemplate() - -var configTemplate *template.Template - -func init() { - var err error - - tmpl := template.New("appConfigFileTemplate") - - if configTemplate, err = tmpl.Parse(OsmosisAppTemplate); err != nil { - panic(err) - } -} +var OsmosisAppTemplate string const ( // app.toml @@ -571,13 +532,17 @@ func overwriteAppTomlValues(serverCtx *server.Context, customAppConfig CustomApp serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) - // Check if customAppConfig can be asserted to CustomAppConfig - recommendedNewMaxGasWantedPerTxValueUint64, err := strconv.ParseUint(recommendedNewMaxGasWantedPerTxValue, 10, 64) - if err != nil { - return fmt.Errorf("failed to parse %s to uint64: %w", recommendedNewMaxGasWantedPerTxValue, err) + var config serverconfig.Config + if err := serverCtx.Viper.Unmarshal(&config); err != nil { + return fmt.Errorf("failed to unmarshal viper config: %w", err) + } + + var customAppConfigNew CustomAppConfig + if err := serverCtx.Viper.Unmarshal(&customAppConfigNew); err != nil { + return fmt.Errorf("failed to unmarshal viper config: %w", err) } - customAppConfig.OsmosisMempoolConfig.MaxGasWantedPerTx = recommendedNewMaxGasWantedPerTxValueUint64 - customAppConfig.OsmosisMempoolConfig.MinGasPriceForArbitrageTx = osmomath.MustNewDecFromStr(recommendedNewArbitrageMinGasFeeValue) + + test := CustomAppConfig{Config: config, OsmosisMempoolConfig: customAppConfigNew.OsmosisMempoolConfig, SidecarQueryServerConfig: customAppConfigNew.SidecarQueryServerConfig, WasmConfig: customAppConfigNew.WasmConfig} defer func() { if err := recover(); err != nil { @@ -590,7 +555,7 @@ func overwriteAppTomlValues(serverCtx *server.Context, customAppConfig CustomApp // It will be re-read in server.InterceptConfigsPreRunHandler // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. - WriteConfigFile(appFilePath, customAppConfig) + WriteConfigFile(appFilePath, test) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } @@ -625,11 +590,65 @@ func initAppConfig() (string, CustomAppConfig) { // 128MB IAVL cache srvCfg.IAVLCacheSize = 781250 - memCfg := txfeestypes.MempoolFeeOptions{MinGasPriceForArbitrageTx: osmomath.MustNewDecFromStr("0.1")} + memCfg := DefaultOsmosisMempoolConfig sqsConfig := sqs.DefaultConfig - OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsConfig, Wasm: wasmtypes.DefaultWasmConfig()} + wasmCfg := wasmtypes.DefaultWasmConfig() + + OsmosisAppTemplate = serverconfig.DefaultConfigTemplate + ` +############################################################################### +### Osmosis Mempool Configuration ### +############################################################################### + +[osmosis-mempool] +# This is the max allowed gas any tx. +# This is only for local mempool purposes, and thus is only ran on check tx. +max-gas-wanted-per-tx = "{{ .OsmosisMempoolConfig.MaxGasWantedPerTx }}" + +# This is the minimum gas fee any arbitrage tx should have, denominated in uosmo per gas +# Default value of ".1" then means that a tx with 1 million gas costs (.1 uosmo/gas) * 1_000_000 gas = .1 osmo +arbitrage-min-gas-fee = "{{ .OsmosisMempoolConfig.MinGasPriceForArbitrageTx }}" + +# This is the minimum gas fee any tx with high gas demand should have, denominated in uosmo per gas +# Default value of ".0025" then means that a tx with 1 million gas costs (.0025 uosmo/gas) * 1_000_000 gas = .0025 osmo +min-gas-price-for-high-gas-tx = "{{ .OsmosisMempoolConfig.MinGasPriceForHighGasTx }}" + +# This parameter enables EIP-1559 like fee market logic in the mempool +adaptive-fee-enabled = "{{ .OsmosisMempoolConfig.Mempool1559Enabled }}" + +############################################################################### +### Osmosis Sidecar Query Server Configuration ### +############################################################################### + +[osmosis-sqs] + +# SQS service is disabled by default. +is-enabled = "{{ .SidecarQueryServerConfig.IsEnabled }}" + +# The hostname of the GRPC sqs service +grpc-ingest-address = "{{ .SidecarQueryServerConfig.GRPCIngestAddress }}" +# The maximum size of the GRPC message that can be received by the sqs service in bytes. +grpc-ingest-max-call-size-bytes = "{{ .SidecarQueryServerConfig.GRPCIngestMaxCallSizeBytes }}" + +############################################################################### +### Wasm Configuration ### +############################################################################### + +[wasm] +# Smart query gas limit is the max gas to be used in a smart query contract call +query_gas_limit = {{ .WasmConfig.SmartQueryGasLimit }} + +# in-memory cache for Wasm contracts. Set to 0 to disable. +# The value is in MiB not bytes +memory_cache_size = {{ .WasmConfig.MemoryCacheSize }} + +# Simulation gas limit is the max gas to be used in a tx simulation call. +# When not set the consensus max block gas is used instead +# simulation_gas_limit = +` + + OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsConfig, WasmConfig: wasmCfg} return OsmosisAppTemplate, OsmosisAppCfg } @@ -1033,18 +1052,28 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin // WriteConfigFile renders config using the template and writes it to // configFilePath. -func WriteConfigFile(configFilePath string, config interface{}) { +func WriteConfigFile(configFilePath string, config CustomAppConfig) { + // Check if the simulation_gas_limit line should be commented out + isCommentedOut := config.WasmConfig.SimulationGasLimit == nil || *config.WasmConfig.SimulationGasLimit == 0 + + // Choose the appropriate template based on whether the line is commented out + if !isCommentedOut { + // Find the simulation_gas_limit line and prepend it with a # + OsmosisAppTemplate = strings.Replace(OsmosisAppTemplate, "# simulation_gas_limit =", "simulation_gas_limit = {{ .WasmConfig.SimulationGasLimit }}", 1) + } + var buffer b.Buffer + tmpl := template.New("appConfigFileTemplate") + configTemplate, err := tmpl.Parse(OsmosisAppTemplate) + if err != nil { + panic(err) + } if err := configTemplate.Execute(&buffer, config); err != nil { panic(err) } - mustWriteFile(configFilePath, buffer.Bytes(), 0o644) -} - -func mustWriteFile(filePath string, contents []byte, mode os.FileMode) { - if err := os.WriteFile(filePath, contents, mode); err != nil { + if err := os.WriteFile(configFilePath, buffer.Bytes(), 0o644); err != nil { fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n") os.Exit(1) } From 0af5c5748d343112cb04b15b10bd6531d4f3fb01 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 19:51:13 -0600 Subject: [PATCH 06/20] push --- cmd/osmosisd/cmd/root.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 881c8531138..82f2273c566 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -342,8 +342,6 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { SetCustomEnvVariablesFromClientToml(initClientCtx) humanReadableDenomsInput, humanReadableDenomsOutput := GetHumanReadableDenomEnvVariables() - var customAppConfigExt CustomAppConfig - rootCmd := &cobra.Command{ Use: "osmosisd", Short: "Start osmosis app", @@ -378,7 +376,6 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return err } customAppTemplate, customAppConfig := initAppConfig() - customAppConfigExt = customAppConfig // If enabled, CLI input will be parsed and human readable denominations will be automatically converted to ibc denoms. if humanReadableDenomsInput { @@ -450,7 +447,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { genAutoCompleteCmd(rootCmd) - initRootCmd(rootCmd, encodingConfig, customAppConfigExt) + initRootCmd(rootCmd, encodingConfig) return rootCmd, encodingConfig } @@ -512,7 +509,7 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // // Silently handles and skips any error/panic due to write permission issues. // No-op otherwise. -func overwriteAppTomlValues(serverCtx *server.Context, customAppConfig CustomAppConfig) error { +func overwriteAppTomlValues(serverCtx *server.Context) error { // Get paths to app.toml and config parent directory rootDir := serverCtx.Viper.GetString(tmcli.HomeFlag) @@ -654,7 +651,7 @@ memory_cache_size = {{ .WasmConfig.MemoryCacheSize }} } // initRootCmd initializes root commands when creating a new root command for simd. -func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, customAppConfig CustomAppConfig) { +func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { cfg := sdk.GetConfig() cfg.Seal() @@ -715,7 +712,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, c return err } - err = overwriteAppTomlValues(serverCtx, customAppConfig) + err = overwriteAppTomlValues(serverCtx) if err != nil { return err } From ef1d831811b2d2023306c134e90d19ff3807f759 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 20:01:24 -0600 Subject: [PATCH 07/20] clean up --- cmd/osmosisd/cmd/root.go | 42 +++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 82f2273c566..e6623f70992 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -115,12 +115,9 @@ var DefaultOsmosisMempoolConfig = OsmosisMempoolConfig{ type CustomAppConfig struct { serverconfig.Config - - OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` - - SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` - - WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` + OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` + SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` + WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` } var OsmosisAppTemplate string @@ -478,6 +475,8 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // config.toml exists // Set new values in viper + // + // Set timeout_commit to 2s serverCtx.Viper.Set(consensusConfigName+"."+timeoutCommitConfigName, recommendedNewTimeoutCommitValue) defer func() { @@ -526,20 +525,29 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // app.toml exists // Set new values in viper + // + // Set max-gas-wanted-per-tx to 60000000 serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) + // Set arbitrage-min-gas-fee to 0.1 serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) - var config serverconfig.Config - if err := serverCtx.Viper.Unmarshal(&config); err != nil { - return fmt.Errorf("failed to unmarshal viper config: %w", err) + // Unmarshal viper baseConfig to get the appConfig struct + // This must be done separately from the other appConfig values + // because the base configs aren't prepended with any specific key + var appConfig serverconfig.Config + if err := serverCtx.Viper.Unmarshal(&appConfig); err != nil { + return fmt.Errorf("failed to unmarshal viper appConfig: %w", err) } - var customAppConfigNew CustomAppConfig - if err := serverCtx.Viper.Unmarshal(&customAppConfigNew); err != nil { + // Unmarshal the remaining viper app configs to get the remainingAppConfigs struct + // This houses the osmosis mempool, sidecar query server, and wasm configs + var remainingAppConfigs CustomAppConfig + if err := serverCtx.Viper.Unmarshal(&remainingAppConfigs); err != nil { return fmt.Errorf("failed to unmarshal viper config: %w", err) } - test := CustomAppConfig{Config: config, OsmosisMempoolConfig: customAppConfigNew.OsmosisMempoolConfig, SidecarQueryServerConfig: customAppConfigNew.SidecarQueryServerConfig, WasmConfig: customAppConfigNew.WasmConfig} + // Create a custom app config struct that brings together the appConfig and the remainingAppConfigs + customAppConfig := CustomAppConfig{Config: appConfig, OsmosisMempoolConfig: remainingAppConfigs.OsmosisMempoolConfig, SidecarQueryServerConfig: remainingAppConfigs.SidecarQueryServerConfig, WasmConfig: remainingAppConfigs.WasmConfig} defer func() { if err := recover(); err != nil { @@ -552,7 +560,7 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // It will be re-read in server.InterceptConfigsPreRunHandler // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. - WriteConfigFile(appFilePath, test) + WriteConfigFile(appFilePath, customAppConfig) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } @@ -1050,12 +1058,14 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin // WriteConfigFile renders config using the template and writes it to // configFilePath. func WriteConfigFile(configFilePath string, config CustomAppConfig) { - // Check if the simulation_gas_limit line should be commented out + // There is a single wasm config that requres special logic to handle + // For some reason, they base a value on whether a line is commented out or not... + + // Check if the simulation_gas_limit line should be commented out (it is either nil or 0) isCommentedOut := config.WasmConfig.SimulationGasLimit == nil || *config.WasmConfig.SimulationGasLimit == 0 - // Choose the appropriate template based on whether the line is commented out + // Modify the template by uncommenting the line if a value is provided for simulation_gas_limit if !isCommentedOut { - // Find the simulation_gas_limit line and prepend it with a # OsmosisAppTemplate = strings.Replace(OsmosisAppTemplate, "# simulation_gas_limit =", "simulation_gas_limit = {{ .WasmConfig.SimulationGasLimit }}", 1) } From 050477b58591c6765803bcbaa339bf90766193b2 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 20:25:40 -0600 Subject: [PATCH 08/20] reduce diff --- cmd/osmosisd/cmd/root.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index e6623f70992..80718671894 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -490,6 +490,8 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // It will be re-read in server.InterceptConfigsPreRunHandler // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. + + // Write the new config.toml file tmcfg.WriteConfigFile(configFilePath, serverCtx.Config) } else { fmt.Println("config.toml is not writable. Cannot apply update. Please consder manually changing timeout_commit to " + recommendedNewTimeoutCommitValue) @@ -560,6 +562,8 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // It will be re-read in server.InterceptConfigsPreRunHandler // this may panic for permissions issues. So we catch the panic. // Note that this exits with a non-zero exit code if fails to write the file. + + // Write the new app.toml file WriteConfigFile(appFilePath, customAppConfig) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) @@ -597,10 +601,12 @@ func initAppConfig() (string, CustomAppConfig) { memCfg := DefaultOsmosisMempoolConfig - sqsConfig := sqs.DefaultConfig + sqsCfg := sqs.DefaultConfig wasmCfg := wasmtypes.DefaultWasmConfig() + OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsCfg, WasmConfig: wasmCfg} + OsmosisAppTemplate = serverconfig.DefaultConfigTemplate + ` ############################################################################### ### Osmosis Mempool Configuration ### @@ -653,8 +659,6 @@ memory_cache_size = {{ .WasmConfig.MemoryCacheSize }} # simulation_gas_limit = ` - OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsConfig, WasmConfig: wasmCfg} - return OsmosisAppTemplate, OsmosisAppCfg } From 76de652bd5578ad0eb321975691f45719f88948a Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 20:31:05 -0600 Subject: [PATCH 09/20] comments --- cmd/osmosisd/cmd/root.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 80718671894..4b23620714e 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -564,7 +564,7 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // Note that this exits with a non-zero exit code if fails to write the file. // Write the new app.toml file - WriteConfigFile(appFilePath, customAppConfig) + WriteCustomAppConfigFile(appFilePath, customAppConfig) } else { fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } @@ -588,7 +588,7 @@ func getHomeEnvironment() string { // initAppConfig helps to override default appConfig template and configs. // return "", nil if no custom configuration is required for the application. -func initAppConfig() (string, CustomAppConfig) { +func initAppConfig() (string, interface{}) { // Optionally allow the chain developer to overwrite the SDK's default // server config. srvCfg := serverconfig.DefaultConfig() @@ -1059,9 +1059,9 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin return "", fmt.Errorf("denom %s not found in asset map", coinDenom) } -// WriteConfigFile renders config using the template and writes it to -// configFilePath. -func WriteConfigFile(configFilePath string, config CustomAppConfig) { +// WriteCustomAppConfigFile first checks the provided config for a special case with the wasm config. +// This determines what the template should be. Based on this, it writes the new app.toml config file. +func WriteCustomAppConfigFile(configFilePath string, config CustomAppConfig) { // There is a single wasm config that requres special logic to handle // For some reason, they base a value on whether a line is commented out or not... @@ -1073,6 +1073,7 @@ func WriteConfigFile(configFilePath string, config CustomAppConfig) { OsmosisAppTemplate = strings.Replace(OsmosisAppTemplate, "# simulation_gas_limit =", "simulation_gas_limit = {{ .WasmConfig.SimulationGasLimit }}", 1) } + // Create a template object via the app template string var buffer b.Buffer tmpl := template.New("appConfigFileTemplate") configTemplate, err := tmpl.Parse(OsmosisAppTemplate) @@ -1080,10 +1081,12 @@ func WriteConfigFile(configFilePath string, config CustomAppConfig) { panic(err) } + // Execute the template with the provided config if err := configTemplate.Execute(&buffer, config); err != nil { panic(err) } + // Write the new app.toml file if err := os.WriteFile(configFilePath, buffer.Bytes(), 0o644); err != nil { fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n") os.Exit(1) From 4e0b60418597874aad0e21d7a9ad8cd7f282d8f5 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 20:44:17 -0600 Subject: [PATCH 10/20] print propper --- cmd/osmosisd/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 4b23620714e..28fb3d8243b 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -566,7 +566,7 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // Write the new app.toml file WriteCustomAppConfigFile(appFilePath, customAppConfig) } else { - fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + "and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) + fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + " and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } } return nil From 2e6fd7b5c5df936ac9151ed09d43ba453f73f1f9 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 21 Apr 2024 20:44:53 -0600 Subject: [PATCH 11/20] lint --- cmd/osmosisd/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 28fb3d8243b..48119015e1e 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -1062,7 +1062,7 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin // WriteCustomAppConfigFile first checks the provided config for a special case with the wasm config. // This determines what the template should be. Based on this, it writes the new app.toml config file. func WriteCustomAppConfigFile(configFilePath string, config CustomAppConfig) { - // There is a single wasm config that requres special logic to handle + // There is a single wasm config that requires special logic to handle // For some reason, they base a value on whether a line is commented out or not... // Check if the simulation_gas_limit line should be commented out (it is either nil or 0) From c79bba52171dd8944aed034976f500897e462ebd Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:24:50 -0600 Subject: [PATCH 12/20] only change the respective lines, rather than the entire file --- cmd/osmosisd/cmd/root.go | 178 ++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 77 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 48119015e1e..5f361b89773 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -1,9 +1,7 @@ package cmd import ( - // "fmt" - - b "bytes" + "bufio" "embed" "encoding/json" "fmt" @@ -13,7 +11,6 @@ import ( "path/filepath" "regexp" "strings" - "text/template" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" "github.com/prometheus/client_golang/prometheus" @@ -120,8 +117,6 @@ type CustomAppConfig struct { WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` } -var OsmosisAppTemplate string - const ( // app.toml mempoolConfigName = "osmosis-mempool" @@ -449,6 +444,12 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return rootCmd, encodingConfig } +type SectionKeyValue struct { + Section string + Key string + Value any +} + // overwriteConfigTomlValues overwrites config.toml values. Returns error if config.toml does not exist // // Currently, overwrites: @@ -474,10 +475,17 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { } else { // config.toml exists - // Set new values in viper - // + // Check if each key is already set to the recommended value + // If it is, we don't need to overwrite it and can also skip the app.toml overwrite + var sectionKeyValues []SectionKeyValue + // Set timeout_commit to 2s - serverCtx.Viper.Set(consensusConfigName+"."+timeoutCommitConfigName, recommendedNewTimeoutCommitValue) + currentTimeoutCommit := serverCtx.Viper.Get(consensusConfigName + "." + timeoutCommitConfigName) + shoudUpdateCurrentTimeoutCommit := currentTimeoutCommit != recommendedNewTimeoutCommitValue + if shoudUpdateCurrentTimeoutCommit { + serverCtx.Viper.Set(consensusConfigName+"."+timeoutCommitConfigName, recommendedNewTimeoutCommitValue) + sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: consensusConfigName, Key: timeoutCommitConfigName, Value: recommendedNewTimeoutCommitValue}) + } defer func() { if err := recover(); err != nil { @@ -492,7 +500,12 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // Note that this exits with a non-zero exit code if fails to write the file. // Write the new config.toml file - tmcfg.WriteConfigFile(configFilePath, serverCtx.Config) + if len(sectionKeyValues) > 0 { + err := OverwriteWithCustomConfig(configFilePath, sectionKeyValues) + if err != nil { + return err + } + } } else { fmt.Println("config.toml is not writable. Cannot apply update. Please consder manually changing timeout_commit to " + recommendedNewTimeoutCommitValue) } @@ -503,8 +516,8 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // overwriteAppTomlValues overwrites app.toml values. Returns error if app.toml does not exist // // Currently, overwrites: -// - arbitrage-min-gas-fee value in app.toml to 0.1. -// - max-gas-wanted-per-tx value in app.toml to 60000000. +// - arbitrage-min-gas-fee +// - max-gas-wanted-per-tx // // Also overwrites the respective viper config value. // @@ -526,37 +539,26 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { } else { // app.toml exists - // Set new values in viper - // + // Check if each key is already set to the recommended value + // If it is, we don't need to overwrite it and can also skip the app.toml overwrite + var sectionKeyValues []SectionKeyValue + // Set max-gas-wanted-per-tx to 60000000 - serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) - // Set arbitrage-min-gas-fee to 0.1 - serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) - - // Unmarshal viper baseConfig to get the appConfig struct - // This must be done separately from the other appConfig values - // because the base configs aren't prepended with any specific key - var appConfig serverconfig.Config - if err := serverCtx.Viper.Unmarshal(&appConfig); err != nil { - return fmt.Errorf("failed to unmarshal viper appConfig: %w", err) + currentMaxGasWantedPerTx := serverCtx.Viper.Get(mempoolConfigName + "." + maxGasWantedPerTxName) + shoudUpdateMaxGasWantedPerTx := currentMaxGasWantedPerTx != recommendedNewMaxGasWantedPerTxValue + if shoudUpdateMaxGasWantedPerTx { + serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) + sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: mempoolConfigName, Key: maxGasWantedPerTxName, Value: recommendedNewMaxGasWantedPerTxValue}) } - // Unmarshal the remaining viper app configs to get the remainingAppConfigs struct - // This houses the osmosis mempool, sidecar query server, and wasm configs - var remainingAppConfigs CustomAppConfig - if err := serverCtx.Viper.Unmarshal(&remainingAppConfigs); err != nil { - return fmt.Errorf("failed to unmarshal viper config: %w", err) + // Set arbitrage-min-gas-fee to 0.1 + currentArbitrageMinGasFee := serverCtx.Viper.Get(mempoolConfigName + "." + arbitrageMinGasFeeConfigName) + shoudUpdateArbitrageMinGasFee := currentArbitrageMinGasFee != recommendedNewArbitrageMinGasFeeValue + if shoudUpdateArbitrageMinGasFee { + serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) + sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: mempoolConfigName, Key: arbitrageMinGasFeeConfigName, Value: recommendedNewArbitrageMinGasFeeValue}) } - // Create a custom app config struct that brings together the appConfig and the remainingAppConfigs - customAppConfig := CustomAppConfig{Config: appConfig, OsmosisMempoolConfig: remainingAppConfigs.OsmosisMempoolConfig, SidecarQueryServerConfig: remainingAppConfigs.SidecarQueryServerConfig, WasmConfig: remainingAppConfigs.WasmConfig} - - defer func() { - if err := recover(); err != nil { - fmt.Printf("failed to write to %s: %s\n", appFilePath, err) - } - }() - // Check if the file is writable if fileInfo.Mode()&os.FileMode(0200) != 0 { // It will be re-read in server.InterceptConfigsPreRunHandler @@ -564,9 +566,14 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // Note that this exits with a non-zero exit code if fails to write the file. // Write the new app.toml file - WriteCustomAppConfigFile(appFilePath, customAppConfig) + if len(sectionKeyValues) > 0 { + err := OverwriteWithCustomConfig(appFilePath, sectionKeyValues) + if err != nil { + return err + } + } } else { - fmt.Println("app.toml is not writable. Cannot apply update. Please consder manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + " and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) + fmt.Println("app.toml is not writable. Cannot apply update. Please consider manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + " and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) } } return nil @@ -593,7 +600,6 @@ func initAppConfig() (string, interface{}) { // server config. srvCfg := serverconfig.DefaultConfig() srvCfg.API.Enable = true - srvCfg.StateSync.SnapshotKeepRecent = 2 srvCfg.MinGasPrices = "0uosmo" // 128MB IAVL cache @@ -607,7 +613,7 @@ func initAppConfig() (string, interface{}) { OsmosisAppCfg := CustomAppConfig{Config: *srvCfg, OsmosisMempoolConfig: memCfg, SidecarQueryServerConfig: sqsCfg, WasmConfig: wasmCfg} - OsmosisAppTemplate = serverconfig.DefaultConfigTemplate + ` + OsmosisAppTemplate := serverconfig.DefaultConfigTemplate + ` ############################################################################### ### Osmosis Mempool Configuration ### ############################################################################### @@ -645,19 +651,7 @@ grpc-ingest-max-call-size-bytes = "{{ .SidecarQueryServerConfig.GRPCIngestMaxCal ############################################################################### ### Wasm Configuration ### ############################################################################### - -[wasm] -# Smart query gas limit is the max gas to be used in a smart query contract call -query_gas_limit = {{ .WasmConfig.SmartQueryGasLimit }} - -# in-memory cache for Wasm contracts. Set to 0 to disable. -# The value is in MiB not bytes -memory_cache_size = {{ .WasmConfig.MemoryCacheSize }} - -# Simulation gas limit is the max gas to be used in a tx simulation call. -# When not set the consensus max block gas is used instead -# simulation_gas_limit = -` +` + wasmtypes.DefaultConfigTemplate() return OsmosisAppTemplate, OsmosisAppCfg } @@ -1059,36 +1053,66 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin return "", fmt.Errorf("denom %s not found in asset map", coinDenom) } -// WriteCustomAppConfigFile first checks the provided config for a special case with the wasm config. -// This determines what the template should be. Based on this, it writes the new app.toml config file. -func WriteCustomAppConfigFile(configFilePath string, config CustomAppConfig) { - // There is a single wasm config that requires special logic to handle - // For some reason, they base a value on whether a line is commented out or not... +// OverwriteWithCustomConfig searches the respective config file for the given section and key and overwrites the value with the given value. +func OverwriteWithCustomConfig(configFilePath string, sectionKeyValues []SectionKeyValue) error { + file, err := os.OpenFile(configFilePath, os.O_RDWR, 0o644) + if err != nil { + return err + } + defer file.Close() - // Check if the simulation_gas_limit line should be commented out (it is either nil or 0) - isCommentedOut := config.WasmConfig.SimulationGasLimit == nil || *config.WasmConfig.SimulationGasLimit == 0 + // Create a map from the sectionKeyValues array + configMap := make(map[string]map[string]string) + for _, skv := range sectionKeyValues { + if _, ok := configMap[skv.Section]; !ok { + configMap[skv.Section] = make(map[string]string) + } + // Check if the value is a string or a number + switch v := skv.Value.(type) { + case string: + configMap[skv.Section][skv.Key] = "\"" + v + "\"" + default: + configMap[skv.Section][skv.Key] = fmt.Sprintf("%v", v) + } + } - // Modify the template by uncommenting the line if a value is provided for simulation_gas_limit - if !isCommentedOut { - OsmosisAppTemplate = strings.Replace(OsmosisAppTemplate, "# simulation_gas_limit =", "simulation_gas_limit = {{ .WasmConfig.SimulationGasLimit }}", 1) + var lines []string + scanner := bufio.NewScanner(file) + currentSection := "" + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { + currentSection = line[1 : len(line)-1] + } else if configMap[currentSection] != nil { + for key, value := range configMap[currentSection] { + if strings.Contains(line, key) { + line = key + " = " + value + break + } + } + } + lines = append(lines, line) } - // Create a template object via the app template string - var buffer b.Buffer - tmpl := template.New("appConfigFileTemplate") - configTemplate, err := tmpl.Parse(OsmosisAppTemplate) + if err := scanner.Err(); err != nil { + return err + } + + _, err = file.Seek(0, 0) if err != nil { - panic(err) + return err } - // Execute the template with the provided config - if err := configTemplate.Execute(&buffer, config); err != nil { - panic(err) + err = file.Truncate(0) + if err != nil { + return err } - // Write the new app.toml file - if err := os.WriteFile(configFilePath, buffer.Bytes(), 0o644); err != nil { - fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n") - os.Exit(1) + for _, line := range lines { + if _, err := file.WriteString(line + "\n"); err != nil { + return err + } } + + return nil } From cdc9230a512b4132ee6ea3d04cdd81812df48041 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:32:31 -0600 Subject: [PATCH 13/20] reduce diff --- cmd/osmosisd/cmd/root.go | 41 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 5f361b89773..b775310005b 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -96,27 +96,6 @@ type DenomUnitMap struct { Exponent uint64 `json:"exponent"` } -type OsmosisMempoolConfig struct { - MaxGasWantedPerTx string `mapstructure:"max-gas-wanted-per-tx"` - MinGasPriceForArbitrageTx string `mapstructure:"arbitrage-min-gas-fee"` - MinGasPriceForHighGasTx string `mapstructure:"min-gas-price-for-high-gas-tx"` - Mempool1559Enabled string `mapstructure:"adaptive-fee-enabled"` -} - -var DefaultOsmosisMempoolConfig = OsmosisMempoolConfig{ - MaxGasWantedPerTx: "60000000", - MinGasPriceForArbitrageTx: ".1", - MinGasPriceForHighGasTx: ".0025", - Mempool1559Enabled: "true", -} - -type CustomAppConfig struct { - serverconfig.Config - OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` - SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` - WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` -} - const ( // app.toml mempoolConfigName = "osmosis-mempool" @@ -596,6 +575,26 @@ func getHomeEnvironment() string { // initAppConfig helps to override default appConfig template and configs. // return "", nil if no custom configuration is required for the application. func initAppConfig() (string, interface{}) { + type OsmosisMempoolConfig struct { + MaxGasWantedPerTx string `mapstructure:"max-gas-wanted-per-tx"` + MinGasPriceForArbitrageTx string `mapstructure:"arbitrage-min-gas-fee"` + MinGasPriceForHighGasTx string `mapstructure:"min-gas-price-for-high-gas-tx"` + Mempool1559Enabled string `mapstructure:"adaptive-fee-enabled"` + } + + type CustomAppConfig struct { + serverconfig.Config + OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` + SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` + WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` + } + + var DefaultOsmosisMempoolConfig = OsmosisMempoolConfig{ + MaxGasWantedPerTx: "60000000", + MinGasPriceForArbitrageTx: ".1", + MinGasPriceForHighGasTx: ".0025", + Mempool1559Enabled: "true", + } // Optionally allow the chain developer to overwrite the SDK's default // server config. srvCfg := serverconfig.DefaultConfig() From 3d7bcdb2e984fa22f3cf6d12907a1e2ff34b1ceb Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:43:57 -0600 Subject: [PATCH 14/20] abstraction --- cmd/osmosisd/cmd/root.go | 80 +++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index b775310005b..f8a1605f0e3 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -96,21 +96,31 @@ type DenomUnitMap struct { Exponent uint64 `json:"exponent"` } -const ( - // app.toml - mempoolConfigName = "osmosis-mempool" - - arbitrageMinGasFeeConfigName = "arbitrage-min-gas-fee" - recommendedNewArbitrageMinGasFeeValue = "0.1" +type SectionKeyValue struct { + Section string + Key string + Value any +} - maxGasWantedPerTxName = "max-gas-wanted-per-tx" - recommendedNewMaxGasWantedPerTxValue = "60000000" +var ( + // app.toml + recommendedArbitrageMinGasFee = SectionKeyValue{ + Section: "osmosis-mempool", + Key: "arbitrage-min-gas-fee", + Value: "0.1", + } + recommendedMaxGasWantedPerTx = SectionKeyValue{ + Section: "osmosis-mempool", + Key: "arbitrage-min-gas-fee", + Value: "0.1", + } // config.toml - consensusConfigName = "consensus" - - timeoutCommitConfigName = "timeout_commit" - recommendedNewTimeoutCommitValue = "2s" + recommendedTimeoutCommit = SectionKeyValue{ + Section: "consensus", + Key: "timeout_commit", + Value: "2s", + } ) var ( @@ -423,12 +433,6 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { return rootCmd, encodingConfig } -type SectionKeyValue struct { - Section string - Key string - Value any -} - // overwriteConfigTomlValues overwrites config.toml values. Returns error if config.toml does not exist // // Currently, overwrites: @@ -456,14 +460,14 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // Check if each key is already set to the recommended value // If it is, we don't need to overwrite it and can also skip the app.toml overwrite - var sectionKeyValues []SectionKeyValue + var sectionKeyValuesToWrite []SectionKeyValue // Set timeout_commit to 2s - currentTimeoutCommit := serverCtx.Viper.Get(consensusConfigName + "." + timeoutCommitConfigName) - shoudUpdateCurrentTimeoutCommit := currentTimeoutCommit != recommendedNewTimeoutCommitValue + currentTimeoutCommit := serverCtx.Viper.Get(recommendedTimeoutCommit.Section + "." + recommendedTimeoutCommit.Key) + shoudUpdateCurrentTimeoutCommit := currentTimeoutCommit != recommendedTimeoutCommit.Value if shoudUpdateCurrentTimeoutCommit { - serverCtx.Viper.Set(consensusConfigName+"."+timeoutCommitConfigName, recommendedNewTimeoutCommitValue) - sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: consensusConfigName, Key: timeoutCommitConfigName, Value: recommendedNewTimeoutCommitValue}) + serverCtx.Viper.Set(recommendedTimeoutCommit.Section+"."+recommendedTimeoutCommit.Key, recommendedTimeoutCommit.Value) + sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedTimeoutCommit) } defer func() { @@ -479,14 +483,14 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // Note that this exits with a non-zero exit code if fails to write the file. // Write the new config.toml file - if len(sectionKeyValues) > 0 { - err := OverwriteWithCustomConfig(configFilePath, sectionKeyValues) + if len(sectionKeyValuesToWrite) > 0 { + err := OverwriteWithCustomConfig(configFilePath, sectionKeyValuesToWrite) if err != nil { return err } } } else { - fmt.Println("config.toml is not writable. Cannot apply update. Please consder manually changing timeout_commit to " + recommendedNewTimeoutCommitValue) + fmt.Printf("config.toml is not writable. Cannot apply update. Please consider manually changing timeout_commit to %v\n", recommendedTimeoutCommit.Value) } } return nil @@ -520,22 +524,22 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // Check if each key is already set to the recommended value // If it is, we don't need to overwrite it and can also skip the app.toml overwrite - var sectionKeyValues []SectionKeyValue + var sectionKeyValuesToWrite []SectionKeyValue // Set max-gas-wanted-per-tx to 60000000 - currentMaxGasWantedPerTx := serverCtx.Viper.Get(mempoolConfigName + "." + maxGasWantedPerTxName) - shoudUpdateMaxGasWantedPerTx := currentMaxGasWantedPerTx != recommendedNewMaxGasWantedPerTxValue + currentMaxGasWantedPerTx := serverCtx.Viper.Get(recommendedArbitrageMinGasFee.Section + "." + recommendedArbitrageMinGasFee.Key) + shoudUpdateMaxGasWantedPerTx := currentMaxGasWantedPerTx != recommendedArbitrageMinGasFee.Value if shoudUpdateMaxGasWantedPerTx { - serverCtx.Viper.Set(mempoolConfigName+"."+maxGasWantedPerTxName, recommendedNewMaxGasWantedPerTxValue) - sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: mempoolConfigName, Key: maxGasWantedPerTxName, Value: recommendedNewMaxGasWantedPerTxValue}) + serverCtx.Viper.Set(recommendedArbitrageMinGasFee.Section+"."+recommendedArbitrageMinGasFee.Key, recommendedArbitrageMinGasFee.Value) + sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedArbitrageMinGasFee) } // Set arbitrage-min-gas-fee to 0.1 - currentArbitrageMinGasFee := serverCtx.Viper.Get(mempoolConfigName + "." + arbitrageMinGasFeeConfigName) - shoudUpdateArbitrageMinGasFee := currentArbitrageMinGasFee != recommendedNewArbitrageMinGasFeeValue + currentArbitrageMinGasFee := serverCtx.Viper.Get(recommendedMaxGasWantedPerTx.Section + "." + recommendedMaxGasWantedPerTx.Key) + shoudUpdateArbitrageMinGasFee := currentArbitrageMinGasFee != recommendedMaxGasWantedPerTx.Value if shoudUpdateArbitrageMinGasFee { - serverCtx.Viper.Set(mempoolConfigName+"."+arbitrageMinGasFeeConfigName, recommendedNewArbitrageMinGasFeeValue) - sectionKeyValues = append(sectionKeyValues, SectionKeyValue{Section: mempoolConfigName, Key: arbitrageMinGasFeeConfigName, Value: recommendedNewArbitrageMinGasFeeValue}) + serverCtx.Viper.Set(recommendedMaxGasWantedPerTx.Section+"."+recommendedMaxGasWantedPerTx.Key, recommendedMaxGasWantedPerTx.Value) + sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedMaxGasWantedPerTx) } // Check if the file is writable @@ -545,14 +549,14 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // Note that this exits with a non-zero exit code if fails to write the file. // Write the new app.toml file - if len(sectionKeyValues) > 0 { - err := OverwriteWithCustomConfig(appFilePath, sectionKeyValues) + if len(sectionKeyValuesToWrite) > 0 { + err := OverwriteWithCustomConfig(appFilePath, sectionKeyValuesToWrite) if err != nil { return err } } } else { - fmt.Println("app.toml is not writable. Cannot apply update. Please consider manually changing arbitrage-min-gas-fee to " + recommendedNewArbitrageMinGasFeeValue + " and max-gas-wanted-per-tx to " + recommendedNewMaxGasWantedPerTxValue) + fmt.Printf("app.toml is not writable. Cannot apply update. Please consider manually changing arbitrage-min-gas-fee to %v and max-gas-wanted-per-tx to %v\n", recommendedTimeoutCommit.Value, recommendedMaxGasWantedPerTx.Value) } } return nil From 84264af9572922ef06c7d9f4f57602bc65bd0d54 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:50:52 -0600 Subject: [PATCH 15/20] even more abstraction --- cmd/osmosisd/cmd/root.go | 74 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index f8a1605f0e3..2efa942e39c 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -103,23 +103,24 @@ type SectionKeyValue struct { } var ( - // app.toml - recommendedArbitrageMinGasFee = SectionKeyValue{ - Section: "osmosis-mempool", - Key: "arbitrage-min-gas-fee", - Value: "0.1", - } - recommendedMaxGasWantedPerTx = SectionKeyValue{ - Section: "osmosis-mempool", - Key: "arbitrage-min-gas-fee", - Value: "0.1", - } - - // config.toml - recommendedTimeoutCommit = SectionKeyValue{ - Section: "consensus", - Key: "timeout_commit", - Value: "2s", + recommendedAppTomlValues = []SectionKeyValue{ + { + Section: "osmosis-mempool", + Key: "arbitrage-min-gas-fee", + Value: "0.1", + }, + { + Section: "osmosis-mempool", + Key: "arbitrage-min-gas-fee", + Value: "0.1", + }} + + recommendedConfigTomlValues = []SectionKeyValue{ + { + Section: "consensus", + Key: "timeout_commit", + Value: "2s", + }, } ) @@ -436,7 +437,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) { // overwriteConfigTomlValues overwrites config.toml values. Returns error if config.toml does not exist // // Currently, overwrites: -// - timeout_commit value in config.toml to 2s. +// - timeout_commit // // Also overwrites the respective viper config value. // @@ -462,12 +463,13 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { // If it is, we don't need to overwrite it and can also skip the app.toml overwrite var sectionKeyValuesToWrite []SectionKeyValue - // Set timeout_commit to 2s - currentTimeoutCommit := serverCtx.Viper.Get(recommendedTimeoutCommit.Section + "." + recommendedTimeoutCommit.Key) - shoudUpdateCurrentTimeoutCommit := currentTimeoutCommit != recommendedTimeoutCommit.Value - if shoudUpdateCurrentTimeoutCommit { - serverCtx.Viper.Set(recommendedTimeoutCommit.Section+"."+recommendedTimeoutCommit.Key, recommendedTimeoutCommit.Value) - sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedTimeoutCommit) + // Set aside which keys need to be updated in the config.toml + for _, rec := range recommendedConfigTomlValues { + currentValue := serverCtx.Viper.Get(rec.Section + "." + rec.Key) + if currentValue != rec.Value { + serverCtx.Viper.Set(rec.Section+"."+rec.Key, rec.Value) + sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, rec) + } } defer func() { @@ -490,7 +492,7 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { } } } else { - fmt.Printf("config.toml is not writable. Cannot apply update. Please consider manually changing timeout_commit to %v\n", recommendedTimeoutCommit.Value) + fmt.Printf("config.toml is not writable. Cannot apply update. Please consider manually changing to the following: %v\n", recommendedConfigTomlValues) } } return nil @@ -526,20 +528,12 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { // If it is, we don't need to overwrite it and can also skip the app.toml overwrite var sectionKeyValuesToWrite []SectionKeyValue - // Set max-gas-wanted-per-tx to 60000000 - currentMaxGasWantedPerTx := serverCtx.Viper.Get(recommendedArbitrageMinGasFee.Section + "." + recommendedArbitrageMinGasFee.Key) - shoudUpdateMaxGasWantedPerTx := currentMaxGasWantedPerTx != recommendedArbitrageMinGasFee.Value - if shoudUpdateMaxGasWantedPerTx { - serverCtx.Viper.Set(recommendedArbitrageMinGasFee.Section+"."+recommendedArbitrageMinGasFee.Key, recommendedArbitrageMinGasFee.Value) - sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedArbitrageMinGasFee) - } - - // Set arbitrage-min-gas-fee to 0.1 - currentArbitrageMinGasFee := serverCtx.Viper.Get(recommendedMaxGasWantedPerTx.Section + "." + recommendedMaxGasWantedPerTx.Key) - shoudUpdateArbitrageMinGasFee := currentArbitrageMinGasFee != recommendedMaxGasWantedPerTx.Value - if shoudUpdateArbitrageMinGasFee { - serverCtx.Viper.Set(recommendedMaxGasWantedPerTx.Section+"."+recommendedMaxGasWantedPerTx.Key, recommendedMaxGasWantedPerTx.Value) - sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, recommendedMaxGasWantedPerTx) + for _, rec := range recommendedAppTomlValues { + currentValue := serverCtx.Viper.Get(rec.Section + "." + rec.Key) + if currentValue != rec.Value { + serverCtx.Viper.Set(rec.Section+"."+rec.Key, rec.Value) + sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, rec) + } } // Check if the file is writable @@ -556,7 +550,7 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { } } } else { - fmt.Printf("app.toml is not writable. Cannot apply update. Please consider manually changing arbitrage-min-gas-fee to %v and max-gas-wanted-per-tx to %v\n", recommendedTimeoutCommit.Value, recommendedMaxGasWantedPerTx.Value) + fmt.Printf("app.toml is not writable. Cannot apply update. Please consider manually changing to the following: %v\n", recommendedAppTomlValues) } } return nil From a14c5673aecafabfcadd4c17bca6a36e1d92da69 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:54:10 -0600 Subject: [PATCH 16/20] add in line comments --- cmd/osmosisd/cmd/root.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 2efa942e39c..fea93a4fc91 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -1050,8 +1050,9 @@ func transformCoinValueToBaseInt(coinValue, coinDenom string, assetMap map[strin return "", fmt.Errorf("denom %s not found in asset map", coinDenom) } -// OverwriteWithCustomConfig searches the respective config file for the given section and key and overwrites the value with the given value. +// OverwriteWithCustomConfig searches the respective config file for the given section and key and overwrites the current value with the given value. func OverwriteWithCustomConfig(configFilePath string, sectionKeyValues []SectionKeyValue) error { + // Open the file for reading and writing file, err := os.OpenFile(configFilePath, os.O_RDWR, 0o644) if err != nil { return err @@ -1059,12 +1060,15 @@ func OverwriteWithCustomConfig(configFilePath string, sectionKeyValues []Section defer file.Close() // Create a map from the sectionKeyValues array + // This map will be used to quickly look up the new values for each section and key configMap := make(map[string]map[string]string) for _, skv := range sectionKeyValues { + // If the section does not exist in the map, create it if _, ok := configMap[skv.Section]; !ok { configMap[skv.Section] = make(map[string]string) } - // Check if the value is a string or a number + // Add the key and value to the section in the map + // If the value is a string, add quotes around it switch v := skv.Value.(type) { case string: configMap[skv.Section][skv.Key] = "\"" + v + "\"" @@ -1073,38 +1077,47 @@ func OverwriteWithCustomConfig(configFilePath string, sectionKeyValues []Section } } + // Read the file line by line var lines []string scanner := bufio.NewScanner(file) currentSection := "" for scanner.Scan() { line := scanner.Text() + // If the line is a section header, update the current section if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { currentSection = line[1 : len(line)-1] } else if configMap[currentSection] != nil { + // If the line is in a section that needs to be overwritten, check each key for key, value := range configMap[currentSection] { + // If the line contains the key, overwrite the line with the new key-value pair if strings.Contains(line, key) { line = key + " = " + value break } } } + // Add the line to the lines slice, whether it was overwritten or not lines = append(lines, line) } + // Check for errors from the scanner if err := scanner.Err(); err != nil { return err } + // Seek to the beginning of the file _, err = file.Seek(0, 0) if err != nil { return err } + // Truncate the file to remove the old content err = file.Truncate(0) if err != nil { return err } + // Write the new lines to the file for _, line := range lines { if _, err := file.WriteString(line + "\n"); err != nil { return err From 05bca218f650d4376ed280662de1926215783357 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:55:55 -0600 Subject: [PATCH 17/20] update config --- cmd/osmosisd/cmd/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index fea93a4fc91..397592db169 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -111,8 +111,8 @@ var ( }, { Section: "osmosis-mempool", - Key: "arbitrage-min-gas-fee", - Value: "0.1", + Key: "max-gas-wanted-per-tx", + Value: "60000000", }} recommendedConfigTomlValues = []SectionKeyValue{ From 2b6a020f2cfc6330d59f739638d6f2f8a2eaf6d7 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 13:59:16 -0600 Subject: [PATCH 18/20] in line comments --- cmd/osmosisd/cmd/root.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 397592db169..6dc15d9af1c 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -467,6 +467,9 @@ func overwriteConfigTomlValues(serverCtx *server.Context) error { for _, rec := range recommendedConfigTomlValues { currentValue := serverCtx.Viper.Get(rec.Section + "." + rec.Key) if currentValue != rec.Value { + // Current value in config.toml is not the recommended value + // Set the value in viper to the recommended value + // and add it to the list of key values we will overwrite in the config.toml serverCtx.Viper.Set(rec.Section+"."+rec.Key, rec.Value) sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, rec) } @@ -531,6 +534,9 @@ func overwriteAppTomlValues(serverCtx *server.Context) error { for _, rec := range recommendedAppTomlValues { currentValue := serverCtx.Viper.Get(rec.Section + "." + rec.Key) if currentValue != rec.Value { + // Current value in app.toml is not the recommended value + // Set the value in viper to the recommended value + // and add it to the list of key values we will overwrite in the app.toml serverCtx.Viper.Set(rec.Section+"."+rec.Key, rec.Value) sectionKeyValuesToWrite = append(sectionKeyValuesToWrite, rec) } From 8bd69b6f347868a4addeb2960f9d8abbbc55fc1c Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 14:10:28 -0600 Subject: [PATCH 19/20] fix overwrite bug --- cmd/osmosisd/cmd/root.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 6dc15d9af1c..27ef311b80c 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -1095,8 +1095,14 @@ func OverwriteWithCustomConfig(configFilePath string, sectionKeyValues []Section } else if configMap[currentSection] != nil { // If the line is in a section that needs to be overwritten, check each key for key, value := range configMap[currentSection] { - // If the line contains the key, overwrite the line with the new key-value pair - if strings.Contains(line, key) { + // Split the line into key and value parts + parts := strings.SplitN(line, "=", 2) + if len(parts) != 2 { + continue + } + // Trim spaces and compare the key part with the target key + if strings.TrimSpace(parts[0]) == key { + // If the keys match, overwrite the line with the new key-value pair line = key + " = " + value break } From ae196d5e7f7613962339555b1e8189a0df96acd1 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 23 Apr 2024 14:13:09 -0600 Subject: [PATCH 20/20] reduce diff --- cmd/osmosisd/cmd/root.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/osmosisd/cmd/root.go b/cmd/osmosisd/cmd/root.go index 27ef311b80c..85c7d39f158 100644 --- a/cmd/osmosisd/cmd/root.go +++ b/cmd/osmosisd/cmd/root.go @@ -588,9 +588,12 @@ func initAppConfig() (string, interface{}) { type CustomAppConfig struct { serverconfig.Config - OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` - SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` - WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` + + OsmosisMempoolConfig OsmosisMempoolConfig `mapstructure:"osmosis-mempool"` + + SidecarQueryServerConfig sqs.Config `mapstructure:"osmosis-sqs"` + + WasmConfig wasmtypes.WasmConfig `mapstructure:"wasm"` } var DefaultOsmosisMempoolConfig = OsmosisMempoolConfig{