Interpretation of the Ethereum Yellow Book (3): The Economic Mechanism behind Gas and Transaction Fees

Read the previous article:

Interpretation of the Ethereum Yellow Book (1/7) Interpretation of the Ethereum Yellow Book (2/7)

Another exciting moment, we once again launched a blog post about the Ethereum Yellow Book! In this article, we will learn more about the Ethereum of Gas and Payment, understand the economic theory behind Ethereum, and explain why fees are so important in the Ethereum ecosystem.

After reading this blog post, you will know why the transaction cost is called Gas, understand the difference between gasPrice and gasLimit, and understand the strategy of the miner node to choose the transaction to be packaged. (This blog post refers to the Ethereum Yellow Book Chapter 5)

This article is the third article in the Ethereum analysis series.

(Disclaimer: This article is based on the Yellow Book of the Byzantine 127780ac version of November 27, 2018)

Introduction

When I first started studying Ethereum, the first confusion I encountered was related to transaction fees. My doubt is, " If I can get the same service as Dapp for free, why spend money on Dapp? " – Soon, I realized that I was stupid and naive.

Computing is costly —I don't mean the cost of buying laptops and tablets, but the cost of running virtual machines, data storage, information processing, and more. Today, we are very accustomed to free services, but often forget a key point: someone is paying for these services! You are using a free Gmail or Yahoo mailbox, and if you want to keep the entire service up and running, the mail provider will pay for the server, data storage, and infrastructure. If you have a Facebook or Instagram account, you know for sure that it takes cost to process and securely store the photos you upload in the cloud. So the difference between the current free service and Dapp's paid service is that the person who pays the bill is different (the free service is not directly paid by you).

Now, we will think of the Ethereum network as a huge computer that can realize computing and reading and writing data. This is different from the computer in your own home. This supercomputer (Ethereum) is shared by everyone. With an Ethereum account, anyone can interact with the entire platform, such as transferring Ethereum and deploying smart contracts. and many more. However, due to the inherent limitations of distributed systems, the question of "who pays for these services" is more difficult to answer.

Gas

Gas Ethereum is the unit of calculation for all calculations. Ethereum uses Gas to solve the problem of “who pays for service” . Do you want to transfer money to another Ethereum account? Do you want to deploy a smart contract to store your phone contacts? No problem, please pay Gas first.

This is easily reminiscent of the relationship between the car and the fuel: if you want to drive from the A to the B, you must spend some fuel. Similarly, if you want to perform some calculations on the Ethereum EVM, Pay Gas. The farther the car wants to drive, the more fuel it needs; the more calculations you want to make on the Ethereum, the more Gas you have to pay .

We can find the amount of Gas consumed for each EVM operation in Appendix E of the Ethereum Yellow Book; these values ​​seem random, but in fact it makes sense. In general, these values ​​reflect the cost of performing the operation (measured by the time dimension) and the occupied permanent memory resources (when the data is written). If you want to know more detailed Gas costing formulas, check out the Ethereum 1.0 Gas Cost Table . I'm not sure if it's consistent with the latest Gas consumption, but at least it gives you a more conceptual idea of ​​the Code's Gas consumption guidelines.

From another perspective, Ethereum's user-paid model can avoid the abuse of resources. Once you have to pay for each type of operation, you will write the code as concise and efficient as possible; the presence of Gas can also prevent an attacker from flooding the Ethereum network with invalid operations. (unless the attacker is willing to pay a large sum of money to perform invalid operations)

gasPrice and gasLimit

Now that we understand Gas, it's time to talk about how Gas is going to be priced; let's get back to the example of cars and fuel.

If your car has a capacity of 50 liters, how much do you have to pay to fill the tank? It depends on the price of oil per liter at the gas station, right? The pricing of Gas in Ethereum is the same! If the transaction you are going to execute requires 10 Gas, the cost you have to pay depends on the price per unit of Gas .

So how do we know the price per unit of Gas? There is a misleading statement: how much you want to decide. Although there is nothing wrong with the technical point of view, to know the pricing of Gas, we need more knowledge as a foreshadowing.

If you've read the previous article in the Ethereum Analytic Series, you should remember that the Ethereum trading structure includes, among other things, gasPrice and gasLimit .

GasPrice represents the price that the sender of the transaction is willing to pay per unit of Gas (measured in Wei), which means that the sender of the transaction can customize the price per unit of Gas that is willing to pay. Assuming that a transaction costs 10 Gas, and we are willing to pay 3 Wei/Gas, the total cost of sending the transaction is 30 Wei (not actual value, just for everyone to understand how to calculate).

gasLimit indicates how many Gases the sender of the transaction can accept to execute the transaction . Because sometimes, you can't know exactly how much Gas you need to execute a transaction; or you have a dead loop bug that never jumps out in your smart contract. If there is no gasLimit, this will cause the sender's account balance to be incorrect. Run out. gasLimit is a security mechanism that prevents anyone from consuming all of the Ethereum in an account because of bug estimates or bugs.

