p2p

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package p2p provides the peer-to-peer abstractions used across different protocols

Index

Constants

View Source
const (
	ReachabilityStatusUnknown = ReachabilityStatus(network.ReachabilityUnknown)
	ReachabilityStatusPublic  = ReachabilityStatus(network.ReachabilityPublic)
	ReachabilityStatusPrivate = ReachabilityStatus(network.ReachabilityPrivate)
)
View Source
const (
	DefaultBlocklistTime = 1 * time.Minute
)
View Source
const (
	HeaderNameTracingSpanContext = "tracing-span-context"
)

Common header names.

Variables

View Source
var (
	// ErrPeerNotFound should be returned by p2p service methods when the requested
	// peer is not found.
	ErrPeerNotFound = errors.New("peer not found")
	// ErrAlreadyConnected is returned if connect was called for already connected node.
	ErrAlreadyConnected = errors.New("already connected")
	// ErrDialLightNode is returned if connect was attempted to a light node.
	ErrDialLightNode = errors.New("target peer is a light node")
	// ErrPeerBlocklisted is returned if peer is on blocklist
	ErrPeerBlocklisted = errors.New("peer blocklisted")

	ErrStreamClosed       = errors.New("stream closed")
	ErrNetworkUnavailable = errors.New("network unavailable")
)
View Source
var ErrUnexpected = errors.New("unexpected request while in light mode")

Functions

func Discover added in v1.4.1

func Discover(ctx context.Context, addr ma.Multiaddr, f func(ma.Multiaddr) (bool, error)) (bool, error)

func NewBlockPeerError added in v1.4.1

func NewBlockPeerError(duration time.Duration, err error) error

NewBlockPeerError wraps error and creates a special error that is treated specially by p2p. It causes peer to be disconnected and blocks any new connection for this peer for the provided duration.

func NewConnectionBackoffError added in v1.4.1

func NewConnectionBackoffError(err error, tryAfter time.Time) error

NewConnectionBackoffError creates new `ConnectionBackoffError` with provided underlying error and `tryAfter` timestamp.

func NewDisconnectError added in v1.4.1

func NewDisconnectError(err error) error

NewDisconnectError wraps error and creates a special error that is treated specially by p2p. It causes peer to disconnect.

func NewProtocolStreamName added in v1.4.1

func NewProtocolStreamName(protocol, version, stream string) string

NewProtocolStreamName constructs a libp2p compatible stream name out of protocol name and version and stream name.

func WithBlocklistStreams added in v1.4.1

func WithBlocklistStreams(dur time.Duration, spec ProtocolSpec)

WithBlocklistStreams will mutate the given spec and replace the handler with a always erroring one.

func WithDisconnectStreams added in v1.4.1

func WithDisconnectStreams(spec ProtocolSpec)

WithDisconnectStreams will mutate the given spec and replace the handler with a always erroring one.

Types

type BlockPeerError added in v1.4.1

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

func (*BlockPeerError) Duration added in v1.4.1

func (e *BlockPeerError) Duration() time.Duration

Duration represents the period for which the peer will be blocked. 0 duration is treated as infinity

func (*BlockPeerError) Error added in v1.4.1

func (e *BlockPeerError) Error() string

Error implements function of the standard go error interface.

func (*BlockPeerError) Unwrap added in v1.4.1

func (e *BlockPeerError) Unwrap() error

Unwrap returns an underlying error.

type BlockPeers added in v1.4.1

type BlockPeers struct {
	Address   boson.Address `json:"address"`
	Timestamp string        `json:"timestamp"`
	Duration  float64       `json:"duration"`
}

type Blocklister added in v1.4.1

type Blocklister interface {
	NetworkStatuser

	// Blocklist will disconnect a peer and put it on a blocklist (blocking in & out connections) for provided duration
	// Duration 0 is treated as an infinite duration.
	Blocklist(overlay boson.Address, duration time.Duration, reason string) error
}

type BuffMessage

type BuffMessage struct {
	bytes.Buffer
}

func (*BuffMessage) Marshal

func (b *BuffMessage) Marshal() ([]byte, error)

func (*BuffMessage) ProtoMessage

func (b *BuffMessage) ProtoMessage()

func (*BuffMessage) Unmarshal

func (b *BuffMessage) Unmarshal(p []byte) error

type Connect added in v1.4.1

type Connect interface {
	// Connect to a peer but do not notify topology about the established connection.
	Connect(ctx context.Context, addr ma.Multiaddr) (peer *Peer, err error)
}

type ConnectionBackoffError added in v1.4.1

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

ConnectionBackoffError indicates that connection calls will not be executed until `tryAfter` timetamp. The reason is provided in the wrappped error.

func (*ConnectionBackoffError) Error added in v1.4.1

func (e *ConnectionBackoffError) Error() string

Error implements function of the standard go error interface.

func (*ConnectionBackoffError) TryAfter added in v1.4.1

func (e *ConnectionBackoffError) TryAfter() time.Time

