p2p

package
v1.0.98 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2020 License: GPL-3.0 Imports: 6 Imported by: 0

README

P2P protocol description

The Messenger interface with its implementation are used to define the way to communicate between Elrond nodes.

There are 2 ways to send data to the other peers:

  1. Broadcasting messages on a pubsub using topics;
  2. Direct sending messages to the connected peers.

The first type is used to send messages that has to reach every node (from corresponding shard, metachain, consensus group, etc.) and the second type is used to resolve requests comming from directly connected peers.

Documentation

Index

Constants

View Source
const (
	// PrioBitsSharder is the variant that uses priority bits
	PrioBitsSharder = "PrioBitsSharder"
	// SimplePrioBitsSharder is the variant that computes the distance without prio bits
	SimplePrioBitsSharder = "SimplePrioBitsSharder"
	// ListsSharder is the variant that uses lists
	ListsSharder = "ListsSharder"
	// OneListSharder is the variant that is shard agnostic and uses one list
	OneListSharder = "OneListSharder"
	// NilListSharder is the variant that will not do connection trimming
	NilListSharder = "NilListSharder"
)

Variables

View Source
var ErrAlreadySeenMessage = errors.New("already seen this message")

ErrAlreadySeenMessage signals that the message has already been seen

View Source
var ErrChannelAlreadyExists = errors.New("channel already exists")

ErrChannelAlreadyExists signals that the channel is already defined (and used)

View Source
var ErrChannelCanNotBeDeleted = errors.New("channel can not be deleted")

ErrChannelCanNotBeDeleted signals that a channel can not be deleted (might be the default channel)

View Source
var ErrChannelDoesNotExist = errors.New("channel does not exist")

ErrChannelDoesNotExist signals that a requested channel does not exist

View Source
var ErrContextDone = errors.New("context done")

ErrContextDone signals that an operation was canceled by context done

View Source
var ErrEmptyBuffer = errors.New("empty buffer")

ErrEmptyBuffer signals that an empty buffer has been provided

View Source
var ErrEmptySeed = errors.New("empty seed")

ErrEmptySeed signals that an empty seed has been provided

View Source
var ErrEmptyTopicList = errors.New("empty topicIDs")

ErrEmptyTopicList signals that a message with empty topic ids has been received

View Source
var ErrInvalidDurationProvided = errors.New("invalid time.Duration provided")

ErrInvalidDurationProvided signals that an invalid time.Duration has been provided

View Source
var ErrInvalidPort = errors.New("invalid port provided")

ErrInvalidPort signals that an invalid port was provided

View Source
var ErrInvalidValue = errors.New("invalid value")

ErrInvalidValue signals that an invalid value has been provided

View Source
var ErrMessageTooLarge = errors.New("buffer too large")

ErrMessageTooLarge signals that the message provided is too large

View Source
var ErrNilConfigVariable = errors.New("nil config variable")

ErrNilConfigVariable signals that a nil config variable has been provided

View Source
var ErrNilContext = errors.New("nil context")

ErrNilContext signals that a nil context was provided

View Source
var ErrNilDirectSendMessageHandler = errors.New("nil direct sender message handler")

ErrNilDirectSendMessageHandler signals that the message handler for new message has not been wired

View Source
var ErrNilFetchPeersOnTopicHandler = errors.New("nil fetch peers on topic handler")

ErrNilFetchPeersOnTopicHandler signals that a nil handler was provided

View Source
var ErrNilFloodPreventer = errors.New("nil flood preventer")

ErrNilFloodPreventer signals that a nil flood preventer has been provided

View Source
var ErrNilHost = errors.New("nil host")

ErrNilHost signals that a nil host has been provided

View Source
var ErrNilMarshalizer = errors.New("nil marshalizer")

ErrNilMarshalizer signals that an operation has been attempted to or with a nil marshalizer implementation

View Source
var ErrNilMessage = errors.New("nil message")

ErrNilMessage signals that a nil message has been received

View Source
var ErrNilMockNet = errors.New("nil mocknet provided")

ErrNilMockNet signals that a nil mocknet was provided. Should occur only in testing!!!

View Source
var ErrNilNetworkShardingCollector = errors.New("nil network sharding collector")

ErrNilNetworkShardingCollector signals that the network sharding collector provided is nil

View Source
var ErrNilPeerBlacklistHandler = errors.New("nil peer black list handler")

ErrNilPeerBlacklistHandler signals that a nil peer black list handler was provided