Another interesting point is that gasLimit can be considered as prepaid Gas. When the node is verifying the transaction, first calculate the fixed cost of the transaction by multiplying gasPrice by gasLimit. If the transaction sender's account balance is less than the transaction's fixed cost, the transaction is considered invalid. After the transaction is executed, the remaining Gas will be returned to the sender's account; of course, if the Gas is exhausted during the execution of the transaction, nothing will be returned. This also explains why the sender of the transaction always sets gasLimit higher than the estimated amount of Gas.

After figuring out the meaning of these two parameters, you may want to ask: " Why is the sender of the transaction decide the price per unit of Gas? " If you go to the nearest gas station and tell the cashier, "I am willing to pay 5 cents per liter of oil"; a better cashier may have passed by, and a sensible cashier may call the police. So to understand the design mechanism, you need to know what the miner's work is and what the commission is.

miner

A block is a data structure that contains a set of transactions, and a miner node in Ethereum is responsible for creating blocks on the chain. When the block is created, the miners select the transaction from the transaction cache pool (waiting for the packaged transaction heap) and start to block.

I am not going to expand the details of the Ethereum mining algorithm (maybe discussed in the follow-up article). You only need to remember that mining is an expensive process, so if mining does not pay off, then there is no miner willing to do it!

In Ethereum, whenever a miner successfully creates a block, he or she can get a fixed block reward and a reward for the uncle block (not here), and also get the handling fee for all the transactions included in this block. So the higher the gasPrice is set in the transaction, the more transaction fees the miner can get . If you want to know more about miners' rewards, please refer here .

Let's assume a simple scenario. Bob has 200 wei in his account and John has 100 wei in his account. Both of them want to send a transaction that consumes 90 Gas.

Bob sets gasLimit = 100, gasPrice = 2; John wants to set gasLimit to 200, but unfortunately he only has 100 wei, this setting will make the transaction fixed cost higher than the account balance; so John finally sets gasLimit =100, gasPrice =1 .

When entering the process of selecting a package to be packaged into a block, the miner tends to choose a transaction with a higher commission. In our case, Bob's gasPrice is twice as high as John's; because both deals require 90 Gas, the miner chooses Bob's deal to receive twice the commission fee.

– Mining unions choose gasPrice's highest deal –

A mechanism for rewarding miners by paying by the sender of the transaction creates a self-regulating economy in Ethereum. Transaction senders do everything possible to reduce transaction costs, and miners always want to maximize revenue, creating a balance between the two. As a sender of the transaction, if you set the gasPrice higher, which means that the miner is more motivated to package your trade, the sooner your trade can be loaded into the block.

Some miners even set their own gasPrice lower limit, directly ignoring those trades whose gasPrice is less than the lower limit.

When sending a trade, it is difficult to know what the minimum gasPrice is currently valid. These tools can scan the entire Ethereum network, calculate the gasprice average of other current transactions, and help the sender choose a reasonable gasPrice that can be accepted by the miners.

in conclusion

From this article, we learned that just like a car consumes fuel, the execution of the Ethereum transaction requires the consumption of Gas . I hope this article will let you know the cost of computing and why we need to pay to get the services of Ethereum.

We also explored the importance of gasPrice and gasLimit; if a smart contract has a bug or an estimation error, gasPrice protects the user from the loss of Ethereum .

Furthermore, we also studied the economic mechanism behind the transaction fee and how the miners choose to trade to maximize revenue . Now we know how to adjust gasPrice so that the transactions it sends out are more attractive to miners, so that the deal is packaged earlier.

In the next blog post, we will discuss the transaction execution model (Ethereum Huangpi Book Chapter 6), which is the most complicated part of the Ethereum Yellow Book (believe me, also in the Yellow Book!) so I may split this part. In a few articles. Please keep an eye on it!

Do you still like the current series? I am looking forward to hearing your thoughts. If you find any errors in the text or are worth improving, please let me know in the comments as usual, I always like to receive feedback 🙂

See you next time!

Original link: https://www.lucassaldanha.com/ethereum-yellow-paper-walkthrough-3-7-gas-and-payment/ Author: Lucas Saldanha translation & proofreading: IAN LIU & A sword

(This article is from the EthFans of Ethereum fans, and it is strictly forbidden to reprint without the permission of the author.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

Sun Yuchen used capital hegemony to control Steem, causing controversy, the integrity of stolen users' voting rights was questioned

Recently, in order to prevent capital power on the chain, Steem witness nodes jointly launched a soft fork. God V des...

News

Inventory of Seven Bills that Could Determine the Future of Cryptocurrency in the United States

Author | DL NEWS compilation | Garyma Wu said the original link of the blockchain https//www.dlnews.com/articles/defi...

Market

Wu's Weekly Picks CoinEX attacked, FTX's coin selling rules, Binance US layoffs, and Top 10 news (September 9-15)

Author | Wu's Top 10 Blockchain News This Week. US August Unadjusted CPI Annual Rate 3.7% Core...

Blockchain

Swiss exchange SIX announces investment in cryptocurrency trading platform Omniex, exact amount not disclosed

According to a report by Finance Magnates on February 25, SIX Group, an operator of the Swiss Stock Exchange, announc...

Blockchain

The kimchi premium breaks $1,000! South Korea’s "disfigured alcohol addiction" has another episode?

The quiet year's kimchi premium once again swayed, and it was a bit like the scene before the bull market in 201...