2019-08-16 17:13:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-19 21:20:56 +00:00
|
|
|
"context"
|
|
|
|
|
2020-06-18 03:53:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2019-08-16 17:13:04 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2020-12-17 18:03:18 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/encoder"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/peers"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1/metadata"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// P2P represents the full p2p interface composed of all of the sub-interfaces.
|
|
|
|
type P2P interface {
|
|
|
|
Broadcaster
|
|
|
|
SetStreamHandler
|
|
|
|
PubSubProvider
|
2020-07-13 02:28:40 +00:00
|
|
|
PubSubTopicUser
|
2022-08-17 06:38:57 +00:00
|
|
|
SenderEncoder
|
2019-08-16 20:03:11 +00:00
|
|
|
PeerManager
|
2019-08-29 16:32:52 +00:00
|
|
|
ConnectionHandler
|
2019-11-30 05:36:02 +00:00
|
|
|
PeersProvider
|
2020-04-14 20:27:03 +00:00
|
|
|
MetadataProvider
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcaster broadcasts messages to peers over the p2p pubsub protocol.
|
|
|
|
type Broadcaster interface {
|
2019-08-19 21:20:56 +00:00
|
|
|
Broadcast(context.Context, proto.Message) error
|
2020-06-09 22:40:48 +00:00
|
|
|
BroadcastAttestation(ctx context.Context, subnet uint64, att *ethpb.Attestation) error
|
2021-08-27 01:34:20 +00:00
|
|
|
BroadcastSyncCommitteeMessage(ctx context.Context, subnet uint64, sMsg *ethpb.SyncCommitteeMessage) error
|
2019-08-16 17:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetStreamHandler configures p2p to handle streams of a certain topic ID.
|
|
|
|
type SetStreamHandler interface {
|
|
|
|
SetStreamHandler(topic string, handler network.StreamHandler)
|
|
|
|
}
|
|
|
|
|
2020-07-13 02:28:40 +00:00
|
|
|
// PubSubTopicUser provides way to join, use and leave PubSub topics.
|
|
|
|
type PubSubTopicUser interface {
|
|
|
|
JoinTopic(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error)
|
|
|
|
LeaveTopic(topic string) error
|
|
|
|
PublishToTopic(ctx context.Context, topic string, data []byte, opts ...pubsub.PubOpt) error
|
|
|
|
SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error)
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:32:52 +00:00
|
|
|
// ConnectionHandler configures p2p to handle connections with a peer.
|
|
|
|
type ConnectionHandler interface {
|
2020-11-13 12:58:13 +00:00
|
|
|
AddConnectionHandler(f func(ctx context.Context, id peer.ID) error,
|
|
|
|
j func(ctx context.Context, id peer.ID) error)
|
2019-09-18 20:02:34 +00:00
|
|
|
AddDisconnectionHandler(f func(ctx context.Context, id peer.ID) error)
|
2020-06-14 07:35:05 +00:00
|
|
|
connmgr.ConnectionGater
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 06:38:57 +00:00
|
|
|
// SenderEncoder allows sending functionality from libp2p as well as encoding for requests and responses.
|
|
|
|
type SenderEncoder interface {
|
|
|
|
EncodingProvider
|
|
|
|
Sender
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
// EncodingProvider provides p2p network encoding.
|
|
|
|
type EncodingProvider interface {
|
|
|
|
Encoding() encoder.NetworkEncoding
|
|
|
|
}
|
|
|
|
|
|
|
|
// PubSubProvider provides the p2p pubsub protocol.
|
|
|
|
type PubSubProvider interface {
|
|
|
|
PubSub() *pubsub.PubSub
|
|
|
|
}
|
2019-08-16 20:03:11 +00:00
|
|
|
|
|
|
|
// PeerManager abstracts some peer management methods from libp2p.
|
|
|
|
type PeerManager interface {
|
|
|
|
Disconnect(peer.ID) error
|
2019-08-24 18:41:24 +00:00
|
|
|
PeerID() peer.ID
|
2020-06-18 03:53:46 +00:00
|
|
|
Host() host.Host
|
|
|
|
ENR() *enr.Record
|
2021-01-07 13:35:42 +00:00
|
|
|
DiscoveryAddresses() ([]multiaddr.Multiaddr, error)
|
2020-05-05 19:26:20 +00:00
|
|
|
RefreshENR()
|
2022-02-01 08:51:17 +00:00
|
|
|
FindPeersWithSubnet(ctx context.Context, topic string, subIndex uint64, threshold int) (bool, error)
|
2020-04-14 20:27:03 +00:00
|
|
|
AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error)
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 21:20:56 +00:00
|
|
|
// Sender abstracts the sending functionality from libp2p.
|
|
|
|
type Sender interface {
|
2020-04-14 20:27:03 +00:00
|
|
|
Send(context.Context, interface{}, string, peer.ID) (network.Stream, error)
|
2019-08-19 21:20:56 +00:00
|
|
|
}
|
2019-11-30 05:36:02 +00:00
|
|
|
|
2019-12-11 10:31:36 +00:00
|
|
|
// PeersProvider abstracts obtaining our current list of known peers status.
|
2019-11-30 05:36:02 +00:00
|
|
|
type PeersProvider interface {
|
2019-12-11 10:31:36 +00:00
|
|
|
Peers() *peers.Status
|
2019-11-30 05:36:02 +00:00
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
|
|
|
|
// MetadataProvider returns the metadata related information for the local peer.
|
|
|
|
type MetadataProvider interface {
|
2021-07-23 20:10:15 +00:00
|
|
|
Metadata() metadata.Metadata
|
2020-04-14 20:27:03 +00:00
|
|
|
MetadataSeq() uint64
|
|
|
|
}
|