bft

package
v0.0.0-...-4dde864 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: LGPL-3.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	INIT = iota
	WAIT_SETUP
	WAIT_SETUP_TIMEOUT
	WAIT_COMMIT
	WAIT_COMMIT_COMPELED
	WAIT_COMMIT_IMEOUT
	WAIT_CHALLENGE
	WAIT_CHALLENGE_TIMEOUT
	WAIT_RESPONSE
	WAIT_RESPONSE_COMPELED
	WAIT_RESPONSE_TIMEOUT
	COMPLETED
	ERROR
)
View Source
const (
	MsgTypeSetUp       = 0
	MsgTypeCommitment  = 1
	MsgTypeResponse    = 2
	MsgTypeChallenge   = 3
	MsgTypeFail        = 4
	MsgTypeValidateReq = 5
	MsgTypeValidateRes = 6

	MaxMsgSize = 20 << 20

	SetupMagic    = 0xfefefbfe
	CommitMagic   = 0xfefefbfd
	ChallegeMagic = 0xfefefbfc
	FailMagic     = 0xfefefbfb
	ResponseMagic = 0xfefefbfa
)

本模块的消息只能在调用本模块(consensus及对应的子模块)的函数中使用,否则会出错 例如MsgTypeCommitment消息,在consensus中定义的,发送和接收此消息必须使用consensus中的函数

View Source
const (
	MODULENAME = "bft"
)

Variables

View Source
var (
	ErrSignBlock          = errors.New("sign block error")
	ErrWalletNotOpen      = errors.New("wallet is close")
	ErrBpConfig           = errors.New("the pubkey config not in bp nodes")
	ErrBFTNotReady        = errors.New("BFT node not ready")
	ErrBpNotInList        = errors.New("bp node not in local list")
	ErrMultiSig           = errors.New("ErrMultiSig")
	ErrWaitCommit         = errors.New("waitForCommit fail")
	ErrWaitResponse       = errors.New("waitForResponse fail")
	ErrChallenge          = errors.New("challenge error")
	ErrSignatureNotValid  = errors.New("signature not valid")
	ErrTimeout            = errors.New("time out")
	ErrLowHeight          = errors.New("leader's height  lower")
	ErrHighHeight         = errors.New("leader's height  higher")
	ErrStatus             = errors.New("error status")
	ErrLeaderMistake      = errors.New("setUp: mistake leader")
	ErrValidateMsg        = errors.New("validate message error")
	ErrGenerateNouncePriv = errors.New("generate nounce fail")
	ErrMsgSize            = errors.New("err msg size")
	ErrGasUsed            = errors.New("gasUsed not match gasUsed in blockheader")
	ErrNotMyTurn          = errors.New("not my turn")
)
View Source
var (
	MinerFlag = cli.BoolFlag{
		Name:  "miner",
		Usage: "is miner",
	}
)
View Source
var (
	MinerPrefix = []byte("miner")
)
View Source
var NumberOfMsg = 7

Functions

This section is empty.

Types

type BftConfig

type BftConfig struct {
	MyPk           *secp256k1.PublicKey `json:"mypk"`
	StartMiner     bool                 `json:"startMiner"`
	ProducerNum    int                  `json:"producerNum"`
	BlockInterval  int64                `json:"blockInterval"`
	ChangeInterval uint64               `json:"changeInterval"`
}

type BftConsensus

type BftConsensus struct {
	CoinBase crypto.CommonAddress
	PrivKey  *secp256k1.PrivateKey

	BlockGenerator blockmgr.IBlockBlockGenerator

	ChainService chain.ChainServiceInterface
	DbService    *database.DatabaseService

	WaitTime time.Duration
	// contains filtered or unexported fields
}

func NewBftConsensus

func NewBftConsensus(
	chainService chain.ChainServiceInterface,
	blockGenerator blockmgr.IBlockBlockGenerator,
	dbService *database.DatabaseService,
	sener Sender,
	config *BftConfig,
	addPeer, removePeer *event.Feed) *BftConsensus

func (*BftConsensus) ChangeTime

