current position:Home>Ethereum Gas Fee Pricing Mechanism

Ethereum Gas Fee Pricing Mechanism

2022-09-22 02:14:13Bitstar

1 The role of Ethereum fuel

        Ethereum gas plays an important role in the transaction process.From the perspective of the Ethereum system,Increase the cost of malicious transactions through gas fees and reduce the attacker's attack revenue,Enhanced security of Ethereum transactions,It also prompts contract developers to optimize the design to reduce the consumption of Ethereum computing power by the contract;From the point of view of the transaction originator,By setting fuel-related parameters,Complete the transaction for as little cost as possible,And through the setting of the fuel cost cap,Prevents attacks caused by contract vulnerabilities or invocation of insecure contracts.

2 The composition of Ethereum fuel

        Gas fee for Ethereum transactionsGasFeeis the price of fuelGaspricewith actual use of fuelGasused的乘积,即GasFee = Gasprice × Gasused,When the actual gas consumption is greater than the gas limit preset by the transaction initiator in the transactionGaslimit时,All state modifications made by the current exchange will be rolled back,But fuel costs will not be refunded.

3 Flaws in the Ethereum gas pricing mechanism

        Different fuel pricing mechanisms were used before and after the London fork.

        Before the London Fork, the gas price was preset by the transaction initiator based on the statistical value of historical transactions,Miners choose the transaction package with the highest bid.This auction-like mechanism for generating fuel prices results in four inefficiencies:

        1)Fluctuations in transaction fee levels do not match the social cost of transactions.The gas price set by the transaction initiator usually causes the change in transaction fee to be much larger than the change in transaction cost.

        2)Unnecessary user transaction delays.A hard limit on the total gas per block and natural fluctuations in the number of transactions,As a result, transactions often need to wait for several blocks to be packaged.

        3)Inefficient first bid.In order to improve the probability of transaction success, the transaction initiator,Higher transaction fees will be set,Causes recurring overpayment of transaction fees.

        4)The blockchain becomes unstable when there is no block reward.Miners through mining“sister block”的方式“偷取”Transaction fees lead to instability in the blockchain.

4 Optimization of the London fork fuel pricing mechanism

4.1 New pricing mechanism

        To address the shortcomings of the Ethereum gas pricing mechanism,EIP-1559An optimization of the fuel cost generation mechanism is proposed,EIP-1559is the main change introduced by the London fork.

        EIP-1559Introduced maximum tip per fuelmaxPriorityFeePerGas、Maximum cost per fuelmaxFeePerGas、Base cost per fuelbaseFeePerGasthese new parameters.

        maxPriorityFeePerGasRepresents the maximum tip paid to miners per fuel.

        maxFeePerGasRepresents the maximum fee paid to miners per share of fuel,包含baseFeePerGas和maxPriorityFeePerGas.

        baseFeePerGasRepresents the base cost per fuel,由系统自动生成,And will be burned by the Ethereum system.

        The formula for calculating the gas fee consumed by the transaction is:

        GasFee = Gasused × (maxFeePerGas) = Gasused × (baseFeePerGas + maxPriorityFeePerGas).

4.2 Algorithm for base fuel cost

        go-ethereum的eip1559.goImplemented the algorithm for the base fuel cost,算法总结如下:

        The initial base gas fee after the London forkInitialBaseFee是1Gwei,Base Fee Variation FactorBaseFeeChangeDenominatorDefines the boundary of the value of the base fee change between blocks,值为8,Elastic multiplierElasticityMultiplierDefines the maximum multiplier bound for the fuel cap,值为2;

        The gas target value of the parent blockparentGasTarget = parent.GasLimit / ElasticityMultiplier;

        When the actual gas value of the parent blockparent.GasUsedEqual to the parent block's gas target valueparentGasTarget时,The base fee of the current block is equal to the base fee of the parent blockparent.BaseFee;

        When the actual gas value of the parent blockparent.GasUsedGreater than the parent block's gas target valueparentGasTarget时,Indicates that the base fee for the current block should be increased,In order to reduce the amount of calculation of the transactions included in the block,The added value of the base fee is taken(1,parent.BaseFee * gasUsedDelta / parentGasTarget / BaseFeeChangeDenominator)的最大值;

        When the actual gas value of the parent blockparent.GasUsedLess than the parent block's gas target valueparentGasTarget时,Indicates that the base fee for the current block should be reduced,to increase the amount of computation of transactions included in blocks,The reduced value of the base fee is taken(0,parent.BaseFee * gasUsedDelta / parentGasTarget / BaseFeeChangeDenominator)的最大值.

        The code to implement the basic gas fee algorithm is as follows:

// CalcBaseFee calculates the basefee of the header.
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
	// If the current block is the first EIP-1559 block, return the InitialBaseFee.
	if !config.IsLondon(parent.Number) {
		return new(big.Int).SetUint64(params.InitialBaseFee)
	}

	parentGasTarget := parent.GasLimit / params.ElasticityMultiplier
	// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
	if parent.GasUsed == parentGasTarget {
		return new(big.Int).Set(parent.BaseFee)
	}

	var (
		num   = new(big.Int)
		denom = new(big.Int)
	)

	if parent.GasUsed > parentGasTarget {
		// If the parent block used more gas than its target, the baseFee should increase.
		// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
		num.SetUint64(parent.GasUsed - parentGasTarget)
		num.Mul(num, parent.BaseFee)
		num.Div(num, denom.SetUint64(parentGasTarget))
		num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator))
		baseFeeDelta := math.BigMax(num, common.Big1)

		return num.Add(parent.BaseFee, baseFeeDelta)
	} else {
		// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
		// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator)
		num.SetUint64(parentGasTarget - parent.GasUsed)
		num.Mul(num, parent.BaseFee)
		num.Div(num, denom.SetUint64(parentGasTarget))
		num.Div(num, denom.SetUint64(params.BaseFeeChangeDenominator))
		baseFee := num.Sub(parent.BaseFee, num)

		return math.BigMax(baseFee, common.Big0)
	}
}

5 Ethereum gas price query

        在网站中https://ethereumprice.org/gas/The average fuel price of the last week can be queried.

 参考

[1] 以太坊开发文档, Gas and fees | ethereum.org

[2] 以太坊改进提议EIP-1559, https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md

[3] Open source Ethereum clientgo-ethereum源代码eip1559.go,go-ethereum/eip1559.go at master · ethereum/go-ethereum · GitHub

[4] Ethereum gas cost query website,Ethereum Gas Price Charts & Historical Gas Fees – ethereumprice

copyright notice
author[Bitstar],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/265/202209220155471186.html

Random recommended