tbft

package module
v2.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeoutPropose Timeout of waitting for a proposal before prevoting nil
	DefaultTimeoutPropose = 30 * time.Second
	// DefaultTimeoutProposeDelta Increased time delta of TimeoutPropose between rounds
	DefaultTimeoutProposeDelta = 1 * time.Second
	// DefaultBlocksPerProposer The number of blocks each proposer can propose
	DefaultBlocksPerProposer = uint64(1)
	// DefaultTimeoutProposeOptimal optimal timeout of waitting for a proposal before prevoting nil
	DefaultTimeoutProposeOptimal = 2 * time.Second
	// TimeoutPrevote Timeout of waiting for >2/3 prevote
	TimeoutPrevote = 30 * time.Second
	// TimeoutPrevoteDelta Increased time delta of TimeoutPrevote between round
	TimeoutPrevoteDelta = 1 * time.Second
	// TimeoutPrecommit Timeout of waiting for >2/3 precommit
	TimeoutPrecommit = 30 * time.Second
	// TimeoutPrecommitDelta Increased time delta of TimeoutPrecommit between round
	TimeoutPrecommitDelta = 1 * time.Second
	// TimeoutCommit Timeout to wait for precommite
	TimeoutCommit = 30 * time.Second
	// TimeDisconnet the duration of node disconnectio(3000ms)
	TimeDisconnet = 3000
)
View Source
const (
	// TypeRemoteTBFTState is the state of remote nodes
	TypeRemoteTBFTState = 1
	// TypeLocalTBFTState is the state of local node
	TypeLocalTBFTState = 2
)
View Source
const (
	// MessageBufferSize 缓存消息大小
	MessageBufferSize = 10240

	// StatusBroadcasterTbft TBFT状态广播器ID
	StatusBroadcasterTbft = "TBFT"
	// InterceptorTbft 标识
	InterceptorTbft = 0
)

Variables

View Source
var (

	// TBFTAddtionalDataKey implements the block key for store tbft infos
	TBFTAddtionalDataKey = "TBFTAddtionalDataKey"
	// TBFT_propose_timeout_key implements the config key for chainconf
	TBFT_propose_timeout_key = "TBFT_propose_timeout"
	// TBFT_propose_delta_timeout_key implements the config key for chainconf
	TBFT_propose_delta_timeout_key = "TBFT_propose_delta_timeout"
	// TBFT_blocks_per_proposer implements the config key for chainconf
	TBFT_blocks_per_proposer = "TBFT_blocks_per_proposer"
	// TBFT_propose_timeout_optimal_key implements the config key for chainconf
	TBFT_propose_timeout_optimal_key = "TBFT_propose_timeout_optimal"
	// TBFT_propose_optimal_key implements the config key for chainconf
	TBFT_propose_optimal_key = "TBFT_propose_optimal"
)
View Source
var (
	// ErrVoteNil implements the error of nil vote
	ErrVoteNil = errors.New("nil vote")
	// ErrUnexceptedStep implements the error of unexpected step in tbft
	ErrUnexceptedStep = errors.New("unexpected step")
	// ErrInvalidValidator implements the error of nil invalid validator
	ErrInvalidValidator = errors.New("invalid validator")
	// ErrVoteForDifferentHash implements the error of invalid hash
	ErrVoteForDifferentHash = errors.New("vote for different hash")
)
View Source
var (
	// ErrInvalidIndex implements the error for invalid index of validators
	ErrInvalidIndex = errors.New("invalid index")
)
View Source
var TimerInterval = 1000 * time.Millisecond

TimerInterval 定时器间隔

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 CurrentTime added in v2.3.3

func CurrentTime() time.Time

CurrentTime return current time

func GetValidatorList

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

GetValidatorList get Validator List From Config @param chainConfig @param store Not currently in use @return validators @return err

func GetValidatorListFromConfig

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

GetValidatorListFromConfig get Validator List From Config @param chainConfig @return validators @return err

func InitLWS

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

InitLWS initialize LWS @param config Consensus Config @param chainId @param nodeId @return lwsInstance @return walWriteMode @return err

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 protocol.Logger, ac protocol.AccessControlProvider,
	validators *validatorSet, roundQC *tbftpb.RoundQC, blockVersion uint32) 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 (*BlockVotes) ToProto

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

ToProto serializes the BlockVotes instance

type ConsensusFutureMsg

type ConsensusFutureMsg struct {
	Proposal map[int32]*tbftpb.Proposal
	// contains filtered or unexported fields
}