func (bftConsensus *BftConsensus) ChangeTime(interval time.Duration)

func (*BftConsensus) Close

func (bftConsensus *BftConsensus) Close()

func (*BftConsensus) GetProducers

func (bftConsensus *BftConsensus) GetProducers(height uint64, topN int) ([]Producer, error)

func (*BftConsensus) ReceiveMsg

func (bftConsensus *BftConsensus) ReceiveMsg(peer *consensusTypes.PeerInfo, t uint64, buf []byte)

func (*BftConsensus) Run

func (bftConsensus *BftConsensus) Run(privKey *secp256k1.PrivateKey) (*types.Block, error)

type BftConsensusService

type BftConsensusService struct {
	P2pServer        p2pService.P2P                       `service:"p2p"`
	ChainService     chainService.ChainServiceInterface   `service:"chain"`
	BroadCastor      blockMgrService.ISendMessage         `service:"blockmgr"`
	BlockMgrNotifier blockMgrService.IBlockNotify         `service:"blockmgr"`
	BlockGenerator   blockMgrService.IBlockBlockGenerator `service:"blockmgr"`
	DatabaseService  *database.DatabaseService            `service:"database"`
	WalletService    *accountService.AccountService       `service:"accounts"`

	BftConsensus *BftConsensus

	Config *BftConfig

	ConsensusEngine consensusTypes.IConsensusEngine
	Miner           *secp256k1.PrivateKey
	// contains filtered or unexported fields
}

func (*BftConsensusService) Api

func (bftConsensusService *BftConsensusService) Api() []app.API

func (*BftConsensusService) CommandFlags

func (bftConsensusService *BftConsensusService) CommandFlags() ([]cli.Command, []cli.Flag)

func (*BftConsensusService) DefaultConfig

func (bftConsensusService *BftConsensusService) DefaultConfig() *BftConfig

func (*BftConsensusService) GetProducers

func (bftConsensusService *BftConsensusService) GetProducers(height uint64, topN int) ([]Producer, error)

func (*BftConsensusService) Init

func (bftConsensusService *BftConsensusService) Init(executeContext *app.ExecuteContext) error

func (*BftConsensusService) Name

func (bftConsensusService *BftConsensusService) Name() string

func (*BftConsensusService) Start

func (bftConsensusService *BftConsensusService) Start(executeContext *app.ExecuteContext) error

func (*BftConsensusService) Stop

func (bftConsensusService *BftConsensusService) Stop(executeContext *app.ExecuteContext) error

type BlockMultiSigValidator

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

func NewBlockMultiSigValidator

func NewBlockMultiSigValidator(getProducers GetProducers, getBlock GetBlock, producerNum int) *BlockMultiSigValidator

func (*BlockMultiSigValidator) ExecuteBlock

func (blockMultiSigValidator *BlockMultiSigValidator) ExecuteBlock(context *chain.BlockExecuteContext) error

func (*BlockMultiSigValidator) VerifyBody

func (blockMultiSigValidator *BlockMultiSigValidator) VerifyBody(block *types.Block) error

func (*BlockMultiSigValidator) VerifyHeader

func (blockMultiSigValidator *BlockMultiSigValidator) VerifyHeader(header, parent *types.BlockHeader) error

type Challenge

type Challenge struct {
	Height      uint64
	Magic       uint32
	Round       int
	SigmaPubKey *secp256k1.PublicKey
	SigmaQ      *secp256k1.PublicKey
	R           []byte
}

func (*Challenge) String

func (Challenge *Challenge) String() string

type Commitment

type Commitment struct {
	Height uint64
	Magic  uint32
	Round  int
	BpKey  *secp256k1.PublicKey
	Q      *secp256k1.PublicKey
}

func (*Commitment) String

func (commitment *Commitment) String() string

type CompletedBlockMessage

type CompletedBlockMessage struct {
	MultiSignature
	StateRoot []byte //sencond round  leader should send stateroot, then member verify
}

func CompletedBlockFromMessage

func CompletedBlockFromMessage(bytes []byte) (*CompletedBlockMessage, error)

