state

package
v0.0.0-...-b6e904f Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DeltaMillSec = 1000
	TimeoutT     = 4 * DeltaMillSec * time.Millisecond // 4delta round-trip

	MsgQueueSize     = 1000
	NoRollbackTmoIdx = 0

	TimeoutProcess  = "TIMEOUT"
	ProposalProcess = "PROPOSAL"
	VoteProcess     = "VOTE"
)
View Source
const (
	TypeCollectVotes = 1 // "collect_votes_type"
	TypeNextRound    = 2 // "next_round_type"

	MaxTimeoutSec = 60 * 60
)

Variables

View Source
var (
	ErrVoteSetOccupied    = errors.New("round occupied")
	ErrComponentsOccupied = errors.New("components occupied")
)
View Source
var (
	ErrNilQC = errors.New("pacemaker meets a nil qc")
)

Functions

func ProposalMsg

func ProposalMsg(round int64, id []byte, justifyParent []byte) *types.ProposalMsg

func ProtoFromConsMsg

func ProtoFromConsMsg(msg MsgInfo) ([]byte, error)

func TimeoutMsg

func TimeoutMsg(round int64, pround int64, pid []byte, idx int64) *types.TimeoutMsg

func VoteMsg

func VoteMsg(round int64, id []byte, pround int64, pid []byte, to string) *types.VoteMsg

Types

type BlockTree

type BlockTree struct {
	DeserializeF  DeserializeQurumCert
	NewQurumCertF NewQurumCert
	FF            SerializeID
	// contains filtered or unexported fields
}

BlockTree is an added storage to achieve consensus in a distributed system, main infomation like entries should be stored in different way.

func NewQCTree

func NewQCTree(user PeerID, rootRound int64, rootID string, rootValue []byte,
	df DeserializeQurumCert, nf NewQurumCert, log libs.Logger) (*BlockTree, error)

func (*BlockTree) ExecuteNInsert

func (t *BlockTree) ExecuteNInsert(qc QuorumCert) error

func (*BlockTree) GetCurrentHighQC

func (t *BlockTree) GetCurrentHighQC() QuorumCert

func (*BlockTree) GetCurrentRoot

func (t *BlockTree) GetCurrentRoot() QuorumCert

func (*BlockTree) GetJustify

func (t *BlockTree) GetJustify() ([]byte, error)

func (*BlockTree) ProcessCommit

func (t *BlockTree) ProcessCommit(key string) error

func (*BlockTree) ProcessVote

func (t *BlockTree) ProcessVote(qc QuorumCert, validators []PeerID) error

func (*BlockTree) Search

func (t *BlockTree) Search(round int64, id []byte) (*bt.Node, error)

type ConsensusConfig

type ConsensusConfig struct {
	StartRound      int64
	StartTimeoutIdx int64
	StartID         string
	StartValue      []byte
	StartValidators []PeerID
}

----------------------------------------------------------------

type DefaultElection

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

func NewDefaultElection

func NewDefaultElection(start int64, init []PeerID) *DefaultElection

func (*DefaultElection) Leader

func (e *DefaultElection) Leader(round int64, roundTimeoutIdxMap map[int64]int64) PeerID

Leader indicates that the system cannot rollback.

func (*DefaultElection) Update

func (e *DefaultElection) Update(round int64, next []PeerID) error

func (*DefaultElection) Validators

func (e *DefaultElection) Validators(round int64, roundTimeoutIdxMap map[int64]int64) []PeerID

type DefaultPacemaker

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

func NewDefaultPacemaker

func NewDefaultPacemaker(latest int64) *DefaultPacemaker

func (*DefaultPacemaker) AdvanceRound

func (p *DefaultPacemaker) AdvanceRound(qc QuorumCert) error

func (*DefaultPacemaker) GetCurrentRound

func (p *DefaultPacemaker) GetCurrentRound() int64

func (*DefaultPacemaker) ProcessTimeoutRound

func (p *DefaultPacemaker) ProcessTimeoutRound(qc QuorumCert) error

ProcessTimeoutRound DefaultPacemaker cannot rollback.

type DefaultQuorumCert

type DefaultQuorumCert struct {
	Round       int64  `json:"round"`
	ID          []byte `json:"id"`
	ParentRound int64  `json:"parent_round"`
	ParentID    []byte `json:"parent_id"`
	SenderID    string `json:"sender"`

	Signs map[string]DefaultSign `json:"signs"`
}

DefaultQuorumCert is the canonical implementation of the QuorumCert interface

func (DefaultQuorumCert) ParentProposal

func (qc DefaultQuorumCert) ParentProposal() (int64, []byte, error)

func (DefaultQuorumCert) Proposal

