Track 1 · Mechanics · lesson 6

Gas: the metering model

8 min


A blockchain runs code that everyone must execute. So someone has to answer a hard question: what stops a program that never terminates?

Not a timeout — nodes would disagree about when it fired, and consensus would break. The answer has to be something every node computes identically.

Every operation the EVM can perform has a fixed price in gas. Adding two numbers costs 3. Reading a storage slot costs 2,100 the first time. Writing a fresh one costs 22,100.

You attach a gas limit — the most you will allow to be spent. Execution stops when the meter runs out. Since the prices are fixed and public, every node counts the same total and reaches the same conclusion.

The halting problem is not solved. It is priced.

Predict

Adding two numbers costs 3 gas. Writing one number to storage costs 22,100. Why the factor of seven thousand?

Choose one answer

Limit, used, and refunded

Three numbers people mix up constantly:

Setting a generous limit costs nothing extra, because you are only charged for what is used. Setting one too low is expensive: the transaction runs until the meter hits zero, then reverts — and you still pay for everything it burned.

Check

You set a gas limit of 100,000. Execution needs 150,000 and runs out. What do you pay?

Choose one answer
Why some operations cost negative gasOptional

Deleting a storage slot refunds gas, because you are releasing an obligation the whole network was carrying.

For a while this was gameable: contracts existed purely to hoard cheap storage when gas was low and release it when gas was high, effectively a gas futures market built out of garbage. Later upgrades capped refunds to kill the trade.

Worth noticing as a pattern — on-chain, any incentive that can be farmed eventually is.

Worth remembering

  • Gas meters execution so every node independently reaches the same result — pricing the halting problem instead of solving it.
  • Each operation has a fixed, public price: 3 gas to add, 22,100 to write a fresh storage slot.
  • Storage is expensive because every node carries it forever.
  • Unused gas is refunded; running out costs everything burned and reverts anyway.