Skip to content

Commit

Permalink
fix: use correct config key for db_backend (backport cosmos#17406) (c…
Browse files Browse the repository at this point in the history
…osmos#17412)

Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
2 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 100531d commit 296c03c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/genutil) [#17296](https://github.com/cosmos/cosmos-sdk/pull/17296) Add `MigrateHandler` to allow reuse migrate genesis related function.
* In v0.46, v0.47 this function is additive to the `genesis migrate` command. However in v0.50+, adding custom migrations to the `genesis migrate` command is directly possible.

## Bug Fixes

* (server) [#17181](https://github.com/cosmos/cosmos-sdk/pull/17181) Fix `db_backend` lookup fallback from `config.toml`.

## [v0.46.14](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.14) - 2023-07-17

### Features
Expand Down
6 changes: 2 additions & 4 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,8 @@ func GetAppDBBackend(opts types.AppOptions) dbm.BackendType {
if len(rv) == 0 {
rv = cast.ToString(opts.Get("db_backend"))
}

// Cosmos SDK has migrated to cosmos-db which does not support all the backends which tm-db supported
if rv == "cleveldb" || rv == "badgerdb" || rv == "boltdb" {
panic(fmt.Sprintf("invalid app-db-backend %q, use %q, %q, %q instead", rv, dbm.GoLevelDBBackend, dbm.PebbleDBBackend, dbm.RocksDBBackend))
if len(rv) == 0 {
rv = cast.ToString(opts.Get("db_backend"))
}

if len(rv) != 0 {
Expand Down
112 changes: 112 additions & 0 deletions server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,115 @@ func TestInterceptConfigsWithBadPermissions(t *testing.T) {
t.Fatalf("Failed to catch permissions error, got: [%T] %v", err, err)
}
}

func TestEmptyMinGasPrices(t *testing.T) {
tempDir := t.TempDir()
err := os.Mkdir(filepath.Join(tempDir, "config"), os.ModePerm)
require.NoError(t, err)
encCfg := simapp.MakeTestEncodingConfig()

// Run InitCmd to create necessary config files.
clientCtx := client.Context{}.WithHomeDir(tempDir).WithCodec(encCfg.Codec)
serverCtx := server.NewDefaultContext()
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
cmd := genutilcli.InitCmd(simapp.ModuleBasics, tempDir)
cmd.SetArgs([]string{"appnode-test"})
err = cmd.ExecuteContext(ctx)
require.NoError(t, err)

// Modify app.toml.
appCfgTempFilePath := filepath.Join(tempDir, "config", "app.toml")
appConf := config.DefaultConfig()
appConf.BaseConfig.MinGasPrices = ""
config.WriteConfigFile(appCfgTempFilePath, appConf)

// Run StartCmd.
cmd = server.StartCmd(nil, tempDir)
cmd.PreRunE = func(cmd *cobra.Command, _ []string) error {
return server.InterceptConfigsPreRunHandler(cmd, "", nil, tmcfg.DefaultConfig())
}
err = cmd.ExecuteContext(ctx)
require.Errorf(t, err, sdkerrors.ErrAppConfig.Error())
}

type mapGetter map[string]interface{}

func (m mapGetter) Get(key string) interface{} {
return m[key]
}

var _ servertypes.AppOptions = mapGetter{}

func TestGetAppDBBackend(t *testing.T) {
origDBBackend := types.DBBackend
defer func() {
types.DBBackend = origDBBackend
}()
tests := []struct {
name string
dbBack string
opts mapGetter
exp dbm.BackendType
}{
{
name: "nothing set",
dbBack: "",
opts: mapGetter{},
exp: dbm.GoLevelDBBackend,
},

{
name: "only db_backend set",
dbBack: "",
opts: mapGetter{"db_backend": "db_backend value 1"},
exp: dbm.BackendType("db_backend value 1"),
},
{
name: "only DBBackend set",
dbBack: "DBBackend value 2",
opts: mapGetter{},
exp: dbm.BackendType("DBBackend value 2"),
},
{
name: "only app-db-backend set",
dbBack: "",
opts: mapGetter{"app-db-backend": "app-db-backend value 3"},
exp: dbm.BackendType("app-db-backend value 3"),
},

{
name: "app-db-backend and db-backend set",
dbBack: "",
opts: mapGetter{"db_backend": "db_backend value 4", "app-db-backend": "app-db-backend value 5"},
exp: dbm.BackendType("app-db-backend value 5"),
},
{
name: "app-db-backend and DBBackend set",
dbBack: "DBBackend value 6",
opts: mapGetter{"app-db-backend": "app-db-backend value 7"},
exp: dbm.BackendType("app-db-backend value 7"),
},
{
name: "db_backend and DBBackend set",
dbBack: "DBBackend value 8",
opts: mapGetter{"db_backend": "db_backend value 9"},
exp: dbm.BackendType("DBBackend value 8"),
},

{
name: "all of app-db-backend db-backend DBBackend set",
dbBack: "DBBackend value 10",
opts: mapGetter{"db_backend": "db_backend value 11", "app-db-backend": "app-db-backend value 12"},
exp: dbm.BackendType("app-db-backend value 12"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(st *testing.T) {
types.DBBackend = tc.dbBack
act := server.GetAppDBBackend(tc.opts)
assert.Equal(st, tc.exp, act)
})
}
}

0 comments on commit 296c03c

Please sign in to comment.