func (qc DefaultQuorumCert) Proposal() (int64, []byte, error)

func (DefaultQuorumCert) Sender

func (qc DefaultQuorumCert) Sender() string

func (DefaultQuorumCert) Serialize

func (qc DefaultQuorumCert) Serialize() ([]byte, error)

func (DefaultQuorumCert) Signatures

func (qc DefaultQuorumCert) Signatures(peerID string) (int, []byte, []byte, error)

func (DefaultQuorumCert) String

func (qc DefaultQuorumCert) String() string

type DefaultSafetyRules

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

func NewDefaultSafetyRules

func NewDefaultSafetyRules(point *State) *DefaultSafetyRules

func (*DefaultSafetyRules) CheckProposal

func (s *DefaultSafetyRules) CheckProposal(new, parent QuorumCert) error

CheckProposal checks whether the proposal is valid: a. check the new proposal has a unique id & round, b. check the proposal's parent is in our tree, which means it's the one of children of the root,

func (*DefaultSafetyRules) CheckTimeout

func (s *DefaultSafetyRules) CheckTimeout(qc QuorumCert) error

func (*DefaultSafetyRules) CheckVote

func (s *DefaultSafetyRules) CheckVote(qc QuorumCert) error

func (*DefaultSafetyRules) UpdatePreferredRound

func (s *DefaultSafetyRules) UpdatePreferredRound(round int64) error

type DefaultSign

type DefaultSign struct {
	PeerID    string
	PublicKey []byte
	Sign      []byte
	Type      int
}

type DefaultTimeoutTicker

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

func (*DefaultTimeoutTicker) Chan

func (t *DefaultTimeoutTicker) Chan() <-chan timeoutInfo

Chan returns a channel on which timeouts are sent.

func (*DefaultTimeoutTicker) ScheduleTimeout

func (t *DefaultTimeoutTicker) ScheduleTimeout(ti timeoutInfo)

ScheduleTimeout schedules a new timeout by sending on the internal tickChan. The timeoutRoutine is always available to read from tickChan, so this won't block. The scheduling may fail if the timeoutRoutine has already scheduled a timeout for a later height/round/step.

func (*DefaultTimeoutTicker) Start

func (t *DefaultTimeoutTicker) Start()

func (*DefaultTimeoutTicker) Stop

func (t *DefaultTimeoutTicker) Stop()

type DeserializeQurumCert

type DeserializeQurumCert func(input []byte) (qc QuorumCert, err error)

type MsgInfo

type MsgInfo interface {
	Validate() error
	String() string
}

Message is a message that can be sent and received within peers

func ConsMsgFromProto

func ConsMsgFromProto(msgbytes []byte) (MsgInfo, error)

MsgFromProto takes a consensus proto message and returns the native hotstuff types

type NewQurumCert

type NewQurumCert func(senderID string, sign []byte, round int64, id []byte,
	parentRound int64, parentID []byte) (qc QuorumCert, err error)

type Pacemaker

type Pacemaker interface {
	// GetCurrentRound return current round of this node.
	GetCurrentRound() int64
	// AdvanceRound increase the round number when the input is valid,
	// it will invoke next_round_event().
	AdvanceRound(qc QuorumCert) error
	// ProcessTimeoutRound decides the next round when the node has enough timeoutMSG,
	// developer can choose different procedures like advance_round only or rollback to the previous round,
	// it will invoke next_round_event().
	// When the builder can accept a recyclic round, which means the next round of round A can also be round A under timeout situation,
	// the next round is calculated by the specific election. Conversely it should return round + 1 when the system cannot rollback.
	ProcessTimeoutRound(qc QuorumCert) error
}

PacemakerInterface is the interface of Pacemaker. It responsible for generating a new round. We assume Pacemaker in all correct replicas will have synchronized leadership after GST. Safty is entirelydecoupled from liveness by any potential instantiation of Packmaker. Different consensus have different pacemaker implement.

type PeerID

type PeerID string

type ProposerElection

type ProposerElection interface {
	// roundTimeoutIdxMap map[int64]int64 is an index-look-up map
	// stores the timeout times in the same round of a rollback system.
	// round-timeoutidx can specialize the unique round on the system.
	Validators(round int64, roundTimeoutIdxMap map[int64]int64) []PeerID
	Leader(round int64, roundTimeoutIdxMap map[int64]int64) PeerID
	// User can dynamically change the validators at runtime by the commands if needed.
	Update(round int64, next []PeerID) error
}

type QuorumCert

type QuorumCert interface {
	Proposal() (round int64, id []byte, err error) // QC's id shoud be unique.
	ParentProposal() (round int64, id []byte, err error)
	Sender() (senderID string)
	Signatures(peerID string) (signType int, sign []byte, pk []byte, err error)
	Serialize() ([]byte, error)
	String() string
}

