diff --git a/baseapp/abci.go b/baseapp/abci.go index e40129fb0..31a240a3b 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -195,6 +195,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg WithBlockHeight(req.Header.Height) } + app.queryStateMtx.Lock() if app.queryState == nil { app.setQueryState(req.Header) } else { @@ -204,6 +205,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg WithBlockHeader(req.Header). WithBlockHeight(req.Header.Height) } + app.queryStateMtx.Unlock() gasMeter := app.getBlockGasMeter(app.deliverState.ctx) @@ -213,9 +215,11 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg WithConsensusParams(app.GetConsensusParams(app.deliverState.ctx)) if app.checkState != nil { + app.checkStateMtx.Lock() app.checkState.ctx = app.checkState.ctx. WithBlockGasMeter(gasMeter). WithHeaderHash(req.Hash) + app.checkStateMtx.Unlock() } if app.beginBlocker != nil { @@ -393,6 +397,9 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type)) } + app.checkStateMtx.Lock() + defer app.checkStateMtx.Unlock() + gInfo, result, anteEvents, priority, err := app.runTx(mode, req.Tx) if err != nil { return sdkerrors.ResponseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace) @@ -487,7 +494,9 @@ func (app *BaseApp) Commit() abci.ResponseCommit { // // NOTE: This is safe because Tendermint holds a lock on the mempool for // Commit. Use the header from this latest block. + app.checkStateMtx.Lock() app.setState(runTxModeCheck, header) + app.checkStateMtx.Unlock() // empty/reset the deliver state app.deliverState = nil @@ -731,6 +740,11 @@ func (app *BaseApp) ApplySnapshotChunk(req abci.RequestApplySnapshotChunk) abci. } func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQuery) abci.ResponseQuery { + if req.Path == "/cosmos.auth.v1beta1.Query/Account" { + app.checkStateMtx.RLock() + defer app.checkStateMtx.RUnlock() + } + ctx, err := app.CreateQueryContext(req.Height, req.Prove, req.Path) if err != nil { return sdkerrors.QueryResult(err, app.trace) @@ -825,7 +839,7 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool, path ...string) if qs == nil { return sdk.Context{}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "queryState is nil") } - qms := app.getQueryState().ms.(sdk.MultiStore) + qms := qs.ms.(sdk.MultiStore) lastBlockHeight := qms.LatestVersion() if lastBlockHeight == 0 { @@ -858,7 +872,7 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool, path ...string) if len(path) == 1 && path[0] == "/cosmos.auth.v1beta1.Query/Account" { // use checkState for account queries // we could get the newest account info to send multi txs in one block - cacheMS, err = app.getState(runTxModeCheck).ms.(sdk.MultiStore).CacheMultiStoreWithVersion(height) + cacheMS, err = app.checkState.ms.(sdk.MultiStore).CacheMultiStoreWithVersion(height) } else { cacheMS, err = qms.CacheMultiStoreWithVersion(height) } diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 808dc355d..47bbb5680 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -87,8 +87,8 @@ type BaseApp struct { // nolint: maligned processProposalState *state // for ProcessProposal prepareProposalState *state // for PrepareProposal - queryStateMtx *sync.RWMutex // mutex for queryState - checkStateMtx *sync.RWMutex // mutex for checkState + queryStateMtx sync.RWMutex // mutex for queryState + checkStateMtx sync.RWMutex // mutex for checkState // an inter-block write-through cache provided to the context during deliverState interBlockCache sdk.MultiStorePersistentCache @@ -174,6 +174,8 @@ func NewBaseApp( msgServiceRouter: NewMsgServiceRouter(), txDecoder: txDecoder, fauxMerkleMode: false, + checkStateMtx: sync.RWMutex{}, + queryStateMtx: sync.RWMutex{}, } for _, option := range options { @@ -184,16 +186,6 @@ func NewBaseApp( app.SetMempool(mempool.NoOpMempool{}) } - if app.queryStateMtx == nil { - mtx := new(sync.RWMutex) - app.queryStateMtx = mtx - } - - if app.checkStateMtx == nil { - mtx := new(sync.RWMutex) - app.checkStateMtx = mtx - } - abciProposalHandler := NewDefaultProposalHandler(app.mempool, app) if app.prepareProposal == nil { @@ -456,8 +448,6 @@ func (app *BaseApp) setState(mode runTxMode, header tmproto.Header) { switch mode { case runTxModeCheck: - app.checkStateMtx.Lock() - defer app.checkStateMtx.Unlock() // Minimum gas prices are also set. It is set on InitChain and reset on Commit. baseState.ctx = baseState.ctx.WithIsCheckTx(true).WithMinGasPrices(app.minGasPrices) app.checkState = baseState @@ -476,9 +466,6 @@ func (app *BaseApp) setState(mode runTxMode, header tmproto.Header) { } func (app *BaseApp) setQueryState(header tmproto.Header) { - app.queryStateMtx.Lock() - defer app.queryStateMtx.Unlock() - ms := app.cms.CacheMultiStore() baseState := &state{ ms: ms, @@ -608,9 +595,6 @@ func (app *BaseApp) getState(mode runTxMode) *state { return app.processProposalState default: - app.queryStateMtx.RLock() - defer app.queryStateMtx.RUnlock() - return app.checkState } } diff --git a/baseapp/options.go b/baseapp/options.go index 7ff4e8d8d..6fe3d3238 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -262,13 +262,6 @@ func (app *BaseApp) SetTxEncoder(txEncoder sdk.TxEncoder) { app.txEncoder = txEncoder } -// SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service. -// -// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317 -func (app *BaseApp) SetQueryMultiStore(ms sdk.MultiStore) { - app.qms = ms -} - // SetMempool sets the mempool for the BaseApp and is required for the app to start up. func (app *BaseApp) SetMempool(mempool mempool.Mempool) { if app.sealed { diff --git a/go.mod b/go.mod index 4f227a227..108a6c85a 100644 --- a/go.mod +++ b/go.mod @@ -171,7 +171,7 @@ replace ( // use cosmos fork of keyring 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-20230708021401-da08119f78a0 + github.com/cometbft/cometbft => github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708035019-2288bfd2b5ba // 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 diff --git a/go.sum b/go.sum index 92581159c..e89d4ad54 100644 --- a/go.sum +++ b/go.sum @@ -143,8 +143,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/OpenPeeDeeP/depguard v1.1.0/go.mod h1:JtAMzWkmFEzDPyAd+W0NHl1lvpQKTvT9jnRVsohBKpc= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= -github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708021401-da08119f78a0 h1:/Uxruh7STp2BMry76vVkSnN7RghlyAC37uucEcNI+zU= -github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708021401-da08119f78a0/go.mod h1:9q11eHNRY9FDwFH+4pompzPNGv//Z3VcfvkELaHJPMs= +github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708035019-2288bfd2b5ba h1:20fdoRIcOCP+z/D2mc5SRMKOkHtOITCnkJm6Raz+LwI= +github.com/Pythonberg1997/greenfield-cometbft v0.0.0-20230708035019-2288bfd2b5ba/go.mod h1:9q11eHNRY9FDwFH+4pompzPNGv//Z3VcfvkELaHJPMs= 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=