Skip to content

Commit

Permalink
Merge branch 'main' into marko/15023_add_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp authored Feb 15, 2023
2 parents 54ffe77 + b3f9506 commit 6c4ed16
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf

### Bug Fixes

* (x/capability) [#15030](https://github.com/cosmos/cosmos-sdk/pull/15030) Fixed bug where `x/capability` consumed `GasMeter` gas during `InitMemStore`.
* [#14995](https://github.com/cosmos/cosmos-sdk/pull/14995) Allow unknown fields in `ParseTypedEvent`.
* [#14952](https://github.com/cosmos/cosmos-sdk/pull/14952) Pin version of github.com/syndtr/goleveldb `v1.0.1-0.20210819022825-2ae1ddf74ef7` to avoid issues in the store.
* (store) [#14931](https://github.com/cosmos/cosmos-sdk/pull/14931) Exclude in-memory KVStores, i.e. `StoreTypeMemory`, from CommitInfo commitments.
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ clean:
rm -rf \
$(BUILDDIR)/ \
artifacts/ \
tmp-swagger-gen/
tmp-swagger-gen/ \
.testnets

.PHONY: distclean clean

Expand Down
19 changes: 8 additions & 11 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
WithBlockHeight(req.Header.Height)
}

// add block gas meter
var gasMeter storetypes.GasMeter
if maxGas := app.GetMaximumBlockGas(app.deliverState.ctx); maxGas > 0 {
gasMeter = storetypes.NewGasMeter(maxGas)
} else {
gasMeter = storetypes.NewInfiniteGasMeter()
}

// NOTE: header hash is not set in NewContext, so we manually set it here
gasMeter := app.getBlockGasMeter(app.deliverState.ctx)

app.deliverState.ctx = app.deliverState.ctx.
WithBlockGasMeter(gasMeter).
Expand Down Expand Up @@ -271,13 +263,15 @@ func (app *BaseApp) PrepareProposal(req abci.RequestPrepareProposal) (resp abci.
panic("PrepareProposal called with invalid height")
}

gasMeter := app.getBlockGasMeter(app.prepareProposalState.ctx)
ctx := app.getContextForProposal(app.prepareProposalState.ctx, req.Height)

ctx = ctx.WithVoteInfos(app.voteInfos).
WithBlockHeight(req.Height).
WithBlockTime(req.Time).
WithProposer(req.ProposerAddress).
WithConsensusParams(app.GetConsensusParams(ctx))
WithConsensusParams(app.GetConsensusParams(ctx)).
WithBlockGasMeter(gasMeter)

defer func() {
if err := recover(); err != nil {
Expand All @@ -287,6 +281,7 @@ func (app *BaseApp) PrepareProposal(req abci.RequestPrepareProposal) (resp abci.
"time", req.Time,
"panic", err,
)

resp = abci.ResponsePrepareProposal{Txs: req.Txs}
}
}()
Expand Down Expand Up @@ -315,6 +310,7 @@ func (app *BaseApp) ProcessProposal(req abci.RequestProcessProposal) (resp abci.
panic("app.ProcessProposal is not set")
}

gasMeter := app.getBlockGasMeter(app.processProposalState.ctx)
ctx := app.getContextForProposal(app.processProposalState.ctx, req.Height)

ctx = ctx.
Expand All @@ -323,7 +319,8 @@ func (app *BaseApp) ProcessProposal(req abci.RequestProcessProposal) (resp abci.
WithBlockTime(req.Time).
WithHeaderHash(req.Hash).
WithProposer(req.ProposerAddress).
WithConsensusParams(app.GetConsensusParams(ctx))
WithConsensusParams(app.GetConsensusParams(ctx)).
WithBlockGasMeter(gasMeter)

defer func() {
if err := recover(); err != nil {
Expand Down
26 changes: 19 additions & 7 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,26 @@ func (app *BaseApp) getState(mode runTxMode) *state {
switch mode {
case runTxModeDeliver:
return app.deliverState

case runTxPrepareProposal:
return app.prepareProposalState

case runTxProcessProposal:
return app.processProposalState

default:
return app.checkState
}
}

func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter {
if maxGas := app.GetMaximumBlockGas(ctx); maxGas > 0 {
return storetypes.NewGasMeter(maxGas)
}

return storetypes.NewInfiniteGasMeter()
}

// retrieve the context for the tx w/ txBytes and other memoized values.
func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context {
modeState := app.getState(mode)
Expand Down Expand Up @@ -625,8 +636,10 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
}()

blockGasConsumed := false
// consumeBlockGas makes sure block gas is consumed at most once. It must happen after
// tx processing, and must be executed even if tx processing fails. Hence, we use trick with `defer`

// consumeBlockGas makes sure block gas is consumed at most once. It must
// happen after tx processing, and must be executed even if tx processing
// fails. Hence, it's execution is deferred.
consumeBlockGas := func() {
if !blockGasConsumed {
blockGasConsumed = true
Expand All @@ -639,8 +652,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
// If BlockGasMeter() panics it will be caught by the above recover and will
// return an error - in any case BlockGasMeter will consume gas past the limit.
//
// NOTE: This must exist in a separate defer function for the above recovery
// to recover from this one.
// NOTE: consumeBlockGas must exist in a separate defer function from the
// general deferred recovery function to recover from consumeBlockGas as it'll
// be executed first (deferred statements are executed as stack).
if mode == runTxModeDeliver {
defer consumeBlockGas()
}
Expand Down Expand Up @@ -718,9 +732,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
// and we're in DeliverTx. Note, runMsgs will never return a reference to a
// Result if any single message fails or does not have a registered Handler.
result, err = app.runMsgs(runMsgCtx, msgs, mode)

if err == nil {

// Run optional postHandlers.
//
// Note: If the postHandler fails, we also revert the runMsgs state.
Expand Down Expand Up @@ -766,7 +778,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s

// NOTE: GasWanted is determined by the AnteHandler and GasUsed by the GasMeter.
for i, msg := range msgs {

if mode != runTxModeDeliver && mode != runTxModeSimulate {
break
}
Expand Down Expand Up @@ -936,6 +947,7 @@ func (app *BaseApp) DefaultProcessProposal() sdk.ProcessProposalHandler {
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}
}
}

return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}
}
}
Expand Down
3 changes: 2 additions & 1 deletion store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (g *basicGasMeter) Limit() Gas {

// GasConsumedToLimit returns the gas limit if gas consumed is past the limit,
// otherwise it returns the consumed gas.
// NOTE: This behaviour is only called when recovering from panic when
//
// NOTE: This behavior is only called when recovering from panic when
// BlockGasMeter consumes gas past the limit.
func (g *basicGasMeter) GasConsumedToLimit() Gas {
if g.IsPastLimit() {
Expand Down
9 changes: 6 additions & 3 deletions x/capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ func (suite *CapabilityTestSuite) TestInitializeMemStore() {

// Mock app beginblock and ensure that no gas has been consumed and memstore is initialized
ctx = suite.app.BaseApp.NewContext(false, cmtproto.Header{}).WithBlockGasMeter(storetypes.NewGasMeter(50))
prevGas := ctx.BlockGasMeter().GasConsumed()
prevBlockGas := ctx.BlockGasMeter().GasConsumed()
prevGas := ctx.GasMeter().GasConsumed()
restartedModule := capability.NewAppModule(suite.cdc, *newKeeper, true)
restartedModule.BeginBlock(ctx, abci.RequestBeginBlock{})
gasUsed := ctx.GasMeter().GasConsumed()
suite.Require().True(newKeeper.IsInitialized(ctx), "memstore initialized flag not set")
gasUsed := ctx.BlockGasMeter().GasConsumed()
blockGasUsed := ctx.BlockGasMeter().GasConsumed()

suite.Require().Equal(prevGas, gasUsed, "beginblocker consumed gas during execution")
suite.Require().Equal(prevBlockGas, blockGasUsed, "ensure beginblocker consumed no block gas during execution")
suite.Require().Equal(prevGas, gasUsed, "ensure beginblocker consumed no gas during execution")

// Mock the first transaction getting capability and subsequently failing
// by using a cached context and discarding all cached writes.
Expand Down
5 changes: 3 additions & 2 deletions x/capability/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func (k *Keeper) InitMemStore(ctx sdk.Context) {
panic(fmt.Sprintf("invalid memory store type; got %s, expected: %s", memStoreType, storetypes.StoreTypeMemory))
}

// create context with no block gas meter to ensure we do not consume gas during local initialization logic.
noGasCtx := ctx.WithBlockGasMeter(storetypes.NewInfiniteGasMeter())
// Create a context with no `BlockGasMeter`, and no `GasMeter`. This ensures we do not consume gas during local
// initialization logic.
noGasCtx := ctx.WithBlockGasMeter(storetypes.NewInfiniteGasMeter()).WithGasMeter(storetypes.NewInfiniteGasMeter())

// check if memory store has not been initialized yet by checking if initialized flag is nil.
if !k.IsInitialized(noGasCtx) {
Expand Down

0 comments on commit 6c4ed16

Please sign in to comment.