p2p

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2020 License: MIT Imports: 48 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// EventPeerConnected is emitted to the eventstream
	// whenever a new peer is found
	EventPeerConnected int = iota
)

Variables

View Source
var ErrDialBackoff = swarm.ErrDialBackoff
View Source
var (
	LocalBootstrap = []string{
		"/ip4/127.0.0.1/tcp/10000/ipfs/16Uiu2HAmTfa6xM13GKVrx6J6WkXxV26CmifQyJVwJZTDHSmMgBu3",
	}
)

Functions

func BootstrapNodes

func BootstrapNodes() []string

BootstrapNodes returns a slice of comma-saparated values from environment variable TUPELO_BOOTSTRAP_NODES or the default bootstrapNodes if the environment variable is not set.

func EcdsaKeyFromPeer

func EcdsaKeyFromPeer(pid peer.ID) (*ecdsa.PublicKey, error)

func NewHostAndBitSwapPeer

func NewHostAndBitSwapPeer(ctx context.Context, userOpts ...Option) (*LibP2PHost, *BitswapPeer, error)

func PeerFromEcdsaKey

func PeerFromEcdsaKey(publicKey *ecdsa.PublicKey) (peer.ID, error)

func PeerIDFromPublicKey

func PeerIDFromPublicKey(publicKey *ecdsa.PublicKey) (peer.ID, error)

Types

type BitswapPeer

type BitswapPeer struct {
	ipld.DAGService
	// contains filtered or unexported fields
}

BitswapPeer is for exchanging ipld blocks on the network it can be used in any of the LibP2PHost networks, but can also be used directly on the IPFS network.

func NewBitswapPeer

func NewBitswapPeer(ctx context.Context, host *LibP2PHost, bitswapOpts ...bitswap.Option) (*BitswapPeer, error)

NewBitswapPeer creates a new block-swapping peer from an existing *LibP2PHost It is important that you bootstrap *after* creating this peer. There is a helper function: NewHostAndBitSwapPeer which can create both the host and this peer at the same time.

func (*BitswapPeer) BlockStore

func (bp *BitswapPeer) BlockStore() blockstore.Blockstore

BlockStore offers access to the blockstore underlying the Peer's DAGService.

func (*BitswapPeer) HasBlock

func (bp *BitswapPeer) HasBlock(c cid.Cid) (bool, error)

HasBlock returns whether a given block is available locally. It is a shorthand for .Blockstore().Has().

func (*BitswapPeer) Session

func (bp *BitswapPeer) Session(ctx context.Context) ipld.NodeGetter

Session returns a session-based NodeGetter.

type Bootstrapper

type Bootstrapper struct {
	// Config
	// MinPeerThreshold is the number of connections it attempts to maintain.
	MinPeerThreshold int

	// Period is the interval at which it periodically checks to see
	// if the threshold is maintained.
	Period time.Duration
	// ConnectionTimeout is how long to wait before timing out a connection attempt.
	ConnectionTimeout time.Duration

	// Does the work. Usually Bootstrapper.bootstrap. Argument is a slice of
	// currently-connected peers (so it won't attempt to reconnect).
	Bootstrap func([]peer.ID)
	// contains filtered or unexported fields
}

Bootstrapper attempts to keep the p2p host connected to the network by keeping a minimum threshold of connections. If the threshold isn't met it connects to a random subset of the bootstrap peers. It does not use peer routing to discover new peers. To stop a Bootstrapper cancel the context passed in Start() or call Stop().

func NewBootstrapper

func NewBootstrapper(bootstrapPeers []peer.AddrInfo, h host.Host, d network.Dialer, r routing.Routing, minPeer int, period time.Duration) *Bootstrapper

NewBootstrapper returns a new Bootstrapper that will attempt to keep connected to the network by connecting to the given bootstrap peers.

func (*Bootstrapper) Close

func (b *Bootstrapper) Close() error

Close implements io.Closer in order to provide the same interface as previously.

func (*Bootstrapper) Start

func (b *Bootstrapper) Start(ctx context.Context)

Start starts the Bootstrapper bootstrapping. Cancel `ctx` or call Stop() to stop it.

func (*Bootstrapper) Stop

func (b *Bootstrapper) Stop()

Stop stops the Bootstrapper.

type Config

