/
service.go
169 lines (145 loc) · 4.06 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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.
package p2p
import (
"context"
"reflect"
"github.com/ovcharovvladimir/essentiaHybrid/event"
"github.com/golang/protobuf/proto"
"github.com/sirupsen/logrus"
floodsub "github.com/libp2p/go-floodsub"
libp2p "github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-host"
pb "github.com/ovcharovvladimir/Prysm/proto/sharding/v1"
)
// Sender represents a struct that is able to relay information via shardp2p.
// Server implements this interface.
type Sender interface {
Send(msg interface{}, peer Peer)
}
// Server is a placeholder for a p2p service. To be designed.
type Server struct {
ctx context.Context
cancel context.CancelFunc
feeds map[reflect.Type]*event.Feed
host host.Host
gsub *floodsub.PubSub
}
// NewServer creates a new p2p server instance.
func NewServer() (*Server, error) {
ctx, cancel := context.WithCancel(context.Background())
opts := buildOptions()
host, err := libp2p.New(ctx, opts...)
if err != nil {
cancel()
return nil, err
}
gsub, err := floodsub.NewGossipSub(ctx, host)
if err != nil {
cancel()
return nil, err
}
return &Server{
ctx: ctx,
cancel: cancel,
feeds: make(map[reflect.Type]*event.Feed),
host: host,
gsub: gsub,
}, nil
}
// Start the main routine for an p2p server.
func (s *Server) Start() {
log.Info("Starting shardp2p server")
if err := startDiscovery(s.ctx, s.host, s.gsub); err != nil {
log.Errorf("Could not start p2p discovery! %v", err)
return
}
// Subscribe to all topics.
for topic, msgType := range topicTypeMapping {
log.WithFields(logrus.Fields{
"topic": topic,
}).Debug("Subscribing to topic")
go s.subscribeToTopic(topic, msgType)
}
}
// Stop the main p2p loop.
func (s *Server) Stop() error {
log.Info("Stopping shardp2p server")
s.cancel()
return nil
}
// Send a message to a specific peer.
func (s *Server) Send(msg interface{}, peer Peer) {
// TODO
// https://github.com/ovcharovvladimir/Prysm/issues/175
// TODO: Support passing value and pointer type messages.
// TODO: Remove debug log after send is implemented.
_ = peer
log.Debug("Broadcasting to everyone rather than sending a single peer")
s.Broadcast(msg)
}
// Broadcast a message to the world.
func (s *Server) Broadcast(msg interface{}) {
// TODO https://github.com/ovcharovvladimir/Prysm/issues/176
topic := topic(msg)
log.WithFields(logrus.Fields{
"topic": topic,
}).Debugf("Broadcasting msg %T", msg)
if topic == pb.Topic_UNKNOWN {
log.Warnf("Topic is unknown for message type %T. %v", msg, msg)
}
// TODO: Next assertion may fail if your msg is not a pointer to a msg.
m, ok := msg.(proto.Message)
if !ok {
log.Errorf("Message to broadcast (type: %T) is not a protobuf message: %v", msg, msg)
return
}
b, err := proto.Marshal(m)
if err != nil {
log.Errorf("Failed to marshal data for broadcast: %v", err)
return
}
s.gsub.Publish(topic.String(), b)
}
func (s *Server) subscribeToTopic(topic pb.Topic, msgType reflect.Type) {
sub, err := s.gsub.Subscribe(topic.String())
if err != nil {
log.Errorf("Failed to subscribe to topic: %v", err)
return
}
defer sub.Cancel()
feed := s.Feed(msgType)
for {
msg, err := sub.Next(s.ctx)
if s.ctx.Err() != nil {
return // Context closed or something.
}
if err != nil {
log.Errorf("Failed to get next message: %v", err)
return
}
// TODO: reflect.Value.Interface() can panic so we should capture that
// panic so the server doesn't crash.
d, ok := reflect.New(msgType).Interface().(proto.Message)
if !ok {
log.Error("Received message is not a protobuf message")
continue
}
err = proto.Unmarshal(msg.Data, d)
if err != nil {
log.Errorf("Failed to decode data: %v", err)
continue
}
i := feed.Send(Message{Data: d})
log.WithFields(logrus.Fields{
"numSubs": i,
}).Debug("Send a request to subs")
}
}