bft

package
v0.0.0-...-a173fca Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: LGPL-3.0 Imports: 37 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

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

The messages of this module can only be used in functions that call this module (consensus and its corresponding submodules) For example, the MsgTypeCommitment message, defined in consensus, must be sent and received using a function in consensus

View Source
const MAX_PRODUCER = 21
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",
	}

	DefaultConfigMainnet = BftConfig{
		MyPk:           nil,
		StartMiner:     true,
		ProducerNum:    params.GenesisProducerNumMainnet,
		BlockInterval:  params.BlockInterval,
		ChangeInterval: params.ChangeInterval,
	}

	DefaultConfigTestnet = BftConfig{
		MyPk:           nil,
		StartMiner:     true,
		ProducerNum:    params.GenesisProducerNumTestnet,
		BlockInterval:  params.BlockInterval,
		ChangeInterval: params.ChangeInterval,
	}
)
View Source
var (
	MinerPrefix = []byte("miner")
)
View Source
var NumberOfMsg = 7

Functions

func GetCandidates

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

Types

type BftConfig

type BftConfig struct {
	MyPk           *secp256k1.PublicKey `json:"mypk"`
	StartMiner     bool                 `json:"startMiner"`
	ProducerNum    int                  `json:"producerNum"`
	BlockInterval  int16                `json:"blockInterval"` //unit second
	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) ([]types.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
	NetType      params.NetType

	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(netType params.NetType) *BftConfig

func (*BftConsensusService) GetProducers

func (bftConsensusService *BftConsensusService) GetProducers(height uint64, topN int) ([]chainTypes.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 (*BlockMultiSigValidator) ExecuteBlock

func (blockMultiSigValidator *BlockMultiSigValidator) ExecuteBlock(context *block.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: consensus api usage: Query the consensus node function prefix:consensus

func (*ConsensusApi) ChangeWaitTime

func (consensusApi *ConsensusApi) ChangeWaitTime(waitTime int)
 name: changeWaitTime
 usage: Modify the waiting time of the leader (ms)
 params:
	1.wait time (ms)
 return:
 example:
	curl http://localhost:10085 -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: Gets the current mining node
 params:
 return: mining nodes's pub key
 example:
	curl http://localhost:10085 -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() ([]*types.Producer, error)

func (*ConsensusOp) SaveProducer

func (consensusOp *ConsensusOp) SaveProducer(p []*types.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) ([]types.Producer, error)

type IConsenMsg

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

type IRewardCalculator

type IRewardCalculator interface {
	AccumulateRewards(sig *MultiSignature, Producers types.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, chBestHeight <-chan uint64) (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, chNewBlock <-chan uint64) (IConsenMsg, error)

func (*Member) Reset

func (member *Member) Reset()

func (*Member) WaitChallenge

func (member *Member) WaitChallenge()

func (*Member) WaitSetUp

func (member *Member) WaitSetUp(round int, chNewBlock <-chan uint64)

type MemberInfo

type MemberInfo struct {
	Peer     types.IPeerInfo
	Producer *chainType.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 RegisterAsProducerTransactionExecutor

type RegisterAsProducerTransactionExecutor struct {
}

func (*RegisterAsProducerTransactionExecutor) ExecuteTransaction

func (registerAsProducerTransactionExecutor *RegisterAsProducerTransactionExecutor) ExecuteTransaction(context *transactions.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 types.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