-
Notifications
You must be signed in to change notification settings - Fork 19
/
requests.go
178 lines (159 loc) · 4.61 KB
/
requests.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
170
171
172
173
174
175
176
177
178
package main
import (
"bufio"
"context"
"fmt"
"github.com/golang/protobuf/proto"
pbmsg "github.com/ethresearch/sharding-p2p-poc/pb/message"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
protocol "github.com/libp2p/go-libp2p-protocol"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
)
// RequestProtocol type
type RequestProtocol struct {
node *Node
}
const shardPeerRequestProtocol = protocol.ID("/shardPeerRequest/1.0.0")
const generalRequestProtocol = protocol.ID("/generalRequest/1.0.0")
// NewRequestProtocol defines the request protocol, which allows others to query data
func NewRequestProtocol(node *Node) *RequestProtocol {
p := &RequestProtocol{
node: node,
}
node.SetStreamHandler(shardPeerRequestProtocol, p.onShardPeerRequest)
node.SetStreamHandler(generalRequestProtocol, p.onGeneralRequest)
return p
}
// helper method - reads a protobuf go data object from a network stream
// data: reference of protobuf go data object(not the object itself)
// s: network stream to read the data from
func readProtoMessage(data proto.Message, s inet.Stream) error {
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
return decoder.Decode(data)
}
// helper method - writes a protobuf go data object to a network stream
// data: reference of protobuf go data object to send (not the object itself)
// s: network stream to write the data to
func sendProtoMessage(data proto.Message, s inet.Stream) error {
writer := bufio.NewWriter(s)
enc := protobufCodec.Multicodec(nil).Encoder(writer)
err := enc.Encode(data)
if err != nil {
return err
}
writer.Flush()
return nil
}
func (p *RequestProtocol) onShardPeerRequest(s inet.Stream) {
defer inet.FullClose(s)
req := &pbmsg.ShardPeerRequest{}
if err := readProtoMessage(req, s); err != nil {
return
}
shardPeers := make(map[ShardIDType]*pbmsg.Peers)
for _, shardID := range req.ShardIDs {
peerIDs := p.node.shardPrefTable.GetPeersInShard(shardID)
shardPeers[shardID] = peerIDsToPBPeers(peerIDs)
}
res := &pbmsg.ShardPeerResponse{
Response: &pbmsg.Response{Status: pbmsg.Response_SUCCESS},
ShardPeers: shardPeers,
}
if err := sendProtoMessage(res, s); err != nil {
logger.Errorf("onShardPeerRequest: failed to send proto message '%v', err: %v", res, err)
return
}
}
func (p *RequestProtocol) requestShardPeer(
ctx context.Context,
peerID peer.ID,
shardIDs []ShardIDType) (map[ShardIDType][]peer.ID, error) {
s, err := p.node.NewStream(
ctx,
peerID,
shardPeerRequestProtocol,
)
if err != nil {
return nil, fmt.Errorf("failed to open new stream")
}
defer s.Close()
req := &pbmsg.ShardPeerRequest{
ShardIDs: shardIDs,
}
if err := sendProtoMessage(req, s); err != nil {
return nil, fmt.Errorf("failed to send request: %v", err)
}
res := &pbmsg.ShardPeerResponse{}
if err := readProtoMessage(res, s); err != nil {
return nil, fmt.Errorf("failed to read response proto")
}
shardPeers := make(map[ShardIDType][]peer.ID)
for shardID, peers := range res.ShardPeers {
peerIDs, err := pbPeersToPeerIDs(peers)
if err != nil {
return nil, fmt.Errorf(
"Failed to convert peer ID from PB string to peerID format, err: %v",
err,
)
}
shardPeers[shardID] = peerIDs
}
return shardPeers, nil
}
func (p *RequestProtocol) onGeneralRequest(s inet.Stream) {
defer inet.FullClose(s)
req := &pbmsg.GeneralRequest{}
if err := readProtoMessage(req, s); err != nil {
logger.Errorf(
"onGeneralRequest: failed to read proto message, reason=%v",
err,
)
return
}
peerID := s.Conn().RemotePeer()
dataBytes, err := p.node.eventNotifier.Receive(context.Background(), peerID, int(req.MsgType), req.Data)
if err != nil {
logger.Errorf(
"onGeneralRequest: failed to receive response from event notifier, reason=%v",
err,
)
return
}
resp := &pbmsg.GeneralResponse{
Data: dataBytes,
}
if err := sendProtoMessage(resp, s); err != nil {
logger.Errorf(
"onGeneralRequest: failed to send proto message %v, reason=%v",
resp,
err,
)
}
}
func (p *RequestProtocol) generalRequest(
ctx context.Context,
peerID peer.ID,
msgType int,
data []byte) ([]byte, error) {
s, err := p.node.NewStream(
ctx,
peerID,
generalRequestProtocol,
)
if err != nil {
return nil, fmt.Errorf("failed to open new stream %v", err)
}
req := &pbmsg.GeneralRequest{
MsgType: PBInt(msgType),
Data: data,
}
if err := sendProtoMessage(req, s); err != nil {
return nil, fmt.Errorf("failed to send request: %v", err)
}
resp := &pbmsg.GeneralResponse{}
if err := readProtoMessage(resp, s); err != nil {
return nil, fmt.Errorf("failed to read response proto")
}
return resp.Data, nil
}