2019-08-16 17:13:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-19 21:20:56 +00:00
|
|
|
"context"
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-16 17:13:04 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2019-11-30 05:36:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/peers"
|
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
|
|
|
|
EncodingProvider
|
|
|
|
PubSubProvider
|
2019-08-16 20:03:11 +00:00
|
|
|
PeerManager
|
2019-08-19 21:20:56 +00:00
|
|
|
Sender
|
2019-08-29 16:32:52 +00:00
|
|
|
ConnectionHandler
|
2019-11-30 05:36:02 +00:00
|
|
|
PeersProvider
|
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
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:32:52 +00:00
|
|
|
// ConnectionHandler configures p2p to handle connections with a peer.
|
|
|
|
type ConnectionHandler interface {
|
|
|
|
AddConnectionHandler(f 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)
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
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
|
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 {
|
2019-09-16 17:54:46 +00:00
|
|
|
Send(context.Context, interface{}, 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
|
|
|
}
|