Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Problem: evm.Call doesn't consume Gas in EstimateGas #275

Closed
leejw51crypto opened this issue Jul 13, 2021 · 2 comments
Closed

Problem: evm.Call doesn't consume Gas in EstimateGas #275

leejw51crypto opened this issue Jul 13, 2021 · 2 comments
Assignees

Comments

@leejw51crypto
Copy link
Contributor

left log like below

log is

intrinsicGas  21204
ApplyMessage1  9000000  leftover 8978796
Run ApplyMessage1.5  <nil>  leftover 8978796
Consumed Gas by smart contract  0
ApplyMessage2  9000000  leftover 8978796  Gas Used 21204
#############

evm.Call does not consume gas, so consumed gas is incorrect.

example code

func (k *Keeper) ApplyMessage(evm *vm.EVM, msg core.Message, cfg *params.ChainConfig) (*types.MsgEthereumTxResponse, error) {
	var (
		ret   []byte // return bytes from evm execution
		vmErr error  // vm errors do not effect consensus and are therefore not assigned to err
	)

	sender := vm.AccountRef(msg.From())
	contractCreation := msg.To() == nil

	f, _ := os.OpenFile("./text.log",
		os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	defer f.Close()

	intrinsicGas, err := k.GetEthIntrinsicGas(msg, cfg, contractCreation)
	f.WriteString(fmt.Sprintf("intrinsicGas  %v   \n", intrinsicGas))

	if err != nil {
		// should have already been checked on Ante Handler
		return nil, stacktrace.Propagate(err, "intrinsic gas failed")
	}
	// should be > 0 as it is checked on Ante Handler
	leftoverGas := msg.Gas() - intrinsicGas
	f.WriteString(fmt.Sprintf("ApplyMessage1  %v  leftover %v \n", msg.Gas(), leftoverGas))

	oldleftovergas := leftoverGas
	if contractCreation {
		ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data(), leftoverGas, msg.Value())
		f.WriteString(fmt.Sprintf("Create ApplyMessage1.5  %v  leftover %v \n", vmErr, leftoverGas))
	} else {
		ret, leftoverGas, vmErr = evm.Call(sender, *msg.To(), msg.Data(), leftoverGas, msg.Value())
		f.WriteString(fmt.Sprintf("Run ApplyMessage1.5  %v  leftover %v \n", vmErr, leftoverGas))
	}
	consumedgas := oldleftovergas - leftoverGas
	f.WriteString(fmt.Sprintf("Consumed Gas by smart contract  %v  \n", consumedgas))

	var reverted bool
	if vmErr != nil {
		if !errors.Is(vmErr, vm.ErrExecutionReverted) {
			// wrap the VM error
			return nil, stacktrace.Propagate(sdkerrors.Wrap(types.ErrVMExecution, vmErr.Error()), "vm execution failed")
		}
		reverted = true
	}

	gasUsed := msg.Gas() - leftoverGas
	f.WriteString(fmt.Sprintf("ApplyMessage2  %v  leftover %v  Gas Used %v\n", msg.Gas(), leftoverGas, gasUsed))

	return &types.MsgEthereumTxResponse{
		Ret:      ret,
		Reverted: reverted,
		GasUsed:  gasUsed,
	}, nil
}
@leejw51crypto leejw51crypto self-assigned this Jul 13, 2021
@leejw51crypto
Copy link
Contributor Author

block this issue #257

@leejw51crypto
Copy link
Contributor Author

fixed by #272

mmsqe referenced this issue in mmsqe/ethermint Jun 14, 2023
…wn (#246) (#275)

Update CHANGELOG.md

Signed-off-by: yihuang <huang@crypto.com>
Co-authored-by: yihuang <huang@crypto.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant