p2p

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
	// package.
	MetricsSubsystem = "p2p"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a P2P client, implemented with libp2p.

Initially, client connects to predefined seed nodes (aka bootnodes, bootstrap nodes). Those seed nodes serve Kademlia DHT protocol, and are agnostic to ORU chain. Using DHT peer routing and discovery clients find other peers within ORU network.

func NewClient

func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, ds datastore.Datastore, logger log.Logger, metrics *Metrics) (*Client, error)

NewClient creates new Client object.

Basic checks on parameters are done, and default parameters are provided for unset-configuration TODO(tzdybal): consider passing entire config, not just P2P config, to reduce number of arguments

func (*Client) Addrs

func (c *Client) Addrs() []multiaddr.Multiaddr

Addrs returns listen addresses of Client.

func (*Client) Close

func (c *Client) Close() error

Close gently stops Client.

func (*Client) ConnectionGater added in v0.7.0

func (c *Client) ConnectionGater() *conngater.BasicConnectionGater

ConnectionGater returns the client's connection gater

func (*Client) GossipTx

func (c *Client) GossipTx(ctx context.Context, tx []byte) error

GossipTx sends the transaction to the P2P network.

func (*Client) Host added in v0.7.0

func (c *Client) Host() host.Host

Host returns the libp2p node in a peer-to-peer network

func (*Client) Info

func (c *Client) Info() (p2p.ID, string, string, error)

Info returns client ID, ListenAddr, and Network info

func (*Client) PeerIDs added in v0.7.0

func (c *Client) PeerIDs() []peer.ID

PeerIDs returns list of peer IDs of connected peers excluding self and inactive

func (*Client) Peers

func (c *Client) Peers() []PeerConnection

Peers returns list of peers connected to Client.

func (*Client) PubSub added in v0.7.0

func (c *Client) PubSub() *pubsub.PubSub

PubSub returns the libp2p node pubsub for adding future subscriptions

func (*Client) SetTxValidator

func (c *Client) SetTxValidator(val GossipValidator)

SetTxValidator sets the callback function, that will be invoked during message gossiping.

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

Start establish Client's P2P connectivity.

Following steps are taken: 1. Setup libp2p host, start listening for incoming connections. 2. Setup gossibsub. 3. Setup DHT, establish connection to seed nodes and initialize peer discovery. 4. Use active peer discovery to look for peers from same ORU network.

type GossipMessage

type GossipMessage struct {
	Data []byte
	From peer.ID
}

GossipMessage represents message gossiped via P2P network (e.g. transaction, Block etc).

type GossipValidator

type GossipValidator func(*GossipMessage) bool

GossipValidator is a callback function type.

type Gossiper

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

Gossiper is an abstraction of P2P publish subscribe mechanism.

func NewGossiper

func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger log.Logger, options ...GossiperOption) (*Gossiper, error)

NewGossiper creates new, ready to use instance of Gossiper.

Returned Gossiper object can be used for sending (Publishing) and receiving messages in topic identified by topicStr.

func (*Gossiper) Close

func (g *Gossiper) Close() error

Close is used to disconnect from topic and free resources used by Gossiper.

func (*Gossiper) ProcessMessages

func (g *Gossiper) ProcessMessages(ctx context.Context)

ProcessMessages waits for messages published in the topic and execute handler.

func (*Gossiper) Publish

func (g *Gossiper) Publish(ctx context.Context, data []byte) error

Publish publishes data to gossip topic.

type GossiperOption

type GossiperOption func(*Gossiper) error

GossiperOption sets optional parameters of Gossiper.

func WithValidator

func WithValidator(validator GossipValidator) GossiperOption

WithValidator options registers topic validator for Gossiper.

type Metrics added in v0.11.19

type Metrics struct {
	// Number of peers.
	Peers metrics.Gauge
	// Number of bytes received from a given peer.
	PeerReceiveBytesTotal metrics.Counter `metrics_labels:"peer_id,chID"`
	// Number of bytes sent to a given peer.
	PeerSendBytesTotal metrics.Counter `metrics_labels:"peer_id,chID"`
	// Pending bytes to be sent to a given peer.
	PeerPendingSendBytes metrics.Gauge `metrics_labels:"peer_id"`
	// Number of transactions submitted by each peer.
	NumTxs metrics.Gauge `metrics_labels:"peer_id"`
	// Number of bytes of each message type received.
	MessageReceiveBytesTotal metrics.Counter `metrics_labels:"message_type"`
	// Number of bytes of each message type sent.
	MessageSendBytesTotal metrics.Counter `metrics_labels:"message_type"`
}

Metrics contains metrics exposed by this package.

func NopMetrics added in v0.11.19

func NopMetrics() *Metrics

NopMetrics returns no-op Metrics.

func PrometheusMetrics added in v0.11.19

func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics

PrometheusMetrics returns Metrics build using Prometheus client library. Optionally, labels can be provided along with their values ("foo", "fooValue").

type PeerConnection

type PeerConnection struct {
	NodeInfo         p2p.DefaultNodeInfo  `json:"node_info"`
	IsOutbound       bool                 `json:"is_outbound"`
	ConnectionStatus p2p.ConnectionStatus `json:"connection_status"`
	RemoteIP         string               `json:"remote_ip"`
}

PeerConnection describe basic information about P2P connection. TODO(tzdybal): move it somewhere

Jump to

Keyboard shortcuts

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