-
Notifications
You must be signed in to change notification settings - Fork 38
/
message.go
468 lines (409 loc) · 13.8 KB
/
message.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package message
import (
"encoding/binary"
"errors"
"fmt"
"io"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
pool "github.com/libp2p/go-buffer-pool"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-msgio"
"google.golang.org/protobuf/proto"
"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/ipldutil"
pb "github.com/ipfs/go-graphsync/message/pb"
)
// IsTerminalSuccessCode returns true if the response code indicates the
// request terminated successfully.
// DEPRECATED: use status.IsSuccess()
func IsTerminalSuccessCode(status graphsync.ResponseStatusCode) bool {
return status.IsSuccess()
}
// IsTerminalFailureCode returns true if the response code indicates the
// request terminated in failure.
// DEPRECATED: use status.IsFailure()
func IsTerminalFailureCode(status graphsync.ResponseStatusCode) bool {
return status.IsFailure()
}
// IsTerminalResponseCode returns true if the response code signals
// the end of the request
// DEPRECATED: use status.IsTerminal()
func IsTerminalResponseCode(status graphsync.ResponseStatusCode) bool {
return status.IsTerminal()
}
// Exportable is an interface that can serialize to a protobuf
type Exportable interface {
ToProto() (*pb.Message, error)
ToNet(w io.Writer) error
}
// GraphSyncRequest is a struct to capture data on a request contained in a
// GraphSyncMessage.
type GraphSyncRequest struct {
root cid.Cid
selector ipld.Node
priority graphsync.Priority
id graphsync.RequestID
extensions map[string][]byte
isCancel bool
isUpdate bool
}
// GraphSyncResponse is an struct to capture data on a response sent back
// in a GraphSyncMessage.
type GraphSyncResponse struct {
requestID graphsync.RequestID
status graphsync.ResponseStatusCode
extensions map[string][]byte
}
type GraphSyncMessage struct {
requests map[graphsync.RequestID]GraphSyncRequest
responses map[graphsync.RequestID]GraphSyncResponse
blocks map[cid.Cid]blocks.Block
}
// NewRequest builds a new Graphsync request
func NewRequest(id graphsync.RequestID,
root cid.Cid,
selector ipld.Node,
priority graphsync.Priority,
extensions ...graphsync.ExtensionData) GraphSyncRequest {
return newRequest(id, root, selector, priority, false, false, toExtensionsMap(extensions))
}
// CancelRequest request generates a request to cancel an in progress request
func CancelRequest(id graphsync.RequestID) GraphSyncRequest {
return newRequest(id, cid.Cid{}, nil, 0, true, false, nil)
}
// UpdateRequest generates a new request to update an in progress request with the given extensions
func UpdateRequest(id graphsync.RequestID, extensions ...graphsync.ExtensionData) GraphSyncRequest {
return newRequest(id, cid.Cid{}, nil, 0, false, true, toExtensionsMap(extensions))
}
func toExtensionsMap(extensions []graphsync.ExtensionData) (extensionsMap map[string][]byte) {
if len(extensions) > 0 {
extensionsMap = make(map[string][]byte, len(extensions))
for _, extension := range extensions {
extensionsMap[string(extension.Name)] = extension.Data
}
}
return
}
func newRequest(id graphsync.RequestID,
root cid.Cid,
selector ipld.Node,
priority graphsync.Priority,
isCancel bool,
isUpdate bool,
extensions map[string][]byte) GraphSyncRequest {
return GraphSyncRequest{
id: id,
root: root,
selector: selector,
priority: priority,
isCancel: isCancel,
isUpdate: isUpdate,
extensions: extensions,
}
}
// NewResponse builds a new Graphsync response
func NewResponse(requestID graphsync.RequestID,
status graphsync.ResponseStatusCode,
extensions ...graphsync.ExtensionData) GraphSyncResponse {
return newResponse(requestID, status, toExtensionsMap(extensions))
}
func newResponse(requestID graphsync.RequestID,
status graphsync.ResponseStatusCode, extensions map[string][]byte) GraphSyncResponse {
return GraphSyncResponse{
requestID: requestID,
status: status,
extensions: extensions,
}
}
func newMessageFromProto(pbm *pb.Message) (GraphSyncMessage, error) {
requests := make(map[graphsync.RequestID]GraphSyncRequest, len(pbm.GetRequests()))
for _, req := range pbm.Requests {
if req == nil {
return GraphSyncMessage{}, errors.New("request is nil")
}
var root cid.Cid
var err error
if !req.Cancel && !req.Update {
root, err = cid.Cast(req.Root)
if err != nil {
return GraphSyncMessage{}, err
}
}
var selector ipld.Node
if !req.Cancel && !req.Update {
selector, err = ipldutil.DecodeNode(req.Selector)
if err != nil {
return GraphSyncMessage{}, err
}
}
exts := req.GetExtensions()
if exts == nil {
exts = make(map[string][]byte)
}
requests[graphsync.RequestID(req.Id)] = newRequest(graphsync.RequestID(req.Id), root, selector, graphsync.Priority(req.Priority), req.Cancel, req.Update, exts)
}
responses := make(map[graphsync.RequestID]GraphSyncResponse, len(pbm.GetResponses()))
for _, res := range pbm.Responses {
if res == nil {
return GraphSyncMessage{}, errors.New("response is nil")
}
exts := res.GetExtensions()
if exts == nil {
exts = make(map[string][]byte)
}
responses[graphsync.RequestID(res.Id)] = newResponse(graphsync.RequestID(res.Id), graphsync.ResponseStatusCode(res.Status), exts)
}
blks := make(map[cid.Cid]blocks.Block, len(pbm.GetData()))
for _, b := range pbm.GetData() {
if b == nil {
return GraphSyncMessage{}, errors.New("block is nil")
}
pref, err := cid.PrefixFromBytes(b.GetPrefix())
if err != nil {
return GraphSyncMessage{}, err
}
c, err := pref.Sum(b.GetData())
if err != nil {
return GraphSyncMessage{}, err
}
blk, err := blocks.NewBlockWithCid(b.GetData(), c)
if err != nil {
return GraphSyncMessage{}, err
}
blks[blk.Cid()] = blk
}
return GraphSyncMessage{
requests, responses, blks,
}, nil
}
func (gsm GraphSyncMessage) Empty() bool {
return len(gsm.blocks) == 0 && len(gsm.requests) == 0 && len(gsm.responses) == 0
}
func (gsm GraphSyncMessage) Requests() []GraphSyncRequest {
requests := make([]GraphSyncRequest, 0, len(gsm.requests))
for _, request := range gsm.requests {
requests = append(requests, request)
}
return requests
}
func (gsm GraphSyncMessage) ResponseCodes() map[graphsync.RequestID]graphsync.ResponseStatusCode {
codes := make(map[graphsync.RequestID]graphsync.ResponseStatusCode, len(gsm.responses))
for id, response := range gsm.responses {
codes[id] = response.Status()
}
return codes
}
func (gsm GraphSyncMessage) Responses() []GraphSyncResponse {
responses := make([]GraphSyncResponse, 0, len(gsm.responses))
for _, response := range gsm.responses {
responses = append(responses, response)
}
return responses
}
func (gsm GraphSyncMessage) Blocks() []blocks.Block {
bs := make([]blocks.Block, 0, len(gsm.blocks))
for _, block := range gsm.blocks {
bs = append(bs, block)
}
return bs
}
// FromNet can read a network stream to deserialized a GraphSyncMessage
func FromNet(r io.Reader) (GraphSyncMessage, error) {
reader := msgio.NewVarintReaderSize(r, network.MessageSizeMax)
return FromMsgReader(reader)
}
// FromMsgReader can deserialize a protobuf message into a GraphySyncMessage.
func FromMsgReader(r msgio.Reader) (GraphSyncMessage, error) {
msg, err := r.ReadMsg()
if err != nil {
return GraphSyncMessage{}, err
}
var pb pb.Message
err = proto.Unmarshal(msg, &pb)
r.ReleaseMsg(msg)
if err != nil {
return GraphSyncMessage{}, err
}
return newMessageFromProto(&pb)
}
func (gsm GraphSyncMessage) ToProto() (*pb.Message, error) {
pbm := new(pb.Message)
pbm.Requests = make([]*pb.Message_Request, 0, len(gsm.requests))
for _, request := range gsm.requests {
var selector []byte
var err error
if request.selector != nil {
selector, err = ipldutil.EncodeNode(request.selector)
if err != nil {
return nil, err
}
}
pbm.Requests = append(pbm.Requests, &pb.Message_Request{
Id: int32(request.id),
Root: request.root.Bytes(),
Selector: selector,
Priority: int32(request.priority),
Cancel: request.isCancel,
Update: request.isUpdate,
Extensions: request.extensions,
})
}
pbm.Responses = make([]*pb.Message_Response, 0, len(gsm.responses))
for _, response := range gsm.responses {
pbm.Responses = append(pbm.Responses, &pb.Message_Response{
Id: int32(response.requestID),
Status: int32(response.status),
Extensions: response.extensions,
})
}
blocks := gsm.Blocks()
pbm.Data = make([]*pb.Message_Block, 0, len(blocks))
for _, b := range blocks {
pbm.Data = append(pbm.Data, &pb.Message_Block{
Data: b.RawData(),
Prefix: b.Cid().Prefix().Bytes(),
})
}
return pbm, nil
}
func (gsm GraphSyncMessage) ToNet(w io.Writer) error {
msg, err := gsm.ToProto()
if err != nil {
return err
}
size := proto.Size(msg)
buf := pool.Get(size + binary.MaxVarintLen64)
defer pool.Put(buf)
n := binary.PutUvarint(buf, uint64(size))
out, err := proto.MarshalOptions{}.MarshalAppend(buf[:n], msg)
if err != nil {
return err
}
_, err = w.Write(out)
return err
}
func (gsm GraphSyncMessage) Loggable() map[string]interface{} {
requests := make([]string, 0, len(gsm.requests))
for _, request := range gsm.requests {
requests = append(requests, fmt.Sprintf("%d", request.id))
}
responses := make([]string, 0, len(gsm.responses))
for _, response := range gsm.responses {
responses = append(responses, fmt.Sprintf("%d", response.requestID))
}
return map[string]interface{}{
"requests": requests,
"responses": responses,
}
}
func (gsm GraphSyncMessage) Clone() GraphSyncMessage {
requests := make(map[graphsync.RequestID]GraphSyncRequest, len(gsm.requests))
for id, request := range gsm.requests {
requests[id] = request
}
responses := make(map[graphsync.RequestID]GraphSyncResponse, len(gsm.responses))
for id, response := range gsm.responses {
responses[id] = response
}
blocks := make(map[cid.Cid]blocks.Block, len(gsm.blocks))
for cid, block := range gsm.blocks {
blocks[cid] = block
}
return GraphSyncMessage{requests, responses, blocks}
}
// ID Returns the request ID for this Request
func (gsr GraphSyncRequest) ID() graphsync.RequestID { return gsr.id }
// Root returns the CID to the root block of this request
func (gsr GraphSyncRequest) Root() cid.Cid { return gsr.root }
// Selector returns the byte representation of the selector for this request
func (gsr GraphSyncRequest) Selector() ipld.Node { return gsr.selector }
// Priority returns the priority of this request
func (gsr GraphSyncRequest) Priority() graphsync.Priority { return gsr.priority }
// Extension returns the content for an extension on a response, or errors
// if extension is not present
func (gsr GraphSyncRequest) Extension(name graphsync.ExtensionName) ([]byte, bool) {
if gsr.extensions == nil {
return nil, false
}
val, ok := gsr.extensions[string(name)]
if !ok {
return nil, false
}
return val, true
}
// ExtensionNames returns the names of the extensions included in this request
func (gsr GraphSyncRequest) ExtensionNames() []string {
var extNames []string
for ext := range gsr.extensions {
extNames = append(extNames, ext)
}
return extNames
}
// IsCancel returns true if this particular request is being cancelled
func (gsr GraphSyncRequest) IsCancel() bool { return gsr.isCancel }
// IsUpdate returns true if this particular request is being updated
func (gsr GraphSyncRequest) IsUpdate() bool { return gsr.isUpdate }
// RequestID returns the request ID for this response
func (gsr GraphSyncResponse) RequestID() graphsync.RequestID { return gsr.requestID }
// Status returns the status for a response
func (gsr GraphSyncResponse) Status() graphsync.ResponseStatusCode { return gsr.status }
// Extension returns the content for an extension on a response, or errors
// if extension is not present
func (gsr GraphSyncResponse) Extension(name graphsync.ExtensionName) ([]byte, bool) {
if gsr.extensions == nil {
return nil, false
}
val, ok := gsr.extensions[string(name)]
if !ok {
return nil, false
}
return val, true
}
// ExtensionNames returns the names of the extensions included in this request
func (gsr GraphSyncResponse) ExtensionNames() []string {
var extNames []string
for ext := range gsr.extensions {
extNames = append(extNames, ext)
}
return extNames
}
// ReplaceExtensions merges the extensions given extensions into the request to create a new request,
// but always uses new data
func (gsr GraphSyncRequest) ReplaceExtensions(extensions []graphsync.ExtensionData) GraphSyncRequest {
req, _ := gsr.MergeExtensions(extensions, func(name graphsync.ExtensionName, oldData []byte, newData []byte) ([]byte, error) {
return newData, nil
})
return req
}
// MergeExtensions merges the given list of extensions to produce a new request with the combination of the old request
// plus the new extensions. When an old extension and a new extension are both present, mergeFunc is called to produce
// the result
func (gsr GraphSyncRequest) MergeExtensions(extensions []graphsync.ExtensionData, mergeFunc func(name graphsync.ExtensionName, oldData []byte, newData []byte) ([]byte, error)) (GraphSyncRequest, error) {
if gsr.extensions == nil {
return newRequest(gsr.id, gsr.root, gsr.selector, gsr.priority, gsr.isCancel, gsr.isUpdate, toExtensionsMap(extensions)), nil
}
newExtensionMap := toExtensionsMap(extensions)
combinedExtensions := make(map[string][]byte)
for name, newData := range newExtensionMap {
oldData, ok := gsr.extensions[name]
if !ok {
combinedExtensions[name] = newData
continue
}
resultData, err := mergeFunc(graphsync.ExtensionName(name), oldData, newData)
if err != nil {
return GraphSyncRequest{}, err
}
combinedExtensions[name] = resultData
}
for name, oldData := range gsr.extensions {
_, ok := combinedExtensions[name]
if ok {
continue
}
combinedExtensions[name] = oldData
}
return newRequest(gsr.id, gsr.root, gsr.selector, gsr.priority, gsr.isCancel, gsr.isUpdate, combinedExtensions), nil
}