p2p

package
v0.10.14 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: MIT Imports: 57 Imported by: 17

Documentation

Index

Constants

View Source
const (
	DefaultMeshD     = 8  // topic stable mesh target count
	DefaultMeshDlo   = 6  // topic stable mesh low watermark
	DefaultMeshDhi   = 12 // topic stable mesh high watermark
	DefaultMeshDlazy = 6  // gossip target
)

Variables

View Source
var (
	DisabledDiscovery   = errors.New("discovery disabled")
	NoConnectionManager = errors.New("no connection manager")
	NoConnectionGater   = errors.New("no connection gater")
)
View Source
var DefaultBootnodes = []*enode.Node{
	enode.MustParse("enode://869d07b5932f17e8490990f75a3f94195e9504ddb6b85f7189e5a9c0a8fff8b00aecf6f3ac450ecba6cdabdb5858788a94bde2b613e0f2d82e9b395355f76d1a@34.65.67.101:0?discport=30305"),
	enode.MustParse("enode://2d4e7e9d48f4dd4efe9342706dd1b0024681bd4c3300d021f86fc75eab7865d4e0cbec6fbc883f011cfd6a57423e7e2f6e104baad2b744c3cafaec6bc7dc92c1@34.65.43.171:0?discport=30305"),
	enode.MustParse("enode://9d7a3efefe442351217e73b3a593bcb8efffb55b4807699972145324eab5e6b382152f8d24f6301baebbfb5ecd4127bd3faab2842c04cd432bdf50ba092f6645@34.65.109.126:0?discport=30305"),
}
View Source
var MessageDomainInvalidSnappy = [4]byte{0, 0, 0, 0}
View Source
var MessageDomainValidSnappy = [4]byte{1, 0, 0, 0}
View Source
var NamespaceRPC = "opp2p"
View Source
var SigningDomainBlocksV1 = [32]byte{}

Functions

func BlockSigningHash

func BlockSigningHash(cfg *rollup.Config, payloadBytes []byte) (common.Hash, error)

func BuildBlocksValidator

func BuildBlocksValidator(log log.Logger, cfg *rollup.Config, runCfg GossipRuntimeConfig) pubsub.ValidatorEx

func BuildGlobalGossipParams

func BuildGlobalGossipParams(cfg *rollup.Config) pubsub.GossipSubParams

func BuildMsgIdFn

func BuildMsgIdFn(cfg *rollup.Config) pubsub.MsgIdFunction

BuildMsgIdFn builds a generic message ID function for gossipsub that can handle compressed payloads, mirroring the eth2 p2p gossip spec.

func BuildSubscriptionFilter

func BuildSubscriptionFilter(cfg *rollup.Config) pubsub.SubscriptionFilter

BuildSubscriptionFilter builds a simple subscription filter, to help protect against peers spamming useless subscriptions.

func DefaultConnGater

func DefaultConnGater(conf *Config) (connmgr.ConnectionGater, error)

func DefaultConnManager

func DefaultConnManager(conf *Config) (connmgr.ConnManager, error)

func FilterEnodes

func FilterEnodes(log log.Logger, cfg *rollup.Config) func(node *enode.Node) bool

func FindActiveTCPPort

func FindActiveTCPPort(h host.Host) (uint16, error)

func LogTopicEvents

func LogTopicEvents(ctx context.Context, log log.Logger, evHandler *pubsub.TopicEventHandler)

func MplexC added in v0.10.10

func MplexC() (lconf.MsMuxC, error)

func NewGossipSub

func NewGossipSub(p2pCtx context.Context, h host.Host, cfg *rollup.Config, gossipConf GossipSetupConfigurables, m GossipMetricer) (*pubsub.PubSub, error)

NewGossipSub configures a new pubsub instance with the specified parameters. PubSub uses a GossipSubRouter as it's router under the hood.

func NewNetworkNotifier

func NewNetworkNotifier(log log.Logger, m metrics.Metricer) network.Notifiee

func NoiseC added in v0.10.10

func NoiseC() (lconf.MsSecC, error)