View Source
var ErrNilPeerShardResolver = errors.New("nil PeerShardResolver")

ErrNilPeerShardResolver signals that the peer shard resolver provided is nil

View Source
var ErrNilReconnecter = errors.New("nil reconnecter")

ErrNilReconnecter signals that a nil reconnecter has been provided

View Source
var ErrNilSharder = errors.New("nil sharder")

ErrNilSharder signals that the provided sharder is nil

View Source
var ErrNilSignerVerifier = errors.New("nil signer-verifier")

ErrNilSignerVerifier signals that the signer-verifier instance provided is nil

View Source
var ErrNilStatusHandler = errors.New("nil status handler")

ErrNilStatusHandler signals that a nil status handler has been provided

View Source
var ErrNilTopic = errors.New("nil topic")

ErrNilTopic signals that a nil topic has been provided

View Source
var ErrNilTopicFloodPreventer = errors.New("nil topic flood preventer")

ErrNilTopicFloodPreventer signals that a nil topic flood preventer has been provided

View Source
var ErrNilValidator = errors.New("no validator has been set for this topic")

ErrNilValidator signals that a validator hasn't been set for the required topic

View Source
var ErrPeerBlacklisted = errors.New("peer is blacklisted")

ErrPeerBlacklisted signals that a peer is blacklisted

View Source
var ErrPeerDiscoveryProcessAlreadyStarted = errors.New("peer discovery is already turned on")

ErrPeerDiscoveryProcessAlreadyStarted signals that a peer discovery is already turned on

View Source
var ErrPeerNotDirectlyConnected = errors.New("peer is not directly connected")

ErrPeerNotDirectlyConnected signals that the peer is not directly connected to self

View Source
var ErrSystemBusy = errors.New("system busy")

ErrSystemBusy signals that the system is busy

View Source
var ErrTimeout = errors.New("timeout")

ErrTimeout signals a timeout

View Source
var ErrTooManyGoroutines = errors.New(" number of goroutines exceeded")

ErrTooManyGoroutines is raised when the number of goroutines has exceeded a threshold

View Source
var ErrTopicAlreadyExists = errors.New("topic already exists")

ErrTopicAlreadyExists signals that a topic already exists

View Source
var ErrTopicValidatorOperationNotSupported = errors.New("topic validator operation is not supported")

ErrTopicValidatorOperationNotSupported signals that an unsupported validator operation occurred

View Source
var ErrUnwantedPeer = errors.New("unwanted peer: will not initiate connection as it will get disconnected")

ErrUnwantedPeer signals that the provided peer has a longer kademlia distance in respect with the already connected peers and a connection to this peer will result in an immediate disconnection

View Source
var ErrWatchdogAlreadyStarted = errors.New("peer discovery watchdog is already started")

ErrWatchdogAlreadyStarted signals that a peer discovery watchdog is already started

View Source
var ErrWatchdogNotStarted = errors.New("peer discovery watchdog is not started")

ErrWatchdogNotStarted signals that a peer discovery watchdog is not started

View Source
var ErrWrongTypeAssertion = errors.New("wrong type assertion")

ErrWrongTypeAssertion signals that a wrong type assertion occurred

Functions

func MessageOriginatorPid

func MessageOriginatorPid(msg MessageP2P) string

MessageOriginatorPid will output the message peer id in a pretty format If it can, it will display the last displayLastPidChars (12) characters from the pid

func MessageOriginatorSeq

func MessageOriginatorSeq(msg MessageP2P) string

MessageOriginatorSeq will output the sequence number as hex

func PeerIdToShortString

func PeerIdToShortString(pid PeerID) string

PeerIdToShortString trims the first displayLastPidChars characters of the provided peer ID after converting the peer ID to string using the Pretty functionality

Types

type BlacklistHandler

type BlacklistHandler interface {
	Has(key string) bool
	IsInterfaceNil() bool
}

BlacklistHandler defines the behavior of a component that is able to decide if a key (peer ID) is black listed or not TODO merge this interface with the PeerShardResolver => P2PProtocolHandler ? TODO move antiflooding inside network messenger

type ChannelLoadBalancer

type ChannelLoadBalancer interface {
	AddChannel(channel string) error
	RemoveChannel(channel string) error
	GetChannelOrDefault(channel string) chan *SendableData
	CollectOneElementFromChannels() *SendableData
	IsInterfaceNil() bool
}

ChannelLoadBalancer defines what a load balancer that uses chans should do

type CommonSharder

