Skip to content

Commit

Permalink
Problem: memiavl db in CacheMultiStoreWithVersion not closed (#1151)
Browse files Browse the repository at this point in the history
* Problem: memiavl db in CacheMultiStoreWithVersion not closed

Solution:
- add a wrapper that supports io.Closer

Impact:
- this PR only adds a Closer method to it, but not call it,
  for example, sdk grpc query service will not call it, unless patched.
- versiondb nodes handle grpc queries without memiavl, so no problem there.
- for other use cases that use memiavl's `CacheMultiStoreWithVersion` method
  should call the `Close()` method explicitly.

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

---------

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang authored Sep 4, 2023
1 parent 04b26e4 commit c575f47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
- [#1099](https://github.com/crypto-org-chain/cronos/pull/1099) clean up memiavl tmp directories left behind.
- [#940](https://github.com/crypto-org-chain/cronos/pull/940) Update rocksdb dependency to 8.1.1.
- [#1149](https://github.com/crypto-org-chain/cronos/pull/1149) memiavl support `WorkingHash` api required by `FinalizeBlock`.
- [#1151](https://github.com/crypto-org-chain/cronos/pull/1151) memiavl `CacheMultiStoreWithVersion` supports `io.Closer`.

*April 13, 2023*

Expand Down
37 changes: 37 additions & 0 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cachemulti

import (
"io"

"github.com/cosmos/cosmos-sdk/store/cachemulti"
"github.com/cosmos/cosmos-sdk/store/types"
dbm "github.com/tendermint/tm-db"
)

var NoopCloser io.Closer = CloserFunc(func() error { return nil })

type CloserFunc func() error

func (fn CloserFunc) Close() error {
return fn()
}

// Store wraps sdk's cachemulti.Store to add io.Closer interface
type Store struct {
cachemulti.Store
io.Closer
}

func NewStore(
db dbm.DB, stores map[types.StoreKey]types.CacheWrapper, keys map[string]types.StoreKey,
traceWriter io.Writer, traceContext types.TraceContext,
closer io.Closer,
) Store {
if closer == nil {
closer = NoopCloser
}
return Store{
Store: cachemulti.NewStore(db, stores, keys, traceWriter, traceContext),
Closer: closer,
}
}
6 changes: 3 additions & 3 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"github.com/cosmos/cosmos-sdk/store/cachemulti"
"github.com/cosmos/cosmos-sdk/store/listenkv"
"github.com/cosmos/cosmos-sdk/store/mem"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
Expand All @@ -24,6 +23,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/crypto-org-chain/cronos/memiavl"
"github.com/crypto-org-chain/cronos/store/cachemulti"
"github.com/crypto-org-chain/cronos/store/memiavlstore"
)

Expand Down Expand Up @@ -191,7 +191,7 @@ func (rs *Store) CacheMultiStore() types.CacheMultiStore {
}
stores[k] = store
}
return cachemulti.NewStore(nil, stores, rs.keysByName, nil, nil)
return cachemulti.NewStore(nil, stores, rs.keysByName, nil, nil, nil)
}

// Implements interface MultiStore
Expand Down Expand Up @@ -222,7 +222,7 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
stores[rs.keysByName[tree.Name]] = memiavlstore.New(tree.Tree, rs.logger)
}

return cachemulti.NewStore(nil, stores, rs.keysByName, nil, nil), nil
return cachemulti.NewStore(nil, stores, rs.keysByName, nil, nil, db), nil
}

// Implements interface MultiStore
Expand Down

0 comments on commit c575f47

Please sign in to comment.