【Introduction to Ethereum】Accounts, Transactions, Gas and Block Gas Limit in Ethereum

This article is used to help people understand some basic concepts and systems on the Ethereum network, including account system, gas, and the role of miners in the block size setting mechanism.

What is an account?

Externally Owned Accounts vs Contract Accounts

There are two types of accounts in Ethereum

  • Externally Owned Account (EOA)
  • contract account

This distinction will be abstracted in the upcoming Metropolis upgrade .

Externally Owned Account

An externally owned account has the following characteristics:

  • have an ether balance

  • Can send transactions (Ether transfer or activate contract code)

  • Controlled by private key

  • no associated code

contract account

A contract account has the following characteristics:

  • have an ether balance

  • have associated code

  • Code execution is activated by transactions or calls sent by other contracts

  • When executed -- runs at random complexity (Turing completeness) -- can only operate on the specific storage it owns, e.g. can own its permanent state -- can call other contracts

All actions on the Ethereum blockchain are activated by transactions sent by various accounts. Every time a contract account receives a transaction, the parameters that come with the transaction will become the input values ​​of the code to run. The contract code is run by the Ethereum Virtual Machine (EVM) on every node participating in the network as a validation of their new blocks.

What are transactions and messages?

trade

The term "transaction" is used in Ethereum to refer to the process by which a signed packet of data used to store a message is sent from one externally owned account to another on the blockchain.

The transaction includes:

  • recipient of this message

  • A signature that proves that the sender intends to send a message to the receiver over the blockchain

  • Value field - the amount of wei (ether/10^18) transferred from sender to receiver

  • An optional data field to store the message sent to the contract

  • A GASLIMIT value that represents the maximum number of computational steps this transaction is allowed to use

  • A GASPRICE value representing the gas fee that the sender of the transaction is willing to pay. A unit of gas represents the execution of a basic instruction, such as a computational step

information

Contracts have the ability to send "messages" to other contracts. A message is a dummy object that is never serialized and exists only in the Ethereum execution environment. They can be understood as function calls.

A message includes:

  • explicit message sender

  • recipient of the message

  • An optional data field, which is the actual input data of the contract

  • A GASLIMIT value that limits the maximum amount of gas available to the code execution originating from this message

In general, a message is like a transaction, except that it is not generated by an external account, but by a contract account. A message is generated when CALL either or both of these commands are run in the code being executed by the contract . DELEGATECALLMessages are sometimes referred to as "internal transactions". Similar to a transaction, a message directs the receiving account to run its code. Therefore, contract accounts can have relationships with other contract accounts, just like external accounts. There are many people who misuse the word transaction to refer to news, so maybe the word news has gradually withdrawn from everyone's vision due to the consensus of the community and is no longer used.

What is gas?

Ethereum implements an operating environment on the blockchain known as the Ethereum Virtual Machine (EVM). Every node participating in the network will run the EVM as part of the block verification protocol. They verify every transaction covered in the block and run the transaction-triggered code in the EVM. Full nodes in each network perform the same computations and store the same values. The fact that contract execution will be repeated many times in all nodes makes contract execution expensive, so it also motivates everyone to not put operations that can be performed off-chain on the blockchain. For each command executed there is a specific cost, counted in units of gas. Each command available to a contract will have a corresponding gas value. The gas consumption of some commands is listed here.

gas and gas consumed by transactions

Every transaction is required to include a gas limit ( sometimes calledstartGas that) and a fee a transaction is willing to pay per unit of gas. Miners can optionally package these transactions and collect these fees. In reality, all transactions today are ultimately selected by miners, but the transaction fee a user chooses to pay affects how long it takes for the transaction to be packaged. If the amount of gas that the transaction needs to use due to computation, including the original message and some other messages triggered, is less than or equal to the set gas limit, then the transaction will be processed. If the total gas consumption exceeds the gas limit, all operations will be resumed, but the transaction will be established and the transaction fee will be charged by the miner. The blockchain will show that the transaction was attempted, but all contract commands were reverted because not enough gas was provided. Therefore, the excess gas that is not used in the transaction will be sent back to the transaction initiator in the form of ether. Because gas consumption is generally only a rough estimate, many users will overpay for gas to guarantee that their transactions will be accepted. This is fine because the excess gas will be returned to you.

Estimate transaction cost

The transaction fee for a transaction consists of two factors:

  • gasUsed: The total amount of gas consumed by the transaction

  • gasPrice: The price per unit of gas in this transaction (in Ether)

transaction fee = gasUsed * gasPrice

gasUsed

Each command in the EVM is set with a corresponding gas consumption value. gasUsed is the sum of the gas consumption values ​​of all executed commands.

If you want to estimate gasUsed, you can use this API of estimateGas

gasPrice

A user can construct and sign a transaction, but each user can individually set what they want to use gasPrice, even 0. However, the Frontier version of the Ethereum client has a default of gasPrice0.05e12 wei. In order for miners to maximize their income, if a large number of transactions use the default gasPricevalue of 0.05e12 wei, then it is basically difficult for miners to accept a low gasPricetransaction, let alone 0  gasPricetransaction.

Transaction Fee Case

With permission, I'll use this example from the MyEtherWallet team and borrow their analysis. Please refer to their gas-related introduction . They also have a small page for you to convert ether into small gas count units .

