chain_communication

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2019 License: LGPL-3.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	P2PMaxPeerCount    = 100
	NormalMaxPeerCount = 45
)
View Source
const (
	StatusMsg          = 0x00
	NewBlockHashesMsg  = 0x01
	TxMsg              = 0x02
	GetBlocksMsg       = 0x03
	BlocksMsg          = 0x04
	NewBlockMsg        = 0x07
	NewBlockByBloomMsg = 0x08

	// finder verifier
	GetVerifiersConnFromBootNode = 0x60
	BootNodeVerifiersConn        = 0x61

	// Estimator & InvBloom msg type
	EiNewBlockHashMsg    = 0x80
	EiEstimatorMsg       = 0x81
	EiNewBlockByBloomMsg = 0x82
	// for wait verify blocks
	EiWaitVerifyBlockHashMsg    = 0x83
	EiWaitVerifyEstimatorMsg    = 0x84
	EiWaitVerifyBlockByBloomMsg = 0x85

	TxV1Msg        = 0x71
	BlockHashesMsg = 0x72
	NewBlockV1Msg  = 0x73
	// verify result
	VerifyBlockHashResultMsg = 0x74
	GetVerifyResultMsg       = 0x75
	VerifyBlockResultMsg     = 0x76

	//verifier halt check protocol
	CurrentBlockNumberRequest    = 0x90
	CurrentBlockNumberResponse   = 0x91
	ProposeEmptyBlockMsg         = 0x92
	SendMinimalHashBlock         = 0x93
	SendMinimalHashBlockResponse = 0x94
)
View Source
const (
	MaxBlockFetch = 16
)
View Source
const ProtocolMaxMsgSize = 10 * 1024 * 1024

Variables

View Source
var (
	BroadcastTimeoutErr = errors.New("eiBlockTransport not broadcast more than 5 min, discard this transport")
)
View Source
var PbftMaxPeerCount int
View Source
var ProtocolBrokenError = []error{io.EOF}

Functions

func InPmBrokenError

func InPmBrokenError(err error) bool

func MakeDefaultMsgDecoder

func MakeDefaultMsgDecoder() *defaultMsgDecoder

func NewVfFetcher

func NewVfFetcher() *vfFetcher

Types

type AbsPeerManager

type AbsPeerManager interface {
	BestPeer() PmAbstractPeer
	SelfIsCurrentVerifier() bool
	SelfIsNextVerifier() bool
	HaveEnoughVerifiers(withOrganizeVSet bool) (missCur uint, missNext uint)
	GetVerifierBootNode() map[string]PmAbstractPeer
	GetSelfNode() *enode.Node
	ConnectPeer(node *enode.Node)
	CurrentVerifierPeersSet() AbstractPeerSet
	NextVerifierPeersSet() AbstractPeerSet
}

type AbstractPbftProtocolManager

type AbstractPbftProtocolManager interface {
	PeerManager
	GetCurrentConnectPeers() map[string]common.Address
	GetVerifierBootNode() map[string]PmAbstractPeer
	GetNextVerifierPeers() map[string]PmAbstractPeer
	SelfIsBootNode() bool
	GetSelfNode() *enode.Node
	MatchCurrentVerifiersToNext()
}

type AbstractPeerSet

type AbstractPeerSet interface {
	BestPeer() PmAbstractPeer

	GetPeers() map[string]PmAbstractPeer

	AddPeer(p PmAbstractPeer) error
	RemovePeer(id string) error
	ReplacePeers(newPeers map[string]PmAbstractPeer)

	Peer(id string) PmAbstractPeer
	Len() int
	Close()

	GetPeersInfo() []*p2p.CsPeerInfo
}

type AbstractProtocolManager

type AbstractProtocolManager interface {
	Start() error
	Stop()
	Protocols() []p2p.Protocol
}

manage peer

type BaseProtocolManager

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

func (*BaseProtocolManager) RemovePeer

func (pm *BaseProtocolManager) RemovePeer(id string)

func (*BaseProtocolManager) Start

func (pm *BaseProtocolManager) Start() error

func (*BaseProtocolManager) Stop

func (pm *BaseProtocolManager) Stop()

type BftOuter

type BftOuter struct {
	*NewBftOuterConfig
	// contains filtered or unexported fields
}

csbft outer msg(broadcast verify result)

func NewBftOuter

func NewBftOuter(config *NewBftOuterConfig) *BftOuter

