p2p

package
v3.3.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2019 License: LGPL-3.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

errors

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

errors

Functions

func Ping added in v1.1.0

func Ping(rw http.ResponseWriter, r *http.Request)

Ping returns a "pong" to client.

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
	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 *common.P2PConfig) (*NetService, error)

NewNetService returns a NetService instance with the config argument.

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) 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, direction connDirection) *Peer

NewPeer returns a new instance of Peer struct.

func (*Peer) Addr added in v1.1.0

func (p *Peer) Addr() string

Addr return the address.

func (*Peer) ID added in v1.1.0

func (p *Peer) ID() string

ID return the net id.

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) AllNeighborCount added in v1.1.0

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 added in v1.1.0

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 added in v1.1.0

func (pm *PeerManager) PutIPToBlack(ip string)

PutIPToBlack puts the ip to black list.

func (*PeerManager) PutPIDToBlack added in v1.1.0

func (pm *PeerManager) PutPIDToBlack(pid peer.ID)

PutPIDToBlack puts the PID and corresponding ip to black list.

func (*PeerManager) PutPeerToBlack added in v1.1.0

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.

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([]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.

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