-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
Co-authored-by: Julien Robert <julien@rbrt.fr> (cherry picked from commit a9f057b) # Conflicts: # server/v2/types.go # store/v2/root/factory.go
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package serverv2 | ||
|
||
import ( | ||
gogoproto "github.com/cosmos/gogoproto/proto" | ||
"github.com/spf13/viper" | ||
|
||
"cosmossdk.io/core/server" | ||
"cosmossdk.io/core/transaction" | ||
"cosmossdk.io/log" | ||
"cosmossdk.io/server/v2/appmanager" | ||
Check failure on line 10 in server/v2/types.go GitHub Actions / split-test-files
Check failure on line 10 in server/v2/types.go GitHub Actions / dependency-review
Check failure on line 10 in server/v2/types.go GitHub Actions / dependency-review
|
||
) | ||
|
||
type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T] | ||
|
||
type AppI[T transaction.Tx] interface { | ||
Name() string | ||
InterfaceRegistry() server.InterfaceRegistry | ||
GetAppManager() *appmanager.AppManager[T] | ||
GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message | ||
GetStore() any | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package root | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"cosmossdk.io/core/log" | ||
corestore "cosmossdk.io/core/store" | ||
"cosmossdk.io/store/v2" | ||
Check failure on line 10 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 10 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/commitment" | ||
Check failure on line 11 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 11 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/commitment/iavl" | ||
Check failure on line 12 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 12 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/commitment/mem" | ||
Check failure on line 13 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 13 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/db" | ||
Check failure on line 14 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 14 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/internal" | ||
Check failure on line 15 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 15 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/pruning" | ||
Check failure on line 16 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 16 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/storage" | ||
Check failure on line 17 in store/v2/root/factory.go GitHub Actions / split-test-files
Check failure on line 17 in store/v2/root/factory.go GitHub Actions / dependency-review
|
||
"cosmossdk.io/store/v2/storage/pebbledb" | ||
Check failure on line 18 in store/v2/root/factory.go GitHub Actions / split-test-files
|
||
"cosmossdk.io/store/v2/storage/rocksdb" | ||
"cosmossdk.io/store/v2/storage/sqlite" | ||
) | ||
|
||
type ( | ||
SSType string | ||
SCType string | ||
) | ||
|
||
const ( | ||
SSTypeSQLite SSType = "sqlite" | ||
SSTypePebble SSType = "pebble" | ||
SSTypeRocks SSType = "rocksdb" | ||
SCTypeIavl SCType = "iavl" | ||
SCTypeIavlV2 SCType = "iavl-v2" | ||
) | ||
|
||
// app.toml config options | ||
type Options struct { | ||
SSType SSType `mapstructure:"ss-type" toml:"ss-type" comment:"SState storage database type. Currently we support: \"sqlite\", \"pebble\" and \"rocksdb\""` | ||
SCType SCType `mapstructure:"sc-type" toml:"sc-type" comment:"State commitment database type. Currently we support: \"iavl\" and \"iavl-v2\""` | ||
SSPruningOption *store.PruningOption `mapstructure:"ss-pruning-option" toml:"ss-pruning-option" comment:"Pruning options for state storage"` | ||
SCPruningOption *store.PruningOption `mapstructure:"sc-pruning-option" toml:"sc-pruning-option" comment:"Pruning options for state commitment"` | ||
IavlConfig *iavl.Config `mapstructure:"iavl-config" toml:"iavl-config"` | ||
} | ||
|
||
// FactoryOptions are the options for creating a root store. | ||
type FactoryOptions struct { | ||
Logger log.Logger | ||
RootDir string | ||
Options Options | ||
StoreKeys []string | ||
SCRawDB corestore.KVStoreWithBatch | ||
} | ||
|
||
// DefaultStoreOptions returns the default options for creating a root store. | ||
func DefaultStoreOptions() Options { | ||
return Options{ | ||
SSType: SSTypeSQLite, | ||
SCType: SCTypeIavl, | ||
SCPruningOption: &store.PruningOption{ | ||
KeepRecent: 2, | ||
Interval: 100, | ||
}, | ||
SSPruningOption: &store.PruningOption{ | ||
KeepRecent: 2, | ||
Interval: 100, | ||
}, | ||
IavlConfig: &iavl.Config{ | ||
CacheSize: 100_000, | ||
SkipFastStorageUpgrade: true, | ||
}, | ||
} | ||
} | ||
|
||
// CreateRootStore is a convenience function to create a root store based on the | ||
// provided FactoryOptions. Strictly speaking app developers can create the root | ||
// store directly by calling root.New, so this function is not | ||
// necessary, but demonstrates the required steps and configuration to create a root store. | ||
func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) { | ||
var ( | ||
ssDb storage.Database | ||
ss *storage.StorageStore | ||
sc *commitment.CommitStore | ||
err error | ||
ensureDir = func(dir string) error { | ||
if err := os.MkdirAll(dir, 0o0755); err != nil { | ||
return fmt.Errorf("failed to create directory %s: %w", dir, err) | ||
} | ||
return nil | ||
} | ||
) | ||
|
||
storeOpts := opts.Options | ||
switch storeOpts.SSType { | ||
case SSTypeSQLite: | ||
dir := fmt.Sprintf("%s/data/ss/sqlite", opts.RootDir) | ||
if err = ensureDir(dir); err != nil { | ||
return nil, err | ||
} | ||
ssDb, err = sqlite.New(dir) | ||
case SSTypePebble: | ||
dir := fmt.Sprintf("%s/data/ss/pebble", opts.RootDir) | ||
if err = ensureDir(dir); err != nil { | ||
return nil, err | ||
} | ||
ssDb, err = pebbledb.New(dir) | ||
case SSTypeRocks: | ||
dir := fmt.Sprintf("%s/data/ss/rocksdb", opts.RootDir) | ||
if err = ensureDir(dir); err != nil { | ||
return nil, err | ||
} | ||
ssDb, err = rocksdb.New(dir) | ||
default: | ||
return nil, fmt.Errorf("unknown storage type: %s", opts.Options.SSType) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
ss = storage.NewStorageStore(ssDb, opts.Logger) | ||
|
||
metadata := commitment.NewMetadataStore(opts.SCRawDB) | ||
latestVersion, err := metadata.GetLatestVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(opts.StoreKeys) == 0 { | ||
lastCommitInfo, err := metadata.GetCommitInfo(latestVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if lastCommitInfo == nil { | ||
return nil, fmt.Errorf("tried to construct a root store with no store keys specified but no commit info found for version %d", latestVersion) | ||
} | ||
for _, si := range lastCommitInfo.StoreInfos { | ||
opts.StoreKeys = append(opts.StoreKeys, string(si.Name)) | ||
} | ||
} | ||
removedStoreKeys, err := metadata.GetRemovedStoreKeys(latestVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newTreeFn := func(key string) (commitment.Tree, error) { | ||
if internal.IsMemoryStoreKey(key) { | ||
return mem.New(), nil | ||
} else { | ||
switch storeOpts.SCType { | ||
case SCTypeIavl: | ||
return iavl.NewIavlTree(db.NewPrefixDB(opts.SCRawDB, []byte(key)), opts.Logger, storeOpts.IavlConfig), nil | ||
case SCTypeIavlV2: | ||
return nil, errors.New("iavl v2 not supported") | ||
default: | ||
return nil, errors.New("unsupported commitment store type") | ||
} | ||
} | ||
} | ||
|
||
trees := make(map[string]commitment.Tree, len(opts.StoreKeys)) | ||
for _, key := range opts.StoreKeys { | ||
tree, err := newTreeFn(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
trees[key] = tree | ||
} | ||
oldTrees := make(map[string]commitment.Tree, len(opts.StoreKeys)) | ||
for _, key := range removedStoreKeys { | ||
tree, err := newTreeFn(string(key)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
oldTrees[string(key)] = tree | ||
} | ||
|
||
sc, err = commitment.NewCommitStore(trees, oldTrees, opts.SCRawDB, opts.Logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption) | ||
return New(opts.Logger, ss, sc, pm, nil, nil) | ||
} |