ante, evm: fix panic when checking BlockGasMeter
(#120)
* ante, evm: fix panic when checking BlockGasMeter * update block gas limit function
This commit is contained in:
parent
9d18414c93
commit
bb91d8d93d
@ -7,6 +7,7 @@ import (
|
|||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
||||||
|
|
||||||
|
ethermint "github.com/cosmos/ethermint/types"
|
||||||
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
@ -296,10 +297,16 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
|
|||||||
ctx.GasMeter().ConsumeGas(intrinsicGas, "intrinsic gas")
|
ctx.GasMeter().ConsumeGas(intrinsicGas, "intrinsic gas")
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate a copy of the gas pool (i.e block gas meter) to see if we've run out of gas for this block
|
// TODO: deprecate after https://github.com/cosmos/cosmos-sdk/issues/9514 is fixed on SDK
|
||||||
// if current gas consumed is greater than the limit, this funcion panics and the error is recovered on the Baseapp
|
blockGasLimit := ethermint.BlockGasLimit(ctx)
|
||||||
gasPool := sdk.NewGasMeter(ctx.BlockGasMeter().Limit())
|
|
||||||
gasPool.ConsumeGas(ctx.GasMeter().GasConsumedToLimit(), "gas pool check")
|
// NOTE: safety check
|
||||||
|
if blockGasLimit > 0 {
|
||||||
|
// generate a copy of the gas pool (i.e block gas meter) to see if we've run out of gas for this block
|
||||||
|
// if current gas consumed is greater than the limit, this funcion panics and the error is recovered on the Baseapp
|
||||||
|
gasPool := sdk.NewGasMeter(blockGasLimit)
|
||||||
|
gasPool.ConsumeGas(ctx.GasMeter().GasConsumedToLimit(), "gas pool check")
|
||||||
|
}
|
||||||
|
|
||||||
// we know that we have enough gas on the pool to cover the intrinsic gas
|
// we know that we have enough gas on the pool to cover the intrinsic gas
|
||||||
// set up the updated context to the evm Keeper
|
// set up the updated context to the evm Keeper
|
||||||
|
@ -96,7 +96,7 @@ func (e *PublicEthAPI) ClientCtx() client.Context {
|
|||||||
// ProtocolVersion returns the supported Ethereum protocol version.
|
// ProtocolVersion returns the supported Ethereum protocol version.
|
||||||
func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
|
func (e *PublicEthAPI) ProtocolVersion() hexutil.Uint {
|
||||||
e.logger.Debugln("eth_protocolVersion")
|
e.logger.Debugln("eth_protocolVersion")
|
||||||
return hexutil.Uint(evmtypes.ProtocolVersion)
|
return hexutil.Uint(ethermint.ProtocolVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChainId returns the chain's identifier in hex format
|
// ChainId returns the chain's identifier in hex format
|
||||||
|
@ -20,7 +20,9 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
|
rpctypes "github.com/cosmos/ethermint/ethereum/rpc/types"
|
||||||
|
ethermint "github.com/cosmos/ethermint/types"
|
||||||
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
||||||
|
|
||||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||||
@ -230,7 +232,7 @@ func TestEth_GetTransactionLogs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEth_protocolVersion(t *testing.T) {
|
func TestEth_protocolVersion(t *testing.T) {
|
||||||
expectedRes := hexutil.Uint(evmtypes.ProtocolVersion)
|
expectedRes := hexutil.Uint(ethermint.ProtocolVersion)
|
||||||
|
|
||||||
rpcRes := call(t, "eth_protocolVersion", []string{})
|
rpcRes := call(t, "eth_protocolVersion", []string{})
|
||||||
|
|
||||||
|
25
types/block.go
Normal file
25
types/block.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
// BlockGasLimit returns the max gas (limit) defined in the block gas meter. If the meter is not
|
||||||
|
// set, it returns the max gas from the application consensus params.
|
||||||
|
// NOTE: see https://github.com/cosmos/cosmos-sdk/issues/9514 for full reference
|
||||||
|
func BlockGasLimit(ctx sdk.Context) uint64 {
|
||||||
|
blockGasMeter := ctx.BlockGasMeter()
|
||||||
|
if blockGasMeter != nil {
|
||||||
|
return blockGasMeter.Limit()
|
||||||
|
}
|
||||||
|
|
||||||
|
cp := ctx.ConsensusParams()
|
||||||
|
if cp == nil || cp.Block == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
maxGas := cp.Block.MaxGas
|
||||||
|
if maxGas > 0 {
|
||||||
|
return uint64(maxGas)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
@ -14,6 +14,7 @@ import (
|
|||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
|
|
||||||
|
ethermint "github.com/cosmos/ethermint/types"
|
||||||
"github.com/cosmos/ethermint/x/evm/types"
|
"github.com/cosmos/ethermint/x/evm/types"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
@ -30,7 +31,7 @@ func (k *Keeper) NewEVM(msg core.Message, config *params.ChainConfig) *vm.EVM {
|
|||||||
Transfer: core.Transfer,
|
Transfer: core.Transfer,
|
||||||
GetHash: k.GetHashFn(),
|
GetHash: k.GetHashFn(),
|
||||||
Coinbase: common.Address{}, // there's no beneficiary since we're not mining
|
Coinbase: common.Address{}, // there's no beneficiary since we're not mining
|
||||||
GasLimit: k.ctx.BlockGasMeter().Limit(),
|
GasLimit: ethermint.BlockGasLimit(k.ctx),
|
||||||
BlockNumber: big.NewInt(k.ctx.BlockHeight()),
|
BlockNumber: big.NewInt(k.ctx.BlockHeight()),
|
||||||
Time: big.NewInt(k.ctx.BlockHeader().Time.Unix()),
|
Time: big.NewInt(k.ctx.BlockHeader().Time.Unix()),
|
||||||
Difficulty: big.NewInt(0), // unused. Only required in PoW context
|
Difficulty: big.NewInt(0), // unused. Only required in PoW context
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package types
|
|
||||||
|
|
||||||
// Constants to match up protocol versions and messages
|
|
||||||
const (
|
|
||||||
eth65 = 65
|
|
||||||
|
|
||||||
// ProtocolVersion is the latest supported version of the eth protocol.
|
|
||||||
ProtocolVersion = eth65
|
|
||||||
)
|
|
Loading…
Reference in New Issue
Block a user