2020-11-18 19:53:52 +00:00
|
|
|
package pubsub
|
2020-10-20 18:18:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/consensus/types"
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
host "github.com/libp2p/go-libp2p-core/host"
|
2020-10-20 18:18:36 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2020-10-21 20:36:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-20 18:18:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PubSubRouter struct {
|
2020-11-18 19:53:52 +00:00
|
|
|
node host.Host
|
|
|
|
Pubsub *pubsub.PubSub
|
|
|
|
context context.Context
|
|
|
|
contextCancel context.CancelFunc
|
|
|
|
serviceSubscription *pubsub.Subscription
|
|
|
|
handlers map[types.MessageType][]Handler
|
|
|
|
oracleTopic string
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
func NewPubSubRouter(h host.Host, oracleTopic string) *PubSubRouter {
|
2020-10-20 18:18:36 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
psr := &PubSubRouter{
|
2020-10-21 19:54:40 +00:00
|
|
|
node: h,
|
2020-10-20 18:18:36 +00:00
|
|
|
context: ctx,
|
|
|
|
contextCancel: ctxCancel,
|
2020-11-18 19:53:52 +00:00
|
|
|
handlers: make(map[types.MessageType][]Handler),
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 10:11:07 +00:00
|
|
|
pb, err := pubsub.NewFloodSub(
|
2020-10-21 20:36:05 +00:00
|
|
|
context.TODO(),
|
2020-10-22 14:36:15 +00:00
|
|
|
psr.node, //pubsub.WithMessageSigning(true),
|
2020-10-21 20:36:05 +00:00
|
|
|
//pubsub.WithStrictSignatureVerification(true),
|
2020-10-20 18:18:36 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Fatal("Error occurred when create PubSub", err)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
psr.oracleTopic = oracleTopic
|
2020-11-18 19:53:52 +00:00
|
|
|
topic, err := pb.Join(oracleTopic)
|
2020-10-20 18:18:36 +00:00
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Fatal("Error occurred when subscribing to service topic", err)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
2020-11-18 19:53:52 +00:00
|
|
|
|
|
|
|
subscription, err := topic.Subscribe()
|
|
|
|
psr.serviceSubscription = subscription
|
2020-10-28 17:35:56 +00:00
|
|
|
psr.Pubsub = pb
|
2020-10-20 18:18:36 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-psr.context.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
msg, err := subscription.Next(psr.context)
|
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Failed to receive pubsub message: ", err.Error())
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
psr.handleMessage(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return psr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (psr *PubSubRouter) handleMessage(p *pubsub.Message) {
|
|
|
|
senderPeerID, err := peer.IDFromBytes(p.From)
|
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Unable to decode sender peer ID! " + err.Error())
|
2020-10-20 18:18:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// We can receive our own messages when sending to the topic. So we should drop them.
|
2020-10-21 19:54:40 +00:00
|
|
|
if senderPeerID == psr.node.ID() {
|
2020-10-20 18:18:36 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-18 19:53:52 +00:00
|
|
|
var message types.Message
|
|
|
|
err = cbor.Unmarshal(p.Data, &message)
|
2020-10-20 18:18:36 +00:00
|
|
|
if err != nil {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.Warn("Unable to decode message data! " + err.Error())
|
2020-10-20 18:18:36 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-15 13:46:58 +00:00
|
|
|
message.From = senderPeerID
|
2020-10-20 18:18:36 +00:00
|
|
|
handlers, ok := psr.handlers[message.Type]
|
|
|
|
if !ok {
|
2020-11-15 13:46:58 +00:00
|
|
|
logrus.Warn("Dropping message " + string(message.Type) + " because we don't have any handlers!")
|
2020-10-20 18:18:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, v := range handlers {
|
2020-10-21 19:54:40 +00:00
|
|
|
go v(&message)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (psr *PubSubRouter) Hook(messageType types.MessageType, handler Handler) {
|
2020-12-02 14:09:46 +00:00
|
|
|
_, ok := psr.handlers[messageType]
|
2020-10-20 18:18:36 +00:00
|
|
|
if !ok {
|
2020-12-02 14:09:46 +00:00
|
|
|
psr.handlers[messageType] = []Handler{}
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
2020-12-02 14:09:46 +00:00
|
|
|
psr.handlers[messageType] = append(psr.handlers[messageType], handler)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:53:52 +00:00
|
|
|
func (psr *PubSubRouter) BroadcastToServiceTopic(msg *types.Message) error {
|
|
|
|
data, err := cbor.Marshal(msg)
|
2020-10-21 19:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-28 17:35:56 +00:00
|
|
|
err = psr.Pubsub.Publish(psr.oracleTopic, data)
|
2020-10-21 19:54:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:18:36 +00:00
|
|
|
func (psr *PubSubRouter) Shutdown() {
|
|
|
|
psr.contextCancel()
|
|
|
|
}
|