2020-08-03 20:01:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/rpcclient"
|
2020-08-03 20:01:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagGenericMessage int = 0x0
|
|
|
|
FlagTopicsRequest int = 0x1
|
|
|
|
FlagTopicsResponse int = 0x2
|
|
|
|
FlagIdentityRequest int = 0x3
|
|
|
|
FlagIdentityResponse int = 0x4
|
|
|
|
FlagGreeting int = 0x5
|
|
|
|
FlagFarewell int = 0x6
|
|
|
|
FlagGreetingRespond int = 0x7
|
2020-08-07 02:42:09 +00:00
|
|
|
FlagEventMessage int = 0x8
|
2020-08-03 20:01:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BaseMessage is the basic message format of our protocol
|
|
|
|
type BaseMessage struct {
|
2020-08-07 02:42:09 +00:00
|
|
|
Body *rpcclient.OracleEvent `json:"body"`
|
|
|
|
To peer.ID `json:"to"`
|
|
|
|
Flag int `json:"flag"`
|
|
|
|
From peer.ID `json:"from"`
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopicsRespondMessage is the format of the message to answer of request for topics
|
|
|
|
// Flag: 0x2
|
|
|
|
type GetTopicsRespondMessage struct {
|
|
|
|
BaseMessage
|
|
|
|
Topics []string `json:"topics"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) sendIdentityResponse(topic string, fromPeerID peer.ID) {
|
|
|
|
var flag int
|
|
|
|
if topic == h.oracleTopic {
|
|
|
|
flag = FlagIdentityResponse
|
|
|
|
} else {
|
|
|
|
flag = FlagGreetingRespond
|
|
|
|
}
|
|
|
|
respond := &BaseMessage{
|
2020-08-07 02:42:09 +00:00
|
|
|
Body: &rpcclient.OracleEvent{},
|
2020-08-03 20:01:38 +00:00
|
|
|
Flag: flag,
|
|
|
|
From: "",
|
|
|
|
To: fromPeerID,
|
|
|
|
}
|
|
|
|
sendData, err := json.Marshal(respond)
|
|
|
|
if err != nil {
|
|
|
|
h.Logger.Warn("Error occurred during marshalling the respond from IdentityRequest")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
h.PbMutex.Lock()
|
|
|
|
if err = h.pb.Publish(topic, sendData); err != nil {
|
|
|
|
h.Logger.Warn("Failed to send new message to pubsub topic", err)
|
|
|
|
}
|
|
|
|
h.PbMutex.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests MatrixID from specific peer
|
|
|
|
// TODO: refactor with promise
|
|
|
|
func (h *Handler) RequestPeerIdentity(peerID peer.ID) {
|
|
|
|
requestPeersIdentity := &BaseMessage{
|
2020-08-07 02:42:09 +00:00
|
|
|
Body: &rpcclient.OracleEvent{},
|
2020-08-03 20:01:38 +00:00
|
|
|
To: peerID,
|
|
|
|
Flag: FlagIdentityRequest,
|
|
|
|
From: h.peerID,
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
h.SendMessageToServiceTopic(requestPeersIdentity)
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: refactor
|
|
|
|
func (h *Handler) SendGreetingInTopic(topic string) {
|
|
|
|
greetingMessage := &BaseMessage{
|
2020-08-07 02:42:09 +00:00
|
|
|
Body: &rpcclient.OracleEvent{},
|
2020-08-03 20:01:38 +00:00
|
|
|
To: "",
|
|
|
|
Flag: FlagGreeting,
|
|
|
|
From: h.peerID,
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
h.SendMessageToTopic(topic, greetingMessage)
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: refactor
|
|
|
|
func (h *Handler) SendFarewellInTopic(topic string) {
|
|
|
|
farewellMessage := &BaseMessage{
|
2020-08-07 02:42:09 +00:00
|
|
|
Body: &rpcclient.OracleEvent{},
|
2020-08-03 20:01:38 +00:00
|
|
|
To: "",
|
|
|
|
Flag: FlagFarewell,
|
|
|
|
From: h.peerID,
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
h.SendMessageToTopic(topic, farewellMessage)
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends marshaled message to the service topic
|
2020-08-07 02:42:09 +00:00
|
|
|
func (h *Handler) SendMessageToServiceTopic(message *BaseMessage) {
|
|
|
|
h.SendMessageToTopic(h.oracleTopic, message)
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 02:42:09 +00:00
|
|
|
func (h *Handler) SendMessageToTopic(topic string, message *BaseMessage) {
|
2020-08-03 20:01:38 +00:00
|
|
|
sendData, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
h.Logger.Warn("Failed to send message to topic", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
h.PbMutex.Lock()
|
|
|
|
if err = h.pb.Publish(topic, sendData); err != nil {
|
|
|
|
h.Logger.Warn("Failed to send new message to pubsub topic", err)
|
|
|
|
}
|
|
|
|
h.PbMutex.Unlock()
|
|
|
|
}()
|
|
|
|
}
|