Transactions and transaction fees in Ethereum

trade

A transaction is a message sent from one account to another (possibly the same account or zero account, see below). Transactions can contain binary data (payload) and ether.

If the target account contains code, that code will execute, and the payload is the input data.

If the target account is a zero account (the account address is 0), the transaction will create a new contract. As mentioned above, this contract address is not a zero address, but is calculated from the contract creator's address and the number of transactions (called a nonce) sent by that address. The payload that creates the contract transaction is executed as EVM bytecode. The output of the execution is permanently stored as contract code. This means that in order to create a contract, you don't need to send the actual contract code to the contract, but code that returns the real code.

Oil: Gas

Every transaction on Ethereum is charged a certain amount of gas, and the purpose of gas is to limit the amount of work required to execute a transaction, while paying a fee for execution. When the EVM executes a transaction, gas will be gradually consumed according to certain rules.

Oil price: gas price

The gas price (in ether) is set by the transaction creator, and the sending account needs to prepay the transaction fee = gas price * gas amount. If there is gas remaining at the end of the execution, the gas will be returned to the sending account.

No matter where the execution is, once the gas is exhausted (for example, it falls to a negative value), an out-of-gas exception will be triggered. All state modifications made by the current calling frame will be rolled back.

Transaction fee cap: gas limit

Every transaction is required to include a gas limit (sometimes called startGas) and a fee that the 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.

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.

Block transaction fee cap: 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 current block gas limit is 4,712,357 gas, the data is from ethstats.net, which means that about 224 transfer transactions (gas limit 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%).

Best Practices

The best way to help understand these concepts is through practice. I recommend two excellent online tutorials from Huizhi.com:

Guess you like

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