type CommonSharder interface {
	SetPeerShardResolver(psp PeerShardResolver) error
	IsInterfaceNil() bool
}

CommonSharder represents the common interface implemented by all sharder implementations

type ConnectedPeersInfo

type ConnectedPeersInfo struct {
	UnknownPeers         []string
	IntraShardValidators []string
	IntraShardObservers  []string
	CrossShardValidators []string
	CrossShardObservers  []string
}

ConnectedPeersInfo represents the DTO structure used to output the metrics for connected peers

type ConnectionMonitorWrapper

type ConnectionMonitorWrapper interface {
	CheckConnectionsBlocking()
	SetBlackListHandler(handler BlacklistHandler) error
	IsInterfaceNil() bool
}

ConnectionMonitorWrapper uses a connection monitor but checks if the peer is blacklisted or not TODO this should be removed after merging of the PeerShardResolver and BlacklistHandler

type DirectSender

type DirectSender interface {
	NextSeqno(counter *uint64) []byte
	Send(topic string, buff []byte, peer PeerID) error
	IsInterfaceNil() bool
}

DirectSender defines a component that can send direct messages to connected peers

type Marshalizer

type Marshalizer interface {
	Marshal(obj interface{}) ([]byte, error)
	Unmarshal(obj interface{}, buff []byte) error
	IsInterfaceNil() bool
}

Marshalizer defines the 2 basic operations: serialize (marshal) and deserialize (unmarshal)

type MessageP2P

type MessageP2P interface {
	From() []byte
	Data() []byte
	SeqNo() []byte
	Topics() []string
	Signature() []byte
	Key() []byte
	Peer() PeerID
	IsInterfaceNil() bool
}

MessageP2P defines what a p2p message can do (should return)

type MessageProcessor

type MessageProcessor interface {
	ProcessReceivedMessage(message MessageP2P, fromConnectedPeer PeerID) error
	IsInterfaceNil() bool
}

MessageProcessor is the interface used to describe what a receive message processor should do All implementations that will be called from Messenger implementation will need to satisfy this interface If the function returns a non nil value, the received message will not be propagated to its connected peers

type Messenger

type Messenger interface {
	io.Closer

	// ID is the Messenger's unique peer identifier across the network (a
	// string). It is derived from the public key of the P2P credentials.
	ID() PeerID

	// Peers is the list of IDs of peers known to the Messenger.
	Peers() []PeerID

	// Addresses is the list of addresses that the Messenger is currently bound
	// to and listening to.
	Addresses() []string

	// ConnectToPeer explicitly connect to a specific peer with a known address (note that the
	// address contains the peer ID). This function is usually not called
	// manually, because any underlying implementation of the Messenger interface
	// should be keeping connections to peers open.
	ConnectToPeer(address string) error

	// IsConnected returns true if the Messenger are connected to a specific peer.
	IsConnected(peerID PeerID) bool

	// ConnectedPeers returns the list of IDs of the peers the Messenger is
	// currently connected to.
	ConnectedPeers() []PeerID

	// ConnectedAddresses returns the list of addresses of the peers to which the
	// Messenger is currently connected.
	ConnectedAddresses() []string

	// PeerAddress builds an address for the given peer ID, e.g.
	// ConnectToPeer(PeerAddress(somePeerID)).
	PeerAddress(pid PeerID) string

	// ConnectedPeersOnTopic returns the IDs of the peers to which the Messenger
	// is currently connected, but filtered by a topic they are registered to.
	ConnectedPeersOnTopic(topic string) []PeerID

	// Bootstrap runs the initialization phase which includes peer discovery,
	// setting up initial connections and self-announcement in the network.
	Bootstrap() error

	// CreateTopic defines a new topic for sending messages, and optionally
	// creates a channel in the LoadBalancer for this topic (otherwise, the topic
	// will use a default channel).
	CreateTopic(name string, createChannelForTopic bool) error

	// HasTopic returns true if the Messenger has declared interest in a topic
	// and it is listening to messages referencing it.
	HasTopic(name string) bool

	// HasTopicValidator returns true if the Messenger has registered a custom
	// validator for a given topic name.
	HasTopicValidator(name string) bool

	// RegisterMessageProcessor adds the provided MessageProcessor to the list
	// of handlers that are invoked whenever a message is received on the
	// specified topic.
	RegisterMessageProcessor(topic string, handler MessageProcessor) error

	// UnregisterMessageProcessor removes the MessageProcessor set by the
	// Messenger from the list of registered handlers for the messages on the
	// given topic.
	UnregisterMessageProcessor(topic string) error

	// OutgoingChannelLoadBalancer returns the ChannelLoadBalancer instance
	// through which the Messenger is sending messages to the network.
	OutgoingChannelLoadBalancer() ChannelLoadBalancer

	// BroadcastOnChannelBlocking asynchronously waits until it can send a
	// message on the channel, but once it is able to, it synchronously sends the
	// message, blocking until sending is completed.
	BroadcastOnChannelBlocking(channel string, topic string, buff []byte) error

	// BroadcastOnChannel asynchronously sends a message on a given topic
	// through a specified channel.
	BroadcastOnChannel(channel string, topic string, buff []byte)

	// Broadcast is a convenience function that calls BroadcastOnChannelBlocking,
	// but implicitly sets the channel to be identical to the specified topic.
	Broadcast(topic string, buff []byte)

	// SendToConnectedPeer asynchronously sends a message to a peer directly,
	// bypassing pubsub and topics. It opens a new connection with the given
	// peer, but reuses a connection and a stream if possible.
	SendToConnectedPeer(topic string, buff []byte, peerID PeerID) error

	IsConnectedToTheNetwork() bool
	ThresholdMinConnectedPeers() int
	SetThresholdMinConnectedPeers(minConnectedPeers int) error
	SetPeerShardResolver(peerShardResolver PeerShardResolver) error
	SetPeerBlackListHandler(handler BlacklistHandler) error
	GetConnectedPeersInfo() *ConnectedPeersInfo

	// IsInterfaceNil returns true if there is no value under the interface
	IsInterfaceNil() bool
}

