diff --git a/CHANGELOG.md b/CHANGELOG.md index b1c77057f0..052169377e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ ### Features - [#1042](https://github.com/crypto-org-chain/cronos/pull/1042) call Close method on app to cleanup resource on graceful shutdown ([ethermint commit](https://github.com/crypto-org-chain/ethermint/commit/0ea7b86532a1144f229961f94b4524d5889e874d)). +- [#1083](https://github.com/crypto-org-chain/cronos/pull/1083) memiavl support both sdk 46 and 47 root hash rules. ### Improvements diff --git a/app/app.go b/app/app.go index 994db8c12f..48e37b8f6a 100644 --- a/app/app.go +++ b/app/app.go @@ -344,7 +344,7 @@ func New( cdc := encodingConfig.Amino interfaceRegistry := encodingConfig.InterfaceRegistry - baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, baseAppOptions) + baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, true, baseAppOptions) bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index e3a03bdd78..d6b3649bad 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -49,12 +49,16 @@ type Store struct { listeners map[types.StoreKey][]types.WriteListener opts memiavl.Options + + // sdk46Compact defines if the root hash is compatible with cosmos-sdk 0.46 and before. + sdk46Compact bool } -func NewStore(dir string, logger log.Logger) *Store { +func NewStore(dir string, logger log.Logger, sdk46Compact bool) *Store { return &Store{ - dir: dir, - logger: logger, + dir: dir, + logger: logger, + sdk46Compact: sdk46Compact, storesParams: make(map[types.StoreKey]storeParams), keysByName: make(map[string]types.StoreKey), @@ -97,7 +101,10 @@ func (rs *Store) Commit() types.CommitID { } } - rs.lastCommitInfo = amendCommitInfo(rs.db.LastCommitInfo(), rs.storesParams) + rs.lastCommitInfo = rs.db.LastCommitInfo() + if rs.sdk46Compact { + rs.lastCommitInfo = amendCommitInfo(rs.lastCommitInfo, rs.storesParams) + } return rs.lastCommitInfo.CommitID() } @@ -316,7 +323,10 @@ func (rs *Store) LoadVersionAndUpgrade(version int64, upgrades *types.StoreUpgra rs.stores = newStores // to keep the root hash compatible with cosmos-sdk 0.46 if db.Version() != 0 { - rs.lastCommitInfo = amendCommitInfo(db.LastCommitInfo(), rs.storesParams) + rs.lastCommitInfo = db.LastCommitInfo() + if rs.sdk46Compact { + rs.lastCommitInfo = amendCommitInfo(rs.lastCommitInfo, rs.storesParams) + } } else { rs.lastCommitInfo = &types.CommitInfo{} } @@ -428,6 +438,9 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { version = rs.db.Version() } + // If the request's height is the latest height we've committed, then utilize + // the store's lastCommitInfo as this commit info may not be flushed to disk. + // Otherwise, we query for the commit info from disk. db := rs.db if version != rs.lastCommitInfo.Version { var err error @@ -458,10 +471,10 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { return sdkerrors.QueryResult(errors.Wrap(sdkerrors.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned"), false) } - // If the request's height is the latest height we've committed, then utilize - // the store's lastCommitInfo as this commit info may not be flushed to disk. - // Otherwise, we query for the commit info from disk. - commitInfo := amendCommitInfo(db.LastCommitInfo(), rs.storesParams) + commitInfo := db.LastCommitInfo() + if rs.sdk46Compact { + commitInfo = amendCommitInfo(commitInfo, rs.storesParams) + } // Restore origin path and append proof op. res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(storeName)) diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 9bbda85b7d..fcfbb15291 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -9,6 +9,6 @@ import ( ) func TestLastCommitID(t *testing.T) { - store := NewStore(t.TempDir(), log.NewNopLogger()) + store := NewStore(t.TempDir(), log.NewNopLogger(), true) require.Equal(t, types.CommitID{}, store.LastCommitID()) } diff --git a/store/setup.go b/store/setup.go index 7a5dfcce00..8e211ddb45 100644 --- a/store/setup.go +++ b/store/setup.go @@ -24,7 +24,7 @@ const ( // SetupMemIAVL insert the memiavl setter in front of baseapp options, so that // the default rootmulti store is replaced by memiavl store, -func SetupMemIAVL(logger log.Logger, homePath string, appOpts servertypes.AppOptions, baseAppOptions []func(*baseapp.BaseApp)) []func(*baseapp.BaseApp) { +func SetupMemIAVL(logger log.Logger, homePath string, appOpts servertypes.AppOptions, sdk46Compact bool, baseAppOptions []func(*baseapp.BaseApp)) []func(*baseapp.BaseApp) { if cast.ToBool(appOpts.Get(FlagMemIAVL)) { opts := memiavl.Options{ AsyncCommitBuffer: cast.ToInt(appOpts.Get(FlagAsyncCommitBuffer)), @@ -35,19 +35,19 @@ func SetupMemIAVL(logger log.Logger, homePath string, appOpts servertypes.AppOpt } // cms must be overridden before the other options, because they may use the cms, // make sure the cms aren't be overridden by the other options later on. - baseAppOptions = append([]func(*baseapp.BaseApp){setMemIAVL(homePath, logger, opts)}, baseAppOptions...) + baseAppOptions = append([]func(*baseapp.BaseApp){setMemIAVL(homePath, logger, opts, sdk46Compact)}, baseAppOptions...) } return baseAppOptions } -func setMemIAVL(homePath string, logger log.Logger, opts memiavl.Options) func(*baseapp.BaseApp) { +func setMemIAVL(homePath string, logger log.Logger, opts memiavl.Options, sdk46Compact bool) func(*baseapp.BaseApp) { return func(bapp *baseapp.BaseApp) { // trigger state-sync snapshot creation by memiavl opts.TriggerStateSyncExport = func(height int64) { go bapp.SnapshotManager().SnapshotIfApplicable(height) } - cms := rootmulti.NewStore(filepath.Join(homePath, "data", "memiavl.db"), logger) + cms := rootmulti.NewStore(filepath.Join(homePath, "data", "memiavl.db"), logger, sdk46Compact) cms.SetMemIAVLOptions(opts) bapp.SetCMS(cms) }