Documentation ¶
Index ¶
- Variables
- func Ping(rw http.ResponseWriter, r *http.Request)
- type IncomingMessage
- type MessagePriority
- type MessageType
- type NetService
- type Peer
- type PeerID
- type PeerManager
- func (pm *PeerManager) AddNeighbor(p *Peer)
- func (pm *PeerManager) AllNeighborCount() int
- func (pm *PeerManager) Broadcast(data []byte, typ MessageType, mp MessagePriority)
- func (pm *PeerManager) CloseAllNeighbors()
- func (pm *PeerManager) ConnectBPs(ids []string)
- func (pm *PeerManager) Deregister(id string, mTyps ...MessageType)
- func (pm *PeerManager) DumpRoutingTable()
- func (pm *PeerManager) GetAllNeighbors() []*Peer
- func (pm *PeerManager) GetNeighbor(peerID peer.ID) *Peer
- func (pm *PeerManager) HandleMessage(msg *p2pMessage, peerID peer.ID)
- func (pm *PeerManager) HandleStream(s libnet.Stream, direction connDirection)
- func (pm *PeerManager) LoadRoutingTable()
- func (pm *PeerManager) NeighborCount(direction connDirection) int
- func (pm *PeerManager) NeighborStat() map[string]interface{}
- func (pm *PeerManager) PutIPToBlack(ip string)
- func (pm *PeerManager) PutPIDToBlack(pid peer.ID)
- func (pm *PeerManager) PutPeerToBlack(id string)
- 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 ( ErrMessageChannelFull = errors.New("message channel is full") ErrDuplicateMessage = errors.New("reduplicate message") )
errors
var (
)errors
Functions ¶
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 PublishTx UrgentMessage = 1 NormalMessage = 2 )
consts.
func (MessageType) String ¶
func (m MessageType) String() string
type NetService ¶
type NetService struct { *PeerManager // contains filtered or unexported fields }
NetService is the implementation of Service interface.
func NewNetService ¶
func NewNetService(config *config.P2PConfig) (*NetService, error)
NewNetService returns a NetService instance with the config argument.
func (*NetService) LocalAddrs ¶
func (ns *NetService) LocalAddrs() []multiaddr.Multiaddr
LocalAddrs returns the local's multiaddrs.
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, direction connDirection) *Peer
NewPeer returns a new instance of Peer struct.
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 *config.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) AllNeighborCount ¶
func (pm *PeerManager) AllNeighborCount() int
AllNeighborCount returns the total neighbor amount.
func (*PeerManager) Broadcast ¶
func (pm *PeerManager) Broadcast(data []byte, typ MessageType, mp MessagePriority)
Broadcast sends message to all the neighbors.
func (*PeerManager) CloseAllNeighbors ¶
func (pm *PeerManager) CloseAllNeighbors()
CloseAllNeighbors close all connections.
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) GetAllNeighbors ¶
func (pm *PeerManager) GetAllNeighbors() []*Peer
GetAllNeighbors returns the peer of the given peerID from the neighbor list.
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, direction connDirection)
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(direction connDirection) int
NeighborCount returns the neighbor amount of the given direction.
func (*PeerManager) NeighborStat ¶
func (pm *PeerManager) NeighborStat() map[string]interface{}
NeighborStat dumps neighbors' status for debug.
func (*PeerManager) PutIPToBlack ¶
func (pm *PeerManager) PutIPToBlack(ip string)
PutIPToBlack puts the ip to black list.
func (*PeerManager) PutPIDToBlack ¶
func (pm *PeerManager) PutPIDToBlack(pid peer.ID)
PutPIDToBlack puts the PID and corresponding ip to black list.
func (*PeerManager) PutPeerToBlack ¶
func (pm *PeerManager) PutPeerToBlack(id string)
PutPeerToBlack puts the peer's PID and IP to black list and close the connection.
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([]string) PutPeerToBlack(string) Broadcast([]byte, MessageType, MessagePriority) SendToPeer(PeerID, []byte, MessageType, MessagePriority) Register(string, ...MessageType) chan IncomingMessage Deregister(string, ...MessageType) GetAllNeighbors() []*Peer }
Service defines all the API of p2p package.