func SigningHash

func SigningHash(domain [32]byte, chainID *big.Int, payloadBytes []byte) (common.Hash, error)

func TlsC added in v0.10.10

func TlsC() (lconf.MsSecC, error)

func YamuxC added in v0.10.10

func YamuxC() (lconf.MsMuxC, error)

Types

type API

type API interface {
	Self(ctx context.Context) (*PeerInfo, error)
	Peers(ctx context.Context, connected bool) (*PeerDump, error)
	PeerStats(ctx context.Context) (*PeerStats, error)
	DiscoveryTable(ctx context.Context) ([]*enode.Node, error)
	BlockPeer(ctx context.Context, p peer.ID) error
	UnblockPeer(ctx context.Context, p peer.ID) error
	ListBlockedPeers(ctx context.Context) ([]peer.ID, error)
	BlockAddr(ctx context.Context, ip net.IP) error
	UnblockAddr(ctx context.Context, ip net.IP) error
	ListBlockedAddrs(ctx context.Context) ([]net.IP, error)
	BlockSubnet(ctx context.Context, ipnet *net.IPNet) error
	UnblockSubnet(ctx context.Context, ipnet *net.IPNet) error
	ListBlockedSubnets(ctx context.Context) ([]*net.IPNet, error)
	ProtectPeer(ctx context.Context, p peer.ID) error
	UnprotectPeer(ctx context.Context, p peer.ID) error
	ConnectPeer(ctx context.Context, addr string) error
	DisconnectPeer(ctx context.Context, id peer.ID) error
}

type APIBackend

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

func NewP2PAPIBackend

func NewP2PAPIBackend(node Node, log log.Logger, m metrics.Metricer) *APIBackend

func (*APIBackend) BlockAddr

func (s *APIBackend) BlockAddr(_ context.Context, ip net.IP) error

BlockAddr adds an IP address to the set of blocked addresses. Note: active connections to the IP address are not automatically closed.

func (*APIBackend) BlockPeer

func (s *APIBackend) BlockPeer(_ context.Context, p peer.ID) error

func (*APIBackend) BlockSubnet

func (s *APIBackend) BlockSubnet(_ context.Context, ipnet *net.IPNet) error

BlockSubnet adds an IP subnet to the set of blocked addresses. Note: active connections to the IP subnet are not automatically closed.

func (*APIBackend) ConnectPeer

func (s *APIBackend) ConnectPeer(ctx context.Context, addr string) error

ConnectPeer connects to a given peer address, and wait for protocol negotiation & identification of the peer

func (*APIBackend) DisconnectPeer

func (s *APIBackend) DisconnectPeer(_ context.Context, id peer.ID) error

func (*APIBackend) DiscoveryTable

func (s *APIBackend) DiscoveryTable(_ context.Context) ([]*enode.Node, error)

func (*APIBackend) ListBlockedAddrs

func (s *APIBackend) ListBlockedAddrs(_ context.Context) ([]net.IP, error)

func (*APIBackend) ListBlockedPeers

func (s *APIBackend) ListBlockedPeers(_ context.Context) ([]peer.ID, error)

func (*APIBackend) ListBlockedSubnets

func (s *APIBackend) ListBlockedSubnets(_ context.Context) ([]*net.IPNet, error)

func (*APIBackend) PeerStats

func (s *APIBackend) PeerStats(_ context.Context) (*PeerStats, error)

func (*APIBackend) Peers

func (s *APIBackend) Peers(ctx context.Context, connected bool) (*PeerDump, error)

Peers lists information of peers. Optionally filter to only retrieve connected peers.

func (*APIBackend) ProtectPeer

func (s *APIBackend) ProtectPeer(_ context.Context, p peer.ID) error

func (*APIBackend) Self

func (s *APIBackend) Self(ctx context.Context) (*PeerInfo, error)

func (*APIBackend) UnblockAddr

func (s *APIBackend) UnblockAddr(_ context.Context, ip net.IP) error

func (*APIBackend) UnblockPeer

