p2p

package
v0.0.0-...-d79950a Latest Latest
Warning

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

Go to latest
Published: May 16, 2020 License: GPL-3.0 Imports: 70 Imported by: 0

Documentation

Overview

Package p2p implements the Ethereum 2.0 networking specification.

Canonical spec reference: https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/p2p-interface.md

Prysm specific implementation design docs

This package is heavily utilizes the libp2p go implementation by Protocol Labs.

Package p2p defines the network protocol implementation for eth2 used by beacon nodes, including peer discovery using discv5, gossip-sub using libp2p, and handing peer lifecycles + handshakes.

Index

Constants

View Source
const (
	// RPCStatusTopic defines the topic for the status rpc method.
	RPCStatusTopic = "/eth2/beacon_chain/req/status/1"
	// RPCGoodByeTopic defines the topic for the goodbye rpc method.
	RPCGoodByeTopic = "/eth2/beacon_chain/req/goodbye/1"
	// RPCBlocksByRangeTopic defines the topic for the blocks by range rpc method.
	RPCBlocksByRangeTopic = "/eth2/beacon_chain/req/beacon_blocks_by_range/1"
	// RPCBlocksByRootTopic defines the topic for the blocks by root rpc method.
	RPCBlocksByRootTopic = "/eth2/beacon_chain/req/beacon_blocks_by_root/1"
	// RPCPingTopic defines the topic for the ping rpc method.
	RPCPingTopic = "/eth2/beacon_chain/req/ping/1"
	// RPCMetaDataTopic defines the topic for the metadata rpc method.
	RPCMetaDataTopic = "/eth2/beacon_chain/req/metadata/1"
)

Variables

View Source
var ErrMessageNotMapped = errors.New("message type is not mapped to a PubSub topic")

ErrMessageNotMapped occurs on a Broadcast attempt when a message has not been defined in the GossipTypeMapping.

View Source
var GossipTopicMappings = map[string]proto.Message{
	"/eth2/%x/beacon_block":                         &pb.SignedBeaconBlock{},
	"/eth2/%x/committee_index%d_beacon_attestation": &pb.Attestation{},
	"/eth2/%x/voluntary_exit":                       &pb.SignedVoluntaryExit{},
	"/eth2/%x/proposer_slashing":                    &pb.ProposerSlashing{},
	"/eth2/%x/attester_slashing":                    &pb.AttesterSlashing{},
	"/eth2/%x/beacon_aggregate_and_proof":           &pb.SignedAggregateAttestationAndProof{},
}

GossipTopicMappings represent the protocol ID to protobuf message type map for easy lookup.

View Source
var GossipTypeMapping = make(map[reflect.Type]string)

GossipTypeMapping is the inverse of GossipTopicMappings so that an arbitrary protobuf message can be mapped to a protocol ID string.

RPCTopicMappings represent the protocol ID to protobuf message type map for easy lookup. These mappings should be used for outbound sending only. Peers may respond with a different message type as defined by the p2p protocol.

Functions

func MakePeer

func MakePeer(addr string) (*peerstore.PeerInfo, error)

MakePeer from multiaddress string.

Types

type Broadcaster

type Broadcaster interface {
	Broadcast(context.Context, proto.Message) error
}

Broadcaster broadcasts messages to peers over the p2p pubsub protocol.

type Config

type Config struct {
	NoDiscovery           bool
	EnableUPnP            bool
	DisableDiscv5         bool
	StaticPeers           []string
	BootstrapNodeAddr     []string
	KademliaBootStrapAddr []string
	Discv5BootStrapAddr   []string
	RelayNodeAddr         string
	LocalIP               string
	HostAddress           string
	HostDNS               string
	PrivateKey            string
	DataDir               string
	MetaDataDir           string
	TCPPort               uint
	UDPPort               uint
	MaxPeers              uint
	WhitelistCIDR         string
	BlacklistCIDR         []string
	Encoding              string
	StateNotifier         statefeed.Notifier
	PubSub                string
}

Config for the p2p service. These parameters are set from application level flags to initialize the p2p service.

type ConnectionHandler

type ConnectionHandler interface {
	AddConnectionHandler(f func(ctx context.Context, id peer.ID) error, g func(context.Context, peer.ID) error)
	AddDisconnectionHandler(f func(ctx context.Context, id peer.ID) error)
}

ConnectionHandler configures p2p to handle connections with a peer.

type EncodingProvider

type EncodingProvider interface {
	Encoding() encoder.NetworkEncoding
}

EncodingProvider provides p2p network encoding.

type Listener

type Listener interface {
	Self() *enode.Node
	Close()
	Lookup(enode.ID) []*enode.Node
	ReadRandomNodes([]*enode.Node) int
	Resolve(*enode.Node) *enode.Node
	LookupRandom() []*enode.Node
	Ping(*enode.Node) error
	RequestENR(*enode.Node) (*enode.Node, error)
	LocalNode() *enode.LocalNode
}

Listener defines the discovery V5 network interface that is used to communicate with other peers.

type MetadataProvider

type MetadataProvider interface {
	Metadata() *pb.MetaData
	MetadataSeq() uint64
}

MetadataProvider returns the metadata related information for the local peer.

type P2P

P2P represents the full p2p interface composed of all of the sub-interfaces.

type PeerManager