func (*BftOuter) BroadcastVerifiedBlock

func (broadcaster *BftOuter) BroadcastVerifiedBlock(vr *model2.VerifyResult)

Broadcast this verification result so everyone can receive the result

func (*BftOuter) MsgHandlers

func (broadcaster *BftOuter) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

func (*BftOuter) SetBlockFetcher

func (broadcaster *BftOuter) SetBlockFetcher(blockFetcher *BlockFetcher)

type BlockFetcher

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

func NewBlockFetcher

func NewBlockFetcher(chainHeight chainHeightFunc, getBlock getBlockByHashFunc, saveBlock saveBlockFunc, blockBroadcaster blockBroadcasterFunc) *BlockFetcher

Make Block Fetcher

func (*BlockFetcher) DoFilter

func (f *BlockFetcher) DoFilter(peerID string, list []*catchupRlp) []*catchupRlp

func (*BlockFetcher) DoTask

func (f *BlockFetcher) DoTask(peerID string, vr *model2.VerifyResult, time time.Time)

call this function after getting verifyresult

func (*BlockFetcher) Notify

func (f *BlockFetcher) Notify(pID string, hash common.Hash, number uint64, time time.Time, vrFunc vrFunc) error

Receive the hash,call this function after determining to fetch this block

func (*BlockFetcher) Start

func (f *BlockFetcher) Start() error

func (*BlockFetcher) Stop

func (f *BlockFetcher) Stop()

type BroadcastDelegate

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

func NewBroadcastDelegate

func NewBroadcastDelegate(txPool TxPool, nodeConfig NodeConf, peerManager PeerManager, chain Chain, pbftNode PbftNode) *BroadcastDelegate

func (*BroadcastDelegate) BroadcastEiBlock

func (delegate *BroadcastDelegate) BroadcastEiBlock(block model.AbstractBlock)

func (*BroadcastDelegate) BroadcastMinedBlock

func (delegate *BroadcastDelegate) BroadcastMinedBlock(block model.AbstractBlock)

func (*BroadcastDelegate) BroadcastTx

func (delegate *BroadcastDelegate) BroadcastTx(txs []model.AbstractTransaction)

type Chain

type Chain interface {
	CurrentBlock() model.AbstractBlock
	GetSlot(block model.AbstractBlock) *uint64
	IsChangePoint(block model.AbstractBlock, isProcessPackageBlock bool) bool
	GetBlockByHash(hash common.Hash) model.AbstractBlock
	GetBlockByNumber(number uint64) model.AbstractBlock
	GetSeenCommit(height uint64) []model.AbstractVerification
	SaveBlock(block model.AbstractBlock, seenCommits []model.AbstractVerification) error
}

type ChainDownloader

type ChainDownloader interface {
	Start() error
	Stop()
	MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error
}

type CommunicationExecutable

type CommunicationExecutable interface {
	Start() error
	Stop()
}

type CommunicationService

type CommunicationService interface {
	MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error
}

specific function

type CsPmPeerSetManager

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

func (*CsPmPeerSetManager) AddPeer

func (ps *CsPmPeerSetManager) AddPeer(p PmAbstractPeer) error

func (*CsPmPeerSetManager) BestPeer

func (ps *CsPmPeerSetManager) BestPeer() PmAbstractPeer

func (*CsPmPeerSetManager) OrganizeVerifiersSet

func (ps *CsPmPeerSetManager) OrganizeVerifiersSet(curs []common.Address, nexts []common.Address)

You can't have a task peer in change verifiers, and you can't remove it. Take out all the peers and re-classify them into each set so that you don't have to move them.

func (*CsPmPeerSetManager) RemovePeer

func (ps *CsPmPeerSetManager) RemovePeer(pid string)

remove a peer from all set

type CsProtocolManager

type CsProtocolManager struct {
	BaseProtocolManager
	*CsProtocolManagerConfig
	// contains filtered or unexported fields
}

Peer management logic: According to their own roles, the peers are processed differently and added.

  1. I am Normal/MineMaster: I will insert all the peers into the nor peer set.
  2. i am a verifier (1)I am the current round and the next round: judging that the other party type . If boot then is added to the boot, if the other party is not the current and the next round then is added to the ordinary, if it is the next round, then join the cur and next set, otherwise belong Which round then join that set (2)I am the current round: I judge the other party type ,if it is boot and add it to the boot. If the other party is not the current one, it is added to the normal one. It is judged that the other party is the current round and joins the current round. (3)I am the next round: judge the other party is boot then add to the boot, the other party is not the next round, then join the ordinary, judge the other party is the next round to join the next round

