memp2p

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNilNetwork = errors.New("nil network")

ErrNilNetwork signals that a nil was given where a memp2p.Network instance was expected

View Source
var ErrNotConnectedToNetwork = errors.New("not connected to network")

ErrNotConnectedToNetwork signals that a peer tried to perform a network-related operation, but is not connected to any network

View Source
var ErrReceivingPeerNotConnected = errors.New("receiving peer not connected to network")

ErrReceivingPeerNotConnected signals that the receiving peer of a sending operation is not connected to the network

Functions

This section is empty.

Types

type Messenger

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

Messenger is an implementation of the p2p.Messenger interface that uses no real networking code, but instead connects to a network simulated in memory (the Network struct). The Messenger is intended for use in automated tests instead of the real libp2p, in order to speed up their execution and reduce resource usage.

All message-sending functions imitate the synchronous/asynchronous behavior of the Messenger struct originally implemented for libp2p. Note that the Network ensures that all messengers are connected to all other messengers, thus when a Messenger is connected to an in-memory network, it reports being connected to all the nodes. Consequently, broadcasting a message will be received by all the messengers in the network.

func NewMessenger

func NewMessenger(network *Network) (*Messenger, error)

NewMessenger constructs a new Messenger that is connected to the Network instance provided as argument.

func (*Messenger) Addresses

func (messenger *Messenger) Addresses() []string

Addresses returns a list of all the physical addresses that this Messenger is bound to and listening to, depending on the available network interfaces of the machine. Being an in-memory simulation, the only possible address to return is an artificial one, built by the constructor NewMessenger().

func (*Messenger) Bootstrap

func (messenger *Messenger) Bootstrap() error

Bootstrap does nothing, as it is not applicable to the in-memory messenger.

func (*Messenger) Broadcast

func (messenger *Messenger) Broadcast(topic string, buff []byte)

Broadcast asynchronously sends the message to all peers in the network. It calls parametricBroadcast() with async=true, which means that peers will have their ReceiveMessage() function independently called as go-routines.

func (*Messenger) BroadcastOnChannel

func (messenger *Messenger) BroadcastOnChannel(_ string, topic string, buff []byte)

BroadcastOnChannel sends the message to all peers in the network. It calls parametricBroadcast() with async=false, which means that peers will have their ReceiveMessage() function called synchronously. The call to parametricBroadcast() is done as a go-routine, which means this function is, in fact, non-blocking, but it is identical with BroadcastOnChannelBlocking() in all other regards.

func (*Messenger) BroadcastOnChannelBlocking

func (messenger *Messenger) BroadcastOnChannelBlocking(_ string, topic string, buff []byte) error

BroadcastOnChannelBlocking sends the message to all peers in the network. It calls parametricBroadcast() with async=false, which means that peers will have their ReceiveMessage() function called synchronously. The call to parametricBroadcast() is done synchronously as well. This function should be called as a go-routine.

func (*Messenger) Close

func (messenger *Messenger) Close() error

Close disconnects this Messenger from the network it was connected to.

func (*Messenger) ConnectToPeer

func (messenger *Messenger) ConnectToPeer(_ string) error

ConnectToPeer usually does nothing, because peers connected to the in-memory network are already all connected to each other. This function will return an error if the Messenger is not connected to the network, though.

func (*Messenger) ConnectedAddresses

func (messenger *Messenger) ConnectedAddresses() []string

ConnectedAddresses returns a slice of peer addresses to which this Messenger is connected. If this Messenger is connected to the network, then the addresses of all the other peers in the network are returned.

func (*Messenger) ConnectedPeers

func (messenger *Messenger) ConnectedPeers() []core.PeerID

ConnectedPeers returns a slice of IDs belonging to the peers to which this Messenger is connected. If the Messenger is connected to the in₋memory network, then the function returns a slice containing the IDs of all the other peers connected to the network. Returns false if the Messenger is not connected.

func (*Messenger) ConnectedPeersOnTopic

func (messenger *Messenger) ConnectedPeersOnTopic(topic string) []core.PeerID

ConnectedPeersOnTopic returns a slice of IDs belonging to the peers in the network that have declared their interest in the given topic and are listening to messages on that topic.

func (*Messenger) CreateTopic

func (messenger *Messenger) CreateTopic(name string, _ bool) error

CreateTopic adds the topic provided as argument to the list of topics of interest for this Messenger. It also registers a nil message validator to handle the messages received on this topic.

func (*Messenger) GetConnectedPeersInfo

func (messenger *Messenger) GetConnectedPeersInfo() *p2p.ConnectedPeersInfo

GetConnectedPeersInfo returns a nil object. Not implemented.

func (*Messenger) HasTopic

func (messenger *Messenger) HasTopic(name string) bool

HasTopic returns true if this Messenger has declared interest in the given topic; returns false otherwise.

func (*Messenger) HasTopicValidator

func (messenger *Messenger) HasTopicValidator(name string) bool

HasTopicValidator returns true if this Messenger has declared interest in the given topic and has registered a non-nil validator on that topic. Returns false otherwise.

func (*Messenger) ID

func (messenger *Messenger) ID() core.PeerID

ID returns the P2P ID of the messenger

func (*Messenger) IsConnected

func (messenger *Messenger) IsConnected(_ core.PeerID) bool

