Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ensure blockGasMeter check to initChainer + better getter checks (#…
…399) Co-authored-by: Julian Compagni Portis <julian@terrascope.io>
- Loading branch information
1 parent
ff804dd
commit 9a208a5
Showing
3 changed files
with
46 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |