[Video/Tutorial] What exactly is the Gas Limit and the Gas Price in Ethereum

In this video we are talking about Gas! Specifically, we are deep-diving into the Gas Limit and the Gas Price and what it’s all about that.

Link to YouTube

So, there the two things. The Limit and the Price. But where does it come from and what does it mean? Let’s put out some simple explanations.

Ethereum instructions basically run on gas. When you are executing a smart contract then it costs gas, not ether directly. So, if you are running a smart contract then each and every instruction costs a certain amount of gas. And that is capped by two factors, the amount you send along and a total block gas limit.

Let’s say you are interacting with a very simple smart contract. A function that saves an unsigned integer 256 value.

A contract could look like this:

pragma solidity ^0.4.19;
contract A {
    uint b;
    function saveB(uint _b) public {
        b = _b;
    }
}

If you are copying and pasting this contract into Remix, you can run this contract. The same way you can interacting with this contract from MIST or via MetaMask from a website.

Let’s run saveB(5) and see what happens in the log window:

log_window_remix

There are three fields that are interesting to us:

  1. The "gas" which is 3,000,000
  2. The "transaction cost": 41642 gas
  3. The "execution cost": 20178 gas.

Total Gas Sent

The first part is easy. The "gas" which is shown here, is the gas that we have sent along. This can be modified by the user who sends off the transaction. See here:

remix_gas_sent

Transaction Cost

The second part, the transaction cost, shown here in Remix, is a mix of the actual transaction cost plus the execution cost. This is a bit misleading in my opinion, but let's see.

If you are sending off a transaction with a data field then the transaction has a base cost and for each byte send and additional cost attached (in gas). Looking at the Appendix of the Ethereum Yellow-Paper:

ethereum_yellow_paper_gas_cost

Let's have a look how the transaction cost of 41642 comes together.

This is our data field that Remix automatically sends along in the transaction: input_remix

Basically this is our Data-Field: 0x348218ec0000000000000000000000000000000000000000000000000000000000000005.

The data field is a combination of the first 4 bytes of the hashed function signature and the 32-byte padded argument. Let's quickly calculate this by hand.

The function signature is saveB(uint256), and if we hash that with SHA3-256 (or Keccak-256) then we get: 348218ec5e13d72ab0b6b9db1556cba7b0b97f5626b126d748db81c97e97e43d and if we take the first 4 bytes (reminder: 1 byte = 8 bit = 2 hexadecimal characters. 1 hex char = 4 bit = 0-15 = 0000 to 1111 = 0x0 to 0xF), then we get 348218ec. Let's add the 0x in front and we get 0x348218ec. The argument is a unsigned integer with 256 bit, which is 32 bytes. That means it will pad the integer "5" to 32 bytes, so in other words, it will add 63 zeros in front of the number: 0000000000000000000000000000000000000000000000000000000000000005.

Now let's take that reference from the yellow paper again:

  • 21000 gas is pad for every transaction
  • 68 gas is paid for every non-zero byte of data or code for a transaction
  • 4 gas is paid for every zero byte of data or code for a transaction

Let's do the math.

  1. 348218ec is 4 bytes of data, obviously non-zero.
  2. 0000000000000000000000000000000000000000000000000000000000000005 is a mix of 31 bytes of zero data and 1 byte of non-zero data.

That makes in total 5 bytes of non-zero data and 31 bytes of zero data.

(5 non-zero-bytes * 68 gas) + (31 zero-bytes * 4 gas) = 340 + 124 = 464 gas

For our input data we have to pay 464 gas. In addition to that we pay 21000 gas, which is paid for every transaction. So in total: 21464 gas for the transaction only.

Let's see if that adds up. remix_gas_cost

Remix says the "transaction cost" is 41642 and the "execution cost" is 20178. And in Remix the "transaction cost" is actually the sum of the transaction cost plus the execution cost. So if we subtract the execution cost from the transaction cost, we should get 21464.

41642 (transaction cost) - 20178 (execution cost) = 21464 which is correct.

Execution Cost

The execution cost is a bit harder to calculate, because there is so much happening, but I'll show you exactly what happens when the contract is executed.

Let's dive a bit into the actual transaction and open the debugger. This can be done by clicking on the "Debug" button right next to the transaction.

debug_button

Then open the "Instructions" Accordion menu plus the "Step Detail" menu. You will see the instructions and how much gas each instruction cost at that specific step.

remix_debugger_step_detail

What you see here are all the Ethereum Assembly instructions. So, we know that Solidity compiles down to EVM Assembly. This is what is actually executed by the miner and this is what the smart contract actually looks like. Let's have a look at the first two statements here:

PUSH1 60
PUSH1 40

That means nothing else than pushing the values 60 and 40 onto the stack. There is obviously a lot more going on there and you can follow what they do by moving the blue slider in the step-by-step debugger.

I'm working on an in-depth article about the Ethereum Virtual Machine - if you are interested then subscribe to this blog to receive updates.

There is a very handy Excel Spreadsheets that lists the Gas-Costs for each instruction, which gives you a nice overview. The gas costs per instruction are also listed in the Ethereum Yellow-Paper/Appendix G, but it's a bit harder to extract the gas cost for each instruction.

I have put together the exact amount of gas each instruction needs based on the sheet and the Ethereum Yellow Paper in order to write the value 5 to storage:

