calculus

package
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcPoSDifficultyV1

func CalcPoSDifficultyV1(
	newBlockTime uint64,
	diff uint64,
	timeTarget *TimeTarget,
) uint64

func CalcPoSDifficultyV2

func CalcPoSDifficultyV2(
	newBlockTime uint64,
	diff uint64,
	timeTarget *TimeTarget,
) *big.Int

CalcPoSDifficultyV2 is our v2 difficulty algorithm this algorithm is a PID controlled difficulty first we take an Exponential Moving Average of the last 60 elapsed block times. EMA was chosen because it favors more recent block times, and so should be more responsive. Then we compute the drift, which is the difference between EMA block time, and the target time of 60 seconds. Finally, we take the integral and derivative of the drift. This gives us 3 terms for PID control: proportional (drift) integral derivative

A PID controller is an excellent way to remove oscillation when approaching a target value. To describe the difficulty algorithm as a PID controller we need a set point, a process variable, and a control variable.

The set point is our 60 second block time. Block time EMA is our process variable. The difficulty itself is the control variable. We calculate a new difficulty as a weighted sum of the difference between the set point and process variable, the integral of this difference, and the derivative of this difference.

The proportional term accounts for current error in block time. The integral term accounts for past error in block time. The derivative term accounts for future error in block time. By carefully weighting these 3, we can quickly approach the set point without much oscillation.

The PID control implemented here is generally called the "standard form" which has only a single gain, and the derivative and integral terms are scaled by time.

See htimeTargetps://en.wikipedia.org/wiki/PID_controller#Mathematical_form for more information.

func CalcPoSHash

func CalcPoSHash(
	time uint64,
	coinbase string,
	target *big.Int,
) (usedWeight uint64)

CalcPoSHash Implements hash consensus

func CalculateBlockTimeDerivative

func CalculateBlockTimeDerivative(drift []int64) (derivative []int64)

CalculateBlockTimeDerivative computes the derivative series of a data series Here we use the central difference formula, for some small step h (each block) f'(x) = 1/2h * (f(x+h) - f(x-h))

func CalculateBlockTimeDrift

func CalculateBlockTimeDrift(ema []uint64, TargetBlockGap uint64) (drift []int64)

CalculateBlockTimeDrift calculates the difference between the target block time and the EMA block time. Drift should be a negative value if blocks are too slow and a positive value if blocks are too fast, representing the direction to adjust the difficulty

func CalculateBlockTimeEMA

func CalculateBlockTimeEMA(blockTimeDifferences []uint64, emaPeriod uint64, TargetBlockGap uint64) (ema []uint64)

CalculateBlockTimeEMA computes the exponential moving average of block times this will return the EMA of block times as microseconds for a description of the EMA algorithm, please see: see htimeTargetps://www.itl.nist.gov/div898/handbook/pmc/section4/pmc431.htm

func CalculateBlockTimeIntegral

func CalculateBlockTimeIntegral(drift []int64) (integral int64)

CalculateBlockTimeIntegral calculates the integral of the block drift function This provides us with some idea fo historical "error", how far the block time has been from the target value for the duration of the period We use the trapezoidal rule here for integration

Types

type TimeTarget

type TimeTarget struct {
	Min, Max, BlockTarget, PeriodTarget uint64
	Drift, Integral, Derivative         int64
}

TimeTarget used for next block calculation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL