p2p

package
v2.0.0-beta.8 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PeerConnectivityProtectionTag is the tag used by Manager to
	// protect known peer connectivity from getting trimmed via the
	// connmgr.ConnManager.
	PeerConnectivityProtectionTag = "peering-manager"
)
View Source
const (
	PrivKeyFileName = "identity.key"
)

Variables

View Source
var (
	ErrPrivKeyInvalid = errors.New("invalid private key")
	ErrNoPrivKeyFound = errors.New("no private key found")
)
View Source
var (
	// ErrCantConnectToItself gets returned if the manager is supposed to create a connection
	// to itself (host wise).
	ErrCantConnectToItself = errors.New("the host can't connect to itself")
	// ErrPeerInManagerAlready gets returned if a peer is tried to be added to the manager which is already added.
	ErrPeerInManagerAlready = errors.New("peer is already in manager")
	// ErrCantAllowItself gets returned if the manager is supposed to allow a connection
	// to itself (host wise).
	ErrCantAllowItself = errors.New("the host can't allow itself")
	// ErrPeerInManagerAlreadyAllowed gets returned if a peer is tried to be allowed in the manager which is already allowed.
	ErrPeerInManagerAlreadyAllowed = errors.New("peer is already allowed in manager")
	// ErrManagerShutdown gets returned if the manager is shutting down.
	ErrManagerShutdown = errors.New("manager is shutting down")
)

Functions

func LoadOrCreateIdentityPrivateKey

func LoadOrCreateIdentityPrivateKey(p2pStorePath string, identityPrivKey string) (crypto.PrivKey, bool, error)

LoadOrCreateIdentityPrivateKey loads an existing Ed25519 based identity private key or creates a new one and stores it as a PEM file in the p2p store folder.

func ManagerStateCaller

func ManagerStateCaller(handler interface{}, params ...interface{})

ManagerStateCaller gets called with a ManagerState.

func ParseEd25519PrivateKeyFromString

func ParseEd25519PrivateKeyFromString(identityPrivKey string) (crypto.PrivKey, error)

ParseEd25519PrivateKeyFromString parses an Ed25519 private key from a hex encoded string.

func PeerCaller

func PeerCaller(handler interface{}, params ...interface{})

PeerCaller gets called with a Peer.

func PeerConnCaller

func PeerConnCaller(handler interface{}, params ...interface{})

PeerConnCaller gets called with a Peer and its associated network.Conn.

func PeerDurationCaller

func PeerDurationCaller(handler interface{}, params ...interface{})

PeerDurationCaller gets called with a Peer and a time.Duration.

func PeerIDCaller

func PeerIDCaller(handler interface{}, params ...interface{})

PeerIDCaller gets called with a peer.ID.

func PeerOptErrorCaller

func PeerOptErrorCaller(handler interface{}, params ...interface{})

PeerOptErrorCaller gets called with a Peer and an error.

func PeerRelationCaller

func PeerRelationCaller(handler interface{}, params ...interface{})

PeerRelationCaller gets called with a Peer and its old PeerRelation.

func ReadEd25519PrivateKeyFromPEMFile

func ReadEd25519PrivateKeyFromPEMFile(filepath string) (crypto.PrivKey, error)

ReadEd25519PrivateKeyFromPEMFile reads an Ed25519 private key from a file with PEM format.

func WriteEd25519PrivateKeyToPEMFile

func WriteEd25519PrivateKeyToPEMFile(filepath string, privateKey crypto.PrivKey) error

WriteEd25519PrivateKeyToPEMFile stores an Ed25519 private key to a file with PEM format.

Types

type ConfigManager

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

ConfigManager handles the list of peers that are stored in the peering config. It calls a function if the list changed.

func NewConfigManager

func NewConfigManager(storeCallback func([]*PeerConfig) error) *ConfigManager

NewConfigManager creates a new config manager.

func (*ConfigManager) AddPeer

func (pm *ConfigManager) AddPeer(multiAddress multiaddr.Multiaddr, alias string) error

AddPeer adds a peer to the config manager.

func (*ConfigManager) Peers

func (pm *ConfigManager) Peers() []*PeerConfig

Peers returns all known peers.

func (*ConfigManager) RemovePeer

func (pm *ConfigManager) RemovePeer(peerID peer.ID) error

RemovePeer removes a peer from the config manager.

func (*ConfigManager) Store

func (pm *ConfigManager) Store() error

