From b5dbc95fcafac1f1b865fc9bdc75b7df57a8d278 Mon Sep 17 00:00:00 2001 From: Yiming Zang Date: Thu, 29 Jun 2023 07:03:08 -0700 Subject: [PATCH 1/3] Add a new snapshot directory config --- baseapp/baseapp.go | 1 + baseapp/options.go | 13 +++++++++++++ server/config/config.go | 6 ++++++ server/config/config_test.go | 5 +++++ server/config/toml.go | 4 ++++ server/start.go | 1 + simapp/simd/cmd/root.go | 11 ++++++++--- 7 files changed, 38 insertions(+), 3 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 9c2a75a94..174924baf 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -214,6 +214,7 @@ type snapshotData struct { snapshotManager *snapshots.Manager snapshotInterval uint64 // block interval between state sync snapshots snapshotKeepRecent uint32 // recent state sync snapshots to keep + snapshotDirectory string // state sync snapshots directory } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a diff --git a/baseapp/options.go b/baseapp/options.go index 84c212607..3eac7f812 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -92,6 +92,11 @@ func SetSnapshotKeepRecent(keepRecent uint32) func(*BaseApp) { return func(app *BaseApp) { app.SetSnapshotKeepRecent(keepRecent) } } +// SetSnapshotDirectory sets the snapshot directory. +func SetSnapshotDirectory(dir string) func(*BaseApp) { + return func(app *BaseApp) { app.SetSnapshotDirectory(dir) } +} + // SetSnapshotStore sets the snapshot store. func SetSnapshotStore(snapshotStore *snapshots.Store) func(*BaseApp) { return func(app *BaseApp) { app.SetSnapshotStore(snapshotStore) } @@ -298,6 +303,14 @@ func (app *BaseApp) SetSnapshotKeepRecent(snapshotKeepRecent uint32) { app.snapshotKeepRecent = snapshotKeepRecent } +// SetSnapshotDirectory sets the snapshot directory. +func (app *BaseApp) SetSnapshotDirectory(dir string) { + if app.sealed { + panic("SetSnapshotDirectory() on sealed BaseApp") + } + app.snapshotDirectory = dir +} + // SetInterfaceRegistry sets the InterfaceRegistry. func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) { app.interfaceRegistry = registry diff --git a/server/config/config.go b/server/config/config.go index 30a2b2549..9a794cd08 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -174,6 +174,10 @@ type StateSyncConfig struct { // SnapshotKeepRecent sets the number of recent state sync snapshots to keep. // 0 keeps all snapshots. SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"` + + // SnapshotDirectory sets the parent directory for where state sync snapshots are persisted. + // Default is emtpy which will then store under the app home directory. + SnapshotDirectory string `mapstructure:"snapshot-directory"` } // Config defines the server's top level configuration @@ -264,6 +268,7 @@ func DefaultConfig() *Config { StateSync: StateSyncConfig{ SnapshotInterval: 0, SnapshotKeepRecent: 2, + SnapshotDirectory: "", }, } } @@ -345,6 +350,7 @@ func GetConfig(v *viper.Viper) (Config, error) { StateSync: StateSyncConfig{ SnapshotInterval: v.GetUint64("state-sync.snapshot-interval"), SnapshotKeepRecent: v.GetUint32("state-sync.snapshot-keep-recent"), + SnapshotDirectory: v.GetString("state-sync.snapshot-directory"), }, }, nil } diff --git a/server/config/config_test.go b/server/config/config_test.go index e828a0716..ce733c346 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -18,3 +18,8 @@ func TestSetMinimumFees(t *testing.T) { cfg.SetMinGasPrices(sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)}) require.Equal(t, "5.000000000000000000foo", cfg.MinGasPrices) } + +func TestSetSnapshotDirectory(t *testing.T) { + cfg := DefaultConfig() + require.Equal(t, "", cfg.StateSync.SnapshotDirectory) +} diff --git a/server/config/toml.go b/server/config/toml.go index 159254c5d..89e305c51 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -232,6 +232,10 @@ snapshot-interval = {{ .StateSync.SnapshotInterval }} # snapshot-keep-recent specifies the number of recent snapshots to keep and serve (0 to keep all). snapshot-keep-recent = {{ .StateSync.SnapshotKeepRecent }} + +# snapshot-directory sets the directory for where state sync snapshots are persisted. +# default is emtpy which will then store under the app home directory same as before. +snapshot-directory = {{ .StateSync.SnapshotDirectory }} ` var configTemplate *template.Template diff --git a/server/start.go b/server/start.go index bb7812e57..abdc97a52 100644 --- a/server/start.go +++ b/server/start.go @@ -74,6 +74,7 @@ const ( // state sync-related flags FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval" FlagStateSyncSnapshotKeepRecent = "state-sync.snapshot-keep-recent" + FlagStateSyncSnapshotDir = "state-sync.snapshot-dir" // gRPC-related flags flagGRPCOnly = "grpc-only" diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 843605252..7912766cc 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -270,12 +270,16 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, t panic(err) } - snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots") - snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir) + snapshotDirectory := cast.ToString(appOpts.Get(server.FlagStateSyncSnapshotDir)) + if snapshotDirectory == "" { + snapshotDirectory = filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots") + } + + snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDirectory) if err != nil { panic(err) } - snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir) + snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDirectory) if err != nil { panic(err) } @@ -298,6 +302,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, t baseapp.SetSnapshotStore(snapshotStore), baseapp.SetSnapshotInterval(cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval))), baseapp.SetSnapshotKeepRecent(cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent))), + baseapp.SetSnapshotDirectory(snapshotDirectory), baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagIAVLFastNode))), baseapp.SetCompactionInterval(cast.ToUint64(appOpts.Get(server.FlagCompactionInterval))), From a1abbeecba1f2d1d63538fd39eb85c8c57d17fb0 Mon Sep 17 00:00:00 2001 From: Yiming Zang Date: Thu, 29 Jun 2023 07:06:28 -0700 Subject: [PATCH 2/3] Fix --- server/start.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/start.go b/server/start.go index abdc97a52..14f4e9770 100644 --- a/server/start.go +++ b/server/start.go @@ -74,7 +74,7 @@ const ( // state sync-related flags FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval" FlagStateSyncSnapshotKeepRecent = "state-sync.snapshot-keep-recent" - FlagStateSyncSnapshotDir = "state-sync.snapshot-dir" + FlagStateSyncSnapshotDir = "state-sync.snapshot-directory" // gRPC-related flags flagGRPCOnly = "grpc-only" From 3200886c0595e9ca260f85ec748510e4d2810f79 Mon Sep 17 00:00:00 2001 From: Yiming Zang Date: Thu, 29 Jun 2023 07:27:14 -0700 Subject: [PATCH 3/3] Add quote --- server/config/toml.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/config/toml.go b/server/config/toml.go index 89e305c51..47571fdff 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -235,7 +235,8 @@ snapshot-keep-recent = {{ .StateSync.SnapshotKeepRecent }} # snapshot-directory sets the directory for where state sync snapshots are persisted. # default is emtpy which will then store under the app home directory same as before. -snapshot-directory = {{ .StateSync.SnapshotDirectory }} +snapshot-directory = "{{ .StateSync.SnapshotDirectory }}" + ` var configTemplate *template.Template