func (*CompletedBlockMessage) AsMessage

func (completedBlockMessage *CompletedBlockMessage) AsMessage() []byte

func (*CompletedBlockMessage) AsSignMessage

func (completedBlockMessage *CompletedBlockMessage) AsSignMessage() []byte

type ConsensusApi

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

name: 共识rpc接口 usage: 查询共识节点功能 prefix:consensus

func (*ConsensusApi) ChangeWaitTime

func (consensusApi *ConsensusApi) ChangeWaitTime(waitTime int)
 name: changeWaitTime
 usage: 修改leader等待时间 (ms)
 params:
	1.等待时间(ms)
 return: 私钥
 example:
	curl http://localhost:15645 -X POST --data '{"jsonrpc":"2.0","method":"consensus_changeWaitTime","params":[100000], "id": 3}' -H "Content-Type:application/json"

response:

{"jsonrpc":"2.0","id":3,"result":null}

func (*ConsensusApi) GetMiners

func (consensusApi *ConsensusApi) GetMiners() []*secp256k1.PublicKey
	 name: getMiners()
	 usage: 获取当前出块节点
	 params:
		无
	 return: 出块节点信息
	 example:
		curl http://localhost:15645 -X POST --data '{"jsonrpc":"2.0","method":"consensus_getMiners","params":[""], "id": 3}' -H "Content-Type:application/json"

	response:
		 {"jsonrpc":"2.0","id":3,"result":['0x02c682c9f503465a27d1941d1a25547b5ea879a7145056283599a33869982513df', '0x036a09f9012cb3f73c11ceb2aae4242265c2aa35ebec20dbc28a78712802f457db']
}

type ConsensusOp

type ConsensusOp struct {
	store.StoreInterface
}

func (*ConsensusOp) GetProducer

func (consensusOp *ConsensusOp) GetProducer() ([]*Producer, error)

func (*ConsensusOp) SaveProducer

func (consensusOp *ConsensusOp) SaveProducer(p []*Producer) error

type Fail

type Fail struct {
	Height uint64
	Magic  uint32
	Round  int

	Reason string
}

func (*Fail) String

func (fail *Fail) String() string

type GetBlock

type GetBlock func(hash *crypto.Hash) (*types.Block, error)

type GetProducers

type GetProducers func(uint64, int) ([]Producer, error)

type IConsenMsg

type IConsenMsg interface {
	AsSignMessage() []byte
	AsMessage() []byte
}

type IRewardCalculator

type IRewardCalculator interface {
	AccumulateRewards(sig *MultiSignature, Producers ProducerSet, totalGasBalance *big.Int, height uint64)
}

type Leader

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

func NewLeader

func NewLeader(privkey *secp256k1.PrivateKey, p2pServer Sender, waitTime time.Duration, producers []*MemberInfo, minMember int, curHeight uint64, msgPool chan *MsgWrap) *Leader

func (*Leader) Close

func (leader *Leader) Close()

func (*Leader) OnCommit

func (leader *Leader) OnCommit(peer consensusTypes.IPeerInfo, commit *Commitment)

func (*Leader) OnResponse

func (leader *Leader) OnResponse(peer consensusTypes.IPeerInfo, response *Response)

func (*Leader) ProcessConsensus

func (leader *Leader) ProcessConsensus(msg IConsenMsg, round int) (error, *secp256k1.Signature, []byte)

leader member setup ----->

<-----      commit

challenge ----->

<-----      response

func (*Leader) Reset

func (leader *Leader) Reset()

func (*Leader) Validate

func (leader *Leader) Validate(msg IConsenMsg, r *big.Int, s *big.Int) bool

type Member

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

func NewMember

func NewMember(prvKey *secp256k1.PrivateKey, p2pServer Sender, waitTime time.Duration, producers []*MemberInfo, minMember int, curHeight uint64, msgPool chan *MsgWrap) *Member

func (*Member) OnChallenge

func (member *Member) OnChallenge(peer consensusTypes.IPeerInfo, challengeMsg *Challenge)

func (*Member) OnFail

func (member *Member) OnFail(peer consensusTypes.IPeerInfo, failMsg *Fail)