func (s *APIBackend) UnblockPeer(_ context.Context, p peer.ID) error

func (*APIBackend) UnblockSubnet

func (s *APIBackend) UnblockSubnet(_ context.Context, ipnet *net.IPNet) error

func (*APIBackend) UnprotectPeer

func (s *APIBackend) UnprotectPeer(_ context.Context, p peer.ID) error

type Client

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

func NewClient

func NewClient(c *rpc.Client) *Client

func (*Client) BlockAddr

func (c *Client) BlockAddr(ctx context.Context, ip net.IP) error

func (*Client) BlockPeer

func (c *Client) BlockPeer(ctx context.Context, p peer.ID) error

func (*Client) BlockSubnet

func (c *Client) BlockSubnet(ctx context.Context, ipnet *net.IPNet) error

func (*Client) ConnectPeer

func (c *Client) ConnectPeer(ctx context.Context, addr string) error

func (*Client) DisconnectPeer

func (c *Client) DisconnectPeer(ctx context.Context, id peer.ID) error

func (*Client) DiscoveryTable

func (c *Client) DiscoveryTable(ctx context.Context) ([]*enode.Node, error)

func (*Client) ListBlockedAddrs

func (c *Client) ListBlockedAddrs(ctx context.Context) ([]net.IP, error)

func (*Client) ListBlockedPeers

func (c *Client) ListBlockedPeers(ctx context.Context) ([]peer.ID, error)

func (*Client) ListBlockedSubnets

func (c *Client) ListBlockedSubnets(ctx context.Context) ([]*net.IPNet, error)

func (*Client) PeerStats

func (c *Client) PeerStats(ctx context.Context) (*PeerStats, error)

func (*Client) Peers

func (c *Client) Peers(ctx context.Context, connected bool) (*PeerDump, error)

func (*Client) ProtectPeer

func (c *Client) ProtectPeer(ctx context.Context, p peer.ID) error

func (*Client) Self

func (c *Client) Self(ctx context.Context) (*PeerInfo, error)

func (*Client) UnblockAddr

func (c *Client) UnblockAddr(ctx context.Context, ip net.IP) error

func (*Client) UnblockPeer

func (c *Client) UnblockPeer(ctx context.Context, p peer.ID) error

func (*Client) UnblockSubnet

func (c *Client) UnblockSubnet(ctx context.Context, ipnet *net.IPNet) error

func (*Client) UnprotectPeer

func (c *Client) UnprotectPeer(ctx context.Context, p peer.ID) error

type Config

type Config struct {
	Priv *crypto.Secp256k1PrivateKey

	DisableP2P  bool
	NoDiscovery bool

	ListenIP      net.IP
	ListenTCPPort uint16

	// Port to bind discv5 to
	ListenUDPPort uint16

	AdvertiseIP      net.IP
	AdvertiseTCPPort uint16
	AdvertiseUDPPort uint16
	Bootnodes        []*enode.Node
	DiscoveryDB      *enode.DB

	StaticPeers []core.Multiaddr

	HostMux             []lconf.MsMuxC
	HostSecurity        []lconf.MsSecC
	NoTransportSecurity bool

	PeersLo    uint
	PeersHi    uint
	PeersGrace time.Duration

	MeshD     int // topic stable mesh target count
	MeshDLo   int // topic stable mesh low watermark
	MeshDHi   int // topic stable mesh high watermark
	MeshDLazy int // gossip target

	// FloodPublish publishes messages from ourselves to peers outside of the gossip topic mesh but supporting the same topic.
	FloodPublish bool

	// If true a NAT manager will host a NAT port mapping that is updated with PMP and UPNP by libp2p/go-nat
	NAT bool

	UserAgent string

	TimeoutNegotiation time.Duration
	TimeoutAccept      time.Duration
	TimeoutDial        time.Duration

	// Underlying store that hosts connection-gater and peerstore data.
	Store ds.Batching

	ConnGater func(conf *Config) (connmgr.ConnectionGater, error)
	ConnMngr  func(conf *Config) (connmgr.ConnManager, error)
}