1. Self is boot: if the other party is boot then is added to the boot, the other party is the current round then join the current round, the other party is the next round then join the next round, the others are added to the ordinary

switch verifiers logic:

func (*CsProtocolManager) BestPeer

func (pm *CsProtocolManager) BestPeer() PmAbstractPeer

func (*CsProtocolManager) BroadcastMsg

func (pm *CsProtocolManager) BroadcastMsg(msgCode uint64, msg interface{})

func (*CsProtocolManager) BroadcastMsgToTargetVerifiers

func (pm *CsProtocolManager) BroadcastMsgToTargetVerifiers(msgCode uint64, from []common.Address, msg interface{})

func (*CsProtocolManager) ChangeVerifiers

func (pm *CsProtocolManager) ChangeVerifiers()

change verifier, This method is only triggered when the change is made, so if there is a problem when in the peer handling process, it is difficult to correct it.

func (*CsProtocolManager) ConnectPeer

func (pm *CsProtocolManager) ConnectPeer(node *enode.Node)

func (*CsProtocolManager) CurrentVerifierPeersSet

func (pm *CsProtocolManager) CurrentVerifierPeersSet() AbstractPeerSet

func (*CsProtocolManager) GetCurrentConnectPeers

func (pm *CsProtocolManager) GetCurrentConnectPeers() map[string]common.Address

returns the map key of the current verifier -- > node id, value -- > address

func (*CsProtocolManager) GetCurrentVerifierPeers

func (pm *CsProtocolManager) GetCurrentVerifierPeers() map[string]PmAbstractPeer

provide get current verifier peers function for external package

func (*CsProtocolManager) GetNextVerifierPeers

func (pm *CsProtocolManager) GetNextVerifierPeers() map[string]PmAbstractPeer

func (*CsProtocolManager) GetPeer

func (pm *CsProtocolManager) GetPeer(id string) PmAbstractPeer

func (*CsProtocolManager) GetPeers

func (pm *CsProtocolManager) GetPeers() map[string]PmAbstractPeer

func (*CsProtocolManager) GetSelfNode

func (pm *CsProtocolManager) GetSelfNode() *enode.Node

func (*CsProtocolManager) GetVerifierBootNode

func (pm *CsProtocolManager) GetVerifierBootNode() map[string]PmAbstractPeer

func (*CsProtocolManager) HandShake

func (pm *CsProtocolManager) HandShake(p PmAbstractPeer) error

handle handshake

func (*CsProtocolManager) HaveEnoughVerifiers

func (pm *CsProtocolManager) HaveEnoughVerifiers(withOrganizeVSet bool) (mc uint, mn uint)

only for finder

func (*CsProtocolManager) IsSync

func (pm *CsProtocolManager) IsSync() bool

func (*CsProtocolManager) MatchCurrentVerifiersToNext

func (pm *CsProtocolManager) MatchCurrentVerifiersToNext()

prepare the next round of verifier

func (*CsProtocolManager) NextVerifierPeersSet

func (pm *CsProtocolManager) NextVerifierPeersSet() AbstractPeerSet

func (*CsProtocolManager) PrintPeerHealthCheck

func (pm *CsProtocolManager) PrintPeerHealthCheck()

func (*CsProtocolManager) Protocols

func (pm *CsProtocolManager) Protocols() []p2p.Protocol

func (*CsProtocolManager) RegisterCommunicationService

func (pm *CsProtocolManager) RegisterCommunicationService(cService CommunicationService, executable CommunicationExecutable)

provide register communication service for external package

func (*CsProtocolManager) RemovePeer

func (pm *CsProtocolManager) RemovePeer(id string)

func (*CsProtocolManager) SelfIsBootNode

func (pm *CsProtocolManager) SelfIsBootNode() bool

func (*CsProtocolManager) SelfIsCurrentVerifier

func (pm *CsProtocolManager) SelfIsCurrentVerifier() bool

determine if you are current verifier

func (*CsProtocolManager) SelfIsNextVerifier

func (pm *CsProtocolManager) SelfIsNextVerifier() bool

determine if you are the next round of verifier It is important to consider how this next concept should be defined after the block is inserted. isRemoteNext is used to mark whether you want to take the real next round when at the change point (because the taken next is actually the upcoming verifier, which is the real current)