Store calls the storeCallback if storeOnChange is active.

func (*ConfigManager) StoreOnChange

func (pm *ConfigManager) StoreOnChange(store bool)

StoreOnChange sets whether storing changes to the config is active or not.

type Manager

type Manager struct {
	// the logger used to log events.
	*logger.WrappedLogger

	// Events happening around the Manager.
	Events *ManagerEvents
	// contains filtered or unexported fields
}

Manager manages a set of known and other connected peers. It also provides the functionality to reconnect to known peers.

func NewManager

func NewManager(host host.Host, opts ...ManagerOption) *Manager

NewManager creates a new Manager.

func (*Manager) AllowPeer

func (m *Manager) AllowPeer(peerID peer.ID) error

AllowPeer allows incoming connections from the given peer (autopeering).

func (*Manager) Call

func (m *Manager) Call(peerID peer.ID, f PeerFunc)

Call calls the given PeerFunc synchronized within the Manager's event loop, if the peer exists. PeerFunc must not call any function on Manager.

func (*Manager) ConnectPeer

func (m *Manager) ConnectPeer(addrInfo *peer.AddrInfo, peerRelation PeerRelation, alias ...string) error

ConnectPeer connects to the given peer. If the peer is considered "known" or "autopeered", then its connection is protected from trimming. Optionally an alias for the peer can be defined to better identify it afterwards.

func (*Manager) ConnectedCount

func (m *Manager) ConnectedCount(relation ...PeerRelation) int

ConnectedCount returns the count of connected peer. Optionally only including peers with the given relation.

func (*Manager) DisallowPeer

func (m *Manager) DisallowPeer(peerID peer.ID) error

DisallowPeer disallows incoming connections from the given peer (autopeering).

func (*Manager) DisconnectPeer

func (m *Manager) DisconnectPeer(peerID peer.ID, disconnectReason ...error) error

DisconnectPeer disconnects the given peer. If the peer is considered "known", then its connection is unprotected from future trimming.

func (*Manager) ForEach

func (m *Manager) ForEach(f PeerForEachFunc, filter ...PeerRelation)

ForEach calls the given PeerForEachFunc on each Peer. Optionally only loops over the peers with the given filter relation.

func (*Manager) IsAllowed

func (m *Manager) IsAllowed(peerID peer.ID) bool

IsAllowed tells whether a connection to the given peer is allowed (autopeering).

func (*Manager) IsConnected

func (m *Manager) IsConnected(peerID peer.ID) bool

IsConnected tells whether there is a connection to the given peer.

func (*Manager) PeerInfoSnapshot

func (m *Manager) PeerInfoSnapshot(id peer.ID) *PeerInfoSnapshot

PeerInfoSnapshot returns a snapshot of information of a peer with given id. If the peer is not known to the Manager, result is nil.

func (*Manager) PeerInfoSnapshots

func (m *Manager) PeerInfoSnapshots() []*PeerInfoSnapshot

PeerInfoSnapshots returns snapshots of information of peers known to the Manager.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context)

Start starts the Manager's event loop. This method blocks until the given context is done.

type ManagerEvents

type ManagerEvents struct {
	// Fired when the Manager is instructed to establish a connection to a peer.
	Connect *events.Event
	// Fired when the Manager is instructed to disconnect a peer.
	Disconnect *events.Event
	// Fired when a peer got allowed.
	Allowed *events.Event
	// Fired when a peer got disallowed.
	Disallowed *events.Event
	// Fired when a peer got connected.
	Connected *events.Event
	// Fired when a peer got disconnected.
	Disconnected *events.Event
	// Fired when a reconnect is scheduled.
	ScheduledReconnect *events.Event
	// Fired when the Manager tries to reconnect to a peer.
	Reconnecting *events.Event
	// Fired when a peer has been reconnected.
	Reconnected *events.Event
	// Fired when the relation to a peer has been updated.
	RelationUpdated *events.Event
	// Fired when the Manager's state changes.
	StateChange *events.Event
	// Fired when internal error happens.
	Error *events.Event
}

ManagerEvents are events happening around a Manager. No methods on Manager must be called from within the event handlers.

type ManagerOption

type ManagerOption func(opts *ManagerOptions)

ManagerOption is a function setting a ManagerOptions option.

func WithManagerLogger

func WithManagerLogger(logger *logger.Logger) ManagerOption

WithManagerLogger enables logging within the Manager.

