p2p

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2018 License: LGPL-3.0 Imports: 33 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStreamCountExceed  = errors.New("stream count exceed")
	ErrMessageChannelFull = errors.New("message channel is full")
	ErrDuplicateMessage   = errors.New("reduplicate message")
)

errors

View Source
var (
	ErrPortUnavailable = errors.New("port is unavailable")
)

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) Data

func (m *IncomingMessage) Data() []byte

Data returns the bytes.

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 MessagePriority

type MessagePriority uint8

MessagePriority represents the message priority.

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) ID

func (ns *NetService) ID() string

ID returns the host's ID.

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.

func (*NetService) Start

func (ns *NetService) Start() error

Start starts the jobs.

func (*NetService) Stop

func (ns *NetService) Stop()

Stop stops all the jobs.

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) AddStream

func (p *Peer) AddStream(stream libnet.Stream) error

AddStream tries to add a Stream in stream pool.

func (*Peer) CloseStream

func (p *Peer) CloseStream(stream libnet.Stream)

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.

func (*Peer) Start

func (p *Peer) Start()

Start starts peer's loop.

func (*Peer) Stop

func (p *Peer) Stop()

Stop stops peer's loop and cuts off the TCP connection.

type PeerID

type PeerID = peer.ID

PeerID is the alias of peer.ID

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.

func (*PeerManager) Start

func (pm *PeerManager) Start()

Start starts peer manager's job.

func (*PeerManager) Stop

func (pm *PeerManager) Stop()

Stop stops peer manager's loop.

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.

Directories

Path Synopsis
Package p2p_mock is a generated GoMock package.
Package p2p_mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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