func (*Member) OnSetUp

func (member *Member) OnSetUp(peer consensusTypes.IPeerInfo, setUp *Setup)

func (*Member) ProcessConsensus

func (member *Member) ProcessConsensus(round int) (IConsenMsg, error)

func (*Member) Reset

func (member *Member) Reset()

func (*Member) WaitChallenge

func (member *Member) WaitChallenge()

func (*Member) WaitSetUp

func (member *Member) WaitSetUp()

type MemberInfo

type MemberInfo struct {
	Peer     types.IPeerInfo
	Producer *Producer
	Status   int
	IsMe     bool
	IsLeader bool
	IsOnline bool
}

type MinerGenesisProcessor

type MinerGenesisProcessor struct {
}

func NewMinerGenesisProcessor

func NewMinerGenesisProcessor() *MinerGenesisProcessor

func (*MinerGenesisProcessor) Genesis

func (minerGenesisProcessor *MinerGenesisProcessor) Genesis(context *chain.GenesisContext) error

type MsgWrap

type MsgWrap struct {
	Peer types.IPeerInfo
	Code uint64
	Msg  []byte
}

type MultiSignature

type MultiSignature struct {
	Sig    secp256k1.Signature
	Leader int
	Bitmap []byte
}

func (*MultiSignature) AsMessage

func (multiSignature *MultiSignature) AsMessage() []byte

func (*MultiSignature) AsSignMessage

func (multiSignature *MultiSignature) AsSignMessage() []byte

func (*MultiSignature) Num

func (multiSignature *MultiSignature) Num() int

type Producer

type Producer struct {
	Pubkey *secp256k1.PublicKey `json:"pubkey"`
	Node   *enode.Node
}

func GetCandidates

func GetCandidates(store store.StoreInterface, topN int) []Producer

func (*Producer) Address

func (producer *Producer) Address() crypto.CommonAddress

type ProducerSet

type ProducerSet []Producer

func (*ProducerSet) IsLocalAddress

func (produceSet *ProducerSet) IsLocalAddress(addr crypto.CommonAddress) bool

func (*ProducerSet) IsLocalIP

func (produceSet *ProducerSet) IsLocalIP(ip string) bool

func (*ProducerSet) IsLocalPk

func (produceSet *ProducerSet) IsLocalPk(pk *secp256k1.PublicKey) bool

type RegisterAsProducerTransactionExecutor

type RegisterAsProducerTransactionExecutor struct {
}

func (*RegisterAsProducerTransactionExecutor) ExecuteTransaction

func (registerAsProducerTransactionExecutor *RegisterAsProducerTransactionExecutor) ExecuteTransaction(context *chain.ExecuteTransactionContext) *types.ExecuteTransactionResult

type RegisterAsProducerTransactionSelector

type RegisterAsProducerTransactionSelector struct{}

***********DEPLOY**************//

func (*RegisterAsProducerTransactionSelector) Select

func (registerAsProducerTransactionSelector *RegisterAsProducerTransactionSelector) Select(tx *types.Transaction) bool

type Response

type Response struct {
	Height uint64
	Magic  uint32
	Round  int
	BpKey  *secp256k1.PublicKey
	S      []byte
}

func (*Response) String

func (response *Response) String() string

type RewardCalculator

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

func NewRewardCalculator

func NewRewardCalculator(trieStore store.StoreInterface, sig *MultiSignature, producers ProducerSet, totalGasBalance *big.Int, height uint64) *RewardCalculator

func (*RewardCalculator) AccumulateRewards

func (calculator *RewardCalculator) AccumulateRewards(height uint64) error

AccumulateRewards credits,The leader gets half of the reward and other ,Other participants get the average of the other half

type Sender

type Sender interface {
	SendAsync(w p2p.MsgWriter, msgType uint64, msg interface{}) chan error
}

type Setup

type Setup struct {
	Height uint64
	Magic  uint32
	Round  int

	Msg []byte
}

func (*Setup) String

func (setup *Setup) String() string

Jump to

Keyboard shortcuts

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