go-ethereum: github.com/ethereum/go-ethereum/les/utils Index | Files

package utils

import "github.com/ethereum/go-ethereum/les/utils"


Package Files

exec_queue.go expiredvalue.go timeutils.go weighted_select.go

type ExecQueue Uses

type ExecQueue struct {
    // contains filtered or unexported fields

ExecQueue implements a queue that executes function calls in a single thread, in the same order as they have been queued.

func NewExecQueue Uses

func NewExecQueue(capacity int) *ExecQueue

NewExecQueue creates a new execution Queue.

func (*ExecQueue) CanQueue Uses

func (q *ExecQueue) CanQueue() bool

CanQueue returns true if more function calls can be added to the execution Queue.

func (*ExecQueue) Clear Uses

func (q *ExecQueue) Clear()

Clear drops all queued functions.

func (*ExecQueue) Queue Uses

func (q *ExecQueue) Queue(f func()) bool

Queue adds a function call to the execution Queue. Returns true if successful.

func (*ExecQueue) Quit Uses

func (q *ExecQueue) Quit()

Quit stops the exec Queue.

Quit waits for the current execution to finish before returning.

type ExpirationFactor Uses

type ExpirationFactor struct {
    Exp    uint64
    Factor float64

ExpirationFactor is calculated from logOffset. 1 <= Factor < 2 and Factor*2^Exp describes the multiplier applicable for additions and the divider for readouts. If logOffset changes slowly then it saves some expensive operations to not calculate them for each addition and readout but cache this intermediate form for some time. It is also useful for structures where multiple values are expired with the same Expirer.

func ExpFactor Uses

func ExpFactor(logOffset Fixed64) ExpirationFactor

ExpFactor calculates ExpirationFactor based on logOffset

func (ExpirationFactor) Value Uses

func (e ExpirationFactor) Value(base float64, exp uint64) float64

Value calculates the expired value based on a floating point base and integer power-of-2 exponent. This function should be used by multi-value expired structures.

type ExpiredValue Uses

type ExpiredValue struct {
    Base, Exp uint64 // rlp encoding works by default

ExpiredValue is a scalar value that is continuously expired (decreased exponentially) based on the provided logarithmic expiration offset value.

The formula for value calculation is: base*2^(exp-logOffset). In order to simplify the calculation of ExpiredValue, its value is expressed in the form of an exponent with a base of 2.

Also here is a trick to reduce a lot of calculations. In theory, when a value X decays over time and then a new value Y is added, the final result should be X*2^(exp-logOffset)+Y. However it's very hard to represent in memory. So the trick is using the idea of inflation instead of exponential decay. At this moment the temporary value becomes: X*2^exp+Y*2^logOffset_1, apply the exponential decay when we actually want to calculate the value.

e.g. t0: V = 100 t1: add 30, inflationary value is: 100 + 30/0.3, 0.3 is the decay coefficient t2: get value, decay coefficient is 0.2 now, final result is: 200*0.2 = 40

func (*ExpiredValue) Add Uses

func (e *ExpiredValue) Add(amount int64, logOffset Fixed64) int64

add adds a signed value at the given moment

func (*ExpiredValue) AddExp Uses

func (e *ExpiredValue) AddExp(a ExpiredValue)

addExp adds another ExpiredValue

func (*ExpiredValue) IsZero Uses

func (e *ExpiredValue) IsZero() bool

IsZero returns true if the value is zero

func (*ExpiredValue) SubExp Uses

func (e *ExpiredValue) SubExp(a ExpiredValue)

subExp subtracts another ExpiredValue

func (ExpiredValue) Value Uses

func (e ExpiredValue) Value(logOffset Fixed64) uint64

value calculates the value at the given moment.

type Expirer Uses

type Expirer struct {
    // contains filtered or unexported fields

Expirer changes logOffset with a linear rate which can be changed during operation. It is not thread safe, if access by multiple goroutines is needed then it should be encapsulated into a locked structure. Note that if neither SetRate nor SetLogOffset are used during operation then LogOffset is thread safe.

func (*Expirer) LogOffset Uses

func (e *Expirer) LogOffset(now mclock.AbsTime) Fixed64

LogOffset returns the current logarithmic offset.

func (*Expirer) SetLogOffset Uses

func (e *Expirer) SetLogOffset(now mclock.AbsTime, logOffset Fixed64)

SetLogOffset sets logOffset instantly.

func (*Expirer) SetRate Uses

func (e *Expirer) SetRate(now mclock.AbsTime, rate float64)

SetRate changes the expiration rate which is the inverse of the time constant in nanoseconds.

type Fixed64 Uses

type Fixed64 int64

Fixed64 implements 64-bit fixed point arithmetic functions.

func Float64ToFixed64 Uses

func Float64ToFixed64(f float64) Fixed64

float64ToFixed64 converts float64 to Fixed64 format.

func Uint64ToFixed64 Uses

func Uint64ToFixed64(f uint64) Fixed64

Uint64ToFixed64 converts uint64 integer to Fixed64 format.

func (Fixed64) Fraction Uses

func (f64 Fixed64) Fraction() Fixed64

fraction returns the fractional part of a Fixed64 value.

func (Fixed64) Pow2 Uses

func (f64 Fixed64) Pow2() float64

pow2Fixed returns the base 2 power of the fixed point value.

func (Fixed64) ToUint64 Uses

func (f64 Fixed64) ToUint64() uint64

toUint64 converts Fixed64 format to uint64.

type LinearExpiredValue Uses

type LinearExpiredValue struct {
    Offset uint64         // The latest time offset
    Val    uint64         // The remaining value, can never be negative
    Rate   mclock.AbsTime `rlp:"-"` // Expiration rate(by nanosecond), will ignored by RLP

LinearExpiredValue is very similar with the expiredValue which the value will continuously expired. But the different part is it's expired linearly.

func (*LinearExpiredValue) Add Uses

func (e *LinearExpiredValue) Add(amount int64, now mclock.AbsTime) uint64

add adds a signed value at the given moment. This function always has the assumption that the given timestamp shouldn't less than the recorded one.

func (LinearExpiredValue) Value Uses

func (e LinearExpiredValue) Value(now mclock.AbsTime) uint64

value calculates the value at the given moment. This function always has the assumption that the given timestamp shouldn't less than the recorded one.

type UpdateTimer Uses

type UpdateTimer struct {
    // contains filtered or unexported fields

func NewUpdateTimer Uses

func NewUpdateTimer(clock mclock.Clock, threshold time.Duration) *UpdateTimer

func (*UpdateTimer) Update Uses

func (t *UpdateTimer) Update(callback func(diff time.Duration) bool) bool

func (*UpdateTimer) UpdateAt Uses

func (t *UpdateTimer) UpdateAt(at mclock.AbsTime, callback func(diff time.Duration) bool) bool

type ValueExpirer Uses

type ValueExpirer interface {
    SetRate(now mclock.AbsTime, rate float64)
    SetLogOffset(now mclock.AbsTime, logOffset Fixed64)
    LogOffset(now mclock.AbsTime) Fixed64

ValueExpirer controls value expiration rate

type WeightFn Uses

type WeightFn func(interface{}) uint64

type WeightedRandomSelect Uses

type WeightedRandomSelect struct {
    // contains filtered or unexported fields

WeightedRandomSelect is capable of weighted random selection from a set of items

func NewWeightedRandomSelect Uses

func NewWeightedRandomSelect(wfn WeightFn) *WeightedRandomSelect

NewWeightedRandomSelect returns a new WeightedRandomSelect structure

func (*WeightedRandomSelect) Choose Uses

func (w *WeightedRandomSelect) Choose() WrsItem

Choose randomly selects an item from the set, with a chance proportional to its current weight. If the weight of the chosen element has been decreased since the last stored value, returns it with a newWeight/oldWeight chance, otherwise just updates its weight and selects another one

func (*WeightedRandomSelect) IsEmpty Uses

func (w *WeightedRandomSelect) IsEmpty() bool

IsEmpty returns true if the set is empty

func (*WeightedRandomSelect) Remove Uses

func (w *WeightedRandomSelect) Remove(item WrsItem)

Remove removes an item from the set

func (*WeightedRandomSelect) Update Uses

func (w *WeightedRandomSelect) Update(item WrsItem)

Update updates an item's weight, adds it if it was non-existent or removes it if the new weight is zero. Note that explicitly updating decreasing weights is not necessary.

type WrsItem Uses

type WrsItem interface{}

Package utils imports 6 packages (graph) and is imported by 23 packages. Updated 2021-01-06. Refresh now. Tools for package owners.