A Truly Beginner’s Guide — Ethereum (Part 2)

Gas, gas, gas.

Ben Starck
Coinmonks
Published in
7 min readMay 20, 2022

--

Photo by fabio on Unsplash

In the previous part, I explained two of the primary differences between Bitcoin and Ethereum. First, Ethereum uses accounts instead of forcing us to keep track of individual transactions. Second, Ethereum allows us to upload entire programs, called smart contracts. In this part we will look closer at smart contracts, where they actually live, and the problems they brought with them.

Part 1 can be found here.

Smart Contracts

If you wanted to download a program like a video game or Microsoft Word, you would have to go to a website, download it, and then install it. Only after performing these steps would you be able to use the program. I’ve told you that programs can now exist on the Ethereum blockchain, but doesn’t that mean someone has to download it, install it, and run it? Yes, in fact many people do.

In my Bitcoin series I explained what “miners” were. Essentially they are volunteers who verify that transactions are allowed to occur, and are compensated for their work by being given a small amount of bitcoin. What I didn’t mention, was that none of these miners trust each other. This isn’t the result of some distant feud, but rather by design. The basic idea is this: if some stranger is telling the truth about the validity of a transaction, than I should be able to perform the same checks as them on my own version of the data and get the same results. So that is what every single miner does, they each have their own version of this massive list of transactions (the blockchain) and when someone wants to make a new transaction, they independently verify that it all checks out. If every miner has the same records and nobody is lying about anything, everyone will have the same results.

For Bitcoin, we’re only keeping track of transactions, which are relatively lightweight. Even so, the current size of the Bitcoin blockchain is pretty big. This means every single miner downloads this entire history before they begin their work. Switching gears back to Ethereum, where we are no longer just keeping track of transactions, we are faced with the reality that downloading the entire blockchain means downloading every program as well. When you make a program and upload it to the Ethereum blockchain, every miner downloads it. When the first of the month comes around, each miner runs your allowance program. The child only gets paid once, but every miner will run it independently so that they can all agree on what it does.

Photo by Khamkéo Vilaysing on Unsplash

Gas

Although our hypothetical allowance smart contract is pretty lightweight and simple, there is nothing stopping someone from creating a truly gargantuan program. Even without programming experience it should be obvious that the larger the program, the more work the miner will have to perform whenever it runs. This means a real person, with a real computer, has to pay for electricity and suffer wear on their computer every time you ask it to run this massive program.

The creators of Ethereum knew about this risk ahead of time, and created something called Gas. Ethereum miners, just like Bitcoin miners, get paid a flat amount of ether for processing transactions. However, me sending a few ether to a friend is much easier on the miner than asking the miner to run this previously mentioned gargantuan program. It would make sense then, that the miners should get paid more in situations like this, and the individual causing the increase in work should be the one paying for it. That’s exactly what gas is.

(There is actually an even greater risk, called the Halting Problem. I will write an article on this in the future, but it’s too technical for this series. Suffice it to say that, without gas, the Halting Problem would shut down the entire Ethereum blockchain instantly.)

As an analogy, let’s consider a scenario where we want to sell a car, but we can’t physically deliver the car. A person 300 miles away is willing to purchase it if you can deliver it. You find a stranger online already going that way and is willing to take it as long as you pay them a flat $200, plus pay for all the gas. For this example we don’t want to pay anyone more than the agreed amount. This means we don’t want to pay for any gas left in the tank after it’s delivered. The buyer agrees to measure the fuel left in the tank after it’s there, and pay you back for any gas in the tank.

The gas in this analogy is very similar to how Ethereum gas works. We ask a miner to run our program (deliver our car). Every single request on the Ethereum network has a pre-defined “distance” measured in gas units. For example, a simple transaction where I send some ether to a friend costs 21,000 gas units. Uploading a program to the blockchain costs at least 30,000 gas units, but can cost more depending on how big it is. Going back to our analogy, if the buyer is 300 miles away and our car perfectly gets 30 miles per gallon, we can say this delivery will use exactly 10 gallons of gas. The further the distance, the more work required, and therefore the more gas needed. It’s the same for Ethereum: the more complex or heavy the program is, the more you’ll have to pay.

Continuing with this analogy, we can accurately predict how much money we will be paying this delivery driver. We know it’s a flat $200, and then 10 gallons of gas at whatever the price of gas currently is. For simplicity, we can say it’s $2.00/gallon (can you imagine a world so perfect?). We can reasonably assume we’ll end up paying $220 for the delivery. But let us consider a few problem scenarios:

  1. We calculated the distance wrong, and it was actually only 270 miles, not 300. In this case, there should be one entire gallon of gas left in the tank since we were 30 miles off. The buyer will pay us $2 as reimbursement for the gas.
  2. We calculated the distance wrong in the other direction, and it was actually 330 miles. In this case, the delivery driver got 30 miles from the destination, but then the car died. The buyer doesn’t get the car, we don’t get paid from the buyer, and the delivery driver hitchhikes the rest of the way with $200 for doing exactly what we paid them to do. An all around failure which cost us $220 and all the lost income from selling the car.
  3. We calculate the distance correctly, but we didn’t estimate the gas cost correctly. In this case, we said gas was $2. But what if the driver simply doesn’t feel like this is enough to cover the gas costs? Maybe they think it’s actually $2.10/gal and they don’t want to have to eat into their flat $200 to cover the gas. So they simply refuse to deliver it and we either increase how much we pay, or find another driver willing to accept this price.

All of these scenarios are possible in Ethereum too, so let’s talk about what happens. In the first scenario we over estimated the distance. In Ethereum, we have to add up all the gas costs for all the operations. Remember, I said it’s 30,000 to upload a program, but could be more. We do our best to estimate it and we end up with 270,000 total gas units (gallons) needed for our allowance smart contract. If the miner only ends up using 250,000, they will refund us the 20,000!

In the second scenario, we under estimated. If a miner starts running our allowance program but runs out of gas part way, they simply stop. We don’t get our money back because the miner still did work, just like the driver did the work. However, it wasn’t enough to finish the job and therefore nothing changes on the blockchain, our child doesn’t get their allowance, and we are out all our gas money.

In the third scenario, the driver isn’t satisfied with the price. This can happen with Ethereum too. Unlike the real world, where gas prices are controlled by the gas station, Ethereum gas prices are controlled by the users. When you are ready to send a transaction, you get to choose how much you pay for gas. The miner then has to decide if they’re happy with that price. If other users are willing to pay more for gas to run their own smart contracts, the miners will prefer to run theirs over yours. You can either increase how much you’ll pay, or wait until the higher paying jobs are done.

In the image above you can see the actual gas statistics for a recent transaction on the Ethereum blockchain. In this case, the top row depicts how much a user was willing to pay for gas, and in the second we can see the predicted amount of gas used and then the actual gas used to the right. In this case the user was willing to pay 0.0000000032[…] ether per gas unit, and they predicted it would cost 21,000 gas units. We can see it ended up using exactly 21,000 gas units.

Now we understand that smart contracts are simply programs living on the blockchain, and we understand that we pay more based on how complex the program is. At this point it should be clear that this works, and now we can move on to actual smart contracts instead of our hypothetical (and boring) allowance smart contract.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Ben Starck
Coinmonks

I am a Computer Science PhD Student, Adjunct Instructor, and Cryptocurrency + Cybersecurity Researcher. My passion is helping others learn what I struggled to.