|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cosmos/cosmos-sdk/baseapp" |
| 7 | + servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 8 | + "github.com/cosmos/cosmos-sdk/storev2/rootmulti" |
| 9 | + "github.com/sei-protocol/sei-db/config" |
| 10 | + "github.com/spf13/cast" |
| 11 | + "github.com/tendermint/tendermint/libs/log" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // SC Store configs |
| 16 | + FlagSCEnable = "state-commit.sc-enable" |
| 17 | + FlagSCDirectory = "state-commit.sc-directory" |
| 18 | + FlagSCAsyncCommitBuffer = "state-commit.sc-async-commit-buffer" |
| 19 | + FlagSCZeroCopy = "state-commit.sc-zero-copy" |
| 20 | + FlagSCSnapshotKeepRecent = "state-commit.sc-keep-recent" |
| 21 | + FlagSCSnapshotInterval = "state-commit.sc-snapshot-interval" |
| 22 | + FlagSCSnapshotWriterLimit = "state-commit.sc-snapshot-writer-limit" |
| 23 | + FlagSCCacheSize = "state-commit.sc-cache-size" |
| 24 | + |
| 25 | + // SS Store configs |
| 26 | + FlagSSEnable = "state-store.ss-enable" |
| 27 | + FlagSSDirectory = "state-store.ss-db-directory" |
| 28 | + FlagSSBackend = "state-store.ss-backend" |
| 29 | + FlagSSAsyncWriterBuffer = "state-store.ss-async-write-buffer" |
| 30 | + FlagSSKeepRecent = "state-store.ss-keep-recent" |
| 31 | + FlagSSPruneInterval = "state-store.ss-prune-interval" |
| 32 | + FlagSSImportNumWorkers = "state-store.ss-import-num-workers" |
| 33 | + |
| 34 | + // Other configs |
| 35 | + FlagSnapshotInterval = "state-sync.snapshot-interval" |
| 36 | +) |
| 37 | + |
| 38 | +func SetupSeiDB( |
| 39 | + logger log.Logger, |
| 40 | + homePath string, |
| 41 | + appOpts servertypes.AppOptions, |
| 42 | + baseAppOptions []func(*baseapp.BaseApp), |
| 43 | +) []func(*baseapp.BaseApp) { |
| 44 | + scEnabled := cast.ToBool(appOpts.Get(FlagSCEnable)) |
| 45 | + if !scEnabled { |
| 46 | + logger.Info("SeiDB is disabled, falling back to IAVL") |
| 47 | + return baseAppOptions |
| 48 | + } |
| 49 | + logger.Info("SeiDB SC is enabled, running node with StoreV2 commit store") |
| 50 | + scConfig := parseSCConfigs(appOpts) |
| 51 | + ssConfig := parseSSConfigs(appOpts) |
| 52 | + if ssConfig.Enable { |
| 53 | + logger.Info(fmt.Sprintf("SeiDB StateStore is enabled, running %s for historical state", ssConfig.Backend)) |
| 54 | + } |
| 55 | + validateConfigs(appOpts) |
| 56 | + |
| 57 | + // cms must be overridden before the other options, because they may use the cms, |
| 58 | + // make sure the cms aren't be overridden by the other options later on. |
| 59 | + cms := rootmulti.NewStore(homePath, logger, scConfig, ssConfig) |
| 60 | + baseAppOptions = append([]func(*baseapp.BaseApp){ |
| 61 | + func(baseApp *baseapp.BaseApp) { |
| 62 | + baseApp.SetCMS(cms) |
| 63 | + }, |
| 64 | + }, baseAppOptions...) |
| 65 | + |
| 66 | + return baseAppOptions |
| 67 | +} |
| 68 | + |
| 69 | +func parseSCConfigs(appOpts servertypes.AppOptions) config.StateCommitConfig { |
| 70 | + return config.StateCommitConfig{ |
| 71 | + Enable: cast.ToBool(appOpts.Get(FlagSCEnable)), |
| 72 | + Directory: cast.ToString(appOpts.Get(FlagSCDirectory)), |
| 73 | + ZeroCopy: cast.ToBool(appOpts.Get(FlagSCZeroCopy)), |
| 74 | + AsyncCommitBuffer: cast.ToInt(appOpts.Get(FlagSCAsyncCommitBuffer)), |
| 75 | + SnapshotKeepRecent: cast.ToUint32(appOpts.Get(FlagSCSnapshotKeepRecent)), |
| 76 | + SnapshotInterval: cast.ToUint32(appOpts.Get(FlagSCSnapshotInterval)), |
| 77 | + SnapshotWriterLimit: cast.ToInt(appOpts.Get(FlagSCSnapshotWriterLimit)), |
| 78 | + CacheSize: cast.ToInt(appOpts.Get(FlagSCCacheSize)), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func parseSSConfigs(appOpts servertypes.AppOptions) config.StateStoreConfig { |
| 83 | + return config.StateStoreConfig{ |
| 84 | + Enable: cast.ToBool(appOpts.Get(FlagSSEnable)), |
| 85 | + Backend: cast.ToString(appOpts.Get(FlagSSBackend)), |
| 86 | + AsyncWriteBuffer: cast.ToInt(appOpts.Get(FlagSSAsyncWriterBuffer)), |
| 87 | + KeepRecent: cast.ToInt(appOpts.Get(FlagSSKeepRecent)), |
| 88 | + PruneIntervalSeconds: cast.ToInt(appOpts.Get(FlagSSPruneInterval)), |
| 89 | + ImportNumWorkers: cast.ToInt(appOpts.Get(FlagSSImportNumWorkers)), |
| 90 | + DBDirectory: cast.ToString(appOpts.Get(FlagSSDirectory)), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func validateConfigs(appOpts servertypes.AppOptions) { |
| 95 | + scEnabled := cast.ToBool(appOpts.Get(FlagSCEnable)) |
| 96 | + ssEnabled := cast.ToBool(appOpts.Get(FlagSSEnable)) |
| 97 | + snapshotExportInterval := cast.ToUint64(appOpts.Get(FlagSnapshotInterval)) |
| 98 | + // Make sure when snapshot is enabled, we should enable SS store |
| 99 | + if snapshotExportInterval > 0 && scEnabled { |
| 100 | + if !ssEnabled { |
| 101 | + panic(fmt.Sprintf("Config validation failed, SeiDB SS store needs to be enabled when snapshot interval %d > 0", snapshotExportInterval)) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments