Skip to content

Commit

Permalink
return eth out of gas error (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Oct 28, 2024
1 parent a2bc6ca commit a9ca6c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
2 changes: 0 additions & 2 deletions jsonrpc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const (
DefaultMaxOpenConnections = 100
// DefaultLogsCap is the default max number of results can be returned from single `eth_getLogs` query.
DefaultLogsCap int32 = 100
// DefaultFilterCap is the default global cap for total number of filters that can be created.
DefaultFilterCap int32 = 200
// DefaultAddress defines the default HTTP server to listen on.
DefaultAddress = "127.0.0.1:8545"
// DefaultAddressWS defines the default WebSocket server address to bind to.
Expand Down
4 changes: 2 additions & 2 deletions x/evm/precompiles/cosmos/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (e CosmosPrecompile) ExtendedRun(caller vm.ContractRef, input []byte, suppl
switch r.(type) {
case storetypes.ErrorOutOfGas:
// convert cosmos out of gas error to EVM out of gas error
usedGas = suppliedGas + 1
err = nil
usedGas = suppliedGas
err = vm.ErrOutOfGas
default:
panic(r)
}
Expand Down
40 changes: 16 additions & 24 deletions x/evm/precompiles/cosmos/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ func Test_CosmosPrecompile_IsBlockedAddress(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_BLOCKED_ADDRESS_GAS-1, true)
require.NoError(t, err)
require.Equal(t, precompiles.IS_BLOCKED_ADDRESS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_BLOCKED_ADDRESS_GAS-1, true)
require.ErrorIs(t, err, vm.ErrOutOfGas)

retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_BLOCKED_ADDRESS_GAS+uint64(len(inputBz)), true)
require.NoError(t, err)
Expand Down Expand Up @@ -127,9 +126,8 @@ func Test_CosmosPrecompile_IsModuleAddress(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_MODULE_ADDRESS_GAS-1, true)
require.NoError(t, err)
require.Equal(t, precompiles.IS_MODULE_ADDRESS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_MODULE_ADDRESS_GAS-1, true)
require.ErrorIs(t, err, vm.ErrOutOfGas)

retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.IS_MODULE_ADDRESS_GAS+uint64(len(inputBz)), true)
require.NoError(t, err)
Expand Down Expand Up @@ -173,9 +171,8 @@ func Test_CosmosPrecompile_ToCosmosAddress(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_COSMOS_ADDRESS_GAS-1, true)
require.NoError(t, err)
require.Equal(t, precompiles.TO_COSMOS_ADDRESS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_COSMOS_ADDRESS_GAS-1, true)
require.ErrorIs(t, err, vm.ErrOutOfGas)

retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_COSMOS_ADDRESS_GAS+uint64(len(inputBz)), true)
require.NoError(t, err)
Expand Down Expand Up @@ -204,9 +201,8 @@ func Test_CosmosPrecompile_ToEVMAddress(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_EVM_ADDRESS_GAS-1, true)
require.NoError(t, err)
require.Equal(t, precompiles.TO_EVM_ADDRESS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_EVM_ADDRESS_GAS-1, true)
require.ErrorIs(t, err, vm.ErrOutOfGas)

retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_EVM_ADDRESS_GAS+uint64(len(inputBz)), true)
require.NoError(t, err)
Expand Down Expand Up @@ -245,9 +241,8 @@ func Test_ExecuteCosmos(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.EXECUTE_COSMOS_GAS-1, false)
require.NoError(t, err)
require.Equal(t, precompiles.EXECUTE_COSMOS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.EXECUTE_COSMOS_GAS-1, false)
require.ErrorIs(t, err, vm.ErrOutOfGas)

// cannot call execute in readonly mode
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.EXECUTE_COSMOS_GAS+uint64(len(inputBz)), true)
Expand Down Expand Up @@ -333,9 +328,8 @@ func Test_QueryCosmos(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.QUERY_COSMOS_GAS-1, false)
require.NoError(t, err)
require.Equal(t, precompiles.QUERY_COSMOS_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.QUERY_COSMOS_GAS-1, false)
require.ErrorIs(t, err, vm.ErrOutOfGas)

// succeed
retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.QUERY_COSMOS_GAS+uint64(len(inputBz)), true)
Expand Down Expand Up @@ -380,9 +374,8 @@ func Test_ToDenom(t *testing.T) {
require.NoError(t, err)

// out of gas error
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_DENOM_GAS-1, false)
require.NoError(t, err)
require.Equal(t, precompiles.TO_DENOM_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_DENOM_GAS-1, false)
require.ErrorIs(t, err, vm.ErrOutOfGas)

// succeed
retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_DENOM_GAS+uint64(len(inputBz)), true)
Expand Down Expand Up @@ -422,9 +415,8 @@ func Test_ToErc20(t *testing.T) {
require.NoError(t, err)

// out of gas panic
_, gasUsed, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_ERC20_GAS-1, false)
require.NoError(t, err)
require.Equal(t, precompiles.TO_ERC20_GAS, gasUsed)
_, _, err = cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_ERC20_GAS-1, false)
require.ErrorIs(t, err, vm.ErrOutOfGas)

// succeed
retBz, _, err := cosmosPrecompile.ExtendedRun(vm.AccountRef(evmAddr), inputBz, precompiles.TO_ERC20_GAS+uint64(len(inputBz)), true)
Expand Down

0 comments on commit a9ca6c4

Please sign in to comment.