-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: state migration from IAVL to SMT (ADR-040) #10962
Merged
Merged
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
4cc992f
feat: add migration for adr40
df0dc4f
chore: removed sort from migration func
fca170d
Merge branch 'master' into sai/migration_for_adr40
gsk967 79583ec
chore: address the pr comments
bcfeff1
Merge branch 'sai/migration_for_adr40' of github.com:vulcanize/cosmos…
d511fa3
Merge branch 'master' into sai/migration_for_adr40
gsk967 00b99a7
Merge branch 'master' into sai/migration_for_adr40
gsk967 0eaec31
Merge branch 'master' into sai/migration_for_adr40
gsk967 9f496e9
test: add test for migrationv2
39b303b
Merge branch 'master' into sai/migration_for_adr40
gsk967 8b16dad
Merge branch 'master' into sai/migration_for_adr40
gsk967 b305280
chore: change the `MigrationV2`
ebc1d3b
Merge branch 'master' into sai/migration_for_adr40
gsk967 64e1cb4
Merge branch 'master' into sai/migration_for_adr40
gsk967 39d51cf
chore: refactored the migrationV2
242939e
Merge branch 'master' into sai/migration_for_adr40
gsk967 a9fa647
Merge branch 'master' into sai/migration_for_adr40
gsk967 ada2859
Merge branch 'master' into sai/migration_for_adr40
gsk967 38527f7
chore: address the pr review comments
1e884cf
Merge branch 'master' into sai/migration_for_adr40
gsk967 1b79269
Merge branch 'master' into sai/migration_for_adr40
gsk967 00b6360
Merge branch 'master' into sai/migration_for_adr40
gsk967 dc57212
Merge branch 'master' into sai/migration_for_adr40
gsk967 4695365
fix: fix the build issue
3a5bdf0
Merge branch 'master' into sai/migration_for_adr40
gsk967 c103f16
Merge branch 'master' into sai/migration_for_adr40
gsk967 4a9feb5
Merge branch 'master' into sai/migration_for_adr40
gsk967 677639e
chore: address the pr comments
074da95
Merge branch 'master' into sai/migration_for_adr40
gsk967 a05f72e
Merge branch 'master' into sai/migration_for_adr40
gsk967 6eb51d1
Merge branch 'master' into sai/migration_for_adr40
gsk967 c16dc89
Merge branch 'master' into sai/migration_for_adr40
gsk967 119b77a
Merge branch 'master' into sai/migration_for_adr40
gsk967 a97ed5a
address the pr comments
179743c
Merge branch 'master' into sai/migration_for_adr40
gsk967 bb61b5d
address the pr comments
f381e09
Merge branch 'master' into sai/migration_for_adr40
gsk967 7eec931
Merge branch 'master' into sai/migration_for_adr40
gsk967 1af9b3b
chore: update changelog
ec331a0
Merge branch 'master' into sai/migration_for_adr40
gsk967 e21a6f8
Merge branch 'master' into sai/migration_for_adr40
gsk967 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package multi | ||
|
||
import ( | ||
dbm "github.com/cosmos/cosmos-sdk/db" | ||
"github.com/cosmos/cosmos-sdk/store/iavl" | ||
"github.com/cosmos/cosmos-sdk/store/mem" | ||
v1Store "github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
"github.com/cosmos/cosmos-sdk/store/transient" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// MigrateFromV1 will migrate the state from iavl to smt | ||
func MigrateFromV1(rootMultiStore *v1Store.Store, store2db dbm.DBConnection, storeConfig StoreConfig) (*Store, error) { | ||
type namedStore struct { | ||
*iavl.Store | ||
name string | ||
} | ||
var stores []namedStore | ||
for _, storeKey := range rootMultiStore.StoreKeysByName() { | ||
keyName := storeKey.Name() | ||
switch store := rootMultiStore.GetStoreByName(keyName).(type) { | ||
case *iavl.Store: | ||
err := storeConfig.RegisterSubstore(keyName, types.StoreTypePersistent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stores = append(stores, namedStore{name: keyName, Store: store}) | ||
case *transient.Store, *mem.Store: | ||
continue | ||
default: | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "don't know how to migrate store %q of type %T", keyName, store) | ||
} | ||
} | ||
|
||
// creating the new store of smt tree | ||
rootStore, err := NewStore(store2db, storeConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if version is 0 there is no state data to commit | ||
if rootMultiStore.LastCommitID().Version == 0 { | ||
return rootStore, nil | ||
} | ||
|
||
// iterate through the rootmulti stores and save the key/values into smt tree | ||
for _, store := range stores { | ||
subStore, err := rootStore.getSubstore(store.name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// iterate all iavl tree node key/values | ||
iterator := store.Iterator(nil, nil) | ||
for ; iterator.Valid(); iterator.Next() { | ||
// set the iavl key,values into smt node | ||
subStore.Set(iterator.Key(), iterator.Value()) | ||
} | ||
} | ||
|
||
// commit the all key/values from iavl to smt tree (SMT Store) | ||
_, err = rootStore.commit(uint64(rootMultiStore.LastCommitID().Version)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return rootStore, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package multi | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/db/memdb" | ||
"github.com/cosmos/cosmos-sdk/store/iavl" | ||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/stretchr/testify/require" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func TestMigrationV2(t *testing.T) { | ||
r := rand.New(rand.NewSource(49872768940)) | ||
|
||
// setup a rootmulti store | ||
db := dbm.NewMemDB() | ||
v1Store := rootmulti.NewStore(db) | ||
|
||
// mount the kvStores | ||
var keys []*types.KVStoreKey | ||
for i := uint8(0); i < 10; i++ { | ||
key := types.NewKVStoreKey(fmt.Sprintf("store%v", i)) | ||
v1Store.MountStoreWithDB(key, types.StoreTypeIAVL, nil) | ||
keys = append(keys, key) | ||
} | ||
|
||
err := v1Store.LoadLatestVersion() | ||
require.Nil(t, err) | ||
|
||
// setup a random test data | ||
for _, key := range keys { | ||
store := v1Store.GetStore(key).(*iavl.Store) | ||
store.Set([]byte("temp_data"), []byte("one")) | ||
|
||
for i := 0; i < len(keys); i++ { | ||
k := make([]byte, 8) | ||
v := make([]byte, 1024) | ||
binary.BigEndian.PutUint64(k, uint64(i)) | ||
_, err := r.Read(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
store.Set(k, v) | ||
} | ||
} | ||
|
||
testCases := []struct { | ||
testName string | ||
emptyStore bool | ||
}{ | ||
{ | ||
"Migration With Empty Store", | ||
true, | ||
}, | ||
{ | ||
"Migration From Root Multi Store (IAVL) to SMT ", | ||
false, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
if !testCase.emptyStore { | ||
v1Store.Commit() | ||
} | ||
|
||
// setup a new root store of smt | ||
db2 := memdb.NewDB() | ||
storeConfig := DefaultStoreConfig() | ||
// migrating the iavl store (v1) to smt store (v2) | ||
v2Store, err := MigrateFromV1(v1Store, db2, storeConfig) | ||
require.NoError(t, err) | ||
|
||
for _, key := range keys { | ||
v2StoreKVStore := v2Store.GetKVStore(key) | ||
if testCase.emptyStore { | ||
// check the empty store | ||
require.Nil(t, v2StoreKVStore.Get([]byte("temp_data"))) | ||
} else { | ||
require.Equal(t, v2StoreKVStore.Get([]byte("temp_data")), []byte("one")) | ||
} | ||
require.Equal(t, v2Store.LastCommitID().Version, v1Store.LastCommitID().Version) | ||
} | ||
err = v2Store.Close() | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
// TestMigrateV2ForEmptyStore checking empty store migration | ||
func TestMigrateV2ForEmptyStore(t *testing.T) { | ||
// setup a rootmulti store | ||
db := dbm.NewMemDB() | ||
v1Store := rootmulti.NewStore(db) | ||
err := v1Store.LoadLatestVersion() | ||
require.Nil(t, err) | ||
db2 := memdb.NewDB() | ||
storeConfig := DefaultStoreConfig() | ||
// migrating the iavl store (v1) to smt store (v2) | ||
v2Store, err := MigrateFromV1(v1Store, db2, storeConfig) | ||
require.NoError(t, err) | ||
require.Equal(t, v2Store.LastCommitID(), v1Store.LastCommitID()) | ||
} | ||
robert-zaremba marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we make sure that we use the same store key when doing migration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are registering the
storeKeys
into v2Store from v1Storehttps://github.com/cosmos/cosmos-sdk/pull/10962/files#diff-ad3a972d3e8f738c0ee269b134134e2beee4e5f0ee9849b0c71eb7cc9d669ce1R24