IsConnected returns true if this Messenger is connected to the peer with the specified ID. It always returns true if the Messenger is connected to the network and false otherwise, regardless of the provided peer ID.

func (*Messenger) IsConnectedToNetwork

func (messenger *Messenger) IsConnectedToNetwork() bool

IsConnectedToNetwork returns true if this messenger is connected to the in-memory network, false otherwise.

func (*Messenger) IsConnectedToTheNetwork

func (messenger *Messenger) IsConnectedToTheNetwork() bool

IsConnectedToTheNetwork returns true as this implementation is always connected to its network

func (*Messenger) IsInterfaceNil

func (messenger *Messenger) IsInterfaceNil() bool

IsInterfaceNil returns true if there is no value under the interface

func (*Messenger) NumMessagesReceived

func (messenger *Messenger) NumMessagesReceived() uint64

NumMessagesReceived returns the number of messages received

func (*Messenger) OutgoingChannelLoadBalancer

func (messenger *Messenger) OutgoingChannelLoadBalancer() p2p.ChannelLoadBalancer

OutgoingChannelLoadBalancer does nothing, as it is not applicable to the in-memory network.

func (*Messenger) PeerAddresses added in v1.0.130

func (messenger *Messenger) PeerAddresses(pid core.PeerID) []string

PeerAddresses creates the address string from a given peer ID.

func (*Messenger) Peers

func (messenger *Messenger) Peers() []core.PeerID

Peers returns a slice containing the P2P IDs of all the other peers that it has knowledge of. Since this is an in-memory network structured as a fully connected graph, this function returns the list of the P2P IDs of all the peers in the network (assuming this Messenger is connected).

func (*Messenger) RegisterMessageProcessor

func (messenger *Messenger) RegisterMessageProcessor(topic string, handler p2p.MessageProcessor) error

RegisterMessageProcessor sets the provided message processor to be the processor of received messages for the given topic.

func (*Messenger) SendToConnectedPeer

func (messenger *Messenger) SendToConnectedPeer(topic string, buff []byte, peerID core.PeerID) error

SendToConnectedPeer sends a message directly to the peer specified by the ID.

func (*Messenger) SetPeerDenialEvaluator added in v1.0.133

func (messenger *Messenger) SetPeerDenialEvaluator(_ p2p.PeerDenialEvaluator) error

SetPeerDenialEvaluator does nothing

func (*Messenger) SetPeerShardResolver

func (messenger *Messenger) SetPeerShardResolver(_ p2p.PeerShardResolver) error

SetPeerShardResolver is a dummy function, not setting anything

func (*Messenger) SetThresholdMinConnectedPeers

func (messenger *Messenger) SetThresholdMinConnectedPeers(_ int) error

SetThresholdMinConnectedPeers does nothing as this implementation is always connected to its network

func (*Messenger) ThresholdMinConnectedPeers

func (messenger *Messenger) ThresholdMinConnectedPeers() int

ThresholdMinConnectedPeers always return 0

func (*Messenger) TrimConnections

func (messenger *Messenger) TrimConnections()

TrimConnections does nothing, as it is not applicable to the in-memory messenger.

func (*Messenger) UnregisterMessageProcessor

func (messenger *Messenger) UnregisterMessageProcessor(topic string) error

UnregisterMessageProcessor unsets the message processor for the given topic (sets it to nil).

type Network

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

Network provides in-memory connectivity for the Messenger struct. It simulates a network where each peer is connected to all the other peers. The peers are connected to the network if they are in the internal `peers` map; otherwise, they are disconnected.

func NewNetwork

func NewNetwork() *Network

NewNetwork constructs a new Network instance with an empty internal map of peers.

func (*Network) IsPeerConnected

func (network *Network) IsPeerConnected(peerID core.PeerID) bool

IsPeerConnected returns true if the peer represented by the provided ID is found in the inner `peers` map of the Network instance, which determines whether it is connected to the network or not.

func (*Network) ListAddressesExceptOne

func (network *Network) ListAddressesExceptOne(peerIDToExclude core.PeerID) []string

ListAddressesExceptOne provides the addresses of the known peers, except a specified one.

func (*Network) PeerIDs

func (network *Network) PeerIDs() []core.PeerID

PeerIDs provides a copy of its internal slice of peerIDs

func (*Network) PeerIDsExceptOne

func (network *Network) PeerIDsExceptOne(peerIDToExclude core.PeerID) []core.PeerID

PeerIDsExceptOne provides a copy of its internal slice of peerIDs, excluding a specific peer.

func (*Network) Peers

func (network *Network) Peers() map[core.PeerID]*Messenger

Peers provides a copy of its internal map of peers

func (*Network) PeersExceptOne

func (network *Network) PeersExceptOne(peerIDToExclude core.PeerID) map[core.PeerID]*Messenger

PeersExceptOne provides a copy of its internal map of peers, excluding a specific peer.

func (*Network) RegisterPeer

func (network *Network) RegisterPeer(messenger *Messenger)

RegisterPeer adds a messenger to the Peers map and its PeerID to the peerIDs slice.

func (*Network) UnregisterPeer

func (network *Network) UnregisterPeer(peerID core.PeerID)

UnregisterPeer removes a messenger from the Peers map and its PeerID from the peerIDs slice.

Jump to

Keyboard shortcuts

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