TryAfter returns a tryAfter timetamp.

func (*ConnectionBackoffError) Unwrap added in v1.4.1

func (e *ConnectionBackoffError) Unwrap() error

Unwrap returns an underlying error.

type DebugService added in v1.4.1

type DebugService interface {
	Service
	SetWelcomeMessage(val string) error
	GetWelcomeMessage() string
}

DebugService extends the Service with method used for debugging.

type DisconnectError added in v1.4.1

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

DisconnectError is an error that is specifically handled inside p2p. If returned by specific protocol handler it causes peer disconnect.

func (*DisconnectError) Error added in v1.4.1

func (e *DisconnectError) Error() string

Error implements function of the standard go error interface.

func (*DisconnectError) Unwrap added in v1.4.1

func (e *DisconnectError) Unwrap() error

Unwrap returns an underlying error.

type Disconnecter added in v1.4.1

type Disconnecter interface {
	Disconnect(overlay boson.Address, reason string) error
	Blocklister
}

type Halter added in v1.4.1

type Halter interface {
	// Halt new incoming connections while shutting down
	Halt()
}

type HandlerFunc added in v1.4.1

type HandlerFunc func(context.Context, Peer, Stream) error

HandlerFunc handles a received Stream from a Peer.

type HandlerMiddleware added in v1.4.1

type HandlerMiddleware func(HandlerFunc) HandlerFunc

HandlerMiddleware decorates a HandlerFunc by returning a new one.

type Headers added in v1.4.1

type Headers map[string][]byte

Headers represents a collection of p2p header key value pairs.

type HeadlerFunc added in v1.4.1

type HeadlerFunc func(Headers, boson.Address) Headers

HeadlerFunc is returning response headers based on the received request headers.

type IncompatibleStreamError added in v1.4.1

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

IncompatibleStreamError is the error that should be returned by p2p service NewStream method when the stream or its version is not supported.

func NewIncompatibleStreamError added in v1.4.1

func NewIncompatibleStreamError(err error) *IncompatibleStreamError

NewIncompatibleStreamError wraps the error that is the cause of stream incompatibility with IncompatibleStreamError that it can be detected and returns it.

func (*IncompatibleStreamError) Error added in v1.4.1

func (e *IncompatibleStreamError) Error() string

Error implements function of the standard go error interface.

func (*IncompatibleStreamError) Unwrap added in v1.4.1

func (e *IncompatibleStreamError) Unwrap() error

Unwrap returns an underlying error.

type NetworkStatus added in v1.4.1

type NetworkStatus int

NetworkStatus represents the network availability status.

const (
	NetworkStatusUnknown     NetworkStatus = 0
	NetworkStatusAvailable   NetworkStatus = 1
	NetworkStatusUnavailable NetworkStatus = 2
)

func (NetworkStatus) String added in v1.4.1

func (ns NetworkStatus) String() string

String implements the fmt.Stringer interface.

type NetworkStatuser added in v1.4.1

type NetworkStatuser interface {
	// NetworkStatus returns current network availability status.
	NetworkStatus() NetworkStatus
}

NetworkStatuser handles bookkeeping of the network availability status.

type Notifier added in v1.4.1

type Notifier interface {
	Connected(context.Context, Peer, bool) error
	Disconnected(peer Peer, reason string)
	Announce(ctx context.Context, peer boson.Address, fullnode bool) error
	AnnounceTo(ctx context.Context, addressee, peer boson.Address, fullnode bool) error
	NotifyPeerState(peer PeerInfo)
}

type Peer added in v1.4.1

type Peer struct {
	Address boson.Address `json:"address"`
	Mode    address.Model `json:"mode"`
}

Peer holds information about a Peer.

type PeerInfo added in v1.4.1

type PeerInfo struct {
	Overlay boson.Address `json:"overlay"`
	Mode    []byte        `json:"mode"`
	State   PeerState     `json:"state"`
	Reason  string        `json:"reason"`
}

type PeerState added in v1.4.1

type PeerState int
const (
	PeerStateConnectIn PeerState = iota + 1
	PeerStateConnectOut
	PeerStateDisconnect
)

type Picker added in v1.4.1

type Picker interface {
	Pick(Peer) bool
}

type PickyNotifier added in v1.4.1

type PickyNotifier interface {
	Picker
	Notifier
	ReachabilityUpdater
	ReachableNotifier
}

PickyNotifier can decide whether a peer should be picked

type Pinger added in v1.4.1

type Pinger interface {
	Ping(ctx context.Context, addr ma.Multiaddr) (rtt time.Duration, err error)
}

Pinger interface is used to ping a underlay address which is not yet known to the node. It uses libp2p's default ping protocol. This is different from the PingPong protocol as this is meant to be used before we know a particular underlay and we can consider it useful

type ProtocolSpec added in v1.4.1

