Skip to content

Commit

Permalink
confgi the migration manager in root factory
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Oct 22, 2024
1 parent cd3ce0c commit 6362846
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"cosmossdk.io/store/v2/commitment/mem"
"cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/internal"
"cosmossdk.io/store/v2/migration"
"cosmossdk.io/store/v2/pruning"
"cosmossdk.io/store/v2/snapshots"
"cosmossdk.io/store/v2/storage"
"cosmossdk.io/store/v2/storage/pebbledb"
"cosmossdk.io/store/v2/storage/rocksdb"
Expand Down Expand Up @@ -139,11 +141,11 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
return nil, err
}

newTreeFn := func(key string) (commitment.Tree, error) {
newTreeFn := func(key string, scType SCType) (commitment.Tree, error) {
if internal.IsMemoryStoreKey(key) {
return mem.New(), nil
} else {
switch storeOpts.SCType {
switch scType {
case SCTypeIavl:
return iavl.NewIavlTree(db.NewPrefixDB(opts.SCRawDB, []byte(key)), opts.Logger, storeOpts.IavlConfig), nil
case SCTypeIavlV2:
Expand All @@ -154,17 +156,31 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
}
}

// check if we need to migrate the store
isMigrating := false
scType := storeOpts.SCType
ssLatestVersion, err := ss.GetLatestVersion()
if err != nil {
return nil, err
}
if ssLatestVersion != latestVersion {
isMigrating = true // need to migrate
if scType != SCTypeIavl {
scType = SCTypeIavl // only support iavl v1 for migration
}
}

trees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range opts.StoreKeys {
tree, err := newTreeFn(key)
tree, err := newTreeFn(key, scType)
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))
tree, err := newTreeFn(string(key), scType)
if err != nil {
return nil, err
}
Expand All @@ -176,6 +192,31 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
return nil, err
}

var mm *migration.Manager
if isMigrating {
snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
if err != nil {
return nil, err
}
snapshotMgr := snapshots.NewManager(snapshotDB, snapshots.SnapshotOptions{}, sc, nil, nil, opts.Logger)
var newSC *commitment.CommitStore
if scType != storeOpts.SCType {
newTrees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range opts.StoreKeys {
tree, err := newTreeFn(key, storeOpts.SCType)
if err != nil {
return nil, err
}
newTrees[key] = tree
}
newSC, err = commitment.NewCommitStore(newTrees, nil, opts.SCRawDB, opts.Logger)
if err != nil {
return nil, err
}
mm = migration.NewManager(opts.SCRawDB, snapshotMgr, ss, newSC, opts.Logger)
}
}

pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, nil, nil)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, mm, nil)
}

0 comments on commit 6362846

Please sign in to comment.