Ethereum gas

An Ethereum transaction can call a smart contract, which can in turn call another smart contract, and then another, and so on. When smart contracts are buggy, it can lead to infinite loops. Outside a blockchain, it is easy to resolve an infinite loop issue. One can stop the out-of-control program by simply shutting down a server, rebooting it, debugging the program, fixing the faulty logic in the code, recompiling it, and redeploying.

With the Ethereum blockchain, this approach simply does not work! Imagine if tens of thousands of nodes went into infinite loops at approximately the same time worldwide. In order to stop infinitely looping smart contracts, all the nodes need to be shut down within a short time window. As long as one node fails to comply, the infinitely looping smart contract would still be alive and running. It is a logistical nightmare to coordinate and shutdown all these nodes.

To resolve this issue, the concept of gas was introduced. A vehicle relies on an engine burning gas to move. When an engine runs out of gas, the vehicle stops. Ethereum introduced the gas concept to achieve the same effect. When submitting a transaction to the Ethereum blockchain, the requester is required to provide a max gas amount. For example, in the following example, a transaction request is submitted to call a HelloWorld smart contract with the maximum consumption not exceeding a specified gas value:

When this request is validated by mining nodes, the HelloWorld smart contract is invoked. Every operation running on the EVM consumes a predefined quantity of gas. For example, ADD (sum operation) consumes three gas and MUL (multiplication operation) uses five gas. For illustration purposes, suppose a smart contract was badly written and contains an infinite loop. Furthermore, we assume each loop consists of  an ADD operation and a MUL operation. Therefore, a loop will consumes eight gas (three gas for ADD and five gas for MUL). After EVM executes enough loops,  the specified maximum gas value will be consumed. Consequently, EVM stops executing the contract. Therefore, all nodes would stop running at approximately the same time. Another advantage of gas is to make spamming monetarily expensive and, therefore, reduce the risk of hacking.

Gas is a metering unit for measuring consumption, just as the kilowatt is the unit for measuring electricity usage. Suppose that, in a month, a family uses 210 KW. Before sending a bill to the family, the utility company first converts 210 KW into USD, based on a predefined conversion rate. Suppose a unit of KW costs $0.2 USD, the total charge for the month is 0.2 * 210 = $42 USD. Similarly, gas usage is converted to Ether and charged to a requester. Ethereum allows a requester to specify the conversion rate when the transaction is submitted. A miner has the right to selectively process transactions by giving higher priority to transactions with higher rates. If a requester does not specify a rate, EVM uses a default rate, which varies. For example, in 2016 the rate was 1 gas = 0.00001 ETH. In 2018, one gas = 0.00000002 ETH.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93