GAS	Instruction
3	000 PUSH1 60
3	002 PUSH1 40
3	004 MSTORE
3	005 PUSH1 04
2	007 CALLDATASIZE
3	008 LT
3	009 PUSH1 3f
10	011 JUMPI
3	012 PUSH1 00
3	014 CALLDATALOAD
3	015 PUSH29 0100000000000000000000000000000000000000000000000000000000
3	045 SWAP1
5	046 DIV
3	047 PUSH4 ffffffff
3	052 AND
3	053 DUP1
3	054 PUSH4 348218ec
3	059 EQ
3	060 PUSH1 44
10	062 JUMPI
1	068 JUMPDEST
2	069 CALLVALUE
3	070 ISZERO
3	071 PUSH1 4e
10	073 JUMPI
3	074 PUSH1 00
3	076 DUP1
1	078 JUMPDEST
3	079 PUSH1 62
3	081 PUSH1 04
3	083 DUP1
3	084 DUP1
3	085 CALLDATALOAD
3	086 SWAP1
3	087 PUSH1 20
3	089 ADD
3	090 SWAP1
3	091 SWAP2
3	092 SWAP1
2	093 POP
2	094 POP
3	095 PUSH1 64
8	097 JUMP
1	100 JUMPDEST
3	101 DUP1
3	102 PUSH1 00
3	104 DUP2
3	105 SWAP1
20000	106 SSTORE
2	107 POP
2	108 POP
8	109 JUMP
1	098 JUMPDEST
0	099 STOP

This gives a total of 20169 gas that is used by the instructions. The attentive reader will say: hey something is off, Remix said it is 20178 gas, so, where did the additional 9 gas go? That is a very good question - and I have no answer. Edit: MSTORE takes up 12 gas, not 3 and I have no idea why actually. Most likely it's a bug, so, I filed an issue with go-ethereum. I'd be very happy if someone would enlighten me if I'm wrong in the comments.

Gas Limit

So, the main point is, as you an see, each and every instruction on the Ethereum Blockchain takes up some gas. If you are writing values to storage it costs a lot. If you are just working with the stack it costs a bit less. But essentially all instructions on the EVM cost gas. That means a smart contract can do only so much until the gas, which is sent along, is used up. In this case it's 3 million gas we sent along.

When you go back to the debugger and the first step, then you see on each step how much gas is left. I have it open here in the first step:

remaining_gas_remix_debugger-2

As you can see it already starts with the transaction cost deducted from the 3 million we send along (3,000,000 - 21464 = 2,978,536).

Once this counter reaches zero then the contract execution will stop immediately, all stored values will be rolled back and you are getting a "Out of Gas" Exception.

Block Gas Limit

In addition to the gas limit you set via a transaction, there is also a so called "block gas limit". That is the maximum amount of gas you can send along. Currently, in the Main-Net that is around 8 million gas.

Gas Refund

There is a good thing about the Gas Limit: You don't really have to calculate it yourself. If you would send 8 million gas to our contract then it would use up the 41642 and would refund the rest. So, it's almost always save to send far more gas than necessary, the rest is automatically refunded back to you.

Transactions Limit or Transaction Amount

The Block-Gas-Limit is directly connected to the amount of transactions that can be processed by the Ethereum network. Unlike in the Bitcoin world, you don't pay here for each transaction directly, you pay for the gas that a transaction uses up.

If there are 10 Transactions outstanding with each using 3 million gas then most likely only 2 will fit into the next block, the rest gets discarded.

The miner has here then two options: Increase the block gas limit by a certain amount or increase the gas price (or both)

Gas Price

The Gas Price determines how likely it is that the transaction is incorporated in the next mined block.

When you send off a transaction then you give the miner an incentive to work on your transaction next. That incentive is the gas price. The miner, once mining a new block, will also incorporate transactions into that block. Which transactions get incorporated into the next block is determined by the miner -- but he will most likely sort them by gas-price from high to low.

Lets say there are 15 transactions outstanding but only 12 of them would fit into the next block. 5 pay 20Gwei, 5 pay 15Gwei and 5 pay 5Gwei gas price. The miner will most likely pick the transactions in this order: 520 + 515 + 2*5 Gwei and incorporate them into the next mined block.

So, basically the Gas Limit determines how many instructions can be executed by the Ethereum Virtual Machine and the Gas Price determines how likely the miner will pick this transaction.

Most wallets set the standard gas price to around 20Gwei (0.00000002 ETH). If you are executing the contract above, then you'd pay around 60-70 cents (US-Dollar cent) with a current exchange rate of 1 ETH = 800 USD. So it's not cheap at all.

Luckily you only need a higher gas price during congestion of the network. That is when many people try to send transactions at the same time. If the network is not congested then you don't need to pay so much gas. EthGasStation estimates that 4Gwei would actually be enough - which is 1/5th of the amount and that adds up. So, with this little function and only 4Gwei of gas it would be around 16cents, instead of 65 cents. A huge difference.

Conclusion

The Etheruem Virtual Machine is a great invention. Gas detaches the computation from the price of an Ether. The economics behind are totally different from both, the perspective of the user and the developer.

As a Developer it's good to know the mechanics behind gas, where it comes from and how it's calculated.

As a User it's good to know that you can influence how fast a transaction is mined.

If you like this article then subscribe below. If you have an opinion about it then let me know in the comments!