Messenger is the main struct used for communication with other peers

type NetworkShardingCollector

type NetworkShardingCollector interface {
	UpdatePeerIdPublicKey(pid PeerID, pk []byte)
	IsInterfaceNil() bool
}

NetworkShardingCollector defines the updating methods used by the network sharding component The interface assures that the collected data will be used by the p2p network sharding components

type PeerCounts

type PeerCounts struct {
	UnknownPeers    int
	IntraShardPeers int
	CrossShardPeers int
}

PeerCounts represents the DTO structure used to output the count metrics for connected peers

type PeerDiscoverer

type PeerDiscoverer interface {
	Bootstrap() error
	Name() string
	IsInterfaceNil() bool
}

PeerDiscoverer defines the behaviour of a peer discovery mechanism

type PeerDiscoveryFactory

type PeerDiscoveryFactory interface {
	CreatePeerDiscoverer() (PeerDiscoverer, error)
	IsInterfaceNil() bool
}

PeerDiscoveryFactory defines the factory for peer discoverer implementation

type PeerID

type PeerID string

PeerID is a p2p peer identity.

func (PeerID) Bytes

func (pid PeerID) Bytes() []byte

Bytes returns the peer ID as byte slice

func (PeerID) Pretty

func (pid PeerID) Pretty() string

Pretty returns a b58-encoded string of the peer id

type PeerShardResolver

type PeerShardResolver interface {
	GetPeerInfo(pid PeerID) core.P2PPeerInfo
	IsInterfaceNil() bool
}

PeerShardResolver is able to resolve the link between the provided PeerID and the shardID

type Reconnecter

type Reconnecter interface {
	ReconnectToNetwork() <-chan struct{}
	IsInterfaceNil() bool
}

Reconnecter defines the behaviour of a network reconnection mechanism

type ReconnecterWithPauseResumeAndWatchdog

type ReconnecterWithPauseResumeAndWatchdog interface {
	Reconnecter
	Pause()  // Pause the peer discovery
	Resume() // Resume the peer discovery

	StartWatchdog(time.Duration) error // StartWatchdog start a discovery resume watchdog
	StopWatchdog() error               // StopWatchdog stops the watchdog
	KickWatchdog() error               // KickWatchdog kicks the watchdog
}

ReconnecterWithPauseResumeAndWatchdog defines a Reconnecter that supports pausing, resuming and watchdog

type SendableData

type SendableData struct {
	Buff  []byte
	Topic string
}

SendableData represents the struct used in data throttler implementation

type SignerVerifier

type SignerVerifier interface {
	Sign(message []byte) ([]byte, error)
	Verify(message []byte, sig []byte, pk []byte) error
	PublicKey() []byte
	IsInterfaceNil() bool
}

SignerVerifier is used in higher level protocol authentication of 2 peers after the basic p2p connection has been made

Jump to

Keyboard shortcuts

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