protocol

package
v0.0.0-...-28a0c76 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Package protocol

@author: xwc1125

Index

Constants

View Source
const (
	MsgPrePrepare  uint64 = iota // prePrepare
	MsgPrepare                   // prepare
	MsgCommit                    // commit
	MsgRoundChange               // 轮换
)

消息类型

Variables

View Source
var (
	ErrUnauthorized    = errors.New("unauthorized validator") // validator未授权
	ErrStoppedEngine   = errors.New("stopped engine")         // 共识已停止
	ErrStartedEngine   = errors.New("started engine")         // 共识已经启动,再次启动时会报错
	ErrOldMessage      = errors.New("old message")            // pbft旧消息
	ErrUnknownAncestor = errors.New("unknown ancestor")       // 校验新区块时,祖先区块未知
	ErrFutureBlock     = errors.New("block in the future")    // 当区块的时间戳大于当前节点的时间戳
)

Functions

func GetSignatureValidator

func GetSignatureValidator(nodeKey protocol.NodeKey, data []byte, sig *signature.SignResult) (string, error)

GetSignatureValidator 从签名数据中获取签名地址

func PrepareCommittedSeal

func PrepareCommittedSeal(hash types.Hash) []byte

PrepareCommittedSeal 根据hash返回committed的签名消息内容

Types

type Message

type Message struct {
	Code          uint64                `json:"code"`                     // 消息类型
	Msg           []byte                `json:"msg"`                      // 消息内容
	Validator     string                `json:"validator"`                // 验证者(peerId)
	Signature     *signature.SignResult `json:"signature" rlp:"nil"`      // 签名内容
	CommittedSeal *signature.SignResult `json:"committed_seal" rlp:"nil"` // 被提交的seal
}

Message pbft消息结构

func (*Message) Decode

func (m *Message) Decode(val interface{}) error

Decode 将message.Msg转换为对象,val必须为指针类型

func (*Message) FromPayload

func (m *Message) FromPayload(msg []byte, validateFn func(data []byte, signResult *signature.SignResult) (string, error)) error

FromPayload 将msgBytes转换为message

func (*Message) Payload

func (m *Message) Payload() ([]byte, error)

Payload message的bytes内容

func (*Message) PayloadNoSig

func (m *Message) PayloadNoSig() ([]byte, error)

PayloadNoSig Signature为空的bytes数据

func (*Message) String

func (m *Message) String() string

String 打印message字符串

type MessageSet

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

MessageSet message集合

func NewMessageSet

func NewMessageSet(valSet ValidatorSet) *MessageSet

NewMessageSet 根据validatorSet构造messageSet

func (*MessageSet) Add

func (ms *MessageSet) Add(msg *Message) error

Add 添加消息

func (*MessageSet) Get

func (ms *MessageSet) Get(val string) *Message

Get 根据peerId获取消息

func (*MessageSet) Size

func (ms *MessageSet) Size() int

Size 获取messages的个数

func (*MessageSet) String

func (ms *MessageSet) String() string

String 打印所有消息

func (*MessageSet) Values

func (ms *MessageSet) Values() (result []*Message)

Values 获取所有的message

func (*MessageSet) View

func (ms *MessageSet) View() *View

View 获取集合中的view

type PBFTBackend

type PBFTBackend interface {
	ID() string                                      // 当前Validator的ID
	Validators(proposal Proposal) ValidatorSet       // validator的集合
	ParentValidators(proposal Proposal) ValidatorSet // 获取给定的提议父区块获取validator集合

	Verify(proposal Proposal) error                                // 验证提议
	Commit(proposal Proposal, seals []*signature.SignResult) error // 提交已批准的提议,提议将会写入区块链

	Sign(data []byte) (*signature.SignResult, error)                               // 使用私钥进行签名数据
	CheckSignature(data []byte, validator string, sig *signature.SignResult) error // 判断签名是否为给定的validator签署的

	LastProposal() (Proposal, string)                // 获取最新的proposal提议,及proposer提议者的地址
	HasProposal(hash types.Hash, height uint64) bool // 检查给定Hash和高度是否有匹配的提议
	GetProposer(height uint64) string                // 根据区块高度获取提议者地址
}

PBFTBackend pbft核心函数接口

type PBFTConfig

type PBFTConfig struct {
	Epoch          uint64          `json:"epoch" mapstructure:"epoch"`           // 期数
	RequestTimeout uint64          `json:"timeout" mapstructure:"timeout"`       // round 超时时间, 在BlockPeriod不为0时有效
	BlockPeriod    uint64          `json:"period" mapstructure:"period"`         // 区块产生间隔(毫秒)
	ProposerPolicy ProposerPolicy  `json:"policy" mapstructure:"policy"`         // Proposer 策略
	Managers       []types.Address `json:"managers" mapstructure:"managers"`     // 创世管理员地址
	Validators     []string        `json:"validators" mapstructure:"validators"` // 创世验证者地址
}