type Config struct {
	RelayOpts            []circuit.RelayOpt
	EnableRelayHop       bool
	EnableAutoRelay      bool
	EnableWebsocket      bool
	WebsocketPort        int
	PubSubRouter         string
	PubSubOptions        []pubsub.Option
	PrivateKey           *ecdsa.PrivateKey
	EnableNATMap         bool
	ExternalAddrs        []string
	ListenAddrs          []string
	AddrFilters          []*net.IPNet
	Port                 int
	ListenIP             string
	DiscoveryNamespaces  []string
	AdditionalP2POptions []libp2p.Option
	Transports           []libp2p.Option // these should only be libp2p.Transport options, but can't detect that with type
	DataStore            ds.Batching
	Blockstore           blockstore.Blockstore
	BandwidthReporter    metrics.Reporter
	Segmenter            []byte
	ClientOnlyDHT        bool
	BitswapOptions       []bitswap.Option
}

type DiscoveryEvent

type DiscoveryEvent struct {
	Namespace string
	Connected uint64
	EventType int
}

type LibP2PHost

type LibP2PHost struct {
	Reporter metrics.Reporter
	// contains filtered or unexported fields
}

func NewHostFromOptions

func NewHostFromOptions(ctx context.Context, userOpts ...Option) (*LibP2PHost, error)

func NewLibP2PHost

func NewLibP2PHost(ctx context.Context, privateKey *ecdsa.PrivateKey, port int) (*LibP2PHost, error)

func NewRelayLibP2PHost

func NewRelayLibP2PHost(ctx context.Context, privateKey *ecdsa.PrivateKey, port int) (*LibP2PHost, error)

func (*LibP2PHost) Addresses

func (h *LibP2PHost) Addresses() []ma.Multiaddr

func (*LibP2PHost) Bootstrap

func (h *LibP2PHost) Bootstrap(peers []string) (io.Closer, error)

func (*LibP2PHost) Connect

func (h *LibP2PHost) Connect(ctx context.Context, publicKey *ecdsa.PublicKey) error

Connect ensures there is a connection between this host and the peer with given public key. This is useful for bitswap exchanges where peers are known, allowing blocks to be exchanged directly, skipping the expensive DHT provides.

Connections are generally persisted, use Disconnect to clean up unused connections.

func (*LibP2PHost) Disconnect

func (h *LibP2PHost) Disconnect(ctx context.Context, publicKey *ecdsa.PublicKey) error

Disconnect closes all connections to the given peer.

func (*LibP2PHost) GetPubSub

func (h *LibP2PHost) GetPubSub() *pubsub.PubSub

func (*LibP2PHost) Identity

func (h *LibP2PHost) Identity() string

func (*LibP2PHost) NewStream

func (h *LibP2PHost) NewStream(ctx context.Context, publicKey *ecdsa.PublicKey, protocol protocol.ID) (network.Stream, error)

func (*LibP2PHost) NewStreamWithPeerID

func (h *LibP2PHost) NewStreamWithPeerID(ctx context.Context, peerID peer.ID, protocol protocol.ID) (network.Stream, error)

func (*LibP2PHost) PublicKey

func (h *LibP2PHost) PublicKey() *ecdsa.PublicKey

func (*LibP2PHost) Publish

func (h *LibP2PHost) Publish(topic string, data []byte) error

func (*LibP2PHost) Send

func (h *LibP2PHost) Send(publicKey *ecdsa.PublicKey, protocol protocol.ID, payload []byte) error

func (*LibP2PHost) SendAndReceive

func (h *LibP2PHost) SendAndReceive(publicKey *ecdsa.PublicKey, protocol protocol.ID, payload []byte) ([]byte, error)

func (*LibP2PHost) SetStreamHandler

func (h *LibP2PHost) SetStreamHandler(protocol protocol.ID, handler network.StreamHandler)

func (*LibP2PHost) StartDiscovery

func (h *LibP2PHost) StartDiscovery(namespace string) error

func (*LibP2PHost) StopDiscovery

func (h *LibP2PHost) StopDiscovery(namespace string)

func (*LibP2PHost) Subscribe

func (h *LibP2PHost) Subscribe(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error)

func (*LibP2PHost) WaitForBootstrap

func (h *LibP2PHost) WaitForBootstrap(peerCount int, timeout time.Duration) error

func (*LibP2PHost) WaitForDiscovery

func (h *LibP2PHost) WaitForDiscovery(namespace string, num int, duration time.Duration) error

type Node