ConsensusFutureMsg represents the consensus msg of future

func NewConsensusFutureMsg

func NewConsensusFutureMsg(logger protocol.Logger, height uint64, round int32,
	validators *validatorSet) *ConsensusFutureMsg

NewConsensusFutureMsg creates a new future msg instance

type ConsensusFutureMsgCache

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

ConsensusFutureMsgCache cache future consensus msg

type ConsensusMsg

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

ConsensusMsg implements transformation of structure and pb

type ConsensusState

type ConsensusState struct {

	// node id
	Id string
	// current height
	Height uint64
	// current round
	Round int32
	// current step
	Step tbftpb.Step

	// proposal
	Proposal *TBFTProposal
	// verifing proposal
	VerifingProposal *TBFTProposal
	LockedRound      int32
	// locked proposal
	LockedProposal *tbftpb.Proposal
	ValidRound     int32
	// valid proposal
	ValidProposal *tbftpb.Proposal
	// contains filtered or unexported fields
}

ConsensusState represents the consensus state of the node

func NewConsensusState

func NewConsensusState(logger protocol.Logger, id string) *ConsensusState

NewConsensusState creates a new ConsensusState instance

type ConsensusTBFTImpl

type ConsensusTBFTImpl struct {
	sync.RWMutex

	// node id
	Id string

	// Current Consensus State
	*ConsensusState

	// Timeout = TimeoutPropose + TimeoutProposeDelta * round
	TimeoutPropose        time.Duration
	TimeoutProposeDelta   time.Duration
	TimeoutProposeOptimal time.Duration
	ProposeOptimal        bool
	ProposeOptimalTimer   *time.Timer
	// 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) Data

func (consensus *ConsensusTBFTImpl) Data() interface{}

Data return Status Data(ConsistentEngine) @receiver consensus @return interface{}

func (*ConsensusTBFTImpl) GetConsensusStateJSON

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

GetConsensusStateJSON get consensus status in json format @receiver consensus @return []byte @return error always return nil

func (*ConsensusTBFTImpl) GetLastHeight

func (consensus *ConsensusTBFTImpl) GetLastHeight() uint64

GetLastHeight get current height from consensus state @receiver consensus @return uint64

func (*ConsensusTBFTImpl) GetValidators

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

GetValidators get validators from consensus state GetValidators get validators from consensus state @receiver consensus @return []string validators @return error always return nil

func (*ConsensusTBFTImpl) InitConsistentEngine added in v2.3.3

func (consensus *ConsensusTBFTImpl) InitConsistentEngine()

InitConsistentEngine init ConsistentEngine @receiver consensus

func (*ConsensusTBFTImpl) InitExtendHandler

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

InitExtendHandler r egistered extendHandler

func (*ConsensusTBFTImpl) OnMessage

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

OnMessage implements the OnMessage method of msgbus.

func (*ConsensusTBFTImpl) OnQuit

func (consensus *ConsensusTBFTImpl) OnQuit()

OnQuit implements the OnQuit method of msgbus.

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 the consensus module monitors the signal of the sync module, when the sync module synchronizes a relatively high block, it notifies the consensus module to start the consensus process

func (*ConsensusTBFTImpl) StartConsistentEngine

func (consensus *ConsensusTBFTImpl) StartConsistentEngine() error

StartConsistentEngine start consistent engine @Description: Start ConsistentEngine @receiver consensus

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

ToGossipStateProto convert *ConsensusTBFTImpl to *tbftpb.GossipState @receiver consensus @return *tbftpb.GossipState

func (*ConsensusTBFTImpl) ToProto

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

ToProto copy *ConsensusState to *tbftpb.ConsensusState @receiver consensus @return *tbftpb.ConsensusState

func (*ConsensusTBFTImpl) Type

func (consensus *ConsensusTBFTImpl) Type() int8

Type return Status Type(ConsistentEngine) @receiver consensus @return int

func (*ConsensusTBFTImpl) Update

func (consensus *ConsensusTBFTImpl) Update(state consistent_service.Status)

Update update state @receiver consensus @param state

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 Node

type Node struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Node 节点信息(local/remoter)

func (*Node) ID

func (l *Node) ID() string

ID 返回节点ID

func (*Node) Statuses

func (l *Node) Statuses() map[int8]consistent_service.Status

Statuses 返回节点状态

func (*Node) UpdateStatus

func (l *Node) UpdateStatus(s consistent_service.Status)

UpdateStatus 更新节点状态

type RemoteState

type RemoteState struct {
	sync.RWMutex
	//node id
	Id string
	//current height
	Height uint64
	// current round
	Round int32
	// current step
	Step tbftpb.Step

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

RemoteState validator status, validator and remote are the same

func (*RemoteState) Data

func (r *RemoteState) Data() interface{}

Data 返回状态内容

func (*RemoteState) Type

func (r *RemoteState) Type() int8

Type 返回当前节点的节点类型, 此处表示远端节点

func (*RemoteState) Update

func (r *RemoteState) Update(state consistent_service.Status)

Update 更新远端节点的状态

type StatusBroadcaster

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

StatusBroadcaster 一致性引擎的状态和消息的broadcaster实现

func NewTBFTStatusBroadcaster

func NewTBFTStatusBroadcaster(log protocol.Logger) *StatusBroadcaster

NewTBFTStatusBroadcaster 得到一个broadcaster实例

func (*StatusBroadcaster) ID

func (tsb *StatusBroadcaster) ID() string

ID 返回broadcaster 标识

func (*StatusBroadcaster) IsRunning

func (tsb *StatusBroadcaster) IsRunning() bool

IsRunning 返回运行状态

func (*StatusBroadcaster) PreBroadcaster

func (tsb *StatusBroadcaster) PreBroadcaster() consistent_service.Broadcast

PreBroadcaster 消息广播前做前置处理,如状态校验/判断是否要发送消息等

func (*StatusBroadcaster) Start

func (tsb *StatusBroadcaster) Start() error

Start 启动

func (*StatusBroadcaster) Stop

func (tsb *StatusBroadcaster) Stop() error

Stop 停止

func (*StatusBroadcaster) TimePattern

func (tsb *StatusBroadcaster) TimePattern() interface{}

TimePattern 状态广播触发模式

type StatusDecoder

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

StatusDecoder 状态解析器

func (*StatusDecoder) Decode

func (tD *StatusDecoder) Decode(d interface{}) interface{}

Decode 解析消息,返回节点状态

func (*StatusDecoder) MsgType

func (tD *StatusDecoder) MsgType() int8

MsgType 返回msgbus消息类型

type StatusInterceptor

type StatusInterceptor struct {
}

StatusInterceptor 状态拦截器

func (*StatusInterceptor) Handle

func (tsb *StatusInterceptor) Handle(status consistent_service.Status) error

Handle 过滤状态类型处理

type TBFTProposal

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

TBFTProposal represents the marshaled proposal

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 TbftConsistentMessage

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

TbftConsistentMessage 实现一致性引擎消息的接收和发送

func NewTbftConsistentMessage

func NewTbftConsistentMessage(msgBus msgbus.MessageBus, log consistent_service.Logger) *TbftConsistentMessage

NewTbftConsistentMessage 初始化得到一致性引擎消息处理实例

func (*TbftConsistentMessage) OnMessage

func (m *TbftConsistentMessage) OnMessage(message *msgbus.Message)

OnMessage 基于msgbus的实现

func (*TbftConsistentMessage) OnQuit

func (m *TbftConsistentMessage) OnQuit()

OnQuit 基于msgbus的实现

func (*TbftConsistentMessage) Receive

func (m *TbftConsistentMessage) Receive() interface{}

Receive 接收一致性引擎的消息

func (*TbftConsistentMessage) Send

func (m *TbftConsistentMessage) Send(payload interface{})

Send 发送一致性引擎的消息

func (*TbftConsistentMessage) Start added in v2.3.2

func (m *TbftConsistentMessage) Start() error

Start 启动Message

func (*TbftConsistentMessage) Stop added in v2.3.2

func (m *TbftConsistentMessage) Stop() error

Stop 停止Message

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 protocol.Logger, voteType tbftpb.VoteType, height uint64, round int32,
	validators *validatorSet) *VoteSet

NewVoteSet creates a new VoteSet instance

func NewVoteSetFromProto

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

NewVoteSetFromProto creates a new VoteSet instance from pb

func VerifyQcFromVotes

func VerifyQcFromVotes(logger protocol.Logger, vs []*tbftpb.Vote, ac protocol.AccessControlProvider,
	validators *validatorSet, voteType tbftpb.VoteType, blockVersion uint32) (*VoteSet, error)

VerifyQcFromVotes verifies whether the signatures in votes verify that the maj32 of the votes error when verify successfully, and return corresponding error when failed.

func (*VoteSet) AddVote

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

AddVote adds a vote to the VoteSet

func (*VoteSet) AddVoteForConsistent

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

AddVoteForConsistent 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