-
Notifications
You must be signed in to change notification settings - Fork 172
/
handler.go
104 lines (83 loc) · 2.79 KB
/
handler.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
package engine
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
ghost "github.com/onflow/flow-go/engine/ghost/protobuf"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/network"
"github.com/onflow/flow-go/network/channels"
)
// Handler handles the GRPC calls from a client
type Handler struct {
log zerolog.Logger
conduitMap map[channels.Channel]network.Conduit
msgChan chan ghost.FlowMessage
codec network.Codec
}
var _ ghost.GhostNodeAPIServer = Handler{}
func NewHandler(log zerolog.Logger, conduitMap map[channels.Channel]network.Conduit, msgChan chan ghost.FlowMessage, codec network.Codec) *Handler {
return &Handler{
log: log.With().Str("component", "ghost_engine_handler").Logger(),
conduitMap: conduitMap,
msgChan: msgChan,
codec: codec,
}
}
func (h Handler) SendEvent(_ context.Context, req *ghost.SendEventRequest) (*empty.Empty, error) {
channelID := req.GetChannelId()
// find the conduit for the channel
conduit, found := h.conduitMap[channels.Channel(channelID)]
if !found {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("conduit not found for given channel %v", channelID))
}
message := req.GetMessage()
event, err := h.codec.Decode(message)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "failed to decode message")
}
targetIDs := req.GetTargetID()
// Convert target ID to flow IDS
var flowIDs = make([]flow.Identifier, len(targetIDs))
for i, id := range targetIDs {
flowIDs[i] = flow.HashToID(id)
}
h.log.Info().
Interface("event", event).
Str("flow_ids", fmt.Sprintf("%v", flowIDs)).
Str("target_ids", fmt.Sprintf("%v", targetIDs)).
Msg("sending message")
// Submit the message over libp2p
// TODO: there is an issue in the Publish method for the ghost node,
// sometimes, it fails to deliver the message to the target without returning any error.
// This becomes one of the big factors contributing to the tests flakeiness.
err = conduit.Publish(event, flowIDs...)
if err != nil {
h.log.Error().
Err(err).
Interface("event", event).
Str("flow_ids", fmt.Sprintf("%v", flowIDs)).
Str("target_ids", fmt.Sprintf("%v", targetIDs)).
Msg("error publishing message")
return nil, err
}
return new(empty.Empty), nil
}
// Subscribe streams ALL the libp2p network messages over GRPC
func (h Handler) Subscribe(_ *ghost.SubscribeRequest, stream ghost.GhostNodeAPI_SubscribeServer) error {
for {
// read the network message from the channel
flowMessage, ok := <-h.msgChan
if !ok {
return nil
}
// send it to the client
err := stream.Send(&flowMessage)
if err != nil {
return status.Errorf(codes.Internal, "failed to stream message: %v", err)
}
}
}