Skip to content

Commit

Permalink
Add ensure blockGasMeter check to initChainer + better getter checks (#…
Browse files Browse the repository at this point in the history
…399)

Co-authored-by: Julian Compagni Portis <julian@terrascope.io>
  • Loading branch information
jcompagni10 and Julian Compagni Portis committed Sep 22, 2023
1 parent ff804dd commit 9a208a5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
14 changes: 14 additions & 0 deletions app/app.go
Expand Up @@ -1063,13 +1063,27 @@ func (app *App) SetCheckTx(handler pobabci.CheckTx) {
app.checkTxHandler = handler
}

func (app *App) EnsureBlockGasMeter(ctx sdk.Context) {
// TrancheKey generation and LimitOrderExpirationPurge both rely on a BlockGas meter.
// check that it works at startup
cp := app.GetConsensusParams(ctx)
if cp == nil || cp.Block == nil || cp.Block.MaxGas <= 0 {
panic("BlockGas meter must be initialized. Genesis must provide value for Block.MaxGas")
}
}




// InitChainer application update at chain initialization
func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
app.EnsureBlockGasMeter(ctx)

return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}

Expand Down
15 changes: 4 additions & 11 deletions x/dex/keeper/limit_order_tranche.go
Expand Up @@ -179,17 +179,10 @@ func (k Keeper) GetAllLimitOrderTrancheAtIndex(
return trancheList
}

func NewTrancheKey(sdkCtx sdk.Context) string {
blockHeight := sdkCtx.BlockHeight()
txGas := sdkCtx.GasMeter().GasConsumed()

var blockGas uint64 = 0

// TEMP: REMOVE ME
if sdkCtx.BlockGasMeter() != nil {
blockGas = sdkCtx.BlockGasMeter().GasConsumed()
}

func NewTrancheKey(ctx sdk.Context) string {
blockHeight := ctx.BlockHeight()
txGas := ctx.GasMeter().GasConsumed()
blockGas := utils.MustGetBlockGasUsed(ctx)
totalGas := blockGas + txGas

blockStr := utils.Uint64ToSortableString(uint64(blockHeight))
Expand Down
28 changes: 28 additions & 0 deletions x/dex/utils/abci.go
@@ -0,0 +1,28 @@
package utils

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func GetBlockGasUsed(ctx sdk.Context) (gasUsed uint64, err error) {
switch {
case ctx.BlockGasMeter() != nil:
return ctx.BlockGasMeter().GasConsumed(), nil
case ctx.IsCheckTx():
// If we are checking a TX or this is a simulation we can return whatever
return 0, nil
default:
// Otherwise, BlockGasMeter should probably be initialized
return 0, sdkerrors.Wrap(sdkerrors.ErrAppConfig, "Block Gas Meter is not initialized")
}
}

func MustGetBlockGasUsed(ctx sdk.Context) uint64 {
gasUsed, err := GetBlockGasUsed(ctx)
if err != nil {
panic(err)
}

return gasUsed
}

0 comments on commit 9a208a5

Please sign in to comment.