func (*CsProtocolManager) SendFetchBlockMsg

func (pm *CsProtocolManager) SendFetchBlockMsg(msgCode uint64, from common.Address, msg *model.FetchBlockReqDecodeMsg) error

func (*CsProtocolManager) ShowPmInfo

func (pm *CsProtocolManager) ShowPmInfo() *p2p.CsPmPeerInfo

func (*CsProtocolManager) Start

func (pm *CsProtocolManager) Start() error

func (*CsProtocolManager) Stop

func (pm *CsProtocolManager) Stop()

type CsProtocolManagerConfig

type CsProtocolManagerConfig struct {
	ChainConfig     chain_config.ChainConfig
	Chain           Chain
	P2PServer       P2PServer
	NodeConf        NodeConf
	VerifiersReader VerifiersReader
	PbftNode        PbftNode
	MsgSigner       PbftSigner
}

type GetVerifiersReq

type GetVerifiersReq struct {
	ID uint64

	CurMiss  uint
	NextMiss uint
	Slot     uint64
}

type GetVerifiersResp

type GetVerifiersResp struct {
	ReqID uint64

	Cur     []string
	Next    []string
	ErrInfo string
}

type HandShakeData

type HandShakeData struct {
	ProtocolVersion uint32
	ChainID         *big.Int
	NetworkId       uint64
	//TD              *big.Int
	CurrentBlock       common.Hash
	CurrentBlockHeight uint64
	GenesisBlock       common.Hash
	NodeType           uint64
	NodeName           string
	// for pbft
	RawUrl string
}

type MineProtocolManager

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

func NewMineProtocolManager

func NewMineProtocolManager(msgHandler p2pMsgHandler) *MineProtocolManager

func (*MineProtocolManager) GetProtocol

func (pm *MineProtocolManager) GetProtocol() p2p.Protocol

type NewBftOuterConfig

type NewBftOuterConfig struct {
	Chain Chain
	Pm    PeerManager
}

type NewBlockBroadcaster

type NewBlockBroadcaster struct {
	*NewBlockBroadcasterConfig
	// contains filtered or unexported fields
}

func (*NewBlockBroadcaster) BroadcastBlock

func (broadcaster *NewBlockBroadcaster) BroadcastBlock(block model.AbstractBlock)

broadcast new block

func (*NewBlockBroadcaster) MsgHandlers

func (broadcaster *NewBlockBroadcaster) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

type NewBlockBroadcasterConfig

type NewBlockBroadcasterConfig struct {
	Chain    Chain
	Pm       PeerManager
	PbftNode PbftNode
}

type NewPbftDownloader

type NewPbftDownloader struct {
	*NewPbftDownloaderConfig
	// contains filtered or unexported fields
}

func MakeNewPbftDownloader

func MakeNewPbftDownloader(config *NewPbftDownloaderConfig) *NewPbftDownloader

func (*NewPbftDownloader) MsgHandlers

func (fd *NewPbftDownloader) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

func (*NewPbftDownloader) Start

func (fd *NewPbftDownloader) Start() error

func (*NewPbftDownloader) Stop

func (fd *NewPbftDownloader) Stop()

type NewPbftDownloaderConfig

type NewPbftDownloaderConfig struct {
	Chain    Chain
	Pm       PeerManager
	PbftNode PbftNode
	// contains filtered or unexported fields
}

type NewTxBroadcaster

type NewTxBroadcaster struct {
	*NewTxBroadcasterConfig
	// contains filtered or unexported fields
}

func (*NewTxBroadcaster) BroadcastTx

func (broadcaster *NewTxBroadcaster) BroadcastTx(txs []model.AbstractTransaction)

func (*NewTxBroadcaster) MsgHandlers

func (broadcaster *NewTxBroadcaster) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

type NewTxBroadcasterConfig

type NewTxBroadcasterConfig struct {
	P2PMsgDecoder P2PMsgDecoder
	TxPool        TxPool
	NodeConf      NodeConf
	Pm            PeerManager
}

type NodeConf

type NodeConf interface {
	GetNodeType() int
	GetNodeName() string
}

type P2PMsgDecoder

type P2PMsgDecoder interface {
	DecodeTxMsg(msg p2p.Msg) (model.AbstractTransaction, error)
	DecoderBlockMsg(msg p2p.Msg) (model.AbstractBlock, error)
	DecodeTxsMsg(msg p2p.Msg) (result []model.AbstractTransaction, err error)
}

