SMART CONTRACTS, DEFI & WEB3 SECURITY - Chapter 2, Exercise 1 Solution ========================================================== A Transaction That Needs More Gas Than Its Own Limit PROBLEM ------- A transaction has a gas limit of 100,000, but genuinely needs 130,000 gas to fully complete. Using this chapter's own explanation of out-of-gas behavior, describe exactly what happens to (a) the transaction's intended state changes, and (b) the gas the sender paid for. SOLUTION -------- (a) THE TRANSACTION'S INTENDED STATE CHANGES Execution proceeds normally, consuming gas step by step, until it hits the 100,000 gas limit partway through - short of the full 130,000 it would have needed to finish. At that exact point, the EVM halts execution and reverts every state change the transaction had attempted to make up to that point. This chapter is specific that reverting means ALL attempted changes are undone, not just the ones that hadn't happened yet - so the transaction, from the outside, ends up having no lasting effect on the contract's own state at all, exactly as if it had never been attempted (aside from the fee, covered below). (b) THE GAS THE SENDER PAID FOR The gas actually consumed doing that partial work - up to the full 100,000 gas limit - is NOT refunded, even though none of the intended state changes actually took effect. This chapter explains this is a deliberate choice: real, genuine computational effort was spent by every node re-executing that partial work, and refunding it would let someone repeatedly trigger doomed, out-of-gas transactions to consume network resources for free. So the sender ends up in a real, specific bad outcome: they pay for 100,000 gas's worth of real computation, and get nothing for it - no successful state change, and no refund of the gas spent getting to the point of failure. ANSWER: The transaction's intended state changes are fully reverted, as if the transaction never happened - but the sender still pays for however much gas was actually consumed before hitting the limit (up to the full 100,000 gas limit in this case), since that computation was genuinely performed by the network even though it ultimately failed. ---- WHY THIS WORKS AS AN ANSWER This correctly separates the two distinct outcomes the question asks about (state changes vs. gas payment) and applies the chapter's own specific reasoning for why the gas isn't refunded, rather than assuming a "failed transaction, no charge" outcome that doesn't match how the EVM actually behaves.