p2p

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: GPL-3.0 Imports: 39 Imported by: 4

Documentation

Overview

Package p2p implements qri peer-to-peer communication.

Index

Constants

View Source
const (
	// MtDatasetLog gets log of a dataset
	MtDatasetLog = MsgType("dataset_log")
	// NumPeersToContact is the number of peers to contact with messages
	NumPeersToContact = 15
)
View Source
const (
	// QriProtocolID is the top level Protocol Identifier
	QriProtocolID = protocol.ID("/qri")
	// QriServiceTag tags the type & version of the qri service
	QriServiceTag = "qri/0.8.0"
)
View Source
const MtConnected = MsgType("connected")

MtConnected announces a peer connecting to the network

View Source
const MtDatasetInfo = MsgType("dataset_info")

MtDatasetInfo gets info on a dataset

View Source
const MtDatasets = MsgType("list_datasets")

MtDatasets is a dataset list message

View Source
const MtEvents = MsgType("list_events")

MtEvents is a message to announce added / removed datasets to the network

View Source
const MtLogDiff = MsgType("log_diff")

MtLogDiff gets info on a dataset

View Source
const (
	// MtPing is a ping/pong message
	MtPing = MsgType("ping")
)
View Source
const MtProfile = MsgType("profile")

MtProfile is a peer info message

View Source
const MtResolveDatasetRef = MsgType("resolve_dataset_ref")

MtResolveDatasetRef resolves a dataset reference

View Source
const MtSearch = MsgType("search")

MtSearch is a search message

Variables

View Source
var ErrNotConnected = fmt.Errorf("no p2p connection")

ErrNotConnected is for a missing required network connection

View Source
var ErrQriProtocolNotSupported = fmt.Errorf("peer doesn't support the qri protocol")

ErrQriProtocolNotSupported is returned when a connection can't be upgraded

View Source
var MtQriPeers = MsgType("qri_peers")

MtQriPeers is a request to get a list of known qri peers

Functions

func MakeHandlers added in v0.3.0

func MakeHandlers(n *QriNode) map[MsgType]HandlerFunc

MakeHandlers generates a map of MsgTypes to their corresponding handler functions

func NewMessageID added in v0.3.0

func NewMessageID() string

NewMessageID generates a random message identifier TODO - replace with UUIDs

func NewTestableQriNode added in v0.5.2

func NewTestableQriNode(r repo.Repo, p2pconf *config.P2P) (p2ptest.TestablePeerNode, error)

NewTestableQriNode creates a new node, as a TestablePeerNode, usable by testing utilities.

func ParseMultiaddrs

func ParseMultiaddrs(addrs []string) (maddrs []ma.Multiaddr, err error)

ParseMultiaddrs turns a slice of strings into a slice of Multiaddrs

Types

type DatasetLogRequest added in v0.5.5

type DatasetLogRequest struct {
	Ref    repo.DatasetRef
	Limit  int
	Offset int
}

DatasetLogRequest encapsulates options for requesting dataset history

type DatasetLogResponse added in v0.5.5

type DatasetLogResponse struct {
	History []repo.DatasetRef
	Err     error
}

DatasetLogResponse encapsulates option for responding to a dataset history request

type DatasetsListParams added in v0.3.0

type DatasetsListParams struct {
	Term   string
	Limit  int
	Offset int
}

DatasetsListParams encapsulates options for requesting datasets

type EventsParams added in v0.3.0

type EventsParams struct {
	Limit, Offset int
	Since         time.Time
}

EventsParams encapsulates options for requesting Event logs

type HandlerFunc added in v0.3.0

type HandlerFunc func(ws *WrappedStream, msg Message) (hangup bool)

HandlerFunc is the signature of a function that can handle p2p messages

type Message

type Message struct {
	Type     MsgType
	ID       string
	Created  time.Time
	Deadline time.Time
	// peer that originated this message
	Initiator peer.ID
	// Headers proxies the concept of HTTP headers, but with no
	// mandatory fields. It's intended to be small & simple on purpose
	// In the future we can upgrade this to map[string]interface{} while keeping
	// backward compatibility
	Headers map[string]string
	// Body carries the payload of a message, if any
	Body []byte
	// contains filtered or unexported fields
}

