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

Problem: memiavl root hash not compatible with sdk 47 #1083

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 22 additions & 9 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
amendCommitInfo(rs.lastCommitInfo, rs.storesParams)
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
rs.lastCommitInfo = &types.CommitInfo{}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions store/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)
}
Expand Down