Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jul 8, 2023
1 parent ae8e001 commit 460dc75
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
18 changes: 16 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 4 additions & 20 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -174,6 +174,8 @@ func NewBaseApp(
msgServiceRouter: NewMsgServiceRouter(),
txDecoder: txDecoder,
fauxMerkleMode: false,
checkStateMtx: sync.RWMutex{},
queryStateMtx: sync.RWMutex{},
}

for _, option := range options {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
}
Expand Down
7 changes: 0 additions & 7 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 460dc75

Please sign in to comment.