1

As we know, the EVM will revert a transaction when out of gas. How it works? I guess the EVM put a check before executing EVERY instruction like this:

accumulatedGas += gasOfLastInstruction;
if accumulatedGas > givenGas then revert;

but that's performance seems very low. how the EVM actually solve the problem?

a related question without answer: Is there a way to monitor code execution in assembly?

2 Answers 2

0

EVM tracks gas by setting aside an amount of starting gas and the remaining gas is set to this starting gas. With each operation, gas is consumed and the amount of remaining gas is lowered through counters. If there is out of gas exception, all operations are reverted and the amount of Ether originally set side is paid to the miner. However, if the transaction completes successfully, the remaining gas originally set aside is refunded back to the sender and the rest is paid to the miner.

See: https://media.consensys.net/ethereum-gas-fuel-and-fees-3333e17fe1dc

1
  • what you said is common knowledge. I want to know implementation details in assembly language level.
    – user861746
    Sep 30, 2022 at 1:12
0

(answer my own question) I cloned geth source code, and found this in core/vm/interpreter.go:

    op = contract.GetOp(pc)
    operation := in.cfg.JumpTable[op]
    cost = operation.constantGas // For tracing
    // Validate stack
    if sLen := stack.len(); sLen < operation.minStack {
        return nil, &ErrStackUnderflow{stackLen: sLen, required: operation.minStack}
    } else if sLen > operation.maxStack {
        return nil, &ErrStackOverflow{stackLen: sLen, limit: operation.maxStack}
    }
    if !contract.UseGas(cost) {
        return nil, ErrOutOfGas
    }

above code is in a getting-instruction loop.

so the answer is Yes to my guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.