p2p

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const ClientVersion = 0

ClientVersion is the version of the client.

View Source
const DHTProtocolID = "/phore/kad/1.0.0"

DHTProtocolID is the protocol ID used for DHT for Phore.

Variables

This section is empty.

Functions

func ParseInitialConnections

func ParseInitialConnections(in string) ([]peerstore.PeerInfo, error)

ParseInitialConnections parses comma separated multiaddresses into peer info.

Types

type ChainProvider added in v0.2.4

type ChainProvider interface {
	Height() uint64
	GenesisHash() chainhash.Hash
}

ChainProvider is the interface from the blockchain to the host node packages.

type Discovery

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

Discovery is the service to discover other peers.

func NewDiscovery

func NewDiscovery(ctx context.Context, host *HostNode, discoveryOptions DiscoveryOptions) *Discovery

NewDiscovery creates a new discovery service.

func (Discovery) HandlePeerFound

func (d Discovery) HandlePeerFound(pi ps.PeerInfo)

HandlePeerFound registers the peer with the host.

func (Discovery) StartDiscovery

func (d Discovery) StartDiscovery() error

StartDiscovery protocols. Currently, this supports discovery via multicast DNS peer discovery.

TODO: add other discovery protocols such as DHT, etc.

type DiscoveryOptions

type DiscoveryOptions struct {
	// Optional. Each element is a peer address to connect with.
	PeerAddresses []ps.PeerInfo

	MDNS MDNSOptions
}

DiscoveryOptions is the options used to discover peers

func NewDiscoveryOptions

func NewDiscoveryOptions() DiscoveryOptions

NewDiscoveryOptions creates a DiscoveryOptions with default values

type Handler

type Handler struct {
	ID          uint64
	MessageName string
}

Handler is a message handler representation.

type HostNode

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

HostNode is the node for p2p host It's the low level P2P communication layer, the App class handles high level protocols The RPC communication is hanlded by App, not HostNode

func NewHostNode

func NewHostNode(listenAddress multiaddr.Multiaddr, publicKey crypto.PubKey, privateKey crypto.PrivKey, options DiscoveryOptions, timeoutInterval time.Duration, maxPeers int, heartbeatInterval time.Duration, chainProvider ChainProvider) (*HostNode, error)

NewHostNode creates a host node

func (*HostNode) Broadcast

func (node *HostNode) Broadcast(topic string, data []byte) error

Broadcast broadcasts a message to the network for a topic.

func (*HostNode) Connect

func (node *HostNode) Connect(peerInfo peerstore.PeerInfo) (*Peer, error)

Connect connects to a peer that we're not already connected to.

func (*HostNode) Connected

func (node *HostNode) Connected() bool

Connected checks if the host node is connected.

func (*HostNode) DisconnectPeer

func (node *HostNode) DisconnectPeer(peer *Peer) error

DisconnectPeer disconnects a peer

func (*HostNode) FindPeerByID

func (node *HostNode) FindPeerByID(id peer.ID) (*Peer, bool)

FindPeerByID finds a peer node by ID, returns nil if not found

func (*HostNode) GetContext

func (node *HostNode) GetContext() context.Context

GetContext returns the context

func (*HostNode) GetHost

func (node *HostNode) GetHost() host.Host

GetHost returns the host

func (*HostNode) GetPeerByID

func (node *HostNode) GetPeerByID(id peer.ID) *Peer

GetPeerByID gets a peer by ID or returns nil if we aren't connected.

func (*HostNode) GetPeerList

func (node *HostNode) GetPeerList() []*Peer

GetPeerList returns a list of all peers.

func (*HostNode) GetPublicKey

func (node *HostNode) GetPublicKey() *crypto.PubKey

GetPublicKey returns the public key

func (*HostNode) IsPeerConnected added in v0.1.2

func (node *HostNode) IsPeerConnected(peerInfo peerstore.PeerInfo) bool

IsPeerConnected checks if a peer is connected

func (*HostNode) PeerDiscovered

func (node *HostNode) PeerDiscovered(pi peerstore.PeerInfo)

PeerDiscovered is run when peers are discovered.

func (*HostNode) PeersConnected

func (node *HostNode) PeersConnected() int

PeersConnected checks how many peers are connected.

func (*HostNode) RegisterMessageHandler

func (node *HostNode) RegisterMessageHandler(messageName string, handler MessageHandler) Handler

RegisterMessageHandler registers a message handler

func (*HostNode) RemoveMessageHandler

func (node *HostNode) RemoveMessageHandler(handler Handler)

RemoveMessageHandler deregisters a message handler.

func (*HostNode) StartDiscovery

func (node *HostNode) StartDiscovery() error

StartDiscovery starts the host node discovering peers.

func (*HostNode) SubscribeMessage

func (node *HostNode) SubscribeMessage(topic string, handler func([]byte, peer.ID)) (*pubsub.Subscription, error)

SubscribeMessage registers a handler for a network topic.

func (*HostNode) UnsubscribeMessage

func (node *HostNode) UnsubscribeMessage(subscription *pubsub.Subscription)

UnsubscribeMessage cancels a subscription to a topic.

type MDNSOptions

type MDNSOptions struct {
	Enabled  bool
	Interval time.Duration
}

MDNSOptions are options for the MDNS discovery mechanism.

type Message

type Message struct {
	From    *Peer
	Message proto.Message
}

Message is a single message from a single peer.

type MessageHandler

type MessageHandler func(peer *Peer, message proto.Message) error

MessageHandler is a function to handle messages.

type Peer

type Peer struct {
	ID         peer.ID
	Outbound   bool
	Connecting bool
	// The last nonce we sent them
	LastPingNonce     uint64
	LastMessageTime   time.Time
	Version           uint64
	ProcessingRequest bool
	// contains filtered or unexported fields
}

Peer is a representation of an external peer.

func (*Peer) Disconnect added in v0.1.5

func (node *Peer) Disconnect()

Disconnect disconnects from a peer cleanly

func (*Peer) GetPeerInfo

func (node *Peer) GetPeerInfo() *peerstore.PeerInfo

GetPeerInfo returns the peer info

func (*Peer) HandleVersionMessage

func (node *Peer) HandleVersionMessage(message *pb.VersionMessage) error

HandleVersionMessage handles VersionMessage from this peer

func (*Peer) IsConnected

func (node *Peer) IsConnected() bool

IsConnected checks if the peers is considered connected.

func (*Peer) IsInbound

func (node *Peer) IsInbound() bool

IsInbound returns true if the connection is an inbound

func (*Peer) IsOutbound

func (node *Peer) IsOutbound() bool

IsOutbound returns true if the connection is an outbound

func (*Peer) Reject

func (node *Peer) Reject(message string) error

Reject sends reject message and disconnect from the peer

func (*Peer) SendMessage

func (node *Peer) SendMessage(message proto.Message)

SendMessage sends a protobuf message to this peer

Jump to

Keyboard shortcuts

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