2020-08-03 20:01:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/rpcclient"
|
2020-08-03 20:01:38 +00:00
|
|
|
mapset "github.com/deckarep/golang-set"
|
|
|
|
"github.com/ipfs/go-log"
|
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
pb *pubsub.PubSub
|
|
|
|
oracleTopic string
|
|
|
|
networkTopics mapset.Set
|
|
|
|
peerID peer.ID
|
|
|
|
identityMap map[peer.ID]string
|
|
|
|
Logger *log.ZapEventLogger
|
|
|
|
PbMutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextMessage is more end-user model of regular text messages
|
2020-08-07 02:42:09 +00:00
|
|
|
type EventMessage struct {
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
Body *rpcclient.OracleEvent `json:"body"`
|
|
|
|
From peer.ID `json:"from"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(pb *pubsub.PubSub, oracleTopic string, peerID peer.ID, networkTopics mapset.Set) *Handler {
|
|
|
|
handler := &Handler{
|
|
|
|
pb: pb,
|
|
|
|
oracleTopic: oracleTopic,
|
|
|
|
networkTopics: networkTopics,
|
|
|
|
peerID: peerID,
|
|
|
|
identityMap: make(map[peer.ID]string),
|
|
|
|
Logger: log.Logger("rendezvous"),
|
|
|
|
}
|
2020-08-06 21:47:58 +00:00
|
|
|
log.SetAllLoggers(log.LevelInfo)
|
|
|
|
|
2020-08-03 20:01:38 +00:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
func (h *Handler) HandleIncomingMessage(topic string, msg pubsub.Message, handleTextMessage func(EventMessage)) {
|
2020-08-03 20:01:38 +00:00
|
|
|
fromPeerID, err := peer.IDFromBytes(msg.From)
|
|
|
|
if err != nil {
|
|
|
|
h.Logger.Warn("Error occurred when reading message from field...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message := &BaseMessage{}
|
|
|
|
if err = json.Unmarshal(msg.Data, message); err != nil {
|
|
|
|
h.Logger.Warn("Error occurred during unmarshalling the base message data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if message.To != "" && message.To != h.peerID {
|
|
|
|
return // Drop message, because it is not for us
|
|
|
|
}
|
|
|
|
|
|
|
|
switch message.Flag {
|
|
|
|
// Getting regular message
|
|
|
|
case FlagGenericMessage:
|
2020-08-07 02:42:09 +00:00
|
|
|
eventMessage := EventMessage{
|
2020-08-03 20:01:38 +00:00
|
|
|
Topic: topic,
|
|
|
|
Body: message.Body,
|
|
|
|
From: fromPeerID,
|
|
|
|
}
|
2020-08-07 02:42:09 +00:00
|
|
|
handleTextMessage(eventMessage)
|
2020-08-03 20:01:38 +00:00
|
|
|
// Getting topic request, answer topic response
|
|
|
|
case FlagTopicsRequest:
|
|
|
|
respond := &GetTopicsRespondMessage{
|
|
|
|
BaseMessage: BaseMessage{
|
2020-08-07 02:42:09 +00:00
|
|
|
Body: &rpcclient.OracleEvent{},
|
2020-08-03 20:01:38 +00:00
|
|
|
Flag: FlagTopicsResponse,
|
|
|
|
To: fromPeerID,
|
|
|
|
},
|
|
|
|
Topics: h.GetTopics(),
|
|
|
|
}
|
|
|
|
sendData, err := json.Marshal(respond)
|
|
|
|
if err != nil {
|
|
|
|
h.Logger.Warn("Error occurred during marshalling the respond from TopicsRequest")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
h.PbMutex.Lock()
|
|
|
|
if err = h.pb.Publish(h.oracleTopic, sendData); err != nil {
|
|
|
|
h.Logger.Warn("Failed to send new message to pubsub topic", err)
|
|
|
|
}
|
|
|
|
h.PbMutex.Unlock()
|
|
|
|
}()
|
|
|
|
// Getting topic respond, adding topics to `networkTopics`
|
|
|
|
case FlagTopicsResponse:
|
|
|
|
respond := &GetTopicsRespondMessage{}
|
|
|
|
if err = json.Unmarshal(msg.Data, respond); err != nil {
|
|
|
|
h.Logger.Warn("Error occurred during unmarshalling the message data from TopicsResponse")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < len(respond.Topics); i++ {
|
|
|
|
h.networkTopics.Add(respond.Topics[i])
|
|
|
|
}
|
|
|
|
// Getting identity request, answer identity response
|
|
|
|
case FlagIdentityRequest:
|
|
|
|
h.sendIdentityResponse(h.oracleTopic, fromPeerID)
|
|
|
|
// Getting identity respond, mapping Multiaddress/MatrixID
|
|
|
|
case FlagIdentityResponse:
|
|
|
|
h.identityMap[peer.ID(fromPeerID.String())] = message.From.String()
|
|
|
|
case FlagGreeting:
|
|
|
|
h.Logger.Info("Greetings from " + fromPeerID.String() + " in topic " + topic)
|
|
|
|
h.sendIdentityResponse(topic, fromPeerID)
|
|
|
|
case FlagGreetingRespond:
|
|
|
|
h.Logger.Info("Greeting respond from " + fromPeerID.String() + ":" + message.From.String() + " in topic " + topic)
|
|
|
|
case FlagFarewell:
|
|
|
|
h.Logger.Info("Greeting respond from " + fromPeerID.String() + ":" + message.From.String() + " in topic " + topic)
|
2020-08-07 02:42:09 +00:00
|
|
|
case FlagEventMessage:
|
|
|
|
eventMessage := EventMessage{
|
|
|
|
Topic: topic,
|
|
|
|
Body: message.Body,
|
|
|
|
From: fromPeerID,
|
|
|
|
}
|
|
|
|
handleTextMessage(eventMessage)
|
2020-08-03 20:01:38 +00:00
|
|
|
default:
|
|
|
|
h.Logger.Info("\nUnknown message type: %#x\n", message.Flag)
|
|
|
|
}
|
|
|
|
}
|