PBFTConfig pbft的配置

type PBFTEngine

type PBFTEngine interface {
	Start() error
	Stop() error
	Request(*Request) error
	RequestTimeout()
	NewChainHead() error
}

PBFTEngine pbft共识引擎

type PrePrepare

type PrePrepare struct {
	View     *View
	Proposal Proposal
}

PrePrepare 预处理消息

func (*PrePrepare) DecodeRLP

func (b *PrePrepare) DecodeRLP(s *rlp.Stream) error

func (*PrePrepare) EncodeRLP

func (b *PrePrepare) EncodeRLP(w io.Writer) error

type Proposal

type Proposal interface {
	Height() uint64    // 返回提议的序列号
	Hash() types.Hash  // 返回提议的hash
	Timestamp() uint64 // 返回提议时间戳
}

Proposal 为区块在共识过程中的抽象接口。

type ProposalSelector

type ProposalSelector func(ValidatorSet, string, uint64) Validator

ProposalSelector 提议选举策略

type ProposerPolicy

type ProposerPolicy uint64

ProposerPolicy 提议策略

const (
	RoundRobin ProposerPolicy = iota // 在每个区块或者round更换时,更换proposer
	Sticky                           // 在round更换时更换proposer
)

type Request

type Request struct {
	Proposal Proposal
}

Request 请求

type State

type State uint64

State pbft共识状态

const (
	StateAcceptRequest State = iota // 接收请求
	StatePrePrepared                // 预处理
	StatePrepared                   // 准备
	StateCommitted                  // 确认
)

func (State) Cmp

func (s State) Cmp(y State) int

Cmp 状态比较

-1 s状态先于y
 0 状态一致
+1 s状态后于y

func (State) String

func (s State) String() string

String 字符串打印

type Subject

type Subject struct {
	View   *View      // 视图
	Digest types.Hash // 摘要(使用的区块hash)
}

Subject prepare消息和commit消息

func (*Subject) DecodeRLP

func (b *Subject) DecodeRLP(s *rlp.Stream) error

func (*Subject) EncodeRLP

func (b *Subject) EncodeRLP(w io.Writer) error

func (*Subject) String

func (b *Subject) String() string

type Validator

type Validator interface {
	ID() string     // ID 返回PBFT节点标识
	String() string // 校验者字符串
}

Validator 验证者

type ValidatorSet

type ValidatorSet interface {
	CalcProposer(lastProposer string, round uint64) // 根据Proposer计算下一个Proposer, 并记录到ValidatorSet, 通过GetProposer查询
	GetProposer() Validator                         // 返回当前proposer
	IsProposer(id string) bool                      // 查询指定的id是否为proposer
	Policy() ProposerPolicy                         // 提议选举策略

	GetByIndex(index uint64) Validator                  // 通过索引查找validator
	GetById(id string) (index int, validator Validator) // 通过ID查找validator, 并返回其索引
	AddValidator(id string) bool                        // 添加validator
	RemoveValidator(id string) bool                     // 删除validator
	List() []Validator                                  // 返回 validator 数组
	Size() int                                          // 返回验证者集合的长度

	Copy() ValidatorSet    // 复制
	FaultTolerantNum() int // 容错节点数
}

ValidatorSet 验证者集

type Validators

type Validators []Validator

Validators 校验者集合

func (Validators) Len

func (slice Validators) Len() int

func (Validators) Less

func (slice Validators) Less(i, j int) bool

func (Validators) Swap

func (slice Validators) Swap(i, j int)

type View

type View struct {
	Sequence *big.Int // 提议的区块高度。每个周期都有一个高度。周期为prePrepare, prepare and commit
	Round    *big.Int // 轮询次数。如果给定的区块未被验证器接受,将发生新一轮更改,并且Round=Round+1
}

View 轮询次数及区块高度

func (*View) Cmp

func (v *View) Cmp(y *View) int

func (*View) DecodeRLP

func (v *View) DecodeRLP(s *rlp.Stream) error

func (*View) EncodeRLP

func (v *View) EncodeRLP(w io.Writer) error

func (*View) String

func (v *View) String() string

Directories

Path Synopsis
Package mockpbft is a generated GoMock package.
Package mockpbft is a generated GoMock package.

Jump to

Keyboard shortcuts

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