forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR cosmos#5709: Add granularity options to pruning state
- Loading branch information
Showing
5 changed files
with
233 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// GetPruningOptionsFromFlags parses start command flags and returns the correct PruningOptions. | ||
// flagPruning prevails over flagPruningKeepEvery and flagPruningSnapshotEvery. | ||
// Default option is PruneSyncable. | ||
func GetPruningOptionsFromFlags() store.PruningOptions { | ||
if viper.IsSet(flagPruning) { | ||
return store.NewPruningOptionsFromString(viper.GetString(flagPruning)) | ||
} | ||
|
||
if viper.IsSet(flagPruningKeepEvery) && viper.IsSet(flagPruningSnapshotEvery) { | ||
return store.PruningOptions{ | ||
KeepEvery: viper.GetInt64(flagPruningKeepEvery), | ||
SnapshotEvery: viper.GetInt64(flagPruningSnapshotEvery), | ||
} | ||
} | ||
|
||
return store.PruneSyncable | ||
} |
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,51 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/store" | ||
) | ||
|
||
func TestGetPruningOptionsFromFlags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
initParams func() | ||
expectedOptions store.PruningOptions | ||
}{ | ||
{ | ||
name: "pruning", | ||
initParams: func() { | ||
viper.Set(flagPruning, store.PruningStrategyNothing) | ||
}, | ||
expectedOptions: store.PruneNothing, | ||
}, | ||
{ | ||
name: "granular pruning", | ||
initParams: func() { | ||
viper.Set(flagPruningSnapshotEvery, 1234) | ||
viper.Set(flagPruningKeepEvery, 4321) | ||
}, | ||
expectedOptions: store.PruningOptions{ | ||
SnapshotEvery: 1234, | ||
KeepEvery: 4321, | ||
}, | ||
}, | ||
{ | ||
name: "default", | ||
initParams: func() {}, | ||
expectedOptions: store.PruneSyncable, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(j *testing.T) { | ||
viper.Reset() | ||
tt.initParams() | ||
require.Equal(t, tt.expectedOptions, GetPruningOptionsFromFlags()) | ||
}) | ||
} | ||
} |
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,104 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPruningOptions(t *testing.T) { | ||
startCommand := StartCmd(nil, nil) | ||
|
||
tests := []struct { | ||
name string | ||
paramInit func() | ||
returnsErr bool | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "none set, returns nil and will use default from flags", | ||
paramInit: func() {}, | ||
returnsErr: false, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "only keep-every provided", | ||
paramInit: func() { | ||
viper.Set(flagPruningKeepEvery, 12345) | ||
}, | ||
returnsErr: true, | ||
expectedErr: errPruningGranularOptions, | ||
}, | ||
{ | ||
name: "only snapshot-every provided", | ||
paramInit: func() { | ||
viper.Set(flagPruningSnapshotEvery, 12345) | ||
}, | ||
returnsErr: true, | ||
expectedErr: errPruningGranularOptions, | ||
}, | ||
{ | ||
name: "pruning flag with other granular options 1", | ||
paramInit: func() { | ||
viper.Set(flagPruning, "set") | ||
viper.Set(flagPruningSnapshotEvery, 1234) | ||
}, | ||
returnsErr: true, | ||
expectedErr: errPruningWithGranularOptions, | ||
}, | ||
{ | ||
name: "pruning flag with other granular options 2", | ||
paramInit: func() { | ||
viper.Set(flagPruning, "set") | ||
viper.Set(flagPruningKeepEvery, 1234) | ||
}, | ||
returnsErr: true, | ||
expectedErr: errPruningWithGranularOptions, | ||
}, | ||
{ | ||
name: "pruning flag with other granular options 3", | ||
paramInit: func() { | ||
viper.Set(flagPruning, "set") | ||
viper.Set(flagPruningKeepEvery, 1234) | ||
viper.Set(flagPruningSnapshotEvery, 1234) | ||
}, | ||
returnsErr: true, | ||
expectedErr: errPruningWithGranularOptions, | ||
}, | ||
{ | ||
name: "only prunning set", | ||
paramInit: func() { | ||
viper.Set(flagPruning, "set") | ||
}, | ||
returnsErr: false, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "only granular set", | ||
paramInit: func() { | ||
viper.Set(flagPruningSnapshotEvery, 12345) | ||
viper.Set(flagPruningKeepEvery, 12345) | ||
}, | ||
returnsErr: false, | ||
expectedErr: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
viper.Reset() | ||
tt.paramInit() | ||
|
||
err := startCommand.PreRunE(nil, nil) | ||
|
||
if tt.returnsErr { | ||
require.EqualError(t, err, tt.expectedErr.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |