BTC
ETH
HTX
SOL
BNB
View Market
简中
繁中
English
日本語
한국어
ภาษาไทย
Tiếng Việt

Popular Science: Interpretation of the Ethereum Yellow Paper (3/7)

以太坊爱好者
特邀专栏作者
This article is about 3447 words, reading the full article takes about 5 minutes
Ethereum uses Gas to solve the problem of "who pays for the service".
AI Summary
Ethereum uses Gas to solve the problem of "who pays for the service".

Editor's Note: This article comes fromEthereum enthusiasts (ID: Ethfans)Ethereum enthusiasts (ID: Ethfans)

, Author: Lucas Saldanha, Translator & Proofreader: IAN LIU & A Jian, reproduced by Odaily with authorization.

It’s exciting time again, we’re back with another blog post about the Ethereum Yellow Paper! In this article, we will learn more about Gas and payments in Ethereum, understand the economics 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 miner nodes to select transactions to be packaged. (This blog post refers to Chapter 5 of the Ethereum Yellow Paper)

secondary title

introduce

introduce

When I first started learning Ethereum, the first confusion I encountered was related to transaction fees. My question was, "why pay for a Dapp if I can get the same service as a Dapp for free?" - very quickly, I realized how stupid and naive I was.

Now, let's think of the Ethereum network as a huge computer that can perform calculations and read and write data. This is different from the computer in your own home. This giant computer (Ethereum) is shared by everyone. As long as there is an Ethereum account, anyone can interact with the entire platform, such as transferring Ethereum and deploying smart contracts. etc. However, due to the inherent limitations of distributed systems, the question of "who will pay for these services" is more difficult to answer.

Gas

secondary title

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

It is easy to think of the relationship between cars and fuel: if you want to drive from A to B, you must consume some fuel; similarly, if you want to perform certain operations on the Ethereum EVM, you must also consume some fuel. Pay for gas. The farther the car wants to drive, the more fuel it needs; the more calculations it wants to perform on Ethereum, the more Gas it needs to pay.

From another point of view, Ethereum adopts the model of user payment, which can avoid the abuse of resources. Once you have to pay for each operation, you will write the code as concisely and efficiently as possible; the existence of Gas can also prevent attackers from flooding the Ethereum network through invalid operations. (Unless the attacker is willing to pay a large sum of money to perform an invalid operation)

secondary title

gasPrice and gasLimit

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

If your car's fuel tank has a capacity of 50 liters, how much will you pay to fill it up? It depends on the price per liter at the gas station, right? The pricing of Gas in Ethereum is the same! If you want to perform a transaction that costs 10 Gas, the fee you pay depends on the price per unit of Gas.

So how do we know the price per unit of Gas? There is a misleading saying: you can order as much as you like. Although there is nothing wrong from a technical point of view, we need more knowledge as a foreshadowing to know the pricing of Gas.

If you read the previous article in the Ethereum analysis series, you should remember that the Ethereum transaction structure contains, among other parts, gasPrice and gasLimit.

gasPrice indicates the price that the transaction sender is willing to pay per unit of Gas (measured in Wei), which means that the transaction sender can customize the price per unit of Gas that the transaction sender 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 this transaction is 30 Wei (not the actual value, just for everyone to understand how to calculate).

gasLimit indicates how much Gas the transaction sender can accept to execute the transaction. Because sometimes, you can't know exactly how much Gas will be consumed to execute a transaction; or there is an infinite loop bug in your smart contract, if there is no gasLimit, this will cause the sender's account balance to be wrong Run out. The gasLimit is a security mechanism to prevent someone from consuming all the ether in the account due to miscalculation or bugs.

secondary title

miner

miner

A block is a data structure that contains a collection of transactions, and miner nodes in Ethereum are responsible for creating blocks on the chain. When creating a block, miners will select transactions from the transaction cache pool (the pile of transactions waiting to be packaged) and start to produce blocks.

I will not go into the details of the Ethereum mining algorithm now (maybe it will be discussed in a follow-up article), everyone just needs to remember that mining is an expensive process, so if mining is not rewarding, then there must be no miners willing to do it!

In Ethereum, every time a miner successfully creates a block, he can get a fixed amount of block rewards and rewards for referencing uncle blocks (not expanded here), and at the same time get all transaction fees included in this block ; So the higher the gasPrice is set in the transaction, the more transaction fees the miners can get. If you want to know more about miner rewards, please refer here.

Let's assume a simple scenario. Bob has 200 wei in his account, John has 100 wei in his account, and they both want to send a transaction that costs 90 Gas.

When entering the link of selecting transactions to pack into blocks, miners tend to choose transactions with higher handling fees. In our example, Bob's gasPrice is twice as high as John's; since both transactions require 90 Gas, miners who choose Bob's transaction can get twice the gas price reward.

image description

-Miners will choose the transaction with the highest gasPrice-

The mechanism of rewarding miners by paying transaction senders forms a self-regulating economic system in Ethereum. The sender of the transaction does everything possible to reduce the transaction cost, while the miner always hopes to maximize the profit, and the two form a balance. As a transaction sender, if you set the gasPrice higher, it means that the miners are more motivated to package your transaction, and the sooner your transaction can be included in the block.

secondary title

in conclusion

in conclusion

From this article, we learned that executing an Ethereum transaction consumes Gas just like a car consumes fuel. I hope this article gave you an idea of ​​the cost of computing and why we need to pay to get Ethereum services.

We also discussed the importance of gasPrice and gasLimit; if the smart contract has a bug or miscalculation, gasPrice can protect users from losing ether for nothing.

Furthermore, we also study the economic mechanism behind transaction fees and how miners choose transactions to maximize revenue. Now we know how to adjust the gasPrice to make the transactions we send more attractive to miners, so that the transactions can be packaged earlier.

ETH
Welcome to Join Odaily Official Community