/
message.go
207 lines (185 loc) · 7.03 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
package message1_1
import (
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
xerrors "golang.org/x/xerrors"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/filecoin-project/go-data-transfer/encoding"
"github.com/filecoin-project/go-data-transfer/message/types"
)
// NewRequest generates a new request for the data transfer protocol
func NewRequest(id datatransfer.TransferID, isRestart bool, isPull bool, vtype datatransfer.TypeIdentifier, voucher encoding.Encodable, baseCid cid.Cid, selector ipld.Node) (datatransfer.Request, error) {
vnode, err := encoding.EncodeToNode(voucher)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
if baseCid == cid.Undef {
return nil, xerrors.Errorf("base CID must be defined")
}
var typ uint64
if isRestart {
typ = uint64(types.RestartMessage)
} else {
typ = uint64(types.NewMessage)
}
return &TransferRequest1_1{
MessageType: typ,
Pull: isPull,
VoucherPtr: &vnode,
SelectorPtr: &selector,
BaseCidPtr: &baseCid,
VoucherTypeIdentifier: vtype,
TransferId: uint64(id),
}, nil
}
// RestartExistingChannelRequest creates a request to ask the other side to restart an existing channel
func RestartExistingChannelRequest(channelId datatransfer.ChannelID) datatransfer.Request {
return &TransferRequest1_1{
MessageType: uint64(types.RestartExistingChannelRequestMessage),
RestartChannel: channelId,
}
}
// CancelRequest request generates a request to cancel an in progress request
func CancelRequest(id datatransfer.TransferID) datatransfer.Request {
return &TransferRequest1_1{
MessageType: uint64(types.CancelMessage),
TransferId: uint64(id),
}
}
// UpdateRequest generates a new request update
func UpdateRequest(id datatransfer.TransferID, isPaused bool) datatransfer.Request {
return &TransferRequest1_1{
MessageType: uint64(types.UpdateMessage),
Pause: isPaused,
TransferId: uint64(id),
}
}
// VoucherRequest generates a new request for the data transfer protocol
func VoucherRequest(id datatransfer.TransferID, vtype datatransfer.TypeIdentifier, voucher encoding.Encodable) (datatransfer.Request, error) {
vnode, err := encoding.EncodeToNode(voucher)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
return &TransferRequest1_1{
MessageType: uint64(types.VoucherMessage),
VoucherPtr: &vnode,
VoucherTypeIdentifier: vtype,
TransferId: uint64(id),
}, nil
}
// RestartResponse builds a new Data Transfer response
func RestartResponse(id datatransfer.TransferID, accepted bool, isPaused bool, voucherResultType datatransfer.TypeIdentifier, voucherResult encoding.Encodable) (datatransfer.Response, error) {
vnode, err := encoding.EncodeToNode(voucherResult)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
return &TransferResponse1_1{
RequestAccepted: accepted,
MessageType: uint64(types.RestartMessage),
Paused: isPaused,
TransferId: uint64(id),
VoucherTypeIdentifier: voucherResultType,
VoucherResultPtr: &vnode,
}, nil
}
// NewResponse builds a new Data Transfer response
func NewResponse(id datatransfer.TransferID, accepted bool, isPaused bool, voucherResultType datatransfer.TypeIdentifier, voucherResult encoding.Encodable) (datatransfer.Response, error) {
vnode, err := encoding.EncodeToNode(voucherResult)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
return &TransferResponse1_1{
RequestAccepted: accepted,
MessageType: uint64(types.NewMessage),
Paused: isPaused,
TransferId: uint64(id),
VoucherTypeIdentifier: voucherResultType,
VoucherResultPtr: &vnode,
}, nil
}
// VoucherResultResponse builds a new response for a voucher result
func VoucherResultResponse(id datatransfer.TransferID, accepted bool, isPaused bool, voucherResultType datatransfer.TypeIdentifier, voucherResult encoding.Encodable) (datatransfer.Response, error) {
vnode, err := encoding.EncodeToNode(voucherResult)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
return &TransferResponse1_1{
RequestAccepted: accepted,
MessageType: uint64(types.VoucherResultMessage),
Paused: isPaused,
TransferId: uint64(id),
VoucherTypeIdentifier: voucherResultType,
VoucherResultPtr: &vnode,
}, nil
}
// UpdateResponse returns a new update response
func UpdateResponse(id datatransfer.TransferID, isPaused bool) datatransfer.Response {
return &TransferResponse1_1{
MessageType: uint64(types.UpdateMessage),
Paused: isPaused,
TransferId: uint64(id),
}
}
// CancelResponse makes a new cancel response message
func CancelResponse(id datatransfer.TransferID) datatransfer.Response {
return &TransferResponse1_1{
MessageType: uint64(types.CancelMessage),
TransferId: uint64(id),
}
}
// CompleteResponse returns a new complete response message
func CompleteResponse(id datatransfer.TransferID, isAccepted bool, isPaused bool, voucherResultType datatransfer.TypeIdentifier, voucherResult encoding.Encodable) (datatransfer.Response, error) {
vnode, err := encoding.EncodeToNode(voucherResult)
if err != nil {
return nil, xerrors.Errorf("Creating request: %w", err)
}
return &TransferResponse1_1{
MessageType: uint64(types.CompleteMessage),
RequestAccepted: isAccepted,
Paused: isPaused,
VoucherTypeIdentifier: voucherResultType,
VoucherResultPtr: &vnode,
TransferId: uint64(id),
}, nil
}
// FromNet can read a network stream to deserialize a GraphSyncMessage
func FromNet(r io.Reader) (datatransfer.Message, error) {
builder := Prototype.TransferMessage.Representation().NewBuilder()
err := dagcbor.Decode(builder, r)
if err != nil {
return nil, err
}
node := builder.Build()
tresp := bindnode.Unwrap(node).(*TransferMessage1_1)
if (tresp.IsRequest && tresp.Request == nil) || (!tresp.IsRequest && tresp.Response == nil) {
return nil, xerrors.Errorf("invalid/malformed message")
}
if tresp.IsRequest {
return tresp.Request, nil
}
return tresp.Response, nil
}
// FromNet can read a network stream to deserialize a GraphSyncMessage
func FromIPLD(node datamodel.Node) (datatransfer.Message, error) {
if tn, ok := node.(schema.TypedNode); ok { // shouldn't need this if from Graphsync
node = tn.Representation()
}
builder := Prototype.TransferMessage.Representation().NewBuilder()
err := builder.AssignNode(node)
if err != nil {
return nil, err
}
tresp := bindnode.Unwrap(builder.Build()).(*TransferMessage1_1)
if (tresp.IsRequest && tresp.Request == nil) || (!tresp.IsRequest && tresp.Response == nil) {
return nil, xerrors.Errorf("invalid/malformed message")
}
if tresp.IsRequest {
return tresp.Request, nil
}
return tresp.Response, nil
}