tbft

package module
v0.0.0-...-1c83461 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: Apache-2.0 Imports: 32 Imported by: 2

README

consensus-tbft

介绍

hercules 链 tbft 共识模块

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

View Source
const (
	DefaultTimeoutPropose      = 30 * time.Second // Timeout of waitting for a proposal before prevoting nil
	DefaultTimeoutProposeDelta = 1 * time.Second  // Increased time delta of TimeoutPropose between rounds
	DefaultBlocksPerProposer   = uint64(1)        // The number of blocks each proposer can propose
	TimeoutPrevote             = 30 * time.Second // Timeout of waitting for >2/3 prevote
	TimeoutPrevoteDelta        = 1 * time.Second  // Increased time delta of TimeoutPrevote between round
	TimeoutPrecommit           = 30 * time.Second // Timeout of waitting for >2/3 precommit
	TimeoutPrecommitDelta      = 1 * time.Second  // Increased time delta of TimeoutPrecommit between round
	TimeoutCommit              = 30 * time.Second
)

Variables

View Source
var (
	TBFTAddtionalDataKey           = "TBFTAddtionalDataKey"
	TBFT_propose_timeout_key       = "TBFT_propose_timeout"
	TBFT_propose_delta_timeout_key = "TBFT_propose_delta_timeout"
	TBFT_blocks_per_proposer       = "TBFT_blocks_per_proposer"
)
View Source
var (
	ErrVoteNil              = errors.New("nil vote")
	ErrUnexceptedStep       = errors.New("unexpected step")
	ErrInvalidValidator     = errors.New("invalid validator")
	ErrVoteForDifferentHash = errors.New("vote for different hash")
)
View Source
var (
	ErrInvalidIndex = errors.New("invalid index")
)

Functions

func CopyBlock

func CopyBlock(block *common.Block) *common.Block

CopyBlock generates a new block with a old block, internally using the same pointer

func CopyProposalWithBlockHeader

func CopyProposalWithBlockHeader(p *tbftpb.Proposal) *tbftpb.Proposal

CopyProposalWithBlockHeader create a new Proposal instance for sign and verify

func GetValidatorList

func GetValidatorList(chainConfig *config.ChainConfig, store protocol.BlockchainStore) (validators []string,
	err error)

func GetValidatorListFromConfig

func GetValidatorListFromConfig(chainConfig *config.ChainConfig) (validators []string, err error)

func InitLWS

func InitLWS(config *config.ConsensusConfig, chainId, nodeId string) (lwsInstance *lws.Lws,
	walWriteMode wal_service.WalWriteMode, err error)

func NewProposal

func NewProposal(voter string, height uint64, round int32, polRound int32, block *common.Block) *tbftpb.Proposal

NewProposal create a new Proposal

func NewProposalBlock

func NewProposalBlock(block *common.Block, txsRwSet map[string]*common.TxRWSet) *consensuspb.ProposalBlock

NewProposalBlock create a new ProposalBlock

func NewVote

func NewVote(typ tbftpb.VoteType, voter string, height uint64, round int32, hash []byte) *tbftpb.Vote

NewVote create a new Vote instance

func VerifyBlockSignatures

func VerifyBlockSignatures(chainConf protocol.ChainConf,
	ac protocol.AccessControlProvider, block *common.Block, store protocol.BlockchainStore,
	validatorListFunc consensus_utils.ValidatorListFunc) error

VerifyBlockSignatures verifies whether the signatures in block is qulified with the consensus algorithm. It should return nil error when verify successfully, and return corresponding error when failed.

func VerifyRoundQc

func VerifyRoundQc(logger *logger.CMLogger, ac protocol.AccessControlProvider,
	validators *validatorSet, roundQC *tbftpb.RoundQC) error

VerifyRoundQc verifies whether the signatures in roundQC verify that the Qc is nil hash and the maj32 of the voteSet error when verify successfully, and return corresponding error when failed.

Types

type BlockVotes

type BlockVotes struct {
	Votes map[string]*tbftpb.Vote
	Sum   uint64
}

BlockVotes traces the vote from different voter

func NewBlockVotes

func NewBlockVotes() *BlockVotes

NewBlockVotes creates a new BlockVotes instance