func DefaultDeserialize

func DefaultDeserialize(input []byte) (QuorumCert, error)

func NewDefaultQuorumCert

func NewDefaultQuorumCert(senderID string, sign []byte,
	round int64, id []byte, parentRound int64, parentID []byte) (QuorumCert, error)

type RoundTimeoutSet

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

type RoundVoteSet

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

type SafetyRules

type SafetyRules interface {
	UpdatePreferredRound(round int64) error
	CheckProposal(new, parent QuorumCert) error
	CheckVote(qc QuorumCert) error
	CheckTimeout(qc QuorumCert) error
}

type SerializeID

type SerializeID func(input []byte) string

type State

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

State handles execution of the hotstuff consensus algorithm. It processes votes and proposals, and upon reaching agreement, commits blocks to the storage and executes them against the application. The internal state machine receives input from peers, the internal validator, and from a timer.

func NewState

func NewState(name PeerID, cc crypto.CryptoClient, timeout TimeoutTicker,
	logger libs.Logger, cfg *ConsensusConfig) (*State, error)

func (*State) GetNextID

func (s *State) GetNextID() ([]byte, error)

GetNextID tries to simulate the data encapsulation.

func (*State) HandleFunc

func (s *State) HandleFunc(chID int32, msgbytes []byte)

Handle define consensus reactor function, NOTE: chID is ignored if it's unknown.

func (*State) NewRoundEvent

func (s *State) NewRoundEvent(action string) error

NewRoundEvent starts a new timer for the next round and broadcasts the proposal msg when the host is the leader.

func (*State) RegisterElection

func (s *State) RegisterElection(election ProposerElection) error

func (*State) RegisterPaceMaker

func (s *State) RegisterPaceMaker(pacemaker Pacemaker) error

func (*State) RegisterSaftyrules

func (s *State) RegisterSaftyrules(safetyrules SafetyRules) error

func (*State) SetSwitch

func (s *State) SetSwitch(p2p libs.Switch)

func (*State) Start

func (s *State) Start()

type StepValidators

type StepValidators struct {
	Start      int64
	Validators []PeerID
}

type TimeoutSet

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

func NewTimeoutSet

func NewTimeoutSet(rootRound int64, latestTimeoutIndex int64) *TimeoutSet

TODO: load from wal

func (*TimeoutSet) AddTimeout

func (s *TimeoutSet) AddTimeout(round int64, idx int64, sender PeerID, validators []PeerID) error

when the rollback strategy is loaded, timeout round can be similar with the previous timeout round (see a timeout round a of the leader a has came out, the system will rollback and rebuild round a which leader may be leader a too), so we use index flag to make timeout unique among the others.

func (*TimeoutSet) GetCurrentTimeoutIndex

func (s *TimeoutSet) GetCurrentTimeoutIndex() int64

func (*TimeoutSet) GetTimeoutIdxMap

func (s *TimeoutSet) GetTimeoutIdxMap() map[int64]int64

func (*TimeoutSet) HasTwoThirdsAny

func (s *TimeoutSet) HasTwoThirdsAny(round int64, idx int64) bool

func (*TimeoutSet) Reset

func (s *TimeoutSet) Reset(round int64, idx int64) error

type TimeoutTicker

type TimeoutTicker interface {
	Start()
	Stop()
	Chan() <-chan timeoutInfo       // on which to receive a timeout
	ScheduleTimeout(ti timeoutInfo) // reset the timer
}

TimeoutTicker is a timer that schedules timeouts conditional on the height/round/step in the timeoutInfo.

func NewDefaultTimeoutTicker

func NewDefaultTimeoutTicker(logger libs.Logger) TimeoutTicker

NewDefaultTimeoutTicker returns a new DefaultTimeoutTicker and invoke timeoutTicker.Start().

type VoteSet

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

func NewVoteSet

func NewVoteSet(rootRound int64) *VoteSet

TODO: load from wal

func (*VoteSet) AddVote

func (s *VoteSet) AddVote(round int64, id []byte, sender PeerID, validators []PeerID) error

func (*VoteSet) HasTwoThirdsAny

func (s *VoteSet) HasTwoThirdsAny(round int64, id []byte) bool

type WAL

type WAL interface {
	Write(WALMessage) error
	WriteSync(WALMessage) error
	FlushAndSync() error

	SearchForEndHeight(height int64) (rd io.ReadCloser, found bool, err error)

	// service methods
	Start() error
	Stop() error
	Wait()
}

WAL is an interface for any write-ahead logger.

type WALMessage

type WALMessage interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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