GAS GAS GAS!

in #blockchain6 years ago

GAS.png

In our previous post we left you with a bunch of homework: try to give the Faucet back its (fake) ETH and take some time to explore Ethereum Ropsten transactions.

If you have tried the first one, you probably hand up with an error: you were not able to give back the 1 ETH the Faucet has given to you. Why? This doesn’t actually make any sense: Faucet has given me 1 ETH, why the hell I can’t give it back? Well, because of “gas”.

To make it simple, gas is the Ethereum fee: you are using a public platform and the system doesn’t like free ride. In fact fees are given to the miners for their work and – in contrast to Bitcoin, where the fees’ amount takes into consideration only the size of transactions – Ethereum gas is sized on every computational task performed by the execution of transaction and smart contracts. You can find on the Ethereum Yellow paper a full table of cost, as each operation performed on the decentralized virtual machine costs a fixed amount of gas.

As far as we know, gas has three main functions:

  1. Reward of miners: miners are working really hard to make your transaction safely stacked in to the Blockchain. They are using their hardware, energy and Internet connection, so they shall be rewarded to make the whole system keep working.
  2. Avoid DDOS attack: an attacker shall spend a lot of money to make a Denial Of Service attack, making inconvenient to perform such a behavior.
  3. Avoid poor written code to compromise the EVM with loopholes: if a transaction isn’t supported by enough gas, it is halted by the system, so there is no way a while loop keep working for ever.

Probably during this reading a question came to your mind: why I am supposed to use gas also on a test network? Well on one hand because you have to understand how the actual things work, and writing code without a real life limitation doesn't make any point; on the other hand malicious behavior can be found also in test environments, so probably Ethereum developers want to avoid that.

As lawyers love definition – and coders love them even more – its important to be a little more precise when talking about fees: in fact, it is crucial to distinguish between gas cost and gas price. The first one is the actual cost for the EVM to perform the given operation or instructions: the table listed in the Yellow Paper can help you to figure out this amount. The later, instead, is the price – in ETH – that the sender is willing to pay for one particular transaction: it can vary according to the hurry the sender has. In fact, miners chose wisely the transactions they want to put in their blocks for validation: the higher the fee, the higher the probability of having your transaction in the first block!
Miners are getting fees according to this equation:

miner fee = gas cost x gas price(ETH)

Nevertheless, you can’t own gas: in fact it is only managed by the EVM, which takes care of convert ETH to gas (when the sender activate the transaction) and it converts it back to ETH when it is time to pay the miner.
Related to this assumption, Another important and confusing definition is “transaction fee”: it may seems the same thing of miner fee, but it is actually different. In fact the transaction fee is given by the following equation:

transaction fee = total gas use x gas price paid(ETH)

When creating a block, miners will look for the transaction with the higher fee and pick them up to populate their block. The more you are willing to pay, the more your transaction will be confirmed fast.

Surprisingly, sometimes you can even have tour gas back. In fact there are at least three situations in which you are getting some ETH back for a gas-related reasons:

  • You have used more gas (which means you have set gas limit) than it was actually needed for the transaction. This happens when the gas limit is higher than the gas cost. As said before, you can’t own gas, so the platform will convert it to ETH for you, as follow:

remain gas = gas limit – gas cost
refund ether(ETH) = remain gas * gas price(ETH)

  • You have used less gas (which means you have set gas limit) than it was actually needed for the transaction. In this case the transaction has run “out of gas” which means that the operation is halted: all changes eventually made till that point are reverted and brought at previous state. In this case you wont get back all of your fee, because you have made the miner and the platform do some work.

  • You delete a contract: yes it is possible! If you have set in your code the “selfdestruct” instruction, you can invoke it and make the contract disappear: the deletion is worth 24,000 gas (which ether conversion depend on the momentum).

Hopefully, we are going to learn how to write high quality cheap code: in fact, total gas cost of your transaction is closely related on the quantity and quality of your smart contract instructions. The more you ask to the EVM, the more you pay.

From a legal perspective, gas seems to be very important both for its scope and for the way the reward/return system has been implemented.
In fact, whenever you are following a smart contract drawing up, you should take into consideration how much gas would a transaction cost: for example, this information may help to correctly chose the payment of the whole service.
Reward/return system provided by Ethereum devs made us think: it is a very clever way to cryptographically forestall any legal conflict between the users and the miners. In fact you don’t need to actually know each miner and make a contract with them to safely store your transaction and correctly send it to the network: the platform itself has solved the issue with economic incentives. Awesome!

One last thing: we read that there is a privacy issue with the Metamask browser extension. Basically by default the application communicates your wallet address – on different level – to websites you visit: this may expose your pseudo-identity to the platform you are talking to. The issue has been discovered recently, so we are going to talk about it when things will be clearer: for the moment, if you don’t want that your extension talk about you, adjusting the buil-in privacy configuration may do the trick.

Next time we are going to explore the faucet contract and start diving a little bit more in code and standards: if you haven’t looked around in the Ethereum Ropsten transactions, give it a try to come well prepared!