func NewBlockVotesFromProto

func NewBlockVotesFromProto(bvProto *tbftpb.BlockVotes) *BlockVotes

NewBlockVotesFromProto creates a new BlockVotes instance from pb

func (*BlockVotes) ToProto

func (bv *BlockVotes) ToProto() *tbftpb.BlockVotes

ToProto serializes the BlockVotes instance

type ConsensusMsg

type ConsensusMsg struct {
	Type tbftpb.TBFTMsgType
	Msg  interface{}
}

type ConsensusState

type ConsensusState struct {
	Id string

	Height uint64
	Round  int32
	Step   tbftpb.Step

	Proposal         *TBFTProposal // proposal
	VerifingProposal *TBFTProposal // verifing proposal
	LockedRound      int32
	LockedProposal   *tbftpb.Proposal // locked proposal
	ValidRound       int32
	ValidProposal    *tbftpb.Proposal // valid proposal

	TriggeredTimeoutPrecommit bool // flag that triggers a precommit timeout
	TriggeredTimeoutPrevote   bool // flag that triggers a prevote timeout
	// contains filtered or unexported fields
}

ConsensusState represents the consensus state of the node

func NewConsensusState

func NewConsensusState(logger *logger.CMLogger, id string) *ConsensusState

NewConsensusState creates a new ConsensusState instance

type ConsensusTBFTImpl

type ConsensusTBFTImpl struct {
	sync.RWMutex

	Id string

	*ConsensusState

	TimeoutPropose      time.Duration
	TimeoutProposeDelta time.Duration
	// contains filtered or unexported fields
}

ConsensusTBFTImpl is the implementation of TBFT algorithm and it implements the ConsensusEngine interface.

func New

New creates a tbft consensus instance

func (*ConsensusTBFTImpl) AddTimeout

func (consensus *ConsensusTBFTImpl) AddTimeout(duration time.Duration, height uint64, round int32,
	step tbftpb.Step)

AddTimeout adds timeout event to timeScheduler

func (*ConsensusTBFTImpl) CommitTimeout

func (consensus *ConsensusTBFTImpl) CommitTimeout(round int32) time.Duration

CommitTimeout returns timeout to wait for precommiting at `round`

func (*ConsensusTBFTImpl) GetConsensusStateJSON

func (consensus *ConsensusTBFTImpl) GetConsensusStateJSON() ([]byte, error)

func (*ConsensusTBFTImpl) GetLastHeight

func (consensus *ConsensusTBFTImpl) GetLastHeight() uint64

func (*ConsensusTBFTImpl) GetValidators

func (consensus *ConsensusTBFTImpl) GetValidators() ([]string, error)

func (*ConsensusTBFTImpl) InitExtendHandler

func (consensus *ConsensusTBFTImpl) InitExtendHandler(handler protocol.ConsensusExtendHandler)

func (*ConsensusTBFTImpl) OnMessage

func (consensus *ConsensusTBFTImpl) OnMessage(message *msgbus.Message)

3. when receive commit block, send block to commitBlockC commitBlockC <- block

func (*ConsensusTBFTImpl) OnQuit

func (consensus *ConsensusTBFTImpl) OnQuit()

func (*ConsensusTBFTImpl) PrecommitTimeout

func (consensus *ConsensusTBFTImpl) PrecommitTimeout(round int32) time.Duration

PrecommitTimeout returns timeout to wait for precommiting at `round`

func (*ConsensusTBFTImpl) PrevoteTimeout

func (consensus *ConsensusTBFTImpl) PrevoteTimeout(round int32) time.Duration

PrevoteTimeout returns timeout to wait for prevoting at `round`

func (*ConsensusTBFTImpl) ProposeTimeout

func (consensus *ConsensusTBFTImpl) ProposeTimeout(round int32) time.Duration

ProposeTimeout returns timeout to wait for proposing at `round`

func (*ConsensusTBFTImpl) Start

func (consensus *ConsensusTBFTImpl) Start() error

Start starts the tbft instance with: 1. Register to message bus for subscribing topics 2. Start background goroutinues for processing events 3. Start timeScheduler for processing timeout shedule

func (*ConsensusTBFTImpl) Stop

func (consensus *ConsensusTBFTImpl) Stop() error

