p2p

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: GPL-3.0 Imports: 47 Imported by: 4

Documentation

Overview

Package p2p implements qri peer-to-peer communication.

Index

Constants

View Source
const (
	// ProfileProtocolID is the protocol id for the profile exchange service
	ProfileProtocolID = protocol.ID("/qri/profile/0.1.0")
	// ProfileTimeout is the length of time we will wait for a response in a
	// profile exchange
	ProfileTimeout = time.Minute * 2
)
View Source
const MtDatasets = MsgType("list_datasets")

MtDatasets is a dataset list message

View Source
const MtProfile = MsgType("profile")

MtProfile is a peer info message

View Source
const (

	// QriProtocolID is the protocol we use to determing if a node speaks qri
	// if it speaks the qri protocol, we assume it speaks any of the qri protocols
	// that we support
	QriProtocolID = protocol.ID("/qri/0.1.0")
)
View Source
const (

	// ResolveRefProtocolID is the protocol on which qri nodes communicate to
	// resolve references
	ResolveRefProtocolID = protocol.ID("/qri/ref/0.1.0")
)

Variables

View Source
var (

	// QriServiceTag marks the type & version of the qri service
	QriServiceTag = fmt.Sprintf("qri/%s", version.Version)
	// ErrNotConnected is for a missing required network connection
	ErrNotConnected = fmt.Errorf("no p2p connection")
	// ErrQriProtocolNotSupported is returned when a connection can't be upgraded
	ErrQriProtocolNotSupported = fmt.Errorf("peer doesn't support the qri protocol")
	// ErrNoQriNode indicates a qri node doesn't exist
	ErrNoQriNode = fmt.Errorf("p2p: no qri node")
)
View Source
var (

	// ErrPeerNotFound is returned when the profile service cannot find the
	// peer in question
	ErrPeerNotFound = fmt.Errorf("peer not found")
)

Functions

func ListPeers added in v0.9.1

func ListPeers(node *QriNode, userID profile.ID, offset, limit int, onlineOnly bool) ([]*config.ProfilePod, error)

ListPeers lists Peers on the qri network userID is the profile identifier of the user making the request

func NewMessageID added in v0.3.0

func NewMessageID() string

NewMessageID generates a random message identifier

func NewTestableQriNode added in v0.5.2

func NewTestableQriNode(r repo.Repo, p2pconf *config.P2P, pub event.Publisher) (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 DatasetsListParams added in v0.3.0

type DatasetsListParams struct {
	Term   string
	Limit  int
	Offset int
}

DatasetsListParams encapsulates options for requesting datasets

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 qfs.MuxFS, 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, pub event.Publisher, localResolver dsref.Resolver) (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() peerstore.AddrBook

Addrs returns the AddrBook for the node.

func (*QriNode) Bootstrap

func (n *QriNode) Bootstrap(boostrapAddrs []string)

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) 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) 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) GoOffline added in v0.9.10

func (n *QriNode) GoOffline() error

GoOffline takes the peer offline and shuts it down

func (*QriNode) GoOnline added in v0.6.0

func (n *QriNode) GoOnline(c context.Context) (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 peer.AddrInfo)

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) IPFS added in v0.9.0

func (n *QriNode) IPFS() (*core.IpfsNode, error)

IPFS exposes the core.IPFS node if one exists. This is currently required by things like remoteClient in other packages, which don't work properly with the CoreAPI implementation

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() peerstore.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) MissingManifest added in v0.9.1

func (node *QriNode) MissingManifest(ctx context.Context, m *dag.Manifest) (missing *dag.Manifest, err error)

MissingManifest returns a manifest describing blocks that are not in this node for a given manifest

func (*QriNode) NewDAGInfo added in v0.9.1

func (node *QriNode) NewDAGInfo(ctx context.Context, path, label string) (*dag.Info, error)

NewDAGInfo generates a DAGInfo for a given node. If a label is given, it will generate a sub-DAGInfo at thea label.

func (*QriNode) NewManifest added in v0.9.1

func (node *QriNode) NewManifest(ctx context.Context, path string) (*dag.Manifest, error)

NewManifest generates a manifest for a given node

func (*QriNode) NewP2PRefResolver added in v0.9.12

func (q *QriNode) NewP2PRefResolver() dsref.Resolver

NewP2PRefResolver creates a resolver backed by a qri node

func (*QriNode) PeerInfo added in v0.3.2

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

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

func (n *QriNode) RequestDatasetsList(ctx context.Context, pid peer.ID, p DatasetsListParams) ([]reporef.DatasetRef, error)

RequestDatasetsList gets a list of a peer's datasets

func (*QriNode) SimpleAddrInfo added in v0.9.10

func (n *QriNode) SimpleAddrInfo() peer.AddrInfo

SimpleAddrInfo returns a PeerInfo with just the ID and Addresses.

type QriProfileService added in v0.9.12

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

QriProfileService manages the profile exchange. This exchange should happen whenever a node connects to a new peer

func NewQriProfileService added in v0.9.12

func NewQriProfileService(profiles profile.Store, p event.Publisher) *QriProfileService

NewQriProfileService creates an profile exchange service

func (*QriProfileService) ConnectedPeerProfile added in v0.9.12

func (q *QriProfileService) ConnectedPeerProfile(pid peer.ID) *profile.Profile

ConnectedPeerProfile returns a profile if that peer id refers to a peer that we have connected to this session.

func (*QriProfileService) ConnectedQriPeers added in v0.9.12

func (q *QriProfileService) ConnectedQriPeers() []peer.ID

ConnectedQriPeers returns a list of currently connected peers

func (*QriProfileService) HandleQriPeerDisconnect added in v0.9.12

func (q *QriProfileService) HandleQriPeerDisconnect(pid peer.ID)

HandleQriPeerDisconnect checks if a given peer is a qri peer. If so, it will wait until all profile exchanging has finished Then remove the peer from the peers map, as well as publish that the peer has disconnected.

func (*QriProfileService) ProfileHandler added in v0.9.12

func (q *QriProfileService) ProfileHandler(s network.Stream)

ProfileHandler listens for profile requests it sends it's node's profile on the given stream whenever a request comes in

func (*QriProfileService) QriProfileRequest added in v0.9.12

func (q *QriProfileService) QriProfileRequest(ctx context.Context, pid peer.ID) error

QriProfileRequest determine if the remote peer speaks the qri protocol if it does, it protects the connection and sends a request for the QriIdentifyService to get the peer's qri profile information

func (*QriProfileService) Start added in v0.9.12

func (q *QriProfileService) Start(h host.Host)

Start adds a profile handler to the host, retains a local reference to the host

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