@@ -693,11 +693,21 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
693693 return ctx
694694}
695695
696+ type poolingStore interface {
697+ storetypes.MultiStore
698+ CacheMultiStorePooled () storetypes.PooledCacheMultiStore
699+ }
700+
696701// cacheTxContext returns a new context based off of the provided context with
697702// a branched multi-store.
698703func (app * BaseApp ) cacheTxContext (ctx sdk.Context , txBytes []byte ) (sdk.Context , storetypes.CacheMultiStore ) {
699704 ms := ctx .MultiStore ()
700- msCache := ms .CacheMultiStore ()
705+ var msCache storetypes.CacheMultiStore
706+ if msPooled , ok := ms .(poolingStore ); ok {
707+ msCache = msPooled .CacheMultiStorePooled ()
708+ } else {
709+ msCache = ms .CacheMultiStore ()
710+ }
701711 if msCache .TracingEnabled () {
702712 msCache = msCache .SetTracingContext (
703713 storetypes .TraceContext (
@@ -913,6 +923,9 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.G
913923 // writes do not happen if aborted/failed. This may have some
914924 // performance benefits, but it'll be more difficult to get right.
915925 anteCtx , msCache = app .cacheTxContext (ctx , txBytes )
926+ if pooledMSCache , ok := msCache .(storetypes.PooledCacheMultiStore ); ok {
927+ defer pooledMSCache .Release ()
928+ }
916929 anteCtx = anteCtx .WithEventManager (sdk .NewEventManager ())
917930 newCtx , err := app .anteHandler (anteCtx , tx , mode == execModeSimulate )
918931
@@ -963,6 +976,9 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.G
963976 // in case message processing fails. At this point, the MultiStore
964977 // is a branch of a branch.
965978 runMsgCtx , msCache := app .cacheTxContext (ctx , txBytes )
979+ if pooledMSCache , ok := msCache .(storetypes.PooledCacheMultiStore ); ok {
980+ defer pooledMSCache .Release ()
981+ }
966982
967983 // Attempt to execute all messages and only update state if all messages pass
968984 // and we're in DeliverTx. Note, runMsgs will never return a reference to a
0 commit comments