-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e2f207
commit e20c2f5
Showing
20 changed files
with
527 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
|
||
"github.com/cosmos/cosmos-sdk/store" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
// GetPruningOptionsFromFlags parses command flags and returns the correct | ||
// PruningOptions. If a pruning strategy is provided, that will be parsed and | ||
// returned, otherwise, it is assumed custom pruning options are provided. | ||
func GetPruningOptionsFromFlags() (types.PruningOptions, error) { | ||
strategy := strings.ToLower(viper.GetString(FlagPruning)) | ||
|
||
switch strategy { | ||
case types.PruningOptionDefault, types.PruningOptionNothing, types.PruningOptionEverything: | ||
return types.NewPruningOptionsFromString(strategy), nil | ||
|
||
case types.PruningOptionCustom: | ||
opts := types.NewPruningOptions( | ||
viper.GetUint64(FlagPruningKeepRecent), | ||
viper.GetUint64(FlagPruningKeepEvery), viper.GetUint64(FlagPruningInterval), | ||
) | ||
|
||
if err := opts.Validate(); err != nil { | ||
return opts, fmt.Errorf("invalid custom pruning options: %w", err) | ||
} | ||
|
||
return opts, nil | ||
|
||
default: | ||
return store.PruningOptions{}, fmt.Errorf("unknown pruning strategy %s", strategy) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
func TestGetPruningOptionsFromFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
initParams func() | ||
expectedOptions types.PruningOptions | ||
wantErr bool | ||
}{ | ||
{ | ||
name: FlagPruning, | ||
initParams: func() { | ||
viper.Set(FlagPruning, types.PruningOptionNothing) | ||
}, | ||
expectedOptions: types.PruneNothing, | ||
}, | ||
{ | ||
name: "custom pruning options", | ||
initParams: func() { | ||
viper.Set(FlagPruning, types.PruningOptionCustom) | ||
viper.Set(FlagPruningKeepRecent, 1234) | ||
viper.Set(FlagPruningKeepEvery, 4321) | ||
viper.Set(FlagPruningInterval, 10) | ||
}, | ||
expectedOptions: types.PruningOptions{ | ||
KeepRecent: 1234, | ||
KeepEvery: 4321, | ||
Interval: 10, | ||
}, | ||
}, | ||
{ | ||
name: types.PruningOptionDefault, | ||
initParams: func() {}, | ||
expectedOptions: types.PruneDefault, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(j *testing.T) { | ||
viper.Reset() | ||
viper.SetDefault(FlagPruning, types.PruningOptionDefault) | ||
tt.initParams() | ||
|
||
opts, err := GetPruningOptionsFromFlags() | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.Equal(t, tt.expectedOptions, opts) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.