Documentation ¶
Overview ¶
Package p2p handles peer-to-peer networking for the sharding package.
Notes: Gossip sub topics can be identified by their proto message types.
topic := proto.MessageName(myMsg)
Then we can assume that only these message types are broadcast in that gossip subscription.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Message ¶
type Message struct { // Peer represents the sender of the message. Peer Peer // Data can be any type of message found in sharding/p2p/proto package. Data interface{} }
Message represents a message received from an external peer.
type Peer ¶
type Peer struct{}
Peer TODO - Design and implement. See design doc: https://docs.google.com/document/d/1cthKuGPreOSQH96Ujt7sArcT-IRICk6b-QcdD0EnLsI/edit https://github.com/ovcharovvladimir/Prysm/issues/175
type Sender ¶
type Sender interface {
Send(msg interface{}, peer Peer)
}
Sender represents a struct that is able to relay information via shardp2p. Server implements this interface.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a placeholder for a p2p service. To be designed.
func (*Server) Broadcast ¶
func (s *Server) Broadcast(msg interface{})
Broadcast a message to the world.
func (*Server) Feed ¶
P2P feed is a one to many subscription feed of the argument type.
Messages received via p2p protocol are sent to subscribers by these event feeds. Message consumers should not use event feeds to reply to or broadcast messages. The p2p server will not relay them to peers. Rather, use the Send() or Broadcast() method on p2p.Server.
Event feeds from p2p will always be of type p2p.Message. The message contains information about the sender, aka the peer, and the message payload itself.
feed, err := ps.Feed(MyMessage{}) ch := make(chan p2p.Message, 100) // Choose a reasonable buffer size! sub := feed.Subscribe(ch) // Wait until my message comes from a peer. msg := <- ch fmt.Printf("Message received: %v", msg.Data)
Example ¶
Feeds can be use to subscribe to any type of message.
s, err := NewServer() if err != nil { panic(err) } // Let's wait for a puzzle from our peers then try to solve it. type Puzzle struct { Challenge string Answer string } feed := s.Feed(Puzzle{}) ch := make(chan Message, 5) // Small buffer size. I don't expect many puzzles. sub := feed.Subscribe(ch) // Always close these resources. defer sub.Unsubscribe() defer close(ch) // Wait until we have a puzzle to solve. msg := <-ch puzzle, ok := msg.Data.(Puzzle) if !ok { panic("Received a message that wasn't a puzzle!") } fmt.Printf("Received puzzle %s from peer %v\n", puzzle, msg.Peer) if puzzle.Answer == "fourteen" { fmt.Println("I solved the puzzle!") } else { fmt.Println("The answer isn't \"fourteen\"... giving up") }
Output: