/
request_response.go
543 lines (446 loc) · 19.4 KB
/
request_response.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Copyright (c) 2019 - for information on the respective copyright owner
// see the NOTICE file and/or the repository at
// https://github.com/direct-state-transfer/dst-go
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package channel
import (
"bytes"
"fmt"
"github.com/direct-state-transfer/dst-go/channel/primitives"
"github.com/direct-state-transfer/dst-go/ethereum/contract"
"github.com/direct-state-transfer/dst-go/ethereum/types"
"github.com/direct-state-transfer/dst-go/identity"
)
// IdentityRequest sends an identity request and waits for identity response from the peer node.
// If response is successfully received, it returns the peer id in the response message.
func (inst *Instance) IdentityRequest(selfID identity.OffChainID) (peerID identity.OffChainID, err error) {
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgIdentityRequest,
Message: primitives.JSONMsgIdentity{ID: selfID},
}
err = inst.Write(idRequestMsg)
if err != nil {
return peerID, err
}
response, err := inst.Read()
if err != nil {
return peerID, err
}
if response.MessageID != primitives.MsgIdentityResponse {
errMsg := ("Invalid response received for id request")
return peerID, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgIdentity)
peerID = msg.ID
return peerID, nil
}
// IdentityRead reads the identity request sent by the peer node and returns the peer id in the message.
func (inst *Instance) IdentityRead() (peerID identity.OffChainID, err error) {
msg, err := inst.Read()
if err != nil {
errMsg := "Error waiting for id request - connection dropped -" + err.Error()
return peerID, fmt.Errorf(errMsg)
}
if msg.MessageID != primitives.MsgIdentityRequest {
errMsg := "First message is not id request"
return peerID, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
idRequestMsg, _ := msg.Message.(primitives.JSONMsgIdentity)
peerID = idRequestMsg.ID
return peerID, nil
}
// IdentityRespond sends an identity response to the peer node with self id in the message.
func (inst *Instance) IdentityRespond(selfID identity.OffChainID) (err error) {
selfIDMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgIdentityResponse,
Message: primitives.JSONMsgIdentity{ID: selfID},
}
err = inst.Write(selfIDMsg)
if err != nil {
errMsg := "Error responding to id request" + err.Error()
return fmt.Errorf(errMsg)
}
return nil
}
// NewChannelRequest sends an new channel request and waits for new channel response from the peer node.
// If response is successfully received, it returns the acceptance status and reason in the response message.
func (inst *Instance) NewChannelRequest(msgProtocolVersion string, contractStoreVersion []byte) (accept primitives.MessageStatus, reason string, err error) {
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgNewChannelRequest,
Message: primitives.JSONMsgNewChannel{
MsgProtocolVersion: msgProtocolVersion,
ContractStoreVersion: contractStoreVersion,
Status: primitives.MessageStatusRequire,
},
}
logger.Debug("Requesting new channel with the other node")
err = inst.Write(idRequestMsg)
if err != nil {
return primitives.MessageStatusUnknown, "", err
}
response, err := inst.Read()
if err != nil {
return primitives.MessageStatusUnknown, "", err
}
if response.MessageID != primitives.MsgNewChannelResponse {
errMsg := ("Invalid response received for id request")
return primitives.MessageStatusUnknown, "", fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgNewChannel)
if !bytes.Equal(contractStoreVersion, msg.ContractStoreVersion) {
errMsg := ("Contract store version modified by peer")
return primitives.MessageStatusUnknown, "", fmt.Errorf(errMsg)
}
if msgProtocolVersion != msg.MsgProtocolVersion {
errMsg := ("Message protocol version modified by peer")
return primitives.MessageStatusUnknown, "", fmt.Errorf(errMsg)
}
accept = msg.Status
reason = msg.Reason
return accept, reason, nil
}
// NewChannelRead reads the new channel request sent by the peer node and returns the message protocol version and contract store version in the message.
func (inst *Instance) NewChannelRead() (msgProtocolVersion string, contractStoreVersion []byte, err error) {
logger.Debug("Reading new channel request from other node")
response, err := inst.Read()
if err != nil {
return "", contractStoreVersion, err
}
if response.MessageID != primitives.MsgNewChannelRequest {
errMsg := ("Invalid response received for id request")
return "", contractStoreVersion, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgNewChannel)
if !primitives.ContainsStatus(primitives.RequestStatusList, msg.Status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", msg.Status, primitives.RequestStatusList)
return "", contractStoreVersion, fmt.Errorf(errMsg)
}
return msg.MsgProtocolVersion, msg.ContractStoreVersion, nil
}
// NewChannelRespond sends an new channel response to the peer node with acceptance status in the message.
func (inst *Instance) NewChannelRespond(msgProtocolVersion string, contractStoreVersion []byte, accept primitives.MessageStatus, reason string) (err error) {
if !primitives.ContainsStatus(primitives.ResponseStatusList, accept) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", accept, primitives.ResponseStatusList)
return fmt.Errorf(errMsg)
}
responsePkt := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgNewChannelResponse,
Message: primitives.JSONMsgNewChannel{
MsgProtocolVersion: msgProtocolVersion,
ContractStoreVersion: contractStoreVersion,
Status: accept,
Reason: reason,
},
}
logger.Debug("Sending response to new channel request")
err = inst.Write(responsePkt)
return err
}
// SessionIDRequest sends an sessiod id request with partial session id and waits for sessiod id response from the peer node.
// If response is successfully received, it returns the complete sid and acceptance status in the response message.
func (inst *Instance) SessionIDRequest(sid primitives.SessionID) (gotSid primitives.SessionID, status primitives.MessageStatus, err error) {
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgSessionIDRequest,
Message: primitives.JSONMsgSessionID{
Sid: sid,
Status: primitives.MessageStatusRequire,
},
}
logger.Debug("Requesting session ID")
err = inst.Write(idRequestMsg)
if err != nil {
return gotSid, "", err
}
response, err := inst.Read()
if err != nil {
return gotSid, "", err
}
if response.MessageID != primitives.MsgSessionIDResponse {
errMsg := ("Invalid response received for id request")
return gotSid, "", fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgSessionID)
if !sid.EqualSender(msg.Sid) {
errMsg := ("Sid sender components modified by peer")
return gotSid, "", fmt.Errorf(errMsg)
}
return msg.Sid, msg.Status, nil
}
// SessionIDRead reads the session id request sent by the peer node and returns the session id in the message.
func (inst *Instance) SessionIDRead() (sid primitives.SessionID, err error) {
logger.Debug("Reading session ID request")
response, err := inst.Read()
if err != nil {
return sid, err
}
if response.MessageID != primitives.MsgSessionIDRequest {
errMsg := ("Invalid response received instead of session id request")
return sid, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgSessionID)
if !primitives.ContainsStatus(primitives.RequestStatusList, msg.Status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", msg.Status, primitives.RequestStatusList)
return sid, fmt.Errorf(errMsg)
}
return msg.Sid, nil
}
// SessionIDRespond sends an session id response to the peer node with complete session id (optional) and acceptance status in the message.
func (inst *Instance) SessionIDRespond(sid primitives.SessionID, status primitives.MessageStatus) (err error) {
if !primitives.ContainsStatus(primitives.ResponseStatusList, status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", status, primitives.ResponseStatusList)
return fmt.Errorf(errMsg)
}
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgSessionIDResponse,
Message: primitives.JSONMsgSessionID{
Sid: sid,
Status: status,
},
}
logger.Debug("Responding to session ID request")
err = inst.Write(idRequestMsg)
return err
}
// ContractAddrRequest sends a contract id request with details of deployed contract and waits for contract address response from the peer node.
// If response is successfully received, it returns the acceptance status in the response message.
func (inst *Instance) ContractAddrRequest(addr types.Address, id contract.Handler) (status primitives.MessageStatus, err error) {
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgContractAddrRequest,
Message: primitives.JSONMsgContractAddr{
Addr: addr,
ContractType: id,
Status: primitives.MessageStatusRequire,
},
}
err = inst.Write(idRequestMsg)
if err != nil {
return "", err
}
response, err := inst.Read()
if err != nil {
return "", err
}
if response.MessageID != primitives.MsgContractAddrResponse {
errMsg := ("Invalid response received for id request")
return "", fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgContractAddr)
if !(id.Equal(msg.ContractType) && bytes.Equal(msg.Addr.Bytes(), addr.Bytes())) {
errMsg := ("Contract handler modified by peer")
return "", fmt.Errorf(errMsg)
}
return msg.Status, nil
}
// ContractAddrRead reads the contract address request sent by the peer node and returns the contract address and handler in the message.
func (inst *Instance) ContractAddrRead() (addr types.Address, id contract.Handler, err error) {
logger.Debug("Reading Contract Address request")
response, err := inst.Read()
if err != nil {
return addr, id, err
}
if response.MessageID != primitives.MsgContractAddrRequest {
errMsg := ("Invalid response received for id request")
return addr, id, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgContractAddr)
if !primitives.ContainsStatus(primitives.RequestStatusList, msg.Status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", msg.Status, primitives.RequestStatusList)
return addr, id, fmt.Errorf(errMsg)
}
return msg.Addr, msg.ContractType, nil
}
// ContractAddrRespond sends an contract address response to the peer node with acceptance status in the message.
func (inst *Instance) ContractAddrRespond(addr types.Address, id contract.Handler, status primitives.MessageStatus) (err error) {
if !primitives.ContainsStatus(primitives.ResponseStatusList, status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", status, primitives.ResponseStatusList)
return fmt.Errorf(errMsg)
}
idRequestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgContractAddrResponse,
Message: primitives.JSONMsgContractAddr{
Addr: addr,
ContractType: id,
Status: status,
},
}
logger.Debug("Responding to Contract Address request")
err = inst.Write(idRequestMsg)
return err
}
// NewMSCBaseStateRequest sends a new msc base request with partial signature and waits for msc base state response from the peer node.
// If response is successfully received, it returns the fully signed msc base state and acceptance status in the response message.
func (inst *Instance) NewMSCBaseStateRequest(newSignedState primitives.MSCBaseStateSigned) (responseState primitives.MSCBaseStateSigned, status primitives.MessageStatus, err error) {
requestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgMSCBaseStateRequest,
Message: primitives.JSONMsgMSCBaseState{
SignedStateVal: newSignedState,
Status: primitives.MessageStatusRequire,
},
}
logger.Debug("Requesting new MSC base state")
err = inst.Write(requestMsg)
if err != nil {
return responseState, "", err
}
response, err := inst.Read()
if err != nil {
return responseState, "", err
}
if response.MessageID != primitives.MsgMSCBaseStateResponse {
errMsg := ("Invalid response received for msc base state request")
return responseState, "", fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgMSCBaseState)
if !newSignedState.MSContractBaseState.Equal(msg.SignedStateVal.MSContractBaseState) {
errMsg := ("MSContract base state modified by peer")
return responseState, "", fmt.Errorf(errMsg)
}
return msg.SignedStateVal, msg.Status, nil
}
// NewMSCBaseStateRead reads the new msc base state request sent by the peer node and returns the msc base state in the message.
func (inst *Instance) NewMSCBaseStateRead() (state primitives.MSCBaseStateSigned, err error) {
logger.Debug("Reading new MSC base state request")
response, err := inst.Read()
if err != nil {
return state, err
}
if response.MessageID != primitives.MsgMSCBaseStateRequest {
errMsg := ("Invalid response received for msc base state request")
return state, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgMSCBaseState)
if !primitives.ContainsStatus(primitives.RequestStatusList, msg.Status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", msg.Status, primitives.RequestStatusList)
return state, fmt.Errorf(errMsg)
}
return msg.SignedStateVal, nil
}
// NewMSCBaseStateRespond sends an msc base state response to the peer node with fully signed state (optional) and acceptance status in the message.
func (inst *Instance) NewMSCBaseStateRespond(state primitives.MSCBaseStateSigned, status primitives.MessageStatus) (err error) {
if !primitives.ContainsStatus(primitives.ResponseStatusList, status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", status, primitives.ResponseStatusList)
return fmt.Errorf(errMsg)
}
response := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgMSCBaseStateResponse,
Message: primitives.JSONMsgMSCBaseState{
SignedStateVal: state,
Status: status,
},
}
logger.Debug("Responding to new MSC base state request")
err = inst.Write(response)
return err
}
// NewVPCStateRequest sends a new vpc request with partial signature and waits for vpc state response from the peer node.
// If response is successfully received, it returns the fully signed vpc state and acceptance status in the response message.
func (inst *Instance) NewVPCStateRequest(newStateSigned primitives.VPCStateSigned) (responseState primitives.VPCStateSigned, status primitives.MessageStatus, err error) {
requestMsg := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgVPCStateRequest,
Message: primitives.JSONMsgVPCState{
SignedStateVal: newStateSigned,
Status: primitives.MessageStatusRequire,
},
}
logger.Debug("Requesting new VPC state")
err = inst.Write(requestMsg)
if err != nil {
return responseState, "", err
}
response, err := inst.Read()
if err != nil {
return responseState, "", err
}
if response.MessageID != primitives.MsgVPCStateResponse {
errMsg := ("Invalid response received for vpc state request")
return responseState, "", fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgVPCState)
if !newStateSigned.VPCState.Equal(msg.SignedStateVal.VPCState) {
errMsg := ("VPC state modified by peer")
return responseState, "", fmt.Errorf(errMsg)
}
return msg.SignedStateVal, msg.Status, nil
}
// NewVPCStateRead reads the new vpc state request sent by the peer node and returns the vpc state in the message.
func (inst *Instance) NewVPCStateRead() (state primitives.VPCStateSigned, err error) {
logger.Debug("Reading new VPC state request")
response, err := inst.Read()
if err != nil {
return state, err
}
if response.MessageID != primitives.MsgVPCStateRequest {
errMsg := ("Invalid response received for vpc state request")
return state, fmt.Errorf(errMsg)
}
//This error should always be nil, since unmarshal function should ensure that,
//DataType of Message field is in agreement with value of MessageID field.
msg, _ := response.Message.(primitives.JSONMsgVPCState)
if !primitives.ContainsStatus(primitives.RequestStatusList, msg.Status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", msg.Status, primitives.RequestStatusList)
return state, fmt.Errorf(errMsg)
}
return msg.SignedStateVal, nil
}
// NewVPCStateRespond sends an vpc state response to the peer node with fully signed state (optional) and acceptance status in the message.
func (inst *Instance) NewVPCStateRespond(state primitives.VPCStateSigned, status primitives.MessageStatus) (err error) {
if !primitives.ContainsStatus(primitives.ResponseStatusList, status) {
errMsg := fmt.Sprintf("Invalid status received - %v. Use %v ", status, primitives.ResponseStatusList)
return fmt.Errorf(errMsg)
}
response := primitives.ChMsgPkt{
Version: primitives.Version,
MessageID: primitives.MsgVPCStateResponse,
Message: primitives.JSONMsgVPCState{
SignedStateVal: state,
Status: status,
},
}
logger.Debug("Responding to new VPC state request")
err = inst.Write(response)
return err
}