memp2p

package
v1.0.62 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCannotSendToSelf = errors.New("cannot send message to itself")

ErrCannotSendToSelf signals that a peer tried to send a message to itself

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 Message added in v1.0.12

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

Message represents a message to be sent through the in-memory network simulated by the Network struct.

func NewMessage added in v1.0.12

func NewMessage(topic string, data []byte, peerID p2p.PeerID) (*Message, error)

NewMessage constructs a new Message instance from arguments

func (*Message) Data added in v1.0.12

func (message *Message) Data() []byte

Data returns the message payload

func (*Message) From added in v1.0.12

func (message *Message) From() []byte

From returns the message originator's peer ID

func (*Message) IsInterfaceNil added in v1.0.16

func (message *Message) IsInterfaceNil() bool

IsInterfaceNil returns true if there is no value under the interface

func (*Message) Key added in v1.0.12

func (message *Message) Key() []byte

Key returns the message public key (if it can not be recovered from From field)

func (*Message) Peer added in v1.0.12

func (message *Message) Peer() p2p.PeerID

Peer returns the peer that originated the message

func (*Message) SeqNo added in v1.0.12

func (message *Message) SeqNo() []byte

SeqNo returns the message sequence number

func (*Message) Signature added in v1.0.12

func (message *Message) Signature() []byte

Signature returns the message signature

func (*Message) TopicIDs added in v1.0.12

func (message *Message) TopicIDs() []string

TopicIDs returns the topic on which the message was sent

type Messenger

type Messenger struct {
	Network     *Network
	P2PID       p2p.PeerID
	Address     string
	Topics      map[string]p2p.MessageProcessor
	TopicsMutex sync.RWMutex
}

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(channel 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(channel 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(address 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() []p2p.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) []p2p.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, createChannelForTopic 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) 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() p2p.PeerID

ID returns the P2P ID of the messenger

func (*Messenger) IsConnected

func (messenger *Messenger) IsConnected(peerID p2p.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) OutgoingChannelLoadBalancer

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

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

func (*Messenger) PeerAddress

func (messenger *Messenger) PeerAddress(pid p2p.PeerID) string

PeerAddress creates the address string from a given peer ID.

func (*Messenger) Peers

func (messenger *Messenger) Peers() []p2p.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) ReceiveMessage added in v1.0.12

func (messenger *Messenger) ReceiveMessage(topic string, message p2p.MessageP2P, allowBroadcast bool) error

ReceiveMessage handles the received message by passing it to the message processor of the corresponding topic, given that this Messenger has previously registered a message processor for that topic. The Network will log the message only if the Network.LogMessages flag is set and only if the Messenger has the requested topic and MessageProcessor.

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 p2p.PeerID) error

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

func (*Messenger) SetThresholdMinConnectedPeers

func (messenger *Messenger) SetThresholdMinConnectedPeers(minConnectedPeers 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 {
	LogMessages bool
	Messages    []p2p.MessageP2P
	// 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, error)

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

func (*Network) GetMessageCount added in v1.0.12

func (network *Network) GetMessageCount() int

GetMessageCount returns the number of messages logged internally by the Network.

func (*Network) IsPeerConnected

func (network *Network) IsPeerConnected(peerID p2p.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) ListAddresses added in v1.0.12

func (network *Network) ListAddresses() []string

ListAddresses provides the addresses of the known peers.

func (*Network) ListAddressesExceptOne

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

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

func (*Network) LogMessage added in v1.0.12

func (network *Network) LogMessage(message p2p.MessageP2P)

LogMessage adds a message to its internal log of messages.

func (*Network) PeerIDs

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

PeerIDs provides a copy of its internal slice of peerIDs

func (*Network) PeerIDsExceptOne

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

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

func (*Network) Peers

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

Peers provides a copy of its internal map of peers

func (*Network) PeersExceptOne

func (network *Network) PeersExceptOne(peerIDToExclude p2p.PeerID) map[p2p.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 p2p.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