state

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MajoritySignRatioNumerator defines the ratio numerator to achieve
	// majority signatures.
	MajoritySignRatioNumerator = float64(2)

	// MajoritySignRatioDenominator defines the ratio denominator to achieve
	// majority signatures.
	MajoritySignRatioDenominator = float64(3)

	// MaxNormalInactiveChangesCount defines the max count arbitrators can
	// change when more than 1/3 arbiters don't sign cause to confirm fail
	MaxNormalInactiveChangesCount = 3

	// MaxSnapshotLength defines the max length the snapshot map should take
	MaxSnapshotLength = 20

	// CheckPointInterval defines interval height between two neighbor check
	// points
	CheckPointInterval = uint32(720)
)
View Source
const (
	DSNormal       degradationState = 0x00
	DSUnderstaffed degradationState = 0x01
	DSInactive     degradationState = 0x02
)
View Source
const (

	// ActivateDuration is about how long we should activate from pending or
	// inactive state
	ActivateDuration = 6
)

Variables

View Source
var (
	ErrInsufficientProducer = errors.New("producers count less than min arbitrators count")
)

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.

func NewArbitrators

func NewArbitrators(chainParams *config.Params,
	store IArbitratorsRecord,
	bestHeight func() uint32,
	bestBlock func() (*types.Block, error),
	getBlockByHeight func(uint32) (*types.Block, error)) (*arbitrators, error)

func UseLogger