Stop implements the Stop method of ConsensusEngine interface.

func (*ConsensusTBFTImpl) ToGossipStateProto

func (consensus *ConsensusTBFTImpl) ToGossipStateProto() *tbftpb.GossipState

func (*ConsensusTBFTImpl) ToProto

func (consensus *ConsensusTBFTImpl) ToProto() *tbftpb.ConsensusState

func (*ConsensusTBFTImpl) Verify

func (consensus *ConsensusTBFTImpl) Verify(consensusType consensuspb.ConsensusType,
	chainConfig *config.ChainConfig) error

Verify implements interface of struct Verifier, This interface is used to verify the validity of parameters, it executes before consensus.

type PeerSendState

type PeerSendState struct {
	Height int64
	Round  int64

	TriggerTime  int64 // The timestamp of sending proposals at the same height
	TriggerCount int64 // The count of sending proposals at the same height
	// contains filtered or unexported fields
}

func NewPeerSendState

func NewPeerSendState(logger *logger.CMLogger) *PeerSendState

NewPeerSendState create a PeerSendState instance

type PeerStateService

type PeerStateService struct {
	sync.Mutex

	Id     string
	Height uint64
	Round  int32
	Step   tbftpb.Step

	Proposal         []byte // proposal
	VerifingProposal []byte
	LockedRound      int32
	LockedProposal   *tbftpb.Proposal // locked proposal
	ValidRound       int32
	ValidProposal    *tbftpb.Proposal // valid proposal
	RoundVoteSet     *roundVoteSet

	*PeerSendState
	// contains filtered or unexported fields
}

PeerStateService represents the consensus state of peer node

func NewPeerStateService

func NewPeerStateService(logger *logger.CMLogger, id string, tbftImpl *ConsensusTBFTImpl) *PeerStateService

NewPeerStateService create a PeerStateService instance

func (*PeerStateService) GetFetchQCC

func (pcs *PeerStateService) GetFetchQCC() chan<- *tbftpb.FetchRoundQC

GetFetchQCC return the fetchQC channel

func (*PeerStateService) GetStateC

func (pcs *PeerStateService) GetStateC() chan<- *tbftpb.GossipState

GetStateC return the stateC channel

type TBFTProposal

type TBFTProposal struct {
	PbMsg *tbftpb.Proposal
	Bytes []byte
}

func NewTBFTProposal

func NewTBFTProposal(proposal *tbftpb.Proposal, marshal bool) *TBFTProposal

NewTBFTProposal create tbft proposal instance

func (*TBFTProposal) Marshal

func (p *TBFTProposal) Marshal()

Marshal marshal the proposal and not care the old bytes

type VoteSet

type VoteSet struct {
	Type         tbftpb.VoteType
	Height       uint64
	Round        int32
	Sum          uint64
	Maj23        []byte
	Votes        map[string]*tbftpb.Vote
	VotesByBlock map[string]*BlockVotes
	// contains filtered or unexported fields
}

VoteSet wraps tbftpb.VoteSet and validatorSet

func NewVoteSet

func NewVoteSet(logger *logger.CMLogger, voteType tbftpb.VoteType, height uint64, round int32,
	validators *validatorSet) *VoteSet

NewVoteSet creates a new VoteSet instance

func NewVoteSetFromProto

func NewVoteSetFromProto(logger *logger.CMLogger, vsProto *tbftpb.VoteSet, validators *validatorSet) *VoteSet

NewVoteSetFromProto creates a new VoteSet instance from pb

func (*VoteSet) AddVote

func (vs *VoteSet) AddVote(vote *tbftpb.Vote) (added bool, err error)

AddVote adds a vote to the VoteSet

func (*VoteSet) HasTwoThirdsMajority

func (vs *VoteSet) HasTwoThirdsMajority() (majority bool)

HasTwoThirdsMajority shoule used when the mutex has been lock

func (*VoteSet) Size

func (vs *VoteSet) Size() int32

Size returns the size of the VoteSet

func (*VoteSet) String

func (vs *VoteSet) String() string

func (*VoteSet) ToProto

func (vs *VoteSet) ToProto() *tbftpb.VoteSet

ToProto serializes the VoteSet instance

Jump to

Keyboard shortcuts

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