* * What is Ethereum Gas? Specific Gas Guide

in dungbui •  7 years ago 

What is Ethereum Gas? - is the lifeblood of the Ethereum ecosystem, and can not be loaded. Gas is a unit of measurement that calculates the cost that it will need to perform certain operations.

  • What is a smart contract?
    What is GAS? How to buy GAS

    Each activity that participates in Ethereum, can be a simple transaction, or a smart deal, or even an ICO will consume a certain amount of gas. Gas is the unit used to calculate the cost that needs to be paid for the network to perform an operation.
    In this tutorial, we will explain to you how gas works. But before we do that, there are some concepts that we have to learn. So let's start exploring more deeply about Ethereum Gas.
    So, what would this transaction be like if it had happened in the Ethereum network?
    Suppose you want to buy something from a vending machine in the Ethereum network, how will this step be?
    Step 1: You give the vending machine some money and this is recorded by all the nodes in the ethereum network and the transaction is updated in the ledger.
    Step 2: You click the node corresponding to the item you want and the request will be updated in the Ethereum and ledger.
    Step 3: The thing you want to buy is given and you take it and this is recorded by all the nodes and ledgers.
    Every transaction you make through smart contracts will be recorded and updated by the network. This makes everyone involved in the contract responsible for their actions. Make people no longer angry by making every action visible to the entire network.
    What is Virtual Ethereal Machine?
    Before we understand the Ethereal Virtual Machine (EVM), we must understand why it is necessary to have a "virtual machine".
    So, come back with a smart contract.
    What are the desired attributes that we need in a smart contract?
    Anything running on a blockchain needs to be invariant and must be able to run across multiple nodes without compromising its integrity. Therefore, the smart contract function should have three things:
    Determined.
    Can be changed
    Isolated.
    Feature # 1: Identify
    A program is defined if it outputs the same output for each input of a given input. For example. If 3 + 1 = 4 then 3 + 1 will ALWAYS produce the result 4 (assuming the same basis). So when a program provides the same output for the same set of inputs in different computers, the program is called 'defined'.
    There are times when a program can act in an indefinite fashion:
    Call unknown functions in the system: When a programmer calls an undefined function in their program.
    Unknown data source: If a program collects data at runtime and an unknown data source, the program will become undefined. For example. Let's say a program collects 10 searches on Google for a particular query. The list may continue to change.
    Dynamic calls: When a program invokes a second program it is called a dynamic call. Because call targets are defined only in the implementation process, not defined in nature.
    Feature # 2: Can be changed
    In mathematical logic, we have an error called "stop the problem". Basically, it means, we can not know whether a certain program can perform its function within a time limit. In 1936, Alan Turing deduced, using Cantor's Diagonal problem, that there was no way to know whether a given program could be completed within a time limit.
    This is clearly a problem for smart contracts because by definition contracts must be able to end within a certain period of time. There are a number of measures taken to ensure there is a way to "kill" the contract from the outside and prevent it from participating in an endless loop as this will cause the loss of resources:
    Turing Incompleteness: A Turing Incomplete blockchain will have limited functionality and is unlikely to perform jump and / or loop operations. Therefore they can not participate in an endless loop.
    Step and Clock: A program can simply track the number of "steps" it has taken, ie the number of instructions it has given, and then terminate when a particular step has been taken. present. Another method is clock charging. The contracts are made with a prepaid fee. Each order requires a specific fee. If the cost exceeds the prepaid fee, the contract will be terminated.
    Timer: This is a pre-defined timer that is stored. If the performance of the contract exceeds the deadline, the contract will be canceled from the outside.
    Feature # 3: Isolated
    In a blockchain, anyone can upload a smart contract. So that contracts may accidentally contain viruses and errors.
    If the contract is not isolated, this can impede the whole system. Therefore, it is important that a contract be segregated in a sandbox to save the whole ecosystem from all negative influences.
    Now that we know what these features are, it's important to know how they are implemented. Typically, smart contracts are run using one of two systems:
    What is Ethereum Gas: Specific Guidelines
    Bitcoin was created to answer two questions that everyone has asked themselves.
    Is it possible to create a transfer of money between two people without intermediaries?
    Is it possible to create a decentralized amount that can work on something like blockchain?
    Satoshi Nakamoto answered these questions when he created Bitcoin. Finally, we have a decentralized currency system that transfers money from one person to another.
    However, there is a problem with bitcoin and also the problem of all first generation blockchain: Users are only allowed to perform currency transactions, can not add conditions for those transactions.
    Alice can send Bob 5 BTC, but she can not impose conditions on those transactions. For example. She can not tell Bob that he will receive the money only when he does some work.
    These conditions will require extremely complex scenarios. Something to record the requirements and make the process of receiving money more seamless.
    ... and "something" is the smart deal.
    What is a smart contract?
    Smart contracts help you exchange money, assets, shares, or anything of value by being transparent, without conflict, and avoiding services from a broker.
    Vitalik Buterin's Ethereum standard is very easy in this generation. They show the world how blockchain can evolve from a simple payment mechanism to something far more meaningful and powerful.
    So what are these "smart contracts" and what is the problem here?
    A smart contract is an automatic contract. They operate with specific instructions written in their code, recording certain conditions that must be fulfilled.
    Smart contracts are the way things are done in the Ethereum ecosystem. When someone wants to have a special job done in Ethereum, they start a smart contract with one or more people.
    Intelligent contracts are a series of instructions, written in the "solidity" programming language, based on IFTTT logic, or IF-THIS-THEN-THAT logic. Basically, if the first instructions are done, then perform the next and subsequent functions then and repeated until you finish the contract.
    The best way to understand that is by imagining how a vending machine works. Each and every step you take acts as a trigger for the next step for it to function. It's like a domino effect. So, check out the steps you'll take when interacting with vending machines:
    Step 1: Put some money into the vending machine.
    Step 2: Click the button corresponding to the type of beverage you want to buy.
    Step 3: This type of drink appears and you take it out of the machine.
    Now look at all the steps and think about it. Are you going to step one step further? Each step is directly related to the previous step. There is another factor to consider, and that is an integral part of the smart deal. You see, in your entire interaction with the vending machine, you (the requester) only work with the machine (service provider). There is absolutely no third party.
    Virtual machines: used by Ethereum.
    Docker: Used by Fabric.
    Let's compare these two and determine what makes a better ecosystem. For simplicity, we will compare Ethereum (virtual machine) with Fabric (Docker).
    Virtual machines provide better identification, changeability, and an isolation environment for smart contracts. However, the docker has a distinct advantage. They provide code language flexibility while in VMs like Ethereum, one needs to learn a whole new language (solidity) to create a smart contract.
    EVM is the Etherex virtual machine, in which all smart contracts operate in Ethereum. This is a simple 256-bit Turing Complete virtual machine. Turing Complete means providing resources and memory, any program implemented in EVM can solve any problem.
    What is Ethereum Gas?
    As explained in the introduction, Gas is a unit of measurement used to calculate the cost that it will need to perform certain activities.
    All smart contracts running in EVM are encrypted using solidity (Ethereum is planning to move to Viper in the future). Each and every line of code will certainly require a certain amount of gas to calculate.
    The image below has been taken from the Ethereum Yellowpage and can be used to get a glimpse of what the price of gas can be, from which to plan wisely for your smart deal.
    To better understand the behavior of gas in Ethereum, let's use a comparison. Suppose you are going on the road. Before doing so, you must do the following:
    You go to the gas station and specify the amount of gas you want to pump into the car.
    You have gasoline in your car.
    You have to pay the amount of gas you bought.
    Now, similarly, let us think of Ethereum.
    The car, is the activity you want to perform, such as a gas contract or a smart contract.
    Gasoline is gas.
    Gas station is your miners.
    The amount you pay for them is the mining fee.
    All activities that users want to perform in the ethereum must provide gas for the following:
    To cover its data is also known as the internal charge.
    To cover its entire cost.
    Now that we have mentioned the basics, you can ask the following questions.
    Why Do We Have Gas Systems?
    The answer is simple ... is to encourage.
    Like any other peer-to-peer system, Ethereum is heavily dependent on miners. As more miners, the system will have more and more hashrate, safe and faster.
    In order to attract more miners into the system, they need to make the system seem like it can be profitable to attract them. In Ethereum, there are two ways in which miners can make money:
    By exploiting blocks and getting bonus blocks.
    By becoming temporary dictators of their exploit blocks.
    Let's explore the second point.
    Miners are responsible for placing transactions in blocks. To do that, they have to use computational power to verify the contract intelligently. The gas system allows them to charge a certain fee to do so.
    This fee is called the coin worker's fee and it helps to encourage them to continue to participate actively in the ecosystem.
    So, how much can they charge? Before we can calculate, let's understand how to measure the gas.
    The smallest measure of gas is the wei. So if we spend 1 unit of gas in operation, we call it a wei.
    Units of measurement will increase as follows.
    So, how do we convert gas into an Ether?
    Without a fixed conversion price, it is entirely up to the operator to determine the conversion price, however, the average conversion rate is: 1 gas = 0.02 micro Ether.
    The following chart shows the average Ethereum gas price chart.
    Before we go any further, it is important to know the Gas Limit concept.
    What is Ethereum Gas Limit?
    To obtain an activity performed at Ethereum, the creator of the activity (i.e. the originator of the transaction or the creator of the smart contract) must specify a limit before sending it to the miner. Once a gas limit has been determined, then the new miner is allowed to begin performing the activity.
    When submitting a gas limit, consider the following:
    Different activities will have different gas costs (as described above).
    The miners will stop running as soon as the gas runs out.
    If there is any leftover gas, it will be returned to the server immediately.
    Take a look at how this works in a hypothetical scenario.
    Let's say we add two numbers and assume that the contract must do the following:
    Store 10 variables. Suppose this activity costs 45 wei gas.
    Add two variables, assuming this cost is 10 wei gas.
    Recording the results, this cost 45 usi gas.
    Suppose the gas limit is 120 wei.
    Total gas used by the operator is (45 + 10 + 45) 100 wei.
    The fees paid to them with the assumption of 1 wei worth 0.02 micro ETH (100 * 0.02 micro ETH) = 0.000002 ETH.
    Now how much gas is left?
    120 - 100 = 20 wei.
    20 wei is returned to the server.
    So, once that has been determined, there are two scenarios that one has to consider:
    Gas Limit is too low.
    Gas Limit is too high.
    Scenario 1: Gas Limit is too low
    If an operation runs out of gas, then it will return to its original state as nothing really happens, however, the server still has to pay the miners fees for their computing costs and Activities will be added to the blockchain (even if it has not been completed).
    Go back to your trip, if you do not have enough gasoline in your car, you will not be able to reach the destination, you paid the owner who pumped gasoline for you and then get back or not?
    Let's see how this works in our hypothetical hypothetical contract. This step is:
    Store 10 variables. Suppose this activity costs 45 wei gas.
    Add 2 variables, assuming this cost is 10 wei gas.
    Recording the results, this cost 45 usi gas.
    However, this time, Gas Limit is 90 wei.
    Now, we know that the gas needed to make the contract is 100 wei, but we only have a gas limit of 90 wei.
    In this case, the miner will calculate what the 90 wei can do and the operating cost of the 90 wei will be (90 * 0.02 micro ETH) 0.000018 ETH.
    In addition, the contract will return to its original state and be added to the blockchain.
    Scenario 2: Gas Limit is too high
    So, if we set Gas Limit too high?
    Is this more reasonable? After all that left over is returned to the server right?
    That sounds good on paper but it does not really work in practice.
    The miner was limited to 6,700,000 gas limits per block. Every simple transaction in Ethereum usually has a gas limit of 21,000. Miners can only add activities worth less than or equal to the gas limit of the block.
    Suppose a transaction A has a gas limit of 42,000 and two transactions B and C have a normal limit of 21,000.
    Which option is better for a miner putting on their block?
    Will they put into transaction A and return a large amount of gas?
    Or would they put the B and C deals and get a little refund for not returning anything?
    The second thing is more economical for them?
    This is the reason why having a large gas limit is not the right way.
    Here is the average gas limit graph.
    High and Low Gas vs High and Low Charges
    It is important to understand that so far gas and ether are not the same thing. Gas is the unit of calculations needed while ether is the price or can be understood as the charge that we pay for that gas.
    Now with the knowledge of everything that we have known so far, let's look at gas and charge scenarios.
    If a gas activity is LOW, then the miners will not even take it because it does not have enough gas to complete the calculation process.
    If a low-cost operation, then it may still have enough gas to cover but the miners will not be profiting at the bits to receive it because of a low-cost operation for them, Not a viable activity.
    If a gas activity is HIGH, it means that the operation will be cumbersome with a high limit gas and therefore the miners will not receive it.
    If an operation has a high cost, then the miners know that they will earn a lot of money from it and it will be picked up immediately.
    Different deals have different gas prices, according to the ethgasstation station:
    What's going on in the gas return scenario?
    In 'solidity', there are two commands that ensure that you receive a refundable amount of gas.
    SUICIDE: This cleans the smart contract basically. This will help you recover 24,000 gas.
    SSTORE: Clear archives, get you 15,000 back.
    So, if your contract is using up to 14,000 gas and cleared in the repossessions, you may be able to get back (15,000-14,000) 1,000 gas that will be refunded to you?
    It's not so simple.
    If that is the case, then the miners will lose all motivation. After all, the miners should not pay you to make the calculations right?
    To avoid such scenarios, a condition has been introduced
    Refunds can not exceed half the amount of gas used in the calculation.
    Let's take an example to clarify this.
    Suppose we have a smart contract that uses up to 14,000 gas.
    The gas limit that we set up is 20,000 gas.
    The smart contract also includes an SSTORAGE order.
    So how much gas will make the contract come back after the calculation?
    First, they will get (20,000-14,000) 6,000 units of unused gas.
    Now, the SSTORAGE command has also been used, so theoretically they will be able to get 15,000 gas again.
    However, the amount of gas used in the contract is 14,000 and because of 15,000> 14,000 / 2, the REPAYMENT AMOUNT will be 14,000 / 2 = 7,000.
    Therefore, the total amount of gas that the creators get back is 6000 + 7000 = 13,000.
    Let's take another example.
    Suppose this time the contract uses 70,000 gas and it includes a SUICIDE function.
    A SUICIDE function will pay you 24,000 gas <70,000 / 2.
    In this case, the amount of gas to be repaid will be 24,000 + unused gas.
    Criticism of Ethereum Gas. Is it reasonable?
    Although the gas system has received praise for offering a smooth operating mechanism that still encourages miners to work hard, it has recently been criticized for being too expensive. red for developers and smart contract creators.
    About this, Danny Ryan has done some interesting research in his article.
    Consider the following scenario:
    When two numbers are added one million times in Ethereum, the cost will be $ 26.55.
    Danny Ryan compares it with a standard AWS system. He said he could add two numbers a million times using python in 0.04 seconds, followed by $ 0.0059 Amazon Kindle hours with an exchange rate of $ 0.000000066.
    This means that the calculation in Ethereum is more expensive 400 million times!
    Based on his research, this is the conclusion:
    To be fair, adding a couple of million times is a bit artificial. A good written contract is likely to perform more sophisticated off-chain calculations and will have to deal more with updating the contract. Storage of large amounts of data into blockchain is not a simple task. Depending on the task, the user can store a cryptographic reference (a hash) of data on the string and hold the remainder of the off-chain data.
    That suggests that we as developers should be aware of these costs, and design the dApp accordingly. We need to find a balance between chain and off-chain complexity, while leveraging the blockchain's ability to delegate. "
    source: st
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!