Skip to content

Commit

Permalink
add deep copy method for root multi store
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jul 8, 2023
1 parent 1d36f49 commit cd05f7f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
2 changes: 0 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ func (app *BaseApp) Commit() abci.ResponseCommit {
// The write to the DeliverTx state writes all state transitions to the root
// MultiStore (app.cms) so when Commit() is called is persists those values.
app.deliverState.ms.Write()
app.queryStateMtx.Lock()
commitID := app.cms.Commit()
app.queryStateMtx.Unlock()

res := abci.ResponseCommit{
Data: commitID.Hash,
Expand Down
11 changes: 8 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
)

type (
Expand Down Expand Up @@ -468,11 +469,15 @@ func (app *BaseApp) setState(mode runTxMode, header tmproto.Header) {
}

func (app *BaseApp) setQueryState(header tmproto.Header) {
baseState := &queryState{
ms: app.cms,
ctx: sdk.NewContext(nil, header, false, app.upgradeChecker, app.logger),
var ms sdk.CommitMultiStore
if _, ok := app.cms.(*rootmulti.Store); ok {
ms = app.cms.(*rootmulti.Store).DeepCopy()
}

baseState := &queryState{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.upgradeChecker, app.logger),
}
app.queryState = baseState
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.23.0
github.com/cometbft/cometbft => github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708045955-82f5c5157b3b

github.com/cosmos/iavl => github.com/Pythonberg1997/greenfield-iavl v0.0.0-20230703060649-e52459a6ddca
// Downgraded to avoid bugs in following commits which caused simulations to fail.
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708045955-82f5c5157b3b h1:Iv+tq7F4026nm2aOrSyI6wcLsUmPiCYss+b0InHBuBU=
github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708045955-82f5c5157b3b/go.mod h1:9q11eHNRY9FDwFH+4pompzPNGv//Z3VcfvkELaHJPMs=
github.com/Pythonberg1997/greenfield-iavl v0.0.0-20230703060649-e52459a6ddca h1:hv1XoBD8BBFA+wjO1VCjDA1bHjj5blK0WqXGueZvYcM=
github.com/Pythonberg1997/greenfield-iavl v0.0.0-20230703060649-e52459a6ddca/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/sarama v1.26.1/go.mod h1:NbSGBSSndYaIhRcBtY9V0U7AyH+x71bG668AuWys/yU=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
Expand Down Expand Up @@ -357,8 +359,6 @@ github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ
github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU=
github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoKuI=
github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek=
github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38=
github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
github.com/cosmos/keyring v1.2.0/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA=
Expand Down
7 changes: 7 additions & 0 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,10 @@ func getProofFromTree(tree *iavl.MutableTree, key []byte, exists bool) *tmcrypto
op := types.NewIavlCommitmentOp(key, commitmentProof)
return &tmcrypto.ProofOps{Ops: []tmcrypto.ProofOp{op.ProofOp()}}
}

func (st *Store) CloneMutableTree() *iavl.MutableTree {
if mutableTree, ok := st.tree.(*iavl.MutableTree); ok {
return iavl.CloneMutableTree(mutableTree)
}
return nil
}
30 changes: 30 additions & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/transient"
"github.com/cosmos/cosmos-sdk/store/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/store/cache"
)

const (
Expand Down Expand Up @@ -555,6 +556,35 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
return cachemulti.NewStore(rs.db, cachedStores, rs.keysByName, rs.traceWriter, rs.getTracingContext()), nil
}

func (rs *Store) DeepCopy() *Store {
stores := make(map[types.StoreKey]types.CommitKVStore)
for k, v := range rs.stores {
var storeCache *cache.CommitKVStoreCache
if store, ok := v.(*cache.CommitKVStoreCache); ok {
if iavlStore, ok := store.CommitKVStore.(*iavl.Store); ok {
tree := iavlStore.CloneMutableTree()
if tree != nil {
storeCache = cache.NewCommitKVStoreCache(iavl.UnsafeNewStore(tree), 1000)
}
}
}
stores[k] = storeCache.CommitKVStore
}

return &Store{
db: rs.db,
logger: rs.logger,
iavlCacheSize: rs.iavlCacheSize,
iavlDisableFastNode: rs.iavlDisableFastNode,
storesParams: rs.storesParams,
stores: stores,
keysByName: rs.keysByName,
listeners: make(map[types.StoreKey][]types.WriteListener),
removalMap: make(map[types.StoreKey]bool),
pruningManager: pruning.NewManager(rs.db, rs.logger),
}
}

// GetStore returns a mounted Store for a given StoreKey. If the StoreKey does
// not exist, it will panic. If the Store is wrapped in an inter-block cache, it
// will be unwrapped prior to being returned.
Expand Down

0 comments on commit cd05f7f

Please sign in to comment.