Message is a serializable/encodable object that we send & receive on a Stream.

func NewJSONBodyMessage added in v0.3.0

func NewJSONBodyMessage(initiator peer.ID, t MsgType, body interface{}) (Message, error)

NewJSONBodyMessage is a convenience wrapper for json-encoding a message

func NewMessage added in v0.3.0

func NewMessage(initiator peer.ID, t MsgType, body []byte) Message

NewMessage creates a message. provided initiator should always be the peerID of the local node

func (Message) Header added in v0.3.0

func (m Message) Header(key string) (value string)

Header gets a header value for a given key

func (Message) Update added in v0.3.0

func (m Message) Update(body []byte) Message

Update returns a new message with an updated body

func (Message) UpdateJSON added in v0.3.0

func (m Message) UpdateJSON(body interface{}) (Message, error)

UpdateJSON updates a messages by JSON-encoding a body

func (Message) WithHeaders added in v0.3.0

func (m Message) WithHeaders(keyval ...string) Message

WithHeaders adds a sequence of key,value,key,value as headers

type MsgType

type MsgType string

MsgType indicates the type of message being sent

func (MsgType) String

func (mt MsgType) String() string

String implements the Stringer interface for MsgType

type PeerConnectionParams added in v0.3.2

type PeerConnectionParams struct {
	Peername  string
	ProfileID profile.ID
	PeerID    peer.ID
	Multiaddr ma.Multiaddr
}

PeerConnectionParams defines parameters for the ConnectToPeer command

type QriNode

type QriNode struct {
	// ID is the node's identifier both locally & on the network
	// Identity has a relationship to privateKey (hash of PublicKey)
	ID peer.ID

	// Online indicates weather this is node is connected to the p2p network
	Online bool

	// Discovery service, can be provided by an ipfs node
	Discovery discovery.Service

	// Repo is a repository of this node's qri data
	// note that repo's are built upon a cafs.Filestore, which
	// may contain a reference to a functioning IPFS node. In that case
	// QriNode should piggyback non-qri-specific p2p functionality on the
	// ipfs node provided by repo
	Repo repo.Repo

	// node keeps a set of IOStreams for "node local" io, often to the
	// command line, to give feedback to the user. These may be piped to
	// local http handlers/websockets/stdio, but these streams are meant for
	// local feedback as opposed to p2p connections
	LocalStreams ioes.IOStreams
	// contains filtered or unexported fields
}

QriNode encapsulates a qri peer-2-peer node

func NewQriNode

func NewQriNode(r repo.Repo, p2pconf *config.P2P) (node *QriNode, err error)

NewQriNode creates a new node from a configuration. To get a fully connected node that's searching for peers call: n, _ := NewQriNode(r, cfg) n.GoOnline()

func (*QriNode) Addrs added in v0.3.2

func (n *QriNode) Addrs() pstore.AddrBook

Addrs returns the AddrBook for the node.

func (*QriNode) AnnounceConnected added in v0.3.2

func (n *QriNode) AnnounceConnected() error

AnnounceConnected kicks off a notice to other peers that a profile has connected

func (*QriNode) Bootstrap

func (n *QriNode) Bootstrap(boostrapAddrs []string, boostrapPeers chan pstore.PeerInfo)

Bootstrap samples a subset of peers & requests their peers list This is a naive version of IPFS bootstrapping, which we'll add in once qri's settled on a shared-state implementation

func (*QriNode) BootstrapIPFS

func (n *QriNode) BootstrapIPFS()

BootstrapIPFS connects this node to standard ipfs nodes for file exchange

func (*QriNode) ClosestConnectedQriPeers added in v0.6.1

func (n *QriNode) ClosestConnectedQriPeers(profileID profile.ID, max int) (pid []peer.ID)

ClosestConnectedQriPeers checks if a peer is connected, and if so adds it to the top of a slice cap(max) of peers to try to connect to TODO - In the future we'll use a few tricks to improve on just iterating the list at a bare minimum we should grab a randomized set of peers

func (*QriNode) ConnectToPeer

