core

package
v0.0.6-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDiscoveryInterval = time.Minute * 10

DiscoveryInterval is how often we re-publish our mDNS records.

View Source
const DefaultDiscoveryServiceTag = "pnet:pubsub"

DiscoveryServiceTag is used in our mDNS advertisements to discover other chat peers.

Variables

This section is empty.

Functions

func AutoClose

func AutoClose(ctx context.Context, c Closeable)

AutoClose closes the given Closeable once the context is done

func BootstrapLibP2P

func BootstrapLibP2P(ctx context.Context, cfg *Config, opts ...libp2p.Option) (host.Host, *kaddht.IpfsDHT, *pubsub.PubSub, error)

BootstrapLibP2P creates an instance of libp2p host + DHT and peer discovery / pubsub (if configured)

func CircuitRelayAddr

func CircuitRelayAddr(relay, target peer.ID) multiaddr.Multiaddr

CircuitRelayAddr construct a circuit relay address of the given relay and target peer

func CircuitRelayAddrInfo

func CircuitRelayAddrInfo(relay, target peer.ID) peer.AddrInfo

func Close

func Close(node LibP2PPeer) []error

Close closes the underlying host, dht and possibly other services (e.g. store, pubsub) returns an array to be compatible with multiple closes

func ConfigureDiscovery

func ConfigureDiscovery(ctx context.Context, h host.Host, opts *DiscoveryConfig, logger logging.EventLogger) error

ConfigureDiscovery binds mDNS discovery services

func Connect

func Connect(node LibP2PPeer, peers []peer.AddrInfo, bootDht bool) chan PeerConnection

Connect will try to connect to each of the given peer a channel is used to publish connection result. the dht should be bootstrapped if the is the first connect of a node

func MAddrs

func MAddrs(addrs []string) []multiaddr.Multiaddr

MAddrs takes []string and creates the corresponding []multiaddrs

func PNetSecret

func PNetSecret() pnet.PSK

PNetSecret creates a new random secret

func Peers

func Peers(addrs []string) []peer.AddrInfo

Peers takes []string ([]multiaddrs) and creates the corresponding peer-info slice

func PrivKey

func PrivKey(keyPath string) crypto.PrivKey

PrivKey is a utility for managing the peer's private key

func Publish

func Publish(node PubSuber, ctx context.Context, topic string, data []byte) error

func SerializeAddrInfo

func SerializeAddrInfo(info peer.AddrInfo) string

SerializeAddrInfo marshals (json) the given peer

func SerializePeer

func SerializePeer(h host.Host) string

SerializePeer marshals (json) the given host's info

func Subscribe

func Subscribe(node PubSuber, topic string) (*pubsub.Subscription, error)

func Topic

func Topic(node PubSuber, name string) (*pubsub.Topic, error)

Types

type BasePeer

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

func NewBasePeer

func NewBasePeer(ctx context.Context, cfg *Config, opts ...libp2p.Option) *BasePeer

func (*BasePeer) Close

func (p *BasePeer) Close() error

func (*BasePeer) Context

func (p *BasePeer) Context() context.Context

func (*BasePeer) DHT

func (p *BasePeer) DHT() *kaddht.IpfsDHT

func (*BasePeer) Host

func (p *BasePeer) Host() host.Host

func (*BasePeer) Logger

func (p *BasePeer) Logger() logging.EventLogger

func (*BasePeer) PrivKey

func (p *BasePeer) PrivKey() crypto.PrivKey

func (*BasePeer) Psk

func (p *BasePeer) Psk() pnet.PSK

func (*BasePeer) PubSub

func (p *BasePeer) PubSub() *pubsub.PubSub

func (*BasePeer) Store

func (p *BasePeer) Store() datastore.Batching

func (*BasePeer) Topics

func (p *BasePeer) Topics() map[string]*pubsub.Topic

type Closeable

type Closeable interface {
	Close() error
}

Closeable represent an object the can be closed

type Config

type Config struct {
	// PrivKey of the current node
	PrivKey crypto.PrivKey
	// Secret is the private network secret ([32]byte)
	Secret pnet.PSK
	// Addrs are Multiaddrs for the current node, will fallback to libp2p defaults
	Addrs []multiaddr.Multiaddr
	// Logger to use (see github.com/ipfs/go-log/v2)
	Logger logging.EventLogger
	// DS is the main data store used by DHT, BlockStore, etc...
	DS datastore.Batching
	// Peers are nodes that we want to connect on bootstrap
	Peers []peer.AddrInfo
	// ConnManagerConfig is used to configure the conn management of current peer
	ConnManagerConfig *ConnManagerConfig
	// Discovery is used to configure discovery (+pubsub)
	Discovery *DiscoveryConfig
}

Config holds the needed configuration for creating a private node instance

func NewConfig

func NewConfig(priv crypto.PrivKey, psk pnet.PSK, store datastore.Batching) *Config

NewConfig creates the minimum needed Config

func (*Config) ToLibP2pOpts

func (cfg *Config) ToLibP2pOpts(customOpts ...libp2p.Option) ([]libp2p.Option, error)

ToLibP2pOpts converts Config into the corresponding []libp2p.Option

type ConnManagerConfig

type ConnManagerConfig struct {
	Low   int
	High  int
	Grace time.Duration
}

ConnManagerConfig used to configure a connection manager (github.com/libp2p/go-libp2p-connmgr)

type DiscoveryConfig

type DiscoveryConfig struct {
	OnPeerFound OnPeerFound
	ServiceTag  string
	Interval    time.Duration
	Services    []discovery.Service
}

DiscoveryConfig

func NewDiscoveryConfig

func NewDiscoveryConfig(onPeerFound OnPeerFound) *DiscoveryConfig

NewDiscoveryConfig creates a new discovery config object with defaults

type GroupNodeFactory

type GroupNodeFactory func(OnPeerFound) LibP2PPeer

type LibP2PPeer

type LibP2PPeer interface {
	Context() context.Context

	Closeable

	PrivKey() crypto.PrivKey
	Psk() pnet.PSK

	Host() host.Host
	DHT() *kaddht.IpfsDHT
	Store() datastore.Batching

	PubSuber

	Logger() logging.EventLogger
}

LibP2PPeer is the base interface

func NewRelayer

func NewRelayer(ctx context.Context, cfg *Config, opts ...libp2p.Option) LibP2PPeer

NewRelayer creates an instance of BasePeer with the needed configuration to be a circuit-relay node

func SetupGroup

func SetupGroup(n int, nodeFactory GroupNodeFactory) ([]LibP2PPeer, error)

SetupGroup will create a group of n local nodes that are connected to each other used in tests

type OnPeerFound

type OnPeerFound = func(pi peer.AddrInfo) bool

OnPeerFound will be triggered on new peer discovery in case it returns false, this node won't connect to the given peer

func OnPeerFoundWaitGroup

func OnPeerFoundWaitGroup(wg *sync.WaitGroup) OnPeerFound

OnPeerFoundWaitGroup creates an OnPeerFound that triggers a WaitGroup

type PeerConnection

type PeerConnection struct {
	Error error
	Info  peer.AddrInfo
	Time  int64
}

ConnectionResult is the used to abstract connection try

type PubSuber

type PubSuber interface {
	PubSub() *pubsub.PubSub
	Topics() map[string]*pubsub.Topic
}

Jump to

Keyboard shortcuts

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