type P2PPeer

type P2PPeer interface {
	Disconnect(reason p2p.DiscReason)
	ID() enode.ID
	RemoteAddr() net.Addr
}

interface are awesome

type P2PServer

type P2PServer interface {
	AddPeer(node *enode.Node)
	RemovePeer(node *enode.Node)
	Self() *enode.Node
}

type PbftNode

type PbftNode interface {
	OnNewWaitVerifyBlock(block model.AbstractBlock, id string)
	OnNewMsg(msg interface{}) error
	ChangePrimary(primary string)

	OnNewP2PMsg(msg p2p.Msg, p PmAbstractPeer) error
	AddPeer(p PmAbstractPeer) error

	OnEnterNewHeight(h uint64)
}

type PbftSigner

type PbftSigner interface {
	GetAddress() common.Address
	SetBaseAddress(address common.Address)
	SignHash(hash []byte) ([]byte, error)
	PublicKey() *ecdsa.PublicKey
	ValidSign(hash []byte, pubKey []byte, sign []byte) error
	Evaluate(account accounts.Account, seed []byte) (index [32]byte, proof []byte, err error)
}

type PeerManager

type PeerManager interface {
	GetPeers() map[string]PmAbstractPeer
	BestPeer() PmAbstractPeer
	IsSync() bool
	GetPeer(id string) PmAbstractPeer
	RemovePeer(id string)
}

type PmAbstractPeer

type PmAbstractPeer interface {
	// add node name
	NodeName() string
	// remote node type
	NodeType() uint64

	SendMsg(msgCode uint64, msg interface{}) error
	// remote node id
	ID() string
	// read peer msg
	ReadMsg() (p2p.Msg, error)

	GetHead() (common.Hash, uint64)

	SetHead(head common.Hash, height uint64)

	GetPeerRawUrl() string

	DisconnectPeer()

	RemoteVerifierAddress() (addr common.Address)
	// remote host and port
	RemoteAddress() net.Addr

	SetRemoteVerifierAddress(addr common.Address)
	SetNodeName(name string)
	SetNodeType(nt uint64)
	SetPeerRawUrl(rawUrl string)

	SetNotRunning()
	IsRunning() bool

	GetCsPeerInfo() *p2p.CsPeerInfo
}

is responsible for sending and receiving messages

type StatusData

type StatusData struct {
	HandShakeData

	PubKey []byte
	Sign   []byte
}

for hand shake

func (*StatusData) DataHash

func (status *StatusData) DataHash() common.Hash

func (*StatusData) Sender

func (status *StatusData) Sender() (result common.Address)

type TxPool

type TxPool interface {
	AddLocal(tx model.AbstractTransaction) error
	AddRemote(tx model.AbstractTransaction) error
	AddLocals(txs []model.AbstractTransaction) []error
	AddRemotes(txs []model.AbstractTransaction) []error
	ConvertPoolToMap() map[common.Hash]model.AbstractTransaction
	Stats() (int, int)
	GetTxsEstimator(broadcastBloom *iblt.Bloom) *iblt.HybridEstimator
	Pending() (map[common.Address][]model.AbstractTransaction, error)
	Queueing() (map[common.Address][]model.AbstractTransaction, error)
}

type VFinder

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

func NewVFinder

func NewVFinder(chain Chain, peerManager AbsPeerManager, chainCfg chain_config.ChainConfig) *VFinder

func (*VFinder) MsgHandlers

func (vf *VFinder) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

func (*VFinder) Start

func (vf *VFinder) Start() error

func (*VFinder) Stop

func (vf *VFinder) Stop()

type VFinderBoot

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

func NewVFinderBoot

func NewVFinderBoot(peerManager AbsPeerManager, chain Chain) *VFinderBoot

func (*VFinderBoot) MsgHandlers

func (vfb *VFinderBoot) MsgHandlers() map[uint64]func(msg p2p.Msg, p PmAbstractPeer) error

func (*VFinderBoot) OnGetVerifiersReq

func (vfb *VFinderBoot) OnGetVerifiersReq(msg p2p.Msg, p PmAbstractPeer) error

type VerifiersReader

type VerifiersReader interface {
	CurrentVerifiers() []common.Address
	NextVerifiers() []common.Address
	ShouldChangeVerifier() bool
}

Jump to

Keyboard shortcuts

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