func (n *QriNode) ConnectToPeer(ctx context.Context, p PeerConnectionParams) (*profile.Profile, error)

ConnectToPeer takes a raw peer ID & tries to work out a route to that peer, explicitly connecting to them.

func (*QriNode) ConnectedPeers

func (n *QriNode) ConnectedPeers() []string

ConnectedPeers lists all IPFS connected peers

func (*QriNode) ConnectedQriPeerIDs added in v0.3.2

func (n *QriNode) ConnectedQriPeerIDs() []peer.ID

ConnectedQriPeerIDs returns a slice of peer.IDs this peer is currently connected to

func (*QriNode) ConnectedQriProfiles added in v0.3.0

func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*config.ProfilePod

ConnectedQriProfiles lists all connected peers that support the qri protocol

func (*QriNode) Context

func (n *QriNode) Context() context.Context

Context returns this node's context

func (*QriNode) DisconnectFromPeer added in v0.3.2

func (n *QriNode) DisconnectFromPeer(ctx context.Context, p PeerConnectionParams) error

DisconnectFromPeer explicitly closes a connection to a peer

func (*QriNode) DiscoverPeerstoreQriPeers

func (n *QriNode) DiscoverPeerstoreQriPeers(store pstore.Peerstore)

DiscoverPeerstoreQriPeers handles the case where a store has seen peers that support the qri protocol, but we haven't added them to our own peers list

func (*QriNode) EncapsulatedAddresses

func (n *QriNode) EncapsulatedAddresses() []ma.Multiaddr

EncapsulatedAddresses returns a slice of full multaddrs for this node

func (*QriNode) GetIPFSNamesys added in v0.8.0

func (n *QriNode) GetIPFSNamesys() (namesys.NameSystem, error)

GetIPFSNamesys returns a namesystem from IPFS

func (*QriNode) GoOnline added in v0.6.0

func (n *QriNode) GoOnline() (err error)

GoOnline puts QriNode on the distributed web, ensuring there's an active peer-2-peer host participating in a peer-2-peer network, and kicks off requests to connect to known bootstrap peers that support the QriProtocol

func (*QriNode) HandlePeerFound

func (n *QriNode) HandlePeerFound(pinfo pstore.PeerInfo)

HandlePeerFound deals with the discovery of a peer that may or may not support the qri protocol

func (*QriNode) Host

func (n *QriNode) Host() host.Host

Host returns the node's Host

func (*QriNode) IPFSCoreAPI added in v0.8.0

func (n *QriNode) IPFSCoreAPI() (coreiface.CoreAPI, error)

IPFSCoreAPI returns a IPFS API interface instance

func (*QriNode) Keys added in v0.3.2

func (n *QriNode) Keys() pstore.KeyBook

Keys returns the KeyBook for the node.

func (*QriNode) ListenAddresses added in v0.3.0

func (n *QriNode) ListenAddresses() ([]string, error)

ListenAddresses gives the listening addresses of this node on the p2p network as a slice of strings

func (*QriNode) PeerInfo added in v0.3.2

func (n *QriNode) PeerInfo(pid peer.ID) pstore.PeerInfo

PeerInfo returns peer peer ID & network multiaddrs from the Host Peerstore

func (*QriNode) Peers

func (n *QriNode) Peers() []peer.ID

Peers returns a list of currently connected peer IDs

func (*QriNode) Ping added in v0.3.0

func (n *QriNode) Ping(peerID peer.ID) (time.Duration, error)

Ping initiates a ping message from peer to a peer.ID

func (*QriNode) QriStreamHandler added in v0.3.0

func (n *QriNode) QriStreamHandler(s net.Stream)

QriStreamHandler is the handler we register with the multistream muxer

func (*QriNode) ReceiveMessages added in v0.3.0

func (n *QriNode) ReceiveMessages() chan Message

ReceiveMessages adds a listener for newly received messages

func (*QriNode) RequestDataset added in v0.3.0

func (n *QriNode) RequestDataset(ref *repo.DatasetRef) (err error)

RequestDataset fetches info about a dataset from qri peers It's expected the local peer has attempted to canonicalize the reference before sending to the network ref is used as an outparam, populating with data on success