type PeerManager interface {
	Disconnect(peer.ID) error
	PeerID() peer.ID
	RefreshENR()
	FindPeersWithSubnet(index uint64) (bool, error)
	AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error)
}

PeerManager abstracts some peer management methods from libp2p.

type PeersProvider

type PeersProvider interface {
	Peers() *peers.Status
}

PeersProvider abstracts obtaining our current list of known peers status.

type PubSubProvider

type PubSubProvider interface {
	PubSub() *pubsub.PubSub
}

PubSubProvider provides the p2p pubsub protocol.

type Sender

type Sender interface {
	Send(context.Context, interface{}, string, peer.ID) (network.Stream, error)
}

Sender abstracts the sending functionality from libp2p.

type Service

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

Service for managing peer to peer (p2p) networking.

func NewService

func NewService(cfg *Config) (*Service, error)

NewService initializes a new p2p service compatible with shared.Service interface. No connections are made until the Start function is called during the service registry startup.

func (*Service) AddConnectionHandler

func (s *Service) AddConnectionHandler(reqFunc func(ctx context.Context, id peer.ID) error,
	goodbyeFunc func(ctx context.Context, id peer.ID) error)

AddConnectionHandler adds a callback function which handles the connection with a newly added peer. It performs a handshake with that peer by sending a hello request and validating the response from the peer.

func (*Service) AddDisconnectionHandler

func (s *Service) AddDisconnectionHandler(handler func(ctx context.Context, id peer.ID) error)

AddDisconnectionHandler disconnects from peers. It handles updating the peer status. This also calls the handler responsible for maintaining other parts of the sync or p2p system.

func (*Service) AddPingMethod

func (s *Service) AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error)

AddPingMethod adds the metadata ping rpc method to the p2p service, so that it can be used to refresh ENR.

func (*Service) Broadcast

func (s *Service) Broadcast(ctx context.Context, msg proto.Message) error

Broadcast a message to the p2p network.

func (*Service) Connect

func (s *Service) Connect(pi peer.AddrInfo) error

Connect to a specific peer.

func (*Service) Disconnect

func (s *Service) Disconnect(pid peer.ID) error

Disconnect from a peer.

func (*Service) Encoding

func (s *Service) Encoding() encoder.NetworkEncoding

Encoding returns the configured networking encoding.

func (*Service) FindPeersWithSubnet

func (s *Service) FindPeersWithSubnet(index uint64) (bool, error)

FindPeersWithSubnet performs a network search for peers subscribed to a particular subnet. Then we try to connect with those peers.

func (*Service) InfoHandler

func (s *Service) InfoHandler(w http.ResponseWriter, _ *http.Request)

InfoHandler is a handler to serve /p2p page in metrics.

func (*Service) Metadata

func (s *Service) Metadata() *pb.MetaData

Metadata returns a copy of the peer's metadata.

func (*Service) MetadataSeq

func (s *Service) MetadataSeq() uint64

MetadataSeq returns the metadata sequence number.

func (*Service) PeerID

func (s *Service) PeerID() peer.ID

PeerID returns the Peer ID of the local peer.

func (*Service) Peers

func (s *Service) Peers() *peers.Status

Peers returns the peer status interface.

func (*Service) PubSub

func (s *Service) PubSub() *pubsub.PubSub

PubSub returns the p2p pubsub framework.

func (*Service) RefreshENR

func (s *Service) RefreshENR()

RefreshENR uses an epoch to refresh the enr entry for our node with the tracked committee id's for the epoch, allowing our node to be dynamically discoverable by others given our tracked committee id's.

func (*Service) Send

func (s *Service) Send(ctx context.Context, message interface{}, baseTopic string, pid peer.ID) (network.Stream, error)

Send a message to a specific peer. The returned stream may be used for reading, but has been closed for writing.

func (*Service) SetStreamHandler

func (s *Service) SetStreamHandler(topic string, handler network.StreamHandler)

SetStreamHandler sets the protocol handler on the p2p host multiplexer. This method is a pass through to libp2pcore.Host.SetStreamHandler.

func (*Service) Start

func (s *Service) Start()

Start the p2p service.

func (*Service) Started

func (s *Service) Started() bool

Started returns true if the p2p service has successfully started.

func (*Service) Status

func (s *Service) Status() error

Status of the p2p service. Will return an error if the service is considered unhealthy to indicate that this node should not serve traffic until the issue has been resolved.

func (*Service) Stop

func (s *Service) Stop() error

Stop the p2p service and terminate all peer connections.

type SetStreamHandler

type SetStreamHandler interface {
	SetStreamHandler(topic string, handler network.StreamHandler)
}

SetStreamHandler configures p2p to handle streams of a certain topic ID.

Directories

Path Synopsis
Package connmgr is forked from github.com/libp2p/go-libp2p-core/connmgr/connmgr.go
Package connmgr is forked from github.com/libp2p/go-libp2p-core/connmgr/connmgr.go
Package encoder allows for registering custom data encoders for information sent as raw bytes over the wire via p2p to other nodes.
Package encoder allows for registering custom data encoders for information sent as raw bytes over the wire via p2p to other nodes.
Package peers provides information about peers at the eth2 protocol level.
Package peers provides information about peers at the eth2 protocol level.
Package testing includes useful utilities for mocking a beacon node's p2p service for unit tests.
Package testing includes useful utilities for mocking a beacon node's p2p service for unit tests.

Jump to

Keyboard shortcuts

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