func UseLogger(logger elalog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using elalog.

Types

type Arbitrators

type Arbitrators interface {
	Start()
	RecoverFromCheckPoints(height uint32) (uint32, error)
	CheckDPOSIllegalTx(block *types.Block) error
	ProcessBlock(block *types.Block, confirm *payload.Confirm)
	ProcessSpecialTxPayload(p types.Payload, height uint32) error
	RollbackTo(height uint32) error

	IsArbitrator(pk []byte) bool
	GetArbitrators() [][]byte
	GetCandidates() [][]byte
	GetNextArbitrators() [][]byte
	GetNextCandidates() [][]byte
	GetNeedConnectArbiters() []peer.PID
	GetDutyIndexByHeight(height uint32) int
	GetDutyIndex() int

	GetCurrentRewardData() RewardData
	GetNextRewardData() RewardData
	GetArbitersRoundReward() map[common.Uint168]common.Fixed64
	GetFinalRoundChange() common.Fixed64
	IsInactiveMode() bool
	IsUnderstaffedMode() bool

	GetCRCArbiters() [][]byte
	GetCRCProducer(publicKey []byte) *Producer
	GetCRCArbitrators() map[string]*Producer
	IsCRCArbitrator(pk []byte) bool
	IsActiveProducer(pk []byte) bool
	IsDisabledProducer(pk []byte) bool

	GetOnDutyArbitrator() []byte
	GetNextOnDutyArbitrator(offset uint32) []byte

	GetOnDutyCrossChainArbitrator() []byte
	GetCrossChainArbiters() [][]byte
	GetCrossChainArbitersCount() int
	GetCrossChainArbitersMajorityCount() int

	GetArbitersCount() int
	GetCRCArbitersCount() int
	GetArbitersMajorityCount() int
	HasArbitersMajorityCount(num int) bool
	HasArbitersMinorityCount(num int) bool

	GetSnapshot(height uint32) []*KeyFrame
	DumpInfo(height uint32)
}

type ArbitratorsMock

type ArbitratorsMock struct {
	CurrentArbitrators          [][]byte
	CRCArbitrators              [][]byte
	CurrentCandidates           [][]byte
	NextArbitrators             [][]byte
	NextCandidates              [][]byte
	CurrentOwnerProgramHashes   []*common.Uint168
	CandidateOwnerProgramHashes []*common.Uint168
	ArbitersRoundReward         map[common.Uint168]common.Fixed64
	OwnerVotesInRound           map[common.Uint168]common.Fixed64
	CRCArbitratorsMap           map[string]*Producer
	TotalVotesInRound           common.Fixed64
	DutyChangedCount            int
	MajorityCount               int
	FinalRoundChange            common.Fixed64
	InactiveMode                bool
	ActiveProducer              [][]byte
	Snapshot                    []*KeyFrame
	CurrentReward               RewardData
	NextReward                  RewardData
}

mock object of arbitrators

func NewArbitratorsMock

func NewArbitratorsMock(arbitersByte [][]byte, changeCount, majorityCount int) *ArbitratorsMock

func (*ArbitratorsMock) CheckDPOSIllegalTx added in v0.3.2

func (a *ArbitratorsMock) CheckDPOSIllegalTx(block *types.Block) error

func (*ArbitratorsMock) DumpInfo

func (a *ArbitratorsMock) DumpInfo(height uint32)

func (*ArbitratorsMock) GetArbitersCount

func (a *ArbitratorsMock) GetArbitersCount() int

func (*ArbitratorsMock) GetArbitersMajorityCount

func (a *ArbitratorsMock) GetArbitersMajorityCount() int

func (*ArbitratorsMock) GetArbitersRoundReward added in v0.3.2

func (a *ArbitratorsMock) GetArbitersRoundReward() map[common.Uint168]common.Fixed64

func (*ArbitratorsMock) GetArbitrators

func (a *ArbitratorsMock) GetArbitrators() [][]byte

func (*ArbitratorsMock) GetCRCArbiters added in v0.3.2

func (a *ArbitratorsMock) GetCRCArbiters() [][]byte

func (*ArbitratorsMock) GetCRCArbitersCount added in v0.3.2

func (a *ArbitratorsMock) GetCRCArbitersCount() int

func (*ArbitratorsMock) GetCRCArbitrators

func (a *ArbitratorsMock) GetCRCArbitrators() map[string]*Producer

func (*ArbitratorsMock) GetCRCProducer

func (a *ArbitratorsMock) GetCRCProducer(publicKey []byte) *Producer

func (*ArbitratorsMock) GetCandidates

func (a *ArbitratorsMock) GetCandidates() [][]byte

func (*ArbitratorsMock) GetCrossChainArbiters added in v0.3.2

func (a *ArbitratorsMock) GetCrossChainArbiters() [][]byte

func (*ArbitratorsMock) GetCrossChainArbitersCount added in v0.3.2

func (a *ArbitratorsMock) GetCrossChainArbitersCount() int

func (*ArbitratorsMock) GetCrossChainArbitersMajorityCount added in v0.3.2

func (a *ArbitratorsMock) GetCrossChainArbitersMajorityCount() int

func (*ArbitratorsMock) GetCurrentRewardData added in v0.3.2

func (a *ArbitratorsMock) GetCurrentRewardData() RewardData

func (*ArbitratorsMock) GetDutyChangeCount

func (a *ArbitratorsMock) GetDutyChangeCount() int

func (*ArbitratorsMock) GetDutyChangedCount

func (a *ArbitratorsMock) GetDutyChangedCount() int

func (*ArbitratorsMock) GetDutyIndex

func (a *ArbitratorsMock) GetDutyIndex() int

func (*ArbitratorsMock) GetDutyIndexByHeight

func (a *ArbitratorsMock) GetDutyIndexByHeight(uint32) int

func (*ArbitratorsMock) GetFinalRoundChange added in v0.3.2

func (a *ArbitratorsMock) GetFinalRoundChange() common.Fixed64

func (*ArbitratorsMock) GetLastConfirmedBlockTimeStamp

func (a *ArbitratorsMock) GetLastConfirmedBlockTimeStamp() uint32

func (*ArbitratorsMock) GetNeedConnectArbiters

func (a *ArbitratorsMock) GetNeedConnectArbiters() []peer.PID

func (*ArbitratorsMock) GetNextArbitrators

func (a *ArbitratorsMock) GetNextArbitrators() [][]byte

func (*ArbitratorsMock) GetNextCandidates

func (a *ArbitratorsMock) GetNextCandidates() [][]byte

func (*ArbitratorsMock) GetNextOnDutyArbitrator

func (a *ArbitratorsMock) GetNextOnDutyArbitrator(offset uint32) []byte

func (*ArbitratorsMock) GetNextRewardData added in v0.3.2

func (a *ArbitratorsMock) GetNextRewardData() RewardData

func (*ArbitratorsMock) GetNormalArbitrators

func (a *ArbitratorsMock) GetNormalArbitrators() ([][]byte, error)

func (*ArbitratorsMock) GetOnDutyArbitrator

func (a *ArbitratorsMock) GetOnDutyArbitrator() []byte

func (*ArbitratorsMock) GetOnDutyCrossChainArbitrator added in v0.3.2

func (a *ArbitratorsMock) GetOnDutyCrossChainArbitrator() []byte

func (*ArbitratorsMock) GetSnapshot added in v0.3.2

func (a *ArbitratorsMock) GetSnapshot(height uint32) []*KeyFrame

func (*ArbitratorsMock) HasArbitersMajorityCount

func (a *ArbitratorsMock) HasArbitersMajorityCount(num int) bool

func (*ArbitratorsMock) HasArbitersMinorityCount

func (a *ArbitratorsMock) HasArbitersMinorityCount(num int) bool

func (*ArbitratorsMock) IsActiveProducer added in v0.3.2

func (a *ArbitratorsMock) IsActiveProducer(pk []byte) bool

func (*ArbitratorsMock) IsArbitrator

func (a *ArbitratorsMock) IsArbitrator(pk []byte) bool

func (*ArbitratorsMock) IsCRCArbitrator

func (a *ArbitratorsMock) IsCRCArbitrator(pk []byte) bool

func (*ArbitratorsMock) IsDisabledProducer added in v0.3.2

func (a *ArbitratorsMock) IsDisabledProducer(pk []byte) bool

func (*ArbitratorsMock) IsInactiveMode added in v0.3.2

func (a *ArbitratorsMock) IsInactiveMode() bool

func (*ArbitratorsMock) IsUnderstaffedMode added in v0.3.2

func (a *ArbitratorsMock) IsUnderstaffedMode() bool

func (*ArbitratorsMock) ProcessBlock

func (a *ArbitratorsMock) ProcessBlock(block *types.Block, confirm *payload.Confirm)

func (*ArbitratorsMock) ProcessSpecialTxPayload

func (a *ArbitratorsMock) ProcessSpecialTxPayload(p types.Payload, height uint32) error

func (*ArbitratorsMock) RecoverFromCheckPoints added in v0.3.2

func (a *ArbitratorsMock) RecoverFromCheckPoints(height uint32) (uint32, error)

func (*ArbitratorsMock) RollbackTo

func (a *ArbitratorsMock) RollbackTo(height uint32) error

func (*ArbitratorsMock) SaveCheckPoint added in v0.3.2

func (a *ArbitratorsMock) SaveCheckPoint(height uint32) error

func (*ArbitratorsMock) SetArbitrators

func (a *ArbitratorsMock) SetArbitrators(ar [][]byte)

func (*ArbitratorsMock) SetCandidates

func (a *ArbitratorsMock) SetCandidates(ca [][]byte)

func (*ArbitratorsMock) SetDutyChangeCount

func (a *ArbitratorsMock) SetDutyChangeCount(count int)

func (*ArbitratorsMock) SetDutyChangedCount

func (a *ArbitratorsMock) SetDutyChangedCount(count int)

func (*ArbitratorsMock) SetNextArbitrators

func (a *ArbitratorsMock) SetNextArbitrators(ar [][]byte)

func (*ArbitratorsMock) SetNextCandidates

func (a *ArbitratorsMock) SetNextCandidates(ca [][]byte)

func (*ArbitratorsMock) Start added in v0.3.2

func (a *ArbitratorsMock) Start()

func (*ArbitratorsMock) TryEnterEmergency

func (a *ArbitratorsMock) TryEnterEmergency(blockTime uint32) bool

type ChangeType

type ChangeType byte

type CheckPoint added in v0.3.2

type CheckPoint struct {
	KeyFrame
	StateKeyFrame
	Height            uint32
	DutyIndex         int
	NextArbitrators   [][]byte
	NextCandidates    [][]byte
	CurrentCandidates [][]byte
	CurrentReward     RewardData
	NextReward        RewardData
}

CheckPoint defines all variables need record in database

type IArbitratorsRecord added in v0.3.2

type IArbitratorsRecord interface {
	GetHeightsDesc() ([]uint32, error)
	GetCheckPoint(height uint32) (*CheckPoint, error)
	SaveArbitersState(point *CheckPoint) error
}

type KeyFrame added in v0.3.2

type KeyFrame struct {
	CurrentArbitrators [][]byte
}

KeyFrame holds necessary state about arbitrators

type Producer

type Producer struct {
	// contains filtered or unexported fields
}

Producer holds a producer's info. It provides read only methods to access producer's info.

func (*Producer) ActivateRequestHeight added in v0.3.2

func (p *Producer) ActivateRequestHeight() uint32

func (*Producer) CancelHeight

func (p *Producer) CancelHeight() uint32

CancelHeight returns the height when the producer was canceled.

func (*Producer) Deserialize added in v0.3.2

func (p *Producer) Deserialize(r io.Reader) (err error)

func (*Producer) IllegalHeight

func (p *Producer) IllegalHeight() uint32

func (*Producer) InactiveSince

func (p *Producer) InactiveSince() uint32

func (*Producer) Info

func (p *Producer) Info() payload.ProducerInfo

Info returns a copy of the origin registered producer info.

func (*Producer) NodePublicKey

func (p *Producer) NodePublicKey() []byte

func (*Producer) OwnerPublicKey

func (p *Producer) OwnerPublicKey() []byte

func (*Producer) Penalty

func (p *Producer) Penalty() common.Fixed64

func (*Producer) RegisterHeight

func (p *Producer) RegisterHeight() uint32

RegisterHeight returns the height when the producer was registered.

func (*Producer) Serialize added in v0.3.2

func (p *Producer) Serialize(w io.Writer) error

func (*Producer) State

func (p *Producer) State() ProducerState

State returns the producer's state, can be pending, active or canceled.

func (*Producer) Votes

func (p *Producer) Votes() common.Fixed64

Votes returns the votes of the producer.

type ProducerState

type ProducerState byte

ProducerState represents the state of a producer.

const (
	// Pending indicates the producer is just registered and didn't get 6
	// confirmations yet.
	Pending ProducerState = iota

	// Active indicates the producer is registered and confirmed by more than
	// 6 blocks.
	Active

	// Inactive indicates the producer has been inactivated for a period which shall
	// be punished and will be activated later.
	Inactive

	// Canceled indicates the producer was canceled.
	Canceled

	// Illegal indicates the producer was found to break the consensus.
	Illegal

	// Returned indicates the producer has canceled and deposit returned.
	Returned
)

func (ProducerState) String

func (ps ProducerState) String() string

type RewardData added in v0.3.2

type RewardData struct {
	OwnerProgramHashes          []*common.Uint168
	CandidateOwnerProgramHashes []*common.Uint168
	OwnerVotesInRound           map[common.Uint168]common.Fixed64
	TotalVotesInRound           common.Fixed64
}

RewardData defines variables to calculate reward of a round

func NewRewardData added in v0.3.2

func NewRewardData() *RewardData

func (*RewardData) Deserialize added in v0.3.2

func (d *RewardData) Deserialize(r io.Reader) (err error)

func (*RewardData) Serialize added in v0.3.2

func (d *RewardData) Serialize(w io.Writer) error

type State

type State struct {
	*StateKeyFrame
	// contains filtered or unexported fields
}

State is a memory database storing DPOS producers state, like pending producers active producers and their votes.

func NewState

func NewState(chainParams *config.Params, getArbiters func() [][]byte) *State

NewState returns a new State instance.

func (*State) GetActiveProducers

func (s *State) GetActiveProducers() []*Producer

GetActiveProducers returns all producers that in active state.

func (*State) GetAllProducers added in v0.3.2

func (s *State) GetAllProducers() []*Producer

GetAllProducers returns all producers including pending, active, canceled, illegal and inactive producers.

func (*State) GetCanceledProducers

func (s *State) GetCanceledProducers() []*Producer

GetCanceledProducers returns all producers that in cancel state.

func (*State) GetHistory

func (s *State) GetHistory(height uint32) (*StateKeyFrame, error)

GetHistory returns a history state instance storing the producers and votes on the historical height.

func (*State) GetIllegalProducers

func (s *State) GetIllegalProducers() []*Producer

GetIllegalProducers returns all illegal producers.

func (*State) GetInactiveProducers

func (s *State) GetInactiveProducers() []*Producer

func (*State) GetPendingCanceledProducers added in v0.3.2

func (s *State) GetPendingCanceledProducers() []*Producer

GetPendingCanceledProducers returns all producers that in pending canceled state.

func (*State) GetPendingProducers

func (s *State) GetPendingProducers() []*Producer

GetPendingProducers returns all producers that in pending state.

func (*State) GetProducer

func (s *State) GetProducer(publicKey []byte) *Producer

GetProducer returns a producer with the producer's node public key or it's owner public key including canceled and illegal producers. If no matches return nil.

func (*State) GetProducers

func (s *State) GetProducers() []*Producer

GetProducers returns all producers including pending and active producers (no canceled and illegal producers).

func (*State) GetReturnedDepositProducers added in v0.3.2

func (s *State) GetReturnedDepositProducers() []*Producer

GetReturnedDepositProducers returns producers that in returned deposit state.

func (*State) GetVotedProducers added in v0.3.2

func (s *State) GetVotedProducers() []*Producer

GetVotedProducers returns all producers that in active state with votes.

func (*State) IsAbleToRecoverFromInactiveMode added in v0.3.2

func (s *State) IsAbleToRecoverFromInactiveMode() bool

IsAbleToRecoverFromInactiveMode returns if most of the emergency arbiters have activated and able to work again

func (*State) IsAbleToRecoverFromUnderstaffedState added in v0.3.2

func (s *State) IsAbleToRecoverFromUnderstaffedState() bool

IsAbleToRecoverFromInactiveMode returns if there are enough active arbiters

func (*State) IsActiveProducer

func (s *State) IsActiveProducer(publicKey []byte) bool

IsActiveProducer returns if a producer is in activate list according to the public key.

func (*State) IsCanceledProducer

func (s *State) IsCanceledProducer(publicKey []byte) bool

IsCanceledProducer returns if a producer is in canceled list according to the public key.

func (*State) IsDPOSTransaction

func (s *State) IsDPOSTransaction(tx *types.Transaction) bool

IsDPOSTransaction returns if a transaction will change the producers and votes state.

func (*State) IsIllegalProducer

func (s *State) IsIllegalProducer(publicKey []byte) bool

IsIllegalProducer returns if a producer is in illegal list according to the public key.

func (*State) IsInactiveProducer

func (s *State) IsInactiveProducer(publicKey []byte) bool

IsInactiveProducer returns if a producer is in inactivate list according to the public key.

func (*State) IsPendingProducer

func (s *State) IsPendingProducer(publicKey []byte) bool

IsPendingProducer returns if a producer is in pending list according to the public key.

func (*State) LeaveEmergency added in v0.3.2

func (s *State) LeaveEmergency()

LeaveEmergency will reset EmergencyInactiveArbiters variable

func (*State) NicknameExists

func (s *State) NicknameExists(nickname string) bool

NicknameExists returns if a nickname is exists.

func (*State) ProcessBlock

func (s *State) ProcessBlock(block *types.Block, confirm *payload.Confirm)

ProcessBlock takes a block and it's confirm to update producers state and votes accordingly.

func (*State) ProcessSpecialTxPayload

func (s *State) ProcessSpecialTxPayload(p types.Payload, height uint32)

ProcessIllegalBlockEvidence takes a illegal block payload and change the producers state immediately. This is a spacial case that can be handled before it packed into a block.

func (*State) ProducerExists

func (s *State) ProducerExists(publicKey []byte) bool

ProducerExists returns if a producer is exists by it's node public key or owner public key.

func (*State) ProducerNodePublicKeyExists added in v0.3.2

func (s *State) ProducerNodePublicKeyExists(publicKey []byte) bool

ProducerExists returns if a producer is exists by it's node public key.

func (*State) ProducerOwnerPublicKeyExists added in v0.3.2

func (s *State) ProducerOwnerPublicKeyExists(publicKey []byte) bool

ProducerExists returns if a producer is exists by it's owner public key.

func (*State) RollbackTo

func (s *State) RollbackTo(height uint32) error

RollbackTo restores the database state to the given height, if no enough history to rollback to return error.

func (*State) SpecialTxExists

func (s *State) SpecialTxExists(tx *types.Transaction) bool

SpecialTxExists returns if a special tx (typically means illegal and inactive tx) is exists by it's hash

type StateKeyFrame added in v0.3.2

type StateKeyFrame struct {
	NodeOwnerKeys            map[string]string // NodePublicKey as key, OwnerPublicKey as value
	PendingProducers         map[string]*Producer
	ActivityProducers        map[string]*Producer
	InactiveProducers        map[string]*Producer
	CanceledProducers        map[string]*Producer
	IllegalProducers         map[string]*Producer
	PendingCanceledProducers map[string]*Producer
	Votes                    map[string]*types.Output
	Nicknames                map[string]struct{}
	SpecialTxHashes          map[common.Uint256]struct{}
	PreBlockArbiters         map[string]struct{}

	EmergencyInactiveArbiters map[string]struct{}
	VersionStartHeight        uint32
	VersionEndHeight          uint32
}

StateKeyFrame holds necessary state about State

func (*StateKeyFrame) Deserialize added in v0.3.2

func (s *StateKeyFrame) Deserialize(r io.Reader) (err error)

func (*StateKeyFrame) DeserializeHashSet added in v0.3.2

func (s *StateKeyFrame) DeserializeHashSet(
	r io.Reader) (vmap map[common.Uint256]struct{}, err error)

func (*StateKeyFrame) DeserializeProducerMap added in v0.3.2

func (s *StateKeyFrame) DeserializeProducerMap(
	r io.Reader) (pmap map[string]*Producer, err error)

func (*StateKeyFrame) DeserializeStringMap added in v0.3.2

func (s *StateKeyFrame) DeserializeStringMap(
	r io.Reader) (smap map[string]string, err error)

func (*StateKeyFrame) DeserializeStringSet added in v0.3.2

func (s *StateKeyFrame) DeserializeStringSet(
	r io.Reader) (vmap map[string]struct{}, err error)

func (*StateKeyFrame) DeserializeVotesMap added in v0.3.2

func (s *StateKeyFrame) DeserializeVotesMap(
	r io.Reader) (vmap map[string]*types.Output, err error)

func (*StateKeyFrame) Serialize added in v0.3.2

func (s *StateKeyFrame) Serialize(w io.Writer) (err error)

func (*StateKeyFrame) SerializeHashSet added in v0.3.2

func (s *StateKeyFrame) SerializeHashSet(vmap map[common.Uint256]struct{},
	w io.Writer) (err error)

func (*StateKeyFrame) SerializeProducerMap added in v0.3.2

func (s *StateKeyFrame) SerializeProducerMap(pmap map[string]*Producer,
	w io.Writer) (err error)

func (*StateKeyFrame) SerializeStringMap added in v0.3.2

func (s *StateKeyFrame) SerializeStringMap(smap map[string]string,
	w io.Writer) (err error)

func (*StateKeyFrame) SerializeStringSet added in v0.3.2

func (s *StateKeyFrame) SerializeStringSet(vmap map[string]struct{},
	w io.Writer) (err error)

func (*StateKeyFrame) SerializeVotesMap added in v0.3.2

func (s *StateKeyFrame) SerializeVotesMap(vmap map[string]*types.Output,
	w io.Writer) (err error)

Jump to

Keyboard shortcuts

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