github.com/amazechain/amc@v0.1.3/internal/p2p/interfaces.go (about) 1 package p2p 2 3 import ( 4 "context" 5 "github.com/amazechain/amc/api/protocol/sync_pb" 6 "github.com/amazechain/amc/conf" 7 "github.com/amazechain/amc/internal/p2p/encoder" 8 "github.com/amazechain/amc/internal/p2p/enr" 9 "github.com/amazechain/amc/internal/p2p/peers" 10 pubsub "github.com/libp2p/go-libp2p-pubsub" 11 "github.com/libp2p/go-libp2p/core/connmgr" 12 "github.com/libp2p/go-libp2p/core/host" 13 "github.com/libp2p/go-libp2p/core/network" 14 "github.com/libp2p/go-libp2p/core/peer" 15 "github.com/multiformats/go-multiaddr" 16 "google.golang.org/protobuf/proto" 17 ) 18 19 // P2P represents the full p2p interface composed of all of the sub-interfaces. 20 type P2P interface { 21 Broadcaster 22 SetStreamHandler 23 PubSubProvider 24 PubSubTopicUser 25 SenderEncoder 26 PeerManager 27 ConnectionHandler 28 PeersProvider 29 PingProvider 30 31 Start() 32 Stop() error 33 GetConfig() *conf.P2PConfig 34 } 35 36 // Broadcaster broadcasts messages to peers over the p2p pubsub protocol. 37 type Broadcaster interface { 38 Broadcast(context.Context, proto.Message) error 39 } 40 41 // SetStreamHandler configures p2p to handle streams of a certain topic ID. 42 type SetStreamHandler interface { 43 SetStreamHandler(topic string, handler network.StreamHandler) 44 } 45 46 // PubSubTopicUser provides way to join, use and leave PubSub topics. 47 type PubSubTopicUser interface { 48 JoinTopic(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error) 49 LeaveTopic(topic string) error 50 PublishToTopic(ctx context.Context, topic string, data []byte, opts ...pubsub.PubOpt) error 51 SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub.Subscription, error) 52 } 53 54 // ConnectionHandler configures p2p to handle connections with a peer. 55 type ConnectionHandler interface { 56 AddConnectionHandler(f func(ctx context.Context, id peer.ID) error, 57 j func(ctx context.Context, id peer.ID) error) 58 AddDisconnectionHandler(f func(ctx context.Context, id peer.ID) error) 59 connmgr.ConnectionGater 60 } 61 62 // SenderEncoder allows sending functionality from libp2p as well as encoding for requests and responses. 63 type SenderEncoder interface { 64 EncodingProvider 65 Sender 66 } 67 68 // EncodingProvider provides p2p network encoding. 69 type EncodingProvider interface { 70 Encoding() encoder.NetworkEncoding 71 } 72 73 // PubSubProvider provides the p2p pubsub protocol. 74 type PubSubProvider interface { 75 PubSub() *pubsub.PubSub 76 } 77 78 // PeerManager abstracts some peer management methods from libp2p. 79 type PeerManager interface { 80 Disconnect(peer.ID) error 81 PeerID() peer.ID 82 Host() host.Host 83 ENR() *enr.Record 84 DiscoveryAddresses() ([]multiaddr.Multiaddr, error) 85 RefreshENR() 86 AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error) 87 } 88 89 // Sender abstracts the sending functionality from libp2p. 90 type Sender interface { 91 Send(context.Context, interface{}, string, peer.ID) (network.Stream, error) 92 } 93 94 // PeersProvider abstracts obtaining our current list of known peers status. 95 type PeersProvider interface { 96 Peers() *peers.Status 97 } 98 99 // PingProvider returns the metadata related information for the local peer. 100 type PingProvider interface { 101 GetPing() *sync_pb.Ping 102 IncSeqNumber() 103 }