Documentation ¶
Index ¶
- Variables
- type IncomingMessage
- type MessagePriority
- type MessageType
- type NetService
- func (ns *NetService) Broadcast(data []byte, typ MessageType, mp MessagePriority)
- func (ns *NetService) ConnectBPs(ids []string)
- func (ns *NetService) Deregister(id string, typs ...MessageType)
- func (ns *NetService) ID() string
- func (ns *NetService) LocalAddrs() []multiaddr.Multiaddr
- func (ns *NetService) NeighborStat() map[string]interface{}
- func (ns *NetService) Register(id string, typs ...MessageType) chan IncomingMessage
- func (ns *NetService) SendToPeer(peerID peer.ID, data []byte, typ MessageType, mp MessagePriority)
- func (ns *NetService) Start() error
- func (ns *NetService) Stop()
- type Peer
- type PeerID
- type PeerManager
- func (pm *PeerManager) AddNeighbor(p *Peer)
- func (pm *PeerManager) Broadcast(data []byte, typ MessageType, mp MessagePriority)
- func (pm *PeerManager) ConnectBPs(ids []string)
- func (pm *PeerManager) Deregister(id string, mTyps ...MessageType)
- func (pm *PeerManager) DumpRoutingTable()
- func (pm *PeerManager) GetNeighbor(peerID peer.ID) *Peer
- func (pm *PeerManager) HandleMessage(msg *p2pMessage, peerID peer.ID)
- func (pm *PeerManager) HandleStream(s libnet.Stream)
- func (pm *PeerManager) LoadRoutingTable()
- func (pm *PeerManager) NeighborCount() int
- func (pm *PeerManager) NeighborStat() map[string]interface{}
- func (pm *PeerManager) Register(id string, mTyps ...MessageType) chan IncomingMessage
- func (pm *PeerManager) RemoveNeighbor(peerID peer.ID)
- func (pm *PeerManager) SendToPeer(peerID peer.ID, data []byte, typ MessageType, mp MessagePriority)
- func (pm *PeerManager) Start()
- func (pm *PeerManager) Stop()
- type Service
Constants ¶
This section is empty.
Variables ¶
var ( ErrStreamCountExceed = errors.New("stream count exceed") ErrMessageChannelFull = errors.New("message channel is full") ErrDuplicateMessage = errors.New("reduplicate message") )
errors
var (
)errors
Functions ¶
This section is empty.
Types ¶
type IncomingMessage ¶
type IncomingMessage struct {
// contains filtered or unexported fields
}
IncomingMessage is the struct which would be sent to the upstream.
func NewIncomingMessage ¶
func NewIncomingMessage(peerID PeerID, data []byte, messageType MessageType) *IncomingMessage
NewIncomingMessage returns a IncomingMessage instance.
func (*IncomingMessage) From ¶
func (m *IncomingMessage) From() PeerID
From returns the peerID who sends the message.
func (*IncomingMessage) Type ¶
func (m *IncomingMessage) Type() MessageType
Type returns the message type.
type MessageType ¶
type MessageType uint16
MessageType represents the message type.
const ( RoutingTableQuery MessageType RoutingTableResponse NewBlock NewBlockHash NewBlockRequest SyncBlockHashRequest SyncBlockHashResponse SyncBlockRequest SyncBlockResponse SyncHeight PublishTxRequest UrgentMessage = 1 NormalMessage = 2 )
consts.
func (MessageType) String ¶
func (m MessageType) String() string
type NetService ¶
type NetService struct {
// contains filtered or unexported fields
}
NetService is the implementation of Service interface.
func NewNetService ¶
func NewNetService(config *common.P2PConfig) (*NetService, error)
NewNetService returns a NetService instance with the config argument.
func (*NetService) Broadcast ¶
func (ns *NetService) Broadcast(data []byte, typ MessageType, mp MessagePriority)
Broadcast broadcasts the data.
func (*NetService) ConnectBPs ¶
func (ns *NetService) ConnectBPs(ids []string)
ConnectBPs makes the local host connected to the block producers directly.
func (*NetService) Deregister ¶
func (ns *NetService) Deregister(id string, typs ...MessageType)
Deregister deregisters a message channel of the given types.
func (*NetService) LocalAddrs ¶
func (ns *NetService) LocalAddrs() []multiaddr.Multiaddr
LocalAddrs returns the local's multiaddrs.
func (*NetService) NeighborStat ¶
func (ns *NetService) NeighborStat() map[string]interface{}
NeighborStat dumps neighbors' status for debug.
func (*NetService) Register ¶
func (ns *NetService) Register(id string, typs ...MessageType) chan IncomingMessage
Register registers a message channel of the given types.
func (*NetService) SendToPeer ¶
func (ns *NetService) SendToPeer(peerID peer.ID, data []byte, typ MessageType, mp MessagePriority)
SendToPeer sends data to the given peer.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a neighbor which we connect directily.
Peer's jobs are:
- managing streams which are responsible for sending and reading messages.
- recording messages we have sent and received so as to reduce redundant message in network.
- maintaning a priority queue of message to be sending.
func NewPeer ¶
func NewPeer(stream libnet.Stream, pm *PeerManager) *Peer
NewPeer returns a new instance of Peer struct.
func (*Peer) CloseStream ¶
CloseStream closes a stream and decrease the stream count.
Notice that it only closes the stream for writing. Reading will still work (that is, the remote side can still write).
func (*Peer) SendMessage ¶
func (p *Peer) SendMessage(msg *p2pMessage, mp MessagePriority, deduplicate bool) error
SendMessage puts message into the corresponding channel.
type PeerManager ¶
type PeerManager struct {
// contains filtered or unexported fields
}
PeerManager manages all peers we connect directily.
PeerManager's jobs are:
- holding a certain amount of peers.
- handling messages according to its type.
- discovering peers and maintaining routing table.
func NewPeerManager ¶
func NewPeerManager(host host.Host, config *common.P2PConfig) *PeerManager
NewPeerManager returns a new instance of PeerManager struct.
func (*PeerManager) AddNeighbor ¶
func (pm *PeerManager) AddNeighbor(p *Peer)
AddNeighbor starts a peer and adds it to the neighbor list.
func (*PeerManager) Broadcast ¶
func (pm *PeerManager) Broadcast(data []byte, typ MessageType, mp MessagePriority)
Broadcast sends message to all the neighbors.
func (*PeerManager) ConnectBPs ¶
func (pm *PeerManager) ConnectBPs(ids []string)
ConnectBPs makes the local host connected to the block producers directly.
func (*PeerManager) Deregister ¶
func (pm *PeerManager) Deregister(id string, mTyps ...MessageType)
Deregister deregisters a message channel of the given types.
func (*PeerManager) DumpRoutingTable ¶
func (pm *PeerManager) DumpRoutingTable()
DumpRoutingTable saves routing table in file.
func (*PeerManager) GetNeighbor ¶
func (pm *PeerManager) GetNeighbor(peerID peer.ID) *Peer
GetNeighbor returns the peer of the given peerID from the neighbor list.
func (*PeerManager) HandleMessage ¶
func (pm *PeerManager) HandleMessage(msg *p2pMessage, peerID peer.ID)
HandleMessage handles messages according to its type.
func (*PeerManager) HandleStream ¶
func (pm *PeerManager) HandleStream(s libnet.Stream)
HandleStream handles the incoming stream.
It checks whether the remote peer already exists. If the peer is new and the neighbor count doesn't reach the threshold, it adds the peer into the neighbor list. If peer already exits, just add the stream to the peer. In other cases, reset the stream.
func (*PeerManager) LoadRoutingTable ¶
func (pm *PeerManager) LoadRoutingTable()
LoadRoutingTable reads routing table file and parses it.
func (*PeerManager) NeighborCount ¶
func (pm *PeerManager) NeighborCount() int
NeighborCount returns the neighbor amount.
func (*PeerManager) NeighborStat ¶
func (pm *PeerManager) NeighborStat() map[string]interface{}
NeighborStat dumps neighbors' status for debug.
func (*PeerManager) Register ¶
func (pm *PeerManager) Register(id string, mTyps ...MessageType) chan IncomingMessage
Register registers a message channel of the given types.
func (*PeerManager) RemoveNeighbor ¶
func (pm *PeerManager) RemoveNeighbor(peerID peer.ID)
RemoveNeighbor stops a peer and removes it from the neighbor list.
func (*PeerManager) SendToPeer ¶
func (pm *PeerManager) SendToPeer(peerID peer.ID, data []byte, typ MessageType, mp MessagePriority)
SendToPeer sends message to the specified peer.
type Service ¶
type Service interface { Start() error Stop() ID() string ConnectBPs(ids []string) Broadcast([]byte, MessageType, MessagePriority) SendToPeer(PeerID, []byte, MessageType, MessagePriority) Register(string, ...MessageType) chan IncomingMessage Deregister(string, ...MessageType) NeighborStat() map[string]interface{} }
Service defines all the API of p2p package.