p2p

package
v0.0.0-...-da200cc Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2019 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

package p2p is a low-level library responsible for peer discovery and sending/receiving messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Topic is a unique string representing the pubsub topic. Only Nodes which
	// have the same topic will share messages with one another.
	Topic string
	// ListenPort is the port on which to listen for new connections. It can be
	// set to 0 to make the OS automatically choose any available port.
	ListenPort int
	// Insecure controls whether or not messages should be encrypted. It should
	// always be set to false in production.
	Insecure bool
	// PrivateKey is the private key which will be used for signing messages and
	// generating a peer ID.
	PrivateKey p2pcrypto.PrivKey
	// MessageHandler is an interface responsible for validating, storing, and
	// finding new messages to share.
	MessageHandler MessageHandler
	// RendezvousString is a unique identifier for the rendezvous point. This node
	// will attempt to find peers with the same Rendezvous string.
	RendezvousString string
	// UseBootstrapList determines whether or not to use the list of predetermined
	// peers to bootstrap the DHT for peer discovery.
	UseBootstrapList bool
}

Config contains configuration options for a Node.

type Message

type Message struct {
	// From is the peer ID of the peer who sent the message.
	From peer.ID
	// Data is the underlying data for the message.
	Data []byte
}

type MessageHandler

type MessageHandler interface {
	// HandleMessages is called whenever new messages are received. It should only
	// return an error if there was a problem handling the messages. It should not
	// return an error for invalid or duplicate messages.
	HandleMessages([]*Message) error
	// GetMessagesToShare returns up to max messages to be shared with peers.
	GetMessagesToShare(max int) ([][]byte, error)
}

MessageHandler is an interface responsible for validating and storing messages as well as selecting messages which are ready to be shared.

type Node

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

Node is the main type for the p2p package. It represents a particpant in the 0x Mesh network who is capable of sending, receiving, validating, and storing messages.

func New

func New(config Config) (*Node, error)

New creates a new Node with the given config.

func (*Node) AddPeerScore

func (n *Node) AddPeerScore(id peer.ID, tag string, diff int)

AddPeerScore adds diff to the current score for a given peer. Tag is a unique identifier for the score. A peer's total score is the sum of the scores associated with each tag. Peers that end up with a low total score will eventually be disconnected.

func (*Node) Close

func (n *Node) Close() error

Close closes the Node and any active connections.

func (*Node) Connect

func (n *Node) Connect(ctx context.Context, peerInfo peerstore.PeerInfo) error

Connect ensures there is a connection between this host and the peer with given peerInfo. If there is not an active connection, Connect will dial the peer, and gateway until a connection is open, or an error is returned.

func (*Node) ID

func (n *Node) ID() peer.ID

ID returns the peer id of the node.

func (*Node) Multiaddrs

func (n *Node) Multiaddrs() []ma.Multiaddr

Multiaddrs returns all multi addresses at which the node is dialable.

func (*Node) SetPeerScore

func (n *Node) SetPeerScore(id peer.ID, tag string, val int)

SetPeerScore sets the current score for a given peer (overwriting any previous value with the same tag). Tag is a unique identifier for the score. A peer's total score is the sum of the scores associated with each tag. Peers that end up with a low total score will eventually be disconnected.

func (*Node) Start

func (n *Node) Start() error

Start causes the Node to continuously send messages to and receive messages from its peers. It blocks until an error is encountered or `Stop` is called.

func (*Node) UnsetPeerScore

func (n *Node) UnsetPeerScore(id peer.ID, tag string)

UnsetPeerScore removes any scores associated with the given tag for a peer (i.e., they will no longer be counted toward the peers total score).

Jump to

Keyboard shortcuts

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