Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store/v2): build the migration manager in the root store factory #22336

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6362846
confgi the migration manager in root factory
cool-develope Oct 22, 2024
e7bd885
minor fix
cool-develope Oct 22, 2024
3bb541b
add simple test
cool-develope Oct 23, 2024
1e6e4f1
small fix
cool-develope Oct 24, 2024
cb86a77
Merge branch 'main' into store/factory_migration
cool-develope Oct 24, 2024
31eb8fd
fix migration
cool-develope Oct 25, 2024
79d978b
Merge branch 'main' into store/factory_migration
cool-develope Oct 25, 2024
eb8029c
fix db close
cool-develope Oct 28, 2024
01a470e
Merge branch 'main' into store/factory_migration
cool-develope Oct 29, 2024
f3091f5
Merge branch 'main' into store/factory_migration
tac0turtle Dec 9, 2024
19c6da3
go mod change
tac0turtle Dec 9, 2024
f58431a
fix diff
tac0turtle Dec 9, 2024
4ef10b7
fix factory
tac0turtle Dec 9, 2024
b854054
Merge branch 'main' into store/factory_migration
tac0turtle Jan 2, 2025
ddaf4da
return error when version is not present
tac0turtle Jan 2, 2025
1b93050
Merge branch 'main' into store/factory_migration
tac0turtle Jan 5, 2025
03fdd2a
Merge branch 'main' into store/factory_migration
tac0turtle Jan 6, 2025
ae75111
fix build
tac0turtle Jan 6, 2025
a10a72f
minor touch up of build
tac0turtle Jan 6, 2025
5f2cb83
fixes
tac0turtle Jan 6, 2025
99a813a
refactor(store/v2): compatible with storev1 (#23201)
tac0turtle Jan 7, 2025
2e13d91
Merge branch 'main' into store/factory_migration
tac0turtle Jan 7, 2025
2b9419e
lint fixes
tac0turtle Jan 7, 2025
2106a3b
fix cometbft tests
tac0turtle Jan 8, 2025
010bc58
Merge branch 'main' into store/factory_migration
tac0turtle Jan 8, 2025
31a4aed
fix simapp tests
tac0turtle Jan 8, 2025
6ee0260
store fix
tac0turtle Jan 8, 2025
ba62dd5
revert make file changes
tac0turtle Jan 8, 2025
cb687e2
revert build file changes
tac0turtle Jan 8, 2025
e04b614
drop convert function
tac0turtle Jan 8, 2025
759cb53
fix lint
tac0turtle Jan 8, 2025
1c5b07d
Merge branch 'main' into store/factory_migration
tac0turtle Jan 8, 2025
76aeebe
fix bank failing test
tac0turtle Jan 8, 2025
f9da53b
fix store tests
tac0turtle Jan 8, 2025
4f8ca79
Merge branch 'main' into store/factory_migration
tac0turtle Jan 8, 2025
9a55795
Merge branch 'main' into store/factory_migration
tac0turtle Jan 9, 2025
b5b2fa0
fix go mod
tac0turtle Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify isMigrating assignment

You can assign the result of the condition directly to isMigrating to make the code more concise.

Apply this diff to streamline the variable initialization:

-isMigrating := false
...
-if ssLatestVersion != latestVersion {
-    isMigrating = true // need to migrate
+isMigrating := ssLatestVersion != latestVersion
 if isMigrating {
     if scType != SCTypeIavl {
         scType = SCTypeIavl // only support iavl v1 for migration
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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
}
}
// check if we need to migrate the store
scType := storeOpts.SCType
ssLatestVersion, err := ss.GetLatestVersion()
if err != nil {
return nil, err
}
isMigrating := ssLatestVersion != latestVersion
if isMigrating {
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
}
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider refactoring migration setup for better maintainability.

The migration setup logic is complex and handles multiple concerns. Consider extracting it into a separate function for better maintainability and testing.

func setupMigrationManager(opts *FactoryOptions, ss *storage.StorageStore, sc *commitment.CommitStore, scType SCType) (*migration.Manager, error) {
    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 != opts.Options.SCType {
        newSC, err = createNewCommitStore(opts, scType)
        if err != nil {
            snapshotDB.Close() // Cleanup on error
            return nil, err
        }
    }
    
    return migration.NewManager(opts.SCRawDB, snapshotMgr, ss, newSC, opts.Logger), nil
}


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)
}
19 changes: 19 additions & 0 deletions store/v2/root/factory_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package root

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"

corestore "cosmossdk.io/core/store"
coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/internal/encoding"
)

func TestFactory(t *testing.T) {
Expand All @@ -26,4 +29,20 @@ func TestFactory(t *testing.T) {
f, err = CreateRootStore(&fop)
require.Error(t, err)
require.Nil(t, f)

require.NoError(t, setLatestVersion(fop.SCRawDB, 1))
fop.Options.SCType = SCTypeIavl
f, err = CreateRootStore(&fop)
require.NoError(t, err)
require.NotNil(t, f)
require.True(t, f.(*Store).isMigrating)
}

func setLatestVersion(db corestore.KVStoreWithBatch, version uint64) error {
var buf bytes.Buffer
buf.Grow(encoding.EncodeUvarintSize(version))
if err := encoding.EncodeUvarint(&buf, version); err != nil {
return err
}
return db.Set([]byte("c/latest"), buf.Bytes())
}
Loading