type Node interface {
	Identity() string
	PublicKey() *ecdsa.PublicKey
	Addresses() []ma.Multiaddr
	Bootstrap(peers []string) (io.Closer, error)
	WaitForBootstrap(peerCount int, timeout time.Duration) error
	SetStreamHandler(protocol protocol.ID, handler network.StreamHandler)
	NewStream(ctx context.Context, publicKey *ecdsa.PublicKey, protocol protocol.ID) (network.Stream, error)
	NewStreamWithPeerID(ctx context.Context, peerID peer.ID, protocol protocol.ID) (network.Stream, error)
	Send(publicKey *ecdsa.PublicKey, protocol protocol.ID, payload []byte) error
	SendAndReceive(publicKey *ecdsa.PublicKey, protocol protocol.ID, payload []byte) ([]byte, error)
	GetPubSub() *pubsub.PubSub
}

Node is the interface any p2p host must implement to be used The interface is here so that other implementations (like in memory for tests) can be used to, for example, simulate large networks on single machines

type Option

type Option func(c *Config) error

Option is a configuration option for the server

func WithAddrFilters

func WithAddrFilters(addrFilters []string) Option

WithAddrFilters takes a string of cidr addresses (0.0.0.0/32) that will not be dialed by swarm.

func WithAutoRelay

func WithAutoRelay(enabled bool) Option

WithAutoRelay enabled AutoRelay in the config, defaults to false

func WithBandwidthReporter

func WithBandwidthReporter(reporter metrics.Reporter) Option

WithBandwidthReporter sets the bandwidth reporter, defaults to a new metrics.Reporter

func WithBitswapOptions

func WithBitswapOptions(opts ...bitswap.Option) Option

func WithBlockstore

func WithBlockstore(store blockstore.Blockstore) Option

WithBlockStore sets the datastore used by the host defaults to nil which will wrap the underlying node datastore.

func WithClientOnlyDHT

func WithClientOnlyDHT(isClientOnly bool) Option

WithClientOnlyDHT sets whether or not the DHT will be put into client/server mode client mode means it will not serve requests on the DHT

func WithDatastore

func WithDatastore(store ds.Batching) Option

WithDatastore sets the datastore used by the host defaults to in-memory map store.

func WithDiscoveryNamespaces

func WithDiscoveryNamespaces(namespaces ...string) Option

WithDiscoveryNamespaces enables discovery of all of the passed in namespaces discovery is part of bootstrap, defaults to empty

func WithExternalAddr

func WithExternalAddr(addr string) Option

WithExternalAddr sets an arbitrary multiaddr formatted string for broadcasting to swarm

func WithExternalIP

func WithExternalIP(ip string, port int) Option

WithExternalIP sets an arbitrary ip/port for broadcasting to swarm

func WithKey

func WithKey(key *ecdsa.PrivateKey) Option

WithKey is the identity key of the host, if not set it the default options will generate you a new key

func WithLibp2pOptions

func WithLibp2pOptions(opts ...libp2p.Option) Option

WithLibp2pOptions allows for additional libp2p options to be passed in

func WithListenIP

func WithListenIP(ip string, port int) Option

WithListenIP sets the listen IP, defaults to 0.0.0.0/0

func WithNATMap

func WithNATMap() Option

WithNATMap enables nat mapping

func WithPubSubOptions

func WithPubSubOptions(opts ...pubsub.Option) Option

WithPubSubOptions sets pubsub options. Defaults to pubsub.WithStrictSignatureVerification(false), pubsub.WithMessageSigning(false)

func WithPubSubRouter

func WithPubSubRouter(routeType string) Option

WithPubSubRouter sets the router type of the pubsub. Supported is: gossip, random, floodsub defaults to gossip

func WithRelayOpts

func WithRelayOpts(opts ...circuit.RelayOpt) Option

WithRelayOpts turns on relay and sets the options defaults to empty (and relay off).

func WithSegmenter

func WithSegmenter(secret []byte) Option

WithSegmenter enables the secret on libp2p in order to make sure that this network does not combine with another. Default is off.

func WithTransports

func WithTransports(transports ...libp2p.Option) Option

WithTransports should only be used with a libp2p.Transport option but cannot detect that using typing

func WithWebSocketExternalIP

func WithWebSocketExternalIP(ip string, port int) Option

WithWebSocketExternalIP sets an arbitrary ip/port for broadcasting ws path to swarm

func WithWebSockets

func WithWebSockets(port int) Option

WithWebSockets turns websockets on at specified port (default to 0)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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