func WithManagerReconnectInterval

func WithManagerReconnectInterval(interval time.Duration, jitter time.Duration) ManagerOption

WithManagerReconnectInterval defines the re-connect interval for peers to which the Manager wants to keep a connection open to.

type ManagerOptions

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

ManagerOptions define options for a Manager.

type ManagerState

type ManagerState string

ManagerState represents the state in which the Manager is in.

const (
	// ManagerStateStarted means that the Manager has been started.
	ManagerStateStarted ManagerState = "started"
	// ManagerStateStopping means that the Manager is halting its operation.
	ManagerStateStopping ManagerState = "stopping"
	// ManagerStateStopped means tha the Manager has halted its operation.
	ManagerStateStopped ManagerState = "stopped"
)

type Peer

type Peer struct {
	// The ID of the peer.
	ID peer.ID
	// The relation to the peer.
	Relation PeerRelation
	// The addresses under which the peer was added.
	Addrs []multiaddr.Multiaddr
	// The alias of the peer for better recognizing it.
	Alias string
	// contains filtered or unexported fields
}

Peer is a remote peer in the network.

func NewPeer

func NewPeer(peerID peer.ID, relation PeerRelation, addrs []multiaddr.Multiaddr, alias string) *Peer

NewPeer creates a new Peer.

func (*Peer) InfoSnapshot

func (p *Peer) InfoSnapshot() *PeerInfoSnapshot

InfoSnapshot returns a snapshot of the peer in time of calling Info().

type PeerConfig

type PeerConfig struct {
	MultiAddress string `json:"multiAddress" koanf:"multiAddress"`
	Alias        string `json:"alias" koanf:"alias"`
}

PeerConfig holds the initial information about peers.

type PeerForEachFunc

type PeerForEachFunc func(p *Peer) bool

PeerForEachFunc is used in Manager.ForEach. Returning false indicates to stop looping. This function must not call any methods on Manager.

type PeerFunc

type PeerFunc func(p *Peer)

PeerFunc gets called with the given Peer.

type PeerInfoSnapshot

type PeerInfoSnapshot struct {
	// The instance of the peer.
	Peer *Peer `json:"-"`
	// The ID of the peer.
	ID string `json:"address"`
	// The addresses of the peer.
	Addresses []multiaddr.Multiaddr `json:"addresses"`
	// The alias of the peer.
	Alias string `json:"alias,omitempty"`
	// The amount of sent packets to the peer.
	SentPackets uint32 `json:"sentPackets"`
	// The amount of dropped packets.
	DroppedSentPackets uint32 `json:"droppedSentPackets"`
	// Whether the peer is connected.
	Connected bool `json:"connected"`
	// The relation to the peer.
	Relation string `json:"relation"`
}

PeerInfoSnapshot acts as a static snapshot of information about a peer.

type PeerOptError

type PeerOptError struct {
	Peer  *Peer
	Error error
}

PeerOptError holds a Peer and optionally an error.

type PeerRelation

type PeerRelation string

PeerRelation defines the type of relation to a remote peer.

const (
	// PeerRelationKnown is a relation to a peer which most
	// likely stems from knowing the operator of said peer.
	// Connections to such peers are subject to automatic reconnections.
	PeerRelationKnown PeerRelation = "known"
	// PeerRelationUnknown is a relation to an unknown peer.
	// Connections to such peers do not have to be retained.
	PeerRelationUnknown PeerRelation = "unknown"
	// PeerRelationAutopeered is a relation to an autopeered peer.
	// Connections to such peers do not have to be retained.
	PeerRelationAutopeered PeerRelation = "autopeered"
)

type PeerStoreContainer

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

PeerStoreContainer is a container for a libp2p peer store.

func NewPeerStoreContainer

func NewPeerStoreContainer(peerStorePath string, dbEngine database.Engine, createDatabaseIfNotExists bool) (*PeerStoreContainer, error)

NewPeerStoreContainer creates a peerstore using kvstore.

func (*PeerStoreContainer) Close

func (psc *PeerStoreContainer) Close() error

Close flushes all outstanding write operations and closes the store.

func (*PeerStoreContainer) Flush

func (psc *PeerStoreContainer) Flush() error

Flush persists all outstanding write operations to disc.

func (*PeerStoreContainer) Peerstore

func (psc *PeerStoreContainer) Peerstore() peerstore.Peerstore

Peerstore returns the libp2p peer store from the container.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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