Config sets up a p2p host and discv5 service from configuration. This implements SetupP2P.

func (*Config) Check

func (conf *Config) Check() error

func (*Config) ConfigureGossip added in v0.10.10

func (p *Config) ConfigureGossip(params *pubsub.GossipSubParams) []pubsub.Option

func (*Config) Disabled added in v0.10.12

func (conf *Config) Disabled() bool

func (*Config) Discovery

func (conf *Config) Discovery(log log.Logger, rollupCfg *rollup.Config, tcpPort uint16) (*enode.LocalNode, *discover.UDPv5, error)

func (*Config) Host

func (conf *Config) Host(log log.Logger, reporter metrics.Reporter) (host.Host, error)

func (*Config) TargetPeers

func (conf *Config) TargetPeers() uint

type ConnectionGater

type ConnectionGater interface {
	connmgr.ConnectionGater

	// BlockPeer adds a peer to the set of blocked peers.
	// Note: active connections to the peer are not automatically closed.
	BlockPeer(p peer.ID) error
	UnblockPeer(p peer.ID) error
	ListBlockedPeers() []peer.ID

	// BlockAddr adds an IP address to the set of blocked addresses.
	// Note: active connections to the IP address are not automatically closed.
	BlockAddr(ip net.IP) error
	UnblockAddr(ip net.IP) error
	ListBlockedAddrs() []net.IP

	// BlockSubnet adds an IP subnet to the set of blocked addresses.
	// Note: active connections to the IP subnet are not automatically closed.
	BlockSubnet(ipnet *net.IPNet) error
	UnblockSubnet(ipnet *net.IPNet) error
	ListBlockedSubnets() []*net.IPNet
}

type ExtraHostFeatures

type ExtraHostFeatures interface {
	host.Host
	ConnectionGater() ConnectionGater
	ConnectionManager() connmgr.ConnManager
}

type GossipIn

type GossipIn interface {
	OnUnsafeL2Payload(ctx context.Context, from peer.ID, msg *eth.ExecutionPayload) error
}

type GossipMetricer

type GossipMetricer interface {
	RecordGossipEvent(evType int32)
}

type GossipOut

type GossipOut interface {
	GossipTopicInfo
	PublishL2Payload(ctx context.Context, msg *eth.ExecutionPayload, signer Signer) error
	Close() error
}

func JoinGossip

func JoinGossip(p2pCtx context.Context, self peer.ID, ps *pubsub.PubSub, log log.Logger, cfg *rollup.Config, runCfg GossipRuntimeConfig, gossipIn GossipIn) (GossipOut, error)

type GossipRuntimeConfig added in v0.10.5

type GossipRuntimeConfig interface {
	P2PSequencerAddress() common.Address
}

type GossipSetupConfigurables added in v0.10.10

type GossipSetupConfigurables interface {
	ConfigureGossip(params *pubsub.GossipSubParams) []pubsub.Option
}

type GossipTopicInfo

type GossipTopicInfo interface {
	BlocksTopicPeers() []peer.ID
}

type LocalSigner

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

LocalSigner is suitable for testing

func NewLocalSigner

func NewLocalSigner(priv *ecdsa.PrivateKey) *LocalSigner

func (*LocalSigner) Close

func (s *LocalSigner) Close() error

func (*LocalSigner) Sign

func (s *LocalSigner) Sign(ctx context.Context, domain [32]byte, chainID *big.Int, encodedMsg []byte) (sig *[65]byte, err error)

type MessageHandler

type MessageHandler func(ctx context.Context, from peer.ID, msg any) error

func BlocksHandler

func BlocksHandler(onBlock func(ctx context.Context, from peer.ID, msg *eth.ExecutionPayload) error) MessageHandler

type Node