type ProtocolSpec struct {
	Name          string
	Version       string
	StreamSpecs   []StreamSpec
	ConnectIn     func(context.Context, Peer) error
	ConnectOut    func(context.Context, Peer) error
	DisconnectIn  func(Peer) error
	DisconnectOut func(Peer) error
}

ProtocolSpec defines a collection of Stream specifications with handlers.

type ReachabilityStatus added in v1.4.1

type ReachabilityStatus network.Reachability

ReachabilityStatus represents the node reachability status.

func (ReachabilityStatus) String added in v1.4.1

func (rs ReachabilityStatus) String() string

String implements the fmt.Stringer interface.

type ReachabilityUpdater added in v1.4.1

type ReachabilityUpdater interface {
	UpdateReachability(ReachabilityStatus)
}

type ReachableNotifier added in v1.4.1

type ReachableNotifier interface {
	Reachable(boson.Address, ReachabilityStatus)
}

type Reacher added in v1.4.1

type Reacher interface {
	Connected(boson.Address, ma.Multiaddr)
	Disconnected(boson.Address)
}

type ReaderChan added in v1.4.1

type ReaderChan struct {
	R   chan []byte
	Err chan error
}

type Service added in v1.4.1

type Service interface {
	CallHandlerWithConnChain(ctx context.Context, last, src Peer, stream Stream, protocolName, protocolVersion, streamName string) error
	CallHandler(ctx context.Context, last Peer, stream Stream) (relayData *pb.RouteRelayReq, w *WriterChan, r *ReaderChan, forward bool, err error)
	AddProtocol(ProtocolSpec) error
	Connect
	Disconnecter
	Peers() []Peer
	PeerID(overlay boson.Address) (id libp2ppeer.ID, found bool)
	ResourceManager() network.ResourceManager
	BlocklistedPeers() ([]BlockPeers, error)
	BlocklistRemove(overlay boson.Address) error
	Addresses() ([]ma.Multiaddr, error)
	NATAddresses() ([]net.Addr, error)
	SetPickyNotifier(PickyNotifier)
	Halter
	NetworkStatuser
}

Service provides methods to handle p2p Peers and Protocols.

type Stream added in v1.4.1

type Stream interface {
	io.ReadWriter
	io.Closer
	ResponseHeaders() Headers
	Headers() Headers
	FullClose() error
	Reset() error
}

Stream represent a bidirectional data Stream.

type StreamSpec added in v1.4.1

type StreamSpec struct {
	Name    string
	Handler HandlerFunc
	Headler HeadlerFunc
}

StreamSpec defines a Stream handling within the protocol.

type StreamVirtual added in v1.4.1

type StreamVirtual struct {
	Stream
	ReadPipe  *io.PipeReader
	WritePipe *io.PipeWriter
}

func NewVirtualStream

func NewVirtualStream(s Stream) *StreamVirtual

func (*StreamVirtual) Close added in v1.4.1

func (s *StreamVirtual) Close() error

func (*StreamVirtual) FullClose added in v1.4.1

func (s *StreamVirtual) FullClose() error

func (*StreamVirtual) Read added in v1.4.1

func (s *StreamVirtual) Read(p []byte) (int, error)

func (*StreamVirtual) Reset added in v1.4.1

func (s *StreamVirtual) Reset() error

type Streamer added in v1.4.1

type Streamer interface {
	NewStream(ctx context.Context, address boson.Address, h Headers, protocol, version, stream string) (Stream, error)
	NewRelayStream(ctx context.Context, address boson.Address, h Headers, protocol, version, stream string, midCall bool) (Stream, error)
	NewConnChainRelayStream(ctx context.Context, target boson.Address, h Headers, protocolName, protocolVersion, streamName string) (Stream, error)
}

Streamer is able to create a new Stream.

type StreamerDisconnecter added in v1.4.1

type StreamerDisconnecter interface {
	Streamer
	Disconnecter
}

type StreamerPinger added in v1.4.1

type StreamerPinger interface {
	Streamer
	Pinger
}

type VirtualStream added in v1.4.1

type VirtualStream interface {
	Stream
	UpdateStatRealStreamClosed()
	Reader() *ReaderChan
	Writer() *WriterChan
	Done() chan struct{}
	RealStream() Stream
}

type WriterChan added in v1.4.1

type WriterChan struct {
	W   chan []byte
	Err chan error
}

Directories

Path Synopsis
internal/handshake/pb
Package pb holds only Protocol Buffer definitions and generated code.
Package pb holds only Protocol Buffer definitions and generated code.
internal/headers/pb
Package pb holds only Protocol Buffer definitions and generated code.
Package pb holds only Protocol Buffer definitions and generated code.
internal/reacher
Package reacher runs a background worker that will ping peers from an internal queue and report back the reachability to the notifier.
Package reacher runs a background worker that will ping peers from an internal queue and report back the reachability to the notifier.
internal/pb
Package pb holds only Protocol Buffer definitions and generated code for testing purposes.
Package pb holds only Protocol Buffer definitions and generated code for testing purposes.

Jump to

Keyboard shortcuts

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