Documentation
¶
Index ¶
- Variables
- type Messenger
- func (messenger *Messenger) Addresses() []string
- func (messenger *Messenger) Bootstrap() error
- func (messenger *Messenger) Broadcast(topic string, buff []byte)
- func (messenger *Messenger) BroadcastOnChannel(_ string, topic string, buff []byte)
- func (messenger *Messenger) BroadcastOnChannelBlocking(_ string, topic string, buff []byte) error
- func (messenger *Messenger) Close() error
- func (messenger *Messenger) ConnectToPeer(_ string) error
- func (messenger *Messenger) ConnectedAddresses() []string
- func (messenger *Messenger) ConnectedPeers() []core.PeerID
- func (messenger *Messenger) ConnectedPeersOnTopic(topic string) []core.PeerID
- func (messenger *Messenger) CreateTopic(name string, _ bool) error
- func (messenger *Messenger) GetConnectedPeersInfo() *p2p.ConnectedPeersInfo
- func (messenger *Messenger) HasTopic(name string) bool
- func (messenger *Messenger) HasTopicValidator(name string) bool
- func (messenger *Messenger) ID() core.PeerID
- func (messenger *Messenger) IsConnected(_ core.PeerID) bool
- func (messenger *Messenger) IsConnectedToNetwork() bool
- func (messenger *Messenger) IsConnectedToTheNetwork() bool
- func (messenger *Messenger) IsInterfaceNil() bool
- func (messenger *Messenger) NumMessagesReceived() uint64
- func (messenger *Messenger) OutgoingChannelLoadBalancer() p2p.ChannelLoadBalancer
- func (messenger *Messenger) PeerAddress(pid core.PeerID) string
- func (messenger *Messenger) Peers() []core.PeerID
- func (messenger *Messenger) RegisterMessageProcessor(topic string, handler p2p.MessageProcessor) error
- func (messenger *Messenger) SendToConnectedPeer(topic string, buff []byte, peerID core.PeerID) error
- func (messenger *Messenger) SetPeerBlackListHandler(_ p2p.PeerBlacklistHandler) error
- func (messenger *Messenger) SetPeerShardResolver(_ p2p.PeerShardResolver) error
- func (messenger *Messenger) SetThresholdMinConnectedPeers(_ int) error
- func (messenger *Messenger) ThresholdMinConnectedPeers() int
- func (messenger *Messenger) TrimConnections()
- func (messenger *Messenger) UnregisterMessageProcessor(topic string) error
- type Network
- func (network *Network) IsPeerConnected(peerID core.PeerID) bool
- func (network *Network) ListAddressesExceptOne(peerIDToExclude core.PeerID) []string
- func (network *Network) PeerIDs() []core.PeerID
- func (network *Network) PeerIDsExceptOne(peerIDToExclude core.PeerID) []core.PeerID
- func (network *Network) Peers() map[core.PeerID]*Messenger
- func (network *Network) PeersExceptOne(peerIDToExclude core.PeerID) map[core.PeerID]*Messenger
- func (network *Network) RegisterPeer(messenger *Messenger)
- func (network *Network) UnregisterPeer(peerID core.PeerID)
Constants ¶
This section is empty.
Variables ¶
var ErrNilNetwork = errors.New("nil network")
ErrNilNetwork signals that a nil was given where a memp2p.Network instance was expected
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
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 ¶
NewMessenger constructs a new Messenger that is connected to the Network instance provided as argument.
func (*Messenger) Addresses ¶
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 ¶
Bootstrap does nothing, as it is not applicable to the in-memory messenger.
func (*Messenger) Broadcast ¶
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 ¶
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 ¶
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) ConnectToPeer ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
HasTopic returns true if this Messenger has declared interest in the given topic; returns false otherwise.
func (*Messenger) HasTopicValidator ¶
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) IsConnected ¶
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 ¶
IsConnectedToNetwork returns true if this messenger is connected to the in-memory network, false otherwise.
func (*Messenger) IsConnectedToTheNetwork ¶
IsConnectedToTheNetwork returns true as this implementation is always connected to its network
func (*Messenger) IsInterfaceNil ¶
IsInterfaceNil returns true if there is no value under the interface
func (*Messenger) NumMessagesReceived ¶
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) PeerAddress ¶
PeerAddress creates the address string from a given peer ID.
func (*Messenger) Peers ¶
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) SetPeerBlackListHandler ¶
func (messenger *Messenger) SetPeerBlackListHandler(_ p2p.PeerBlacklistHandler) error
SetPeerBlackListHandler does nothing
func (*Messenger) SetPeerShardResolver ¶
func (messenger *Messenger) SetPeerShardResolver(_ p2p.PeerShardResolver) error
SetPeerShardResolver is a dummy function, not setting anything
func (*Messenger) SetThresholdMinConnectedPeers ¶
SetThresholdMinConnectedPeers does nothing as this implementation is always connected to its network
func (*Messenger) ThresholdMinConnectedPeers ¶
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 ¶
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 ¶
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 ¶
ListAddressesExceptOne provides the addresses of the known peers, except a specified one.
func (*Network) PeerIDsExceptOne ¶
PeerIDsExceptOne provides a copy of its internal slice of peerIDs, excluding a specific peer.
func (*Network) PeersExceptOne ¶
PeersExceptOne provides a copy of its internal map of peers, excluding a specific peer.
func (*Network) RegisterPeer ¶
RegisterPeer adds a messenger to the Peers map and its PeerID to the peerIDs slice.
func (*Network) UnregisterPeer ¶
UnregisterPeer removes a messenger from the Peers map and its PeerID from the peerIDs slice.