valset

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxTotalVotingPower      = int64(math.MaxInt64) / 8
	PriorityWindowSizeFactor = 2
)

Variables

This section is empty.

Functions

func IsErrTooMuchChange

func IsErrTooMuchChange(err error) bool

func ValidatorListString

func ValidatorListString(vals []*Validator) string

ValidatorListString returns a prettified validator list for logging purposes.

Types

type InvalidStartEndBlockError

type InvalidStartEndBlockError struct {
	Start         uint64
	End           uint64
	CurrentHeader uint64
}

func (*InvalidStartEndBlockError) Error

func (e *InvalidStartEndBlockError) Error() string

type MinimalVal

type MinimalVal struct {
	ID          uint64         `json:"ID"`
	VotingPower uint64         `json:"power"` // TODO add 10^-18 here so that we dont overflow easily
	Signer      common.Address `json:"signer"`
}

MinimalVal is the minimal validator representation Used to send validator information to bor validator contract

func SortMinimalValByAddress

func SortMinimalValByAddress(a []MinimalVal) []MinimalVal

SortMinimalValByAddress sorts validators

func ValidatorsToMinimalValidators

func ValidatorsToMinimalValidators(vals []Validator) (minVals []MinimalVal)

ValidatorsToMinimalValidators converts array of validators to minimal validators

type TotalVotingPowerExceededError

type TotalVotingPowerExceededError struct {
	Sum        int64
	Validators []*Validator
}

TotalVotingPowerExceededError is returned when the maximum allowed total voting power is exceeded

func (*TotalVotingPowerExceededError) Error

type Validator

type Validator struct {
	ID               uint64         `json:"ID"`
	Address          common.Address `json:"signer"`
	VotingPower      int64          `json:"power"`
	ProposerPriority int64          `json:"accum"`
}

Validator represets Volatile state for each Validator

func NewValidator

func NewValidator(address common.Address, votingPower int64) *Validator

NewValidator creates new validator

func ParseValidators

func ParseValidators(validatorsBytes []byte) ([]*Validator, error)

ParseValidators returns validator set bytes

func (*Validator) Cmp

func (v *Validator) Cmp(other *Validator) *Validator

Cmp returns the one validator with a higher ProposerPriority. If ProposerPriority is same, it returns the validator with lexicographically smaller address

func (*Validator) Copy

func (v *Validator) Copy() *Validator

Copy creates a new copy of the validator so we can mutate ProposerPriority. Panics if the validator is nil.

func (*Validator) HeaderBytes

func (v *Validator) HeaderBytes() []byte

HeaderBytes return header bytes

func (*Validator) MinimalVal

func (v *Validator) MinimalVal() MinimalVal

MinimalVal returns block number of last validator update

func (*Validator) PowerBytes

func (v *Validator) PowerBytes() []byte

PowerBytes return power bytes

func (*Validator) String

func (v *Validator) String() string

type ValidatorSet

type ValidatorSet struct {
	// NOTE: persisted via reflect, must be exported.
	Validators []*Validator `json:"validators"`
	Proposer   *Validator   `json:"proposer"`
	// contains filtered or unexported fields
}

ValidatorSet represent a set of *Validator at a given height. The validators can be fetched by address or index. The index is in order of .Address, so the indices are fixed for all rounds of a given blockchain height - ie. the validators are sorted by their address. On the other hand, the .ProposerPriority of each validator and the designated .GetProposer() of a set changes every round, upon calling .IncrementProposerPriority(). NOTE: Not goroutine-safe. NOTE: All get/set to validators should copy the value for safety.

func NewValidatorSet

func NewValidatorSet(valz []*Validator) *ValidatorSet

NewValidatorSet initializes a ValidatorSet by copying over the values from `valz`, a list of Validators. If valz is nil or empty, the new ValidatorSet will have an empty list of Validators. The addresses of validators in `valz` must be unique otherwise the function panics.

func (*ValidatorSet) Copy

func (vals *ValidatorSet) Copy() *ValidatorSet

Copy each validator into a new ValidatorSet.

func (*ValidatorSet) CopyIncrementProposerPriority

func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet

Increment ProposerPriority and update the proposer on a copy, and return it.

func (*ValidatorSet) GetByAddress

func (vals *ValidatorSet) GetByAddress(address common.Address) (index int, val *Validator)

GetByAddress returns an index of the validator with address and validator itself if found. Otherwise, -1 and nil are returned.

func (*ValidatorSet) GetByIndex

func (vals *ValidatorSet) GetByIndex(index int) (address common.Address, val *Validator)

GetByIndex returns the validator's address and validator itself by index. It returns nil values if index is less than 0 or greater or equal to len(ValidatorSet.Validators).

func (*ValidatorSet) GetProposer

func (vals *ValidatorSet) GetProposer() (proposer *Validator)

GetProposer returns the current proposer. If the validator set is empty, nil is returned.

func (*ValidatorSet) HasAddress

func (vals *ValidatorSet) HasAddress(address common.Address) bool

HasAddress returns true if address given is in the validator set, false - otherwise.

func (*ValidatorSet) IncrementProposerPriority

func (vals *ValidatorSet) IncrementProposerPriority(times int)

IncrementProposerPriority increments ProposerPriority of each validator and updates the proposer. Panics if validator set is empty. `times` must be positive.

func (*ValidatorSet) IsNilOrEmpty

func (vals *ValidatorSet) IsNilOrEmpty() bool

Nil or empty validator sets are invalid.

func (*ValidatorSet) Iterate

func (vals *ValidatorSet) Iterate(fn func(index int, val *Validator) bool)

Iterate will run the given function over the set.

func (*ValidatorSet) RescalePriorities

func (vals *ValidatorSet) RescalePriorities(diffMax int64)

func (*ValidatorSet) Size

func (vals *ValidatorSet) Size() int

Size returns the length of the validator set.

func (*ValidatorSet) String

func (vals *ValidatorSet) String() string

func (*ValidatorSet) StringIndented

func (vals *ValidatorSet) StringIndented(indent string) string

func (*ValidatorSet) TotalVotingPower

func (vals *ValidatorSet) TotalVotingPower() int64

TotalVotingPower returns the sum of the voting powers of all validators. It recomputes the total voting power if required.

func (*ValidatorSet) UpdateTotalVotingPower

func (vals *ValidatorSet) UpdateTotalVotingPower() error

Force recalculation of the set's total voting power.

func (*ValidatorSet) UpdateValidatorMap

func (vals *ValidatorSet) UpdateValidatorMap()

func (*ValidatorSet) UpdateWithChangeSet

func (vals *ValidatorSet) UpdateWithChangeSet(changes []*Validator) error

UpdateWithChangeSet attempts to update the validator set with 'changes'. It performs the following steps:

  • validates the changes making sure there are no duplicates and splits them in updates and deletes
  • verifies that applying the changes will not result in errors
  • computes the total voting power BEFORE removals to ensure that in the next steps the priorities across old and newly added validators are fair
  • computes the priorities of new validators against the final set
  • applies the updates against the validator set
  • applies the removals against the validator set
  • performs scaling and centering of priority values

If an error is detected during verification steps, it is returned and the validator set is not changed.

type ValidatorsByAddress

type ValidatorsByAddress []*Validator

Sort validators by address.

func (ValidatorsByAddress) Len

func (valz ValidatorsByAddress) Len() int

func (ValidatorsByAddress) Less

func (valz ValidatorsByAddress) Less(i, j int) bool

func (ValidatorsByAddress) Swap

func (valz ValidatorsByAddress) Swap(i, j int)

Jump to

Keyboard shortcuts

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