You can think of gasLimit as the upper limit of your car's fuel tank. At the same time, gasPrice is understood as oil price.

For a car, the gas price might be $2.5 (price) per liter (unit). In Ethereum, that is 20 GWei (price) per gas (unit). To fill your "tank", 10 liters of oil for $2.50 = $25. Likewise, 21000 20 GWei gas = 0.00042 ETH.

Therefore, the total transaction fee will be 0.00042 ETH.

Sending tokens usually consumes about 50,000 to 100,000 gas, so the total transaction fee will go up by 0.001 to 0.002 ETH.

What is "block gas limit"?

The block gas limit is the maximum amount of gas allowed in a single block, which can be used to determine how many transactions can be packaged in a single block. For example, we have 5 transactions with gas limits of 10, 20, 30, 40, and 50. If the block gas limit is 100, then the first 4 transactions can be successfully packaged into this block. Miners have the power to decide which transactions are included in the block. So, another miner can choose to pack the last two transactions into this block (50+40), and then pack the first transaction (10). If you try to package a transaction that would use more than the current block gas limit, the transaction will be rejected by the network and your Ethereum client will report the error "Transaction exceeds block gas limit". The following example is from an Ethereum StackExhcange post.

The gas limit of the current block is  4,712,357 gas, the data is from ethstats.net , which means that about 224 transfer transactions (gas limit is 21000) can be packed into a block (block time fluctuates about 15-20 seconds ). This protocol allows miners of each block to adjust the block gas limit, plus or minus 1/2024 (0.0976%).

who decides

The gas limit of a block is determined by the miners on the network. Unlike the adjustable block gas limit protocol is a default mining strategy, that is, most clients default to a minimum block gas limit of 4,712,388.

How the block gas limit is changed

Miners on Ethereum need to use a mining software such as ethminer. It will connect to a geth or Parity Ethereum client. Both Geth and Pairty have options that allow miners to change the configuration. Here are the geth mining command line options along with Parity's options.

What is a "DoS" attack on the Ethereum network?

There have been recent comments that the Ethereum network is slowly slowing down, becoming congested or even unusable. The comments referred to this slowdown as a "DoS" attack on the Ethereum network. A so-called DoS situation occurs when the Ethereum network continues to see full blocks and a large number of transactions are pending on the network. At the same time, miners have the right to choose which transactions to package based on transaction fees. If there are thousands of transactions waiting to be packaged in the queue (in the transaction pool) at that time, it may cause an abnormal transaction delay of several hours. DDoS can be malicious or non-malicious.

Malicious DoS

Last fall, Ethereum was attacked by someone or some group, by making a lot of junk transactions. The attack is described in the following blog :

攻击者通过在他们的智能合约中反复的调用某些命令来让客户端难以处理这些计算,但是这些命令都只消耗少量的gas所以调用起来十分廉价。

In this attack, miners were asked to lower the gas limit to 1.5 million , which was changed to 2 million in another incident later . There have been several other incidents requiring miners to lower the block gas limit when the network is attacked.

non-malicious DoS

A non-malicious DoS is when the network is faced with a large number of transactions and it takes more time than usual to process a transaction. The Ethereum network has been filled with transactions many times recently due to the popularity of ICOs. Infura's friends wrote a technical analysis article about this .

Why is the block gas limit not automatically adjusted when the block is full?

The main reason: miners do not have the ability to dynamically adjust the gas limit.

There is a mechanism in the Ethereum protocol that allows miners to vote to determine the gas limit, so the block size can be adjusted without a hard fork. Originally, this mechanism was tied to another default policy, that is, miners voted to have a block gas limit of at least 4.7 million by default, and tended to be 1.5 times the gas usage of the last 1024 blocks. This allows the block size to automatically increase based on demand, while also having a limit that can be used to prevent spam.

As mentioned in the "Malicious DoS" section, there have been several times in history where miners have had to use non-default settings due to attacks to help reduce the impact of the attack. But the problem now is that the mining pool did not change the settings back to default after the attack. About a month ago, miners were asked to change the gas limit and gas price settings to add dynamic gas limit adjustment again . Because of the recent explosion of token sales, many blocks are filled and blockchain transactions are blocked.

ETH Gas Station is a website where people can check the latest block gas limit settings .

What do miners need to do to fix this?

Miners can change the settings in the Geth or Parity client to restart dynamic gas limit adjustment. Note: These settings were found on this Reddit thread, and can actually be set higher (see this thread).

Geth

Recommended settings

--gasprice 4000000000 --targetgaslimit 4712388

explain

--targetgaslimit Target gas limit sets the artificial target gas floor for the blocks to mine (default: “4712388”) --gasprice Minimal gas price to accept for mining a transactions (default: “20000000000”). Note: gasprice is listed in wei.

Parity

Recommended settings
--gas-floor-target 4712388 --gas-cap 9000000 --gasprice 4000000000

explain

--gas-floor-target Amount of gas per block to target when sealing a new block (default: 4700000).

--gas-cap A cap on how large we will raise the gas limit per block due to transaction volume (default: 6283184).

--gasprice Minimum amount of Wei per GAS to be paid for a transaction to be accepted for mining. Note: gasprice is listed in wei. Note 2: --gasprice is a “Legacy Option”

Other mining setup options

You can refer to the CLI options page to see how the miner can adjust the optimization settings.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325772346&siteId=291194637