type Node interface {
	// Host returns the libp2p host
	Host() host.Host
	// Dv5Local returns the control over the Discv5 data of the local node, nil if disabled
	Dv5Local() *enode.LocalNode
	// Dv5Udp returns the control over the Discv5 network, nil if disabled
	Dv5Udp() *discover.UDPv5
	// GossipSub returns the gossip router
	GossipSub() *pubsub.PubSub
	// GossipOut returns the gossip output/info control
	GossipOut() GossipOut
	// ConnectionGater returns the connection gater, to ban/unban peers with, may be nil
	ConnectionGater() ConnectionGater
	// ConnectionManager returns the connection manager, to protect peers with, may be nil
	ConnectionManager() connmgr.ConnManager
}

type NodeP2P

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

NodeP2P is a p2p node, which can be used to gossip messages.

func NewNodeP2P

func NewNodeP2P(resourcesCtx context.Context, rollupCfg *rollup.Config, log log.Logger, setup SetupP2P, gossipIn GossipIn, runCfg GossipRuntimeConfig, metrics metrics.Metricer) (*NodeP2P, error)

NewNodeP2P creates a new p2p node, and returns a reference to it. If the p2p is disabled, it returns nil. If metrics are configured, a bandwidth monitor will be spawned in a goroutine.

func (*NodeP2P) Close

func (n *NodeP2P) Close() error

func (*NodeP2P) ConnectionGater

func (n *NodeP2P) ConnectionGater() ConnectionGater

func (*NodeP2P) ConnectionManager

func (n *NodeP2P) ConnectionManager() connmgr.ConnManager

func (*NodeP2P) DiscoveryProcess

func (n *NodeP2P) DiscoveryProcess(ctx context.Context, log log.Logger, cfg *rollup.Config, connectGoal uint)

DiscoveryProcess runs a discovery process that randomly walks the DHT to fill the peerstore, and connects to nodes in the peerstore that we are not already connected to. Nodes from the peerstore will be shuffled, unsuccessful connection attempts will cause peers to be avoided, and only nodes with addresses (under TTL) will be connected to.

func (*NodeP2P) Dv5Local

func (n *NodeP2P) Dv5Local() *enode.LocalNode

func (*NodeP2P) Dv5Udp

func (n *NodeP2P) Dv5Udp() *discover.UDPv5

func (*NodeP2P) GossipOut

func (n *NodeP2P) GossipOut() GossipOut

func (*NodeP2P) GossipSub

func (n *NodeP2P) GossipSub() *pubsub.PubSub

func (*NodeP2P) Host

func (n *NodeP2P) Host() host.Host

type NotificationsMetricer

type NotificationsMetricer interface {
	IncPeerCount()
	DecPeerCount()
	IncStreamCount()
	DecStreamCount()
}

type OpStackENRData added in v0.10.4

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

The discovery ENRs are just key-value lists, and we filter them by records tagged with the "opstack" key, and then check the chain ID and version.

func (*OpStackENRData) DecodeRLP added in v0.10.4

func (o *OpStackENRData) DecodeRLP(s *rlp.Stream) error

func (*OpStackENRData) ENRKey added in v0.10.4

func (o *OpStackENRData) ENRKey() string

func (*OpStackENRData) EncodeRLP added in v0.10.4

func (o *OpStackENRData) EncodeRLP(w io.Writer) error

type PeerDump

type PeerDump struct {
	TotalConnected uint                 `json:"totalConnected"`
	Peers          map[string]*PeerInfo `json:"peers"`
	BannedPeers    []peer.ID            `json:"bannedPeers"`
	BannedIPS      []net.IP             `json:"bannedIPS"`
	BannedSubnets  []*net.IPNet         `json:"bannedSubnets"`
}

type PeerInfo

