Skip to content

Commit

Permalink
fix cli & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 23, 2024
1 parent 53fa495 commit ce176da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
23 changes: 19 additions & 4 deletions x/evm/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/hex"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -38,6 +39,20 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
return txCmd
}

func readContractBinFile(binFile string) ([]byte, error) {
contractBz, err := os.ReadFile(binFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read contract file")
}

contractBz, err = hex.DecodeString(string(contractBz))
if err != nil {
return nil, errors.Wrap(err, "failed to read contract file")
}

return contractBz, nil
}

func CreateCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create [bin-file] --input [input-hex-string] --value [value]",
Expand All @@ -64,9 +79,9 @@ $ %s tx evm create ERC20.bin --input 0x1234 --value 100 --from mykey
return err
}

contractBz, err := os.ReadFile(args[0])
contractBz, err := readContractBinFile(args[0])
if err != nil {
return errors.Wrap(err, "failed to read contract file")
return err
}

input, err := cmd.Flags().GetString(FlagInput)
Expand Down Expand Up @@ -133,9 +148,9 @@ $ %s tx evm create2 100 ERC20.bin --input 0x1234 --value 100 --from mykey
if err != nil {
return errors.Wrap(err, "failed to parse salt")
}
contractBz, err := os.ReadFile(args[1])
contractBz, err := readContractBinFile(args[1])
if err != nil {
return errors.Wrap(err, "failed to read contract file")
return err
}

input, err := cmd.Flags().GetString(FlagInput)
Expand Down
12 changes: 12 additions & 0 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"encoding/json"
"fmt"
"math/big"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -41,6 +42,7 @@ func (k Keeper) computeGasLimit(sdkCtx sdk.Context) uint64 {
gasLimit = k.config.ContractSimulationGasLimit
}

fmt.Println("gasLimit", gasLimit)
return gasLimit
}

Expand Down Expand Up @@ -261,6 +263,11 @@ func (k Keeper) EVMCallWithTracer(ctx context.Context, caller common.Address, co
value,
)

// evm sometimes return 0 gasRemaining, but it's not an out of gas error.
if gasRemaining == 0 && err != nil && err != vm.ErrOutOfGas {
return nil, nil, types.ErrEVMCreateFailed.Wrap(err.Error())
}

// London enforced
gasUsed := types.CalGasUsed(gasBalance, gasRemaining, evm.StateDB.GetRefund())
sdkCtx.GasMeter().ConsumeGas(gasUsed, "EVM gas consumption")
Expand Down Expand Up @@ -358,6 +365,11 @@ func (k Keeper) EVMCreateWithTracer(ctx context.Context, caller common.Address,
)
}

// evm sometimes return 0 gasRemaining, but it's not an out of gas error.
if gasRemaining == 0 && err != nil && err != vm.ErrOutOfGas {
return nil, common.Address{}, nil, types.ErrEVMCreateFailed.Wrap(err.Error())
}

// London enforced
gasUsed := types.CalGasUsed(gasBalance, gasRemaining, evm.StateDB.GetRefund())
sdkCtx.GasMeter().ConsumeGas(gasUsed, "EVM gas consumption")
Expand Down
2 changes: 1 addition & 1 deletion x/evm/precompiles/cosmos/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (e CosmosPrecompile) ExtendedRun(caller vm.ContractRef, input []byte, suppl
return nil, ctx.GasMeter().GasConsumedToLimit(), types.ErrPrecompileFailed.Wrap(err.Error())
}
case METHOD_IS_MODULE_ADDRESS:
ctx.GasMeter().ConsumeGas(IS_MODULE_ADDRESS_GAS, "is_blocked_address")
ctx.GasMeter().ConsumeGas(IS_MODULE_ADDRESS_GAS, "is_module_address")

var isModuleAddressArguments IsModuleAddressArguments
if err := method.Inputs.Copy(&isModuleAddressArguments, args); err != nil {
Expand Down

0 comments on commit ce176da

Please sign in to comment.