consensus

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: GPL-3.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Hundred = uint64(100)
)

Variables

View Source
var (
	BigBalance = new(big.Int).Div(math.MaxBig256, big.NewInt(2))
)

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

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 https://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

func MigrationTx

func MigrationTx(
	signer types.Signer,
	header *types.Header,
	migration_file string,
	engine consensus.Engine,
) (res *types.Transaction)

func ValidateMigration

func ValidateMigration(
	block *types.Block,
	migration_file string,
) bool

Types

type AccountsFn

type AccountsFn func() []common.Address

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) Equal

func (cs ConsensusSigner) Equal(s2 types.Signer) bool

func (ConsensusSigner) Hash

func (ConsensusSigner) Sender

func (ConsensusSigner) SignatureValues

func (cs ConsensusSigner) SignatureValues(tx *types.Transaction, sig []byte) (r, s, v *big.Int, err error)

type DiffFn

type DiffFn func(uint64, *types.Header, *TimeTarget) *big.Int

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

func (e *Energi) Author(header *types.Header) (common.Address, error)

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

func (e *Energi) CalcDifficulty(
	chain ChainReader, time uint64, parent *types.Header,
) *big.Int

CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have.

func (*Energi) Close

func (e *Energi) Close() error

Close terminates any background threads maintained by the consensus engine.

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

func (e *Energi) GetMinerNonceCap() uint64

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) SealHash

func (e *Energi) SealHash(header *types.Header) (hash common.Hash)

SealHash returns the hash of a block prior to it being sealed.

func (*Energi) SetMinerCB

func (e *Energi) SetMinerCB(
	accountsFn AccountsFn,
	signerFn SignerFn,
	peerCountFn PeerCountFn,
	isMiningFn IsMiningFn,
)

func (*Energi) SetMinerNonceCap

func (e *Energi) SetMinerNonceCap(nonceCap uint64)

SetMinerNonceCap sets the nonce cap for the miner

func (*Energi) SignatureHash

func (e *Energi) SignatureHash(header *types.Header) (hash common.Hash)

SignatureHash generates the hash that will be used by the signer

func (*Energi) VerifyHeader

func (e *Energi) VerifyHeader(
	chain ChainReader, header *types.Header, seal bool,
) error

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 (a *EngineAPI) SetNonceCap(nonce *uint64) (oldNonce uint64)

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 KnownStakes = sync.Map

type PeerCountFn

type PeerCountFn func() int

type SignerFn

type SignerFn func(common.Address, []byte) ([]byte, error)

type StakingAccount

type StakingAccount struct {
	Account common.Address
	Weight  uint64
}

type StakingStatusInfo

type StakingStatusInfo struct {
	Hash        common.Hash
	Height      uint64
	Miner       bool
	NonceCap    uint64
	Staking     bool
	TotalWeight uint64
	Accounts    []StakingAccount
}

type TimeTarget

type TimeTarget struct {
	Drift, Integral, Derivative int64
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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