func (*QriNode) RequestDatasetLog added in v0.1.1

func (n *QriNode) RequestDatasetLog(ref repo.DatasetRef, limit, offset int) ([]repo.DatasetRef, error)

RequestDatasetLog gets the log information of Peer's dataset

func (*QriNode) RequestDatasetsList

func (n *QriNode) RequestDatasetsList(pid peer.ID, p DatasetsListParams) ([]repo.DatasetRef, error)

RequestDatasetsList gets a list of a peer's datasets

func (*QriNode) RequestEventsList added in v0.3.0

func (n *QriNode) RequestEventsList(pid peer.ID, p EventsParams) ([]*repo.Event, error)

RequestEventsList fetches a log of events from a peer

func (*QriNode) RequestLogDiff added in v0.6.0

func (n *QriNode) RequestLogDiff(ref *repo.DatasetRef) (ldr base.LogDiffResult, err error)

RequestLogDiff fetches info about a dataset from qri peers It's expected the local peer has attempted to canonicalize the reference before sending to the network

func (*QriNode) RequestNewPeers added in v0.5.6

func (n *QriNode) RequestNewPeers(ctx context.Context, peers []QriPeer)

RequestNewPeers intersects a provided list of peer info with this node's existing connections and attempts to connect to any peers it doesn't have connections to

func (*QriNode) RequestProfile added in v0.3.0

func (n *QriNode) RequestProfile(pid peer.ID) (*profile.Profile, error)

RequestProfile get's qri profile information on a peer ID

func (*QriNode) RequestQriPeers added in v0.5.6

func (n *QriNode) RequestQriPeers(id peer.ID) ([]QriPeer, error)

RequestQriPeers asks a designated peer for a list of qri peers

func (*QriNode) ResolveDatasetRef added in v0.5.2

func (n *QriNode) ResolveDatasetRef(ref *repo.DatasetRef) (err error)

ResolveDatasetRef completes a dataset reference

func (*QriNode) Search

func (n *QriNode) Search(terms string, limit, offset int) (res []*repo.DatasetRef, err error)

Search broadcasts a search request to all connected peers, aggregating results

func (*QriNode) SendMessage

func (n *QriNode) SendMessage(msg Message, replies chan Message, pids ...peer.ID) error

SendMessage opens a stream & sends a message from p to one ore more peerIDs

func (*QriNode) SimplePeerInfo added in v0.3.2

func (n *QriNode) SimplePeerInfo() pstore.PeerInfo

SimplePeerInfo returns a PeerInfo with just the ID and Addresses.

func (*QriNode) StartDiscovery

func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error

StartDiscovery initiates peer discovery, allocating a discovery services if one doesn't exist, then registering to be notified on peer discovery

func (*QriNode) UpgradeToQriConnection added in v0.6.0

func (n *QriNode) UpgradeToQriConnection(pinfo pstore.PeerInfo) error

UpgradeToQriConnection attempts to open a Qri protocol connection to a peer it records whether the peer supports Qri in the host Peerstore, returns ErrQriProtocolNotSupported if the connection cannot be upgraded, and sets a priority in the host Connection Manager if the connection is upgraded

type QriPeer added in v0.5.6

type QriPeer struct {
	ProfileID    string
	PeerID       string
	NetworkAddrs []string
}

QriPeer is a minimial struct that combines a profileID & network addresses

type WrappedStream

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

WrappedStream wraps a libp2p stream. We encode/decode whenever we write/read from a stream, so we can just carry the encoders and bufios with us

func WrapStream

func WrapStream(s net.Stream) *WrappedStream

WrapStream takes a stream and complements it with r/w bufios and decoder/encoder. In order to write raw data to the stream we can use wrap.w.Write(). To encode something into it we can wrap.enc.Encode(). Finally, we should wrap.w.Flush() to actually send the data. Handling incoming data works similarly with wrap.r.Read() for raw-reading and wrap.dec.Decode() to decode.

Directories

Path Synopsis
Package p2ptest defines utilities for qri peer-2-peer testing
Package p2ptest defines utilities for qri peer-2-peer testing

Jump to

Keyboard shortcuts

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