Documentation ¶
Index ¶
- Constants
- Variables
- func CalcPoSDifficultyV2(newBlockTime uint64, parent *types.Header, timeTarget *TimeTarget, ...) *big.Int
- func CalculateBlockTimeDerivative(drift []int64) (derivative []int64)
- func CalculateBlockTimeDrift(ema []uint64, targetBlockGap uint64) (drift []int64)
- func CalculateBlockTimeEMA(blockTimeDifferences []uint64, emaPeriod uint64, targetBlockGap uint64) (ema []uint64)
- func CalculateBlockTimeIntegral(drift []int64) (integral int64)
- func MigrationTx(signer types.Signer, header *types.Header, migration_file string, ...) (res *types.Transaction)
- func ValidateMigration(block *types.Block, migration_file string) bool
- type AccountsFn
- type ChainReader
- type ConsensusSigner
- func (cs ConsensusSigner) Equal(s2 types.Signer) bool
- func (cs ConsensusSigner) Hash(tx *types.Transaction) common.Hash
- func (cs ConsensusSigner) Sender(tx *types.Transaction) (common.Address, error)
- func (cs ConsensusSigner) SignatureValues(tx *types.Transaction, sig []byte) (r, s, v *big.Int, err error)
- type DiffFn
- type Energi
- func (e *Energi) APIs(chain ChainReader) []rpc.API
- func (e *Energi) Author(header *types.Header) (common.Address, error)
- func (e *Energi) CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
- func (e *Energi) Close() error
- func (e *Energi) Finalize(chain ChainReader, header *types.Header, state *state.StateDB, ...) (*types.Block, []*types.Receipt, error)
- func (e *Energi) GetMinerNonceCap() uint64
- func (e *Energi) PoSPrepareV1(chain ChainReader, header *types.Header, parent *types.Header) (timeTarget *TimeTarget, err error)
- func (e *Energi) PoSPrepareV2(chain ChainReader, header *types.Header, parent *types.Header) (timeTarget *TimeTarget, err error)
- func (e *Energi) Prepare(chain ChainReader, header *types.Header) (err error)
- func (e *Energi) Seal(chain ChainReader, block *types.Block, ...) (err error)
- func (e *Energi) SealHash(header *types.Header) (hash common.Hash)
- func (e *Energi) SetMinerCB(accountsFn AccountsFn, signerFn SignerFn, peerCountFn PeerCountFn, ...)
- func (e *Energi) SetMinerNonceCap(nonceCap uint64)
- func (e *Energi) SignatureHash(header *types.Header) (hash common.Hash)
- func (e *Energi) VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
- func (e *Energi) VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error, chan<- bool)
- func (e *Energi) VerifySeal(chain ChainReader, header *types.Header) error
- func (e *Energi) VerifyUncles(chain ChainReader, block *types.Block) error
- type EngineAPI
- type IsMiningFn
- type KnownStakeKey
- type KnownStakeValue
- type KnownStakes
- type PeerCountFn
- type SignerFn
- type StakingAccount
- type StakingStatusInfo
- type TimeTarget
Constants ¶
const (
Hundred = uint64(100)
)
Variables ¶
Functions ¶
func CalcPoSDifficultyV2 ¶
func CalcPoSDifficultyV2( newBlockTime uint64, parent *types.Header, timeTarget *TimeTarget, testing bool, ) *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 https://en.wikipedia.org/wiki/PID_controller#Mathematical_form for more information.
func CalculateBlockTimeDerivative ¶
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 ¶
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 https://www.itl.nist.gov/div898/handbook/pmc/section4/pmc431.htm
func CalculateBlockTimeIntegral ¶
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
func MigrationTx ¶
Types ¶
type AccountsFn ¶
type ChainReader ¶
type ChainReader = eth_consensus.ChainReader
type ConsensusSigner ¶
type ConsensusSigner struct{}
NOTE: it MUST NOT for untrusted transactions
func NewConsensusSigner ¶
func NewConsensusSigner() *ConsensusSigner
func (ConsensusSigner) Hash ¶
func (cs ConsensusSigner) Hash(tx *types.Transaction) common.Hash
func (ConsensusSigner) Sender ¶
func (cs ConsensusSigner) Sender(tx *types.Transaction) (common.Address, error)
func (ConsensusSigner) SignatureValues ¶
func (cs ConsensusSigner) SignatureValues(tx *types.Transaction, sig []byte) (r, s, v *big.Int, err error)
type Energi ¶
type Energi struct {
// contains filtered or unexported fields
}
Energi is the state data for Energi Proof of Stake consensus
func New ¶
func New(config *params.EnergiConfig, db ethdb.Database) *Energi
New returns a newly initialized Energi state structure
func (*Energi) APIs ¶
func (e *Energi) APIs(chain ChainReader) []rpc.API
APIs returns the RPC APIs this consensus engine provides.
func (*Energi) Author ¶
Author retrieves the Ethereum address of the account that minted the given block, which may be different from the header's coinbase if a consensus engine is based on signatures.
func (*Energi) CalcDifficulty ¶
CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have.
func (*Energi) Finalize ¶
func (e *Energi) Finalize( chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, ) (*types.Block, []*types.Receipt, error)
Finalize runs any post-transaction state modifications (e.g., block rewards), and assembles the final block.
Note: The block header and state database might be updated to reflect any consensus rules that happen at finalization (e.g. block rewards).
func (*Energi) GetMinerNonceCap ¶
GetMinerNonceCap returns the currently set nonce cap for the miner
func (*Energi) PoSPrepareV1 ¶
func (e *Energi) PoSPrepareV1( chain ChainReader, header *types.Header, parent *types.Header, ) (timeTarget *TimeTarget, err error)
PoSPrepare generates a time target for a PoS mining round
func (*Energi) PoSPrepareV2 ¶
func (e *Energi) PoSPrepareV2( chain ChainReader, header *types.Header, parent *types.Header, ) (timeTarget *TimeTarget, err error)
posPrepareV2 version 2
func (*Energi) Prepare ¶
func (e *Energi) Prepare(chain ChainReader, header *types.Header) (err error)
Prepare initializes the consensus fields of a block header according to the rules of a particular engine. The changes are executed inline.
func (*Energi) Seal ¶
func (e *Energi) Seal( chain ChainReader, block *types.Block, results chan<- *eth_consensus.SealResult, stop <-chan struct{}, ) (err error)
Seal generates a new sealing request for the given input block and pushes the result into the given channel.
Note, the method returns immediately and will send the result async. More than one result may also be returned depending on the consensus algorithm.
func (*Energi) SetMinerCB ¶
func (e *Energi) SetMinerCB( accountsFn AccountsFn, signerFn SignerFn, peerCountFn PeerCountFn, isMiningFn IsMiningFn, )
func (*Energi) SetMinerNonceCap ¶
SetMinerNonceCap sets the nonce cap for the miner
func (*Energi) SignatureHash ¶
SignatureHash generates the hash that will be used by the signer
func (*Energi) VerifyHeader ¶
VerifyHeader checks whether a header conforms to the consensus rules of a given engine. Verifying the seal may be done optionally here, or explicitly via the VerifySeal method.
func (*Energi) VerifyHeaders ¶
func (e *Energi) VerifyHeaders( chain ChainReader, headers []*types.Header, seals []bool, ) ( chan<- struct{}, <-chan error, chan<- bool, )
VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers concurrently. The method returns a quit channel to abort the operations and a results channel to retrieve the async verifications (the order is that of the input slice).
func (*Energi) VerifySeal ¶
func (e *Energi) VerifySeal(chain ChainReader, header *types.Header) error
VerifySeal checks whether the crypto seal on a header is valid according to the consensus rules of the given engine.
func (*Energi) VerifyUncles ¶
func (e *Energi) VerifyUncles(chain ChainReader, block *types.Block) error
VerifyUncles verifies that the given block's uncles conform to the consensus rules of a given engine.
type EngineAPI ¶
type EngineAPI struct {
// contains filtered or unexported fields
}
func NewEngineAPI ¶
func NewEngineAPI(chain ChainReader, engine *Energi) *EngineAPI
func (*EngineAPI) SetNonceCap ¶
func (*EngineAPI) StakingStatus ¶
func (a *EngineAPI) StakingStatus() *StakingStatusInfo
type IsMiningFn ¶
type IsMiningFn func() bool
type KnownStakeKey ¶
type KnownStakeKey struct {
// contains filtered or unexported fields
}
type KnownStakeValue ¶
type KnownStakeValue struct {
// contains filtered or unexported fields
}
type KnownStakes ¶
type PeerCountFn ¶
type PeerCountFn func() int
type StakingAccount ¶
type StakingStatusInfo ¶
type TimeTarget ¶
type TimeTarget struct {
Drift, Integral, Derivative int64
// contains filtered or unexported fields
}