-
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
Changes from 1 commit
4cc992f
df0dc4f
fca170d
79583ec
bcfeff1
d511fa3
00b99a7
0eaec31
9f496e9
39b303b
8b16dad
b305280
ebc1d3b
64e1cb4
39d51cf
242939e
a9fa647
ada2859
38527f7
1e884cf
1b79269
00b6360
dc57212
4695365
3a5bdf0
c103f16
4a9feb5
677639e
074da95
a05f72e
6eb51d1
c16dc89
119b77a
a97ed5a
179743c
bb61b5d
f381e09
7eec931
1af9b3b
ec331a0
e21a6f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package root | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
prefixdb "github.com/cosmos/cosmos-sdk/db/prefix" | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// MigrationFromIAVLStoreToSMTStore will migrate the complete state from iavl to smt | ||
func MigrationFromIAVLStoreToSMTStore(rs *v1Store.Store, rootStore *Store) error { | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Collect stores to snapshot (only IAVL stores are supported) | ||
type namedStore struct { | ||
*iavl.Store | ||
name string | ||
} | ||
var stores []namedStore | ||
for key := range rs.GetStores() { | ||
switch store := rs.GetCommitKVStore(key).(type) { | ||
case *iavl.Store: | ||
stores = append(stores, namedStore{name: key.Name(), Store: store}) | ||
case *transient.Store, *mem.Store: | ||
continue | ||
default: | ||
continue | ||
} | ||
} | ||
|
||
sort.Slice(stores, func(i, j int) bool { | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return strings.Compare(stores[i].name, stores[j].name) == -1 | ||
}) | ||
|
||
// make new smt store schema | ||
if len(rootStore.schema) != 0 { | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// schema is already exists | ||
gsk967 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "smt store already have schema") | ||
} | ||
|
||
// set the schema to smt store | ||
schemaWriter := prefixdb.NewPrefixWriter(rootStore.stateTxn, schemaPrefix) | ||
for _, store := range stores { | ||
rootStore.schema[store.name] = types.StoreTypePersistent | ||
err := schemaWriter.Set([]byte(store.name), []byte{byte(types.StoreTypePersistent)}) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "error at set the store schema key values") | ||
} | ||
} | ||
|
||
// iterate through all iavl stores | ||
for _, store := range stores { | ||
subStore, err := rootStore.getSubstore(store.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. we are registering the |
||
if err != nil { | ||
return 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) | ||
rootStore.Commit() | ||
return nil | ||
} |
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.
It seams we only need store keys. Let's change this function to:
This way we won't expose the internal map.
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.
@robert-zaremba is there any concern with exposing the
StoreKey
here? or as long as it's not exposed to modules fromContext
, is it fine?