Documentation ¶
Overview ¶
The gas package implements dynamic gas pricing specified in ACP-103: https://github.com/avalanche-foundation/ACPs/tree/main/ACPs/103-dynamic-fees
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInsufficientCapacity = errors.New("insufficient capacity")
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Weights to merge fee dimensions into a single gas value. Weights Dimensions `json:"weights"` // Maximum amount of gas the chain is allowed to store for future use. MaxCapacity Gas `json:"maxCapacity"` // Maximum amount of gas the chain is allowed to consume per second. MaxPerSecond Gas `json:"maxPerSecond"` // Target amount of gas the chain should consume per second to keep the fees // stable. TargetPerSecond Gas `json:"targetPerSecond"` // Minimum price per unit of gas. MinPrice Price `json:"minPrice"` // Constant used to convert excess gas to a gas price. ExcessConversionConstant Gas `json:"excessConversionConstant"` }
type Dimensions ¶
type Dimensions [NumDimensions]uint64
func (Dimensions) Add ¶
func (d Dimensions) Add(os ...*Dimensions) (Dimensions, error)
Add returns d + sum(os...).
If overflow occurs, an error is returned.
func (Dimensions) Sub ¶
func (d Dimensions) Sub(os ...*Dimensions) (Dimensions, error)
Sub returns d - sum(os...).
If underflow occurs, an error is returned.
func (Dimensions) ToGas ¶
func (d Dimensions) ToGas(weights Dimensions) (Gas, error)
ToGas returns d · weights.
If overflow occurs, an error is returned.
type Gas ¶
type Gas uint64
func (Gas) AddPerSecond ¶
AddPerSecond returns g + gasPerSecond * seconds.
If overflow would occur, MaxUint64 is returned.
type Price ¶
type Price uint64
func CalculatePrice ¶
CalculatePrice returns the gas price given the minimum gas price, the excess gas, and the excess conversion constant.
It is defined as an approximation of:
minPrice * e^(excess / excessConversionConstant)
This implements the EIP-4844 fake exponential formula:
def fake_exponential(factor: int, numerator: int, denominator: int) -> int: i = 1 output = 0 numerator_accum = factor * denominator while numerator_accum > 0: output += numerator_accum numerator_accum = (numerator_accum * numerator) // (denominator * i) i += 1 return output // denominator
This implementation is optimized with the knowledge that any value greater than MaxUint64 gets returned as MaxUint64. This means that every intermediate value is guaranteed to be at most MaxUint193. So, we can safely use uint256.Int.
This function does not perform any memory allocations.
type State ¶
type State struct { Capacity Gas `serialize:"true" json:"capacity"` Excess Gas `serialize:"true" json:"excess"` }
func (State) AdvanceTime ¶
func (s State) AdvanceTime( maxCapacity Gas, maxPerSecond Gas, targetPerSecond Gas, duration uint64, ) State
AdvanceTime adds maxPerSecond to capacity and subtracts targetPerSecond from excess over the provided duration.
Capacity is capped at maxCapacity. Excess to be removed is capped at excess.