Better documentation, add p2p message container type

Former-commit-id: 2790cf05b6dbae32cc8ced818e0af70bb20f8582 [formerly 782d2cbb05d9fe391f867d33c9ae0820b3fda203]
Former-commit-id: 78a21d732a68267fae930858f9e09b26ea9419b8
This commit is contained in:
Preston Van Loon
2018-06-12 22:06:59 -04:00
parent 0b50d2778e
commit c17db32069
6 changed files with 81 additions and 45 deletions

33
sharding/p2p/feed.go Normal file
View File

@@ -0,0 +1,33 @@
package p2p
import (
"reflect"
"github.com/ethereum/go-ethereum/event"
)
// 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 := ps.Feed(MyMessage{})
// ch := make(chan p2p.Message, 100) // Choose a reasonable buffer size!
// feed.Subscribe(ch)
//
// // Wait until my message comes from a peer.
// msg := <- ch
// fmt.Printf("Message received: %v", msg.Data)
func (s *Server) Feed(msg interface{}) (*event.Feed, error) {
t := reflect.TypeOf(msg)
if s.feeds[t] == nil {
s.feeds[t] = new(event.Feed)
}
return s.feeds[t], nil
}

33
sharding/p2p/feed_test.go Normal file
View File

@@ -0,0 +1,33 @@
package p2p
import "testing"
func TestFeed_ReturnsSameFeed(t *testing.T) {
tests := []struct {
a interface{}
b interface{}
want bool
}{
// Equalality tests
{a: 1, b: 2, want: true},
{a: 'a', b: 'b', want: true},
{a: struct{ c int }{c: 1}, b: struct{ c int }{c: 2}, want: true},
{a: struct{ c string }{c: "a"}, b: struct{ c string }{c: "b"}, want: true},
// Inequality tests
{a: 1, b: '2', want: false},
{a: 'a', b: 1, want: false},
{a: struct{ c int }{c: 1}, b: struct{ c int64 }{c: 2}, want: false},
{a: struct{ c string }{c: "a"}, b: struct{ c float64 }{c: 3.4}, want: false},
}
s, _ := NewServer()
for _, tt := range tests {
feed1, _ := s.Feed(tt.a)
feed2, _ := s.Feed(tt.b)
if (feed1 == feed2) != tt.want {
t.Errorf("Expected %v == %v to be %t", feed1, feed2, tt.want)
}
}
}

9
sharding/p2p/message.go Normal file
View File

@@ -0,0 +1,9 @@
package p2p
// Message represents a message received from an external peer.
type Message struct {
// Peer represents the sender of the message.
Peer Peer
// Data can be any type of message found in sharding/p2p/messages package.
Data interface{}
}

3
sharding/p2p/peer.go Normal file
View File

@@ -0,0 +1,3 @@
package p2p
type Peer struct{}

View File

@@ -13,31 +13,21 @@ type Server struct {
feeds map[reflect.Type]*event.Feed
}
// NewServer creates a new shardp2p service instance.
// NewServer creates a new p2p server instance.
func NewServer() (*Server, error) {
return &Server{
feeds: make(map[reflect.Type]*event.Feed),
}, nil
}
// Start the main routine for an shardp2p server.
// Start the main routine for an p2p server.
func (s *Server) Start() error {
log.Info("Starting shardp2p server")
return nil
}
// Stop the main shardp2p loop..
// Stop the main p2p loop.
func (s *Server) Stop() error {
log.Info("Stopping shardp2p server")
return nil
}
// Feed returns a event feed for the given message type.
// TODO(prestonvanloon): Add more to this GoDoc before merging.
func (s *Server) Feed(msg interface{}) (*event.Feed, error) {
t := reflect.TypeOf(msg)
if s.feeds[t] == nil {
s.feeds[t] = new(event.Feed)
}
return s.feeds[t], nil
}

View File

@@ -1,40 +1,8 @@
package p2p
import (
"testing"
"github.com/ethereum/go-ethereum/sharding"
)
// Verifies that Server implements the ShardP2P interface.
var _ = sharding.ShardP2P(&Server{})
func TestFeed_ReturnsSameFeed(t *testing.T) {
tests := []struct {
a interface{}
b interface{}
want bool
}{
// Equalality tests
{a: 1, b: 2, want: true},
{a: 'a', b: 'b', want: true},
{a: struct{ c int }{c: 1}, b: struct{ c int }{c: 2}, want: true},
{a: struct{ c string }{c: "a"}, b: struct{ c string }{c: "b"}, want: true},
// Inequality tests
{a: 1, b: '2', want: false},
{a: 'a', b: 1, want: false},
{a: struct{ c int }{c: 1}, b: struct{ c int64 }{c: 2}, want: false},
{a: struct{ c string }{c: "a"}, b: struct{ c float64 }{c: 3.4}, want: false},
}
s, _ := NewServer()
for _, tt := range tests {
feed1, _ := s.Feed(tt.a)
feed2, _ := s.Feed(tt.b)
if (feed1 == feed2) != tt.want {
t.Errorf("Expected %v == %v to be %t", feed1, feed2, tt.want)
}
}
}