type PeerInfo struct {
	PeerID          peer.ID  `json:"peerID"`
	NodeID          enode.ID `json:"nodeID"`
	UserAgent       string   `json:"userAgent"`
	ProtocolVersion string   `json:"protocolVersion"`
	ENR             string   `json:"ENR"`       // might not always be known, e.g. if the peer connected us instead of us discovering them
	Addresses       []string `json:"addresses"` // multi-addresses. may be mix of LAN / docker / external IPs. All of them are communicated.
	Protocols       []string `json:"protocols"` // negotiated protocols list
	//GossipScore float64
	//PeerScore float64
	Connectedness network.Connectedness `json:"connectedness"` // "NotConnected", "Connected", "CanConnect" (gracefully disconnected), or "CannotConnect" (tried but failed)
	Direction     network.Direction     `json:"direction"`     // "Unknown", "Inbound" (if the peer contacted us), "Outbound" (if we connected to them)
	Protected     bool                  `json:"protected"`     // Protected peers do not get
	ChainID       uint64                `json:"chainID"`       // some peers might try to connect, but we figure out they are on a different chain later. This may be 0 if the peer is not an optimism node at all.
	Latency       time.Duration         `json:"latency"`

	GossipBlocks bool `json:"gossipBlocks"` // if the peer is in our gossip topic
}

type PeerStats

type PeerStats struct {
	Connected   uint `json:"connected"`
	Table       uint `json:"table"`
	BlocksTopic uint `json:"blocksTopic"`
	Banned      uint `json:"banned"`
	Known       uint `json:"known"`
}

type Prepared

type Prepared struct {
	HostP2P   host.Host
	LocalNode *enode.LocalNode
	UDPv5     *discover.UDPv5
}

Prepared provides a p2p host and discv5 service that is already set up. This implements SetupP2P.

func (*Prepared) Check

func (p *Prepared) Check() error

func (*Prepared) ConfigureGossip added in v0.10.10

func (p *Prepared) ConfigureGossip(params *pubsub.GossipSubParams) []pubsub.Option

func (*Prepared) Disabled added in v0.10.12

func (p *Prepared) Disabled() bool

func (*Prepared) Discovery

func (p *Prepared) Discovery(log log.Logger, rollupCfg *rollup.Config, tcpPort uint16) (*enode.LocalNode, *discover.UDPv5, error)

Discovery creates a disc-v5 service. Returns nil, nil, nil if discovery is disabled.

func (*Prepared) Host

func (p *Prepared) Host(log log.Logger, reporter metrics.Reporter) (host.Host, error)

Host creates a libp2p host service. Returns nil, nil if p2p is disabled.

func (*Prepared) TargetPeers

func (p *Prepared) TargetPeers() uint

type PreparedSigner

type PreparedSigner struct {
	Signer
}

func (*PreparedSigner) SetupSigner

func (p *PreparedSigner) SetupSigner(ctx context.Context) (Signer, error)

type Secp256k1

type Secp256k1 crypto.Secp256k1PublicKey

Secp256k1 is like the geth Secp256k1 enr entry type, but using the libp2p pubkey representation instead

func (*Secp256k1) DecodeRLP

func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error

DecodeRLP implements rlp.Decoder.

func (Secp256k1) ENRKey

func (v Secp256k1) ENRKey() string

func (Secp256k1) EncodeRLP

func (v Secp256k1) EncodeRLP(w io.Writer) error

EncodeRLP implements rlp.Encoder.

type SetupP2P

type SetupP2P interface {
	Check() error
	Disabled() bool
	// Host creates a libp2p host service. Returns nil, nil if p2p is disabled.
	Host(log log.Logger, reporter metrics.Reporter) (host.Host, error)
	// Discovery creates a disc-v5 service. Returns nil, nil, nil if discovery is disabled.
	Discovery(log log.Logger, rollupCfg *rollup.Config, tcpPort uint16) (*enode.LocalNode, *discover.UDPv5, error)
	TargetPeers() uint
	GossipSetupConfigurables
}

SetupP2P provides a host and discovery service for usage in the rollup node.

type Signer

type Signer interface {
	Sign(ctx context.Context, domain [32]byte, chainID *big.Int, encodedMsg []byte) (sig *[65]byte, err error)
	io.Closer
}

type SignerSetup

type SignerSetup interface {
	SetupSigner(ctx context.Context) (Signer, error)
}

type TopicSubscriber

type TopicSubscriber func(ctx context.Context, sub *pubsub.Subscription)

func MakeSubscriber

func MakeSubscriber(log log.Logger, msgHandler MessageHandler) TopicSubscriber

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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