/
handler.go
executable file
·868 lines (780 loc) · 34.2 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 chaincode
import (
"fmt"
"io"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/looplab/fsm"
"github.com/op/go-logging"
pb "github.com/openblockchain/obc-peer/protos"
"golang.org/x/net/context"
"github.com/openblockchain/obc-peer/openchain/ledger"
)
const (
createdstate = "created" //start state
establishedstate = "established" //in: CREATED, rcv: REGISTER, send: REGISTERED, INIT
initstate = "init" //in:ESTABLISHED, rcv:-, send: INIT
readystate = "ready" //in:ESTABLISHED,TRANSACTION, rcv:COMPLETED
transactionstate = "transaction" //in:READY, rcv: xact from consensus, send: TRANSACTION
busyinitstate = "busyinit" //in:INIT, rcv: PUT_STATE, DEL_STATE, INVOKE_CHAINCODE
busyxactstate = "busyxact" //in:TRANSACION, rcv: PUT_STATE, DEL_STATE, INVOKE_CHAINCODE
endstate = "end" //in:INIT,ESTABLISHED, rcv: error, terminate container
)
var chaincodeLogger = logging.MustGetLogger("chaincode")
// PeerChaincodeStream interface for stream between Peer and chaincode instance.
type PeerChaincodeStream interface {
Send(*pb.ChaincodeMessage) error
Recv() (*pb.ChaincodeMessage, error)
}
// MessageHandler interface for handling chaincode messages (common between Peer chaincode support and chaincode)
type MessageHandler interface {
HandleMessage(msg *pb.ChaincodeMessage) error
SendMessage(msg *pb.ChaincodeMessage) error
}
// Handler responsbile for managment of Peer's side of chaincode stream
type Handler struct {
sync.RWMutex
ChatStream PeerChaincodeStream
FSM *fsm.FSM
ChaincodeID *pb.ChaincodeID
chaincodeSupport *ChaincodeSupport
registered bool
readyNotify chan bool
responseNotifiers map[string]chan *pb.ChaincodeMessage
// Uuids of all in-progress state invocations
uuidMap map[string]bool
// Track which UUIDs are queries; Although the shim maintains this, it cannot be trusted.
isTransaction map[string]bool
}
func (handler *Handler) deregister() error {
if handler.registered {
handler.chaincodeSupport.deregisterHandler(handler)
}
return nil
}
func (handler *Handler) processStream() error {
defer handler.deregister()
for {
in, err := handler.ChatStream.Recv()
// Defer the deregistering of the this handler.
if err == io.EOF {
chaincodeLogger.Debug("Received EOF, ending chaincode support stream")
return err
}
if err != nil {
chaincodeLog.Error(fmt.Sprintf("Error handling chaincode support stream: %s", err))
return err
}
err = handler.HandleMessage(in)
if err != nil {
return fmt.Errorf("Error handling message, ending stream: %s", err)
}
}
}
// HandleChaincodeStream Main loop for handling the associated Chaincode stream
func HandleChaincodeStream(chaincodeSupport *ChaincodeSupport, stream pb.ChaincodeSupport_RegisterServer) error {
deadline, ok := stream.Context().Deadline()
chaincodeLogger.Debug("Current context deadline = %s, ok = %v", deadline, ok)
handler := newChaincodeSupportHandler(chaincodeSupport, stream)
return handler.processStream()
}
func newChaincodeSupportHandler(chaincodeSupport *ChaincodeSupport, peerChatStream PeerChaincodeStream) *Handler {
v := &Handler{
ChatStream: peerChatStream,
}
v.chaincodeSupport = chaincodeSupport
v.FSM = fsm.NewFSM(
createdstate,
fsm.Events{
//Send REGISTERED, then, if deploy { trigger INIT(via INIT) } else { trigger READY(via COMPLETED) }
{Name: pb.ChaincodeMessage_REGISTER.String(), Src: []string{createdstate}, Dst: establishedstate},
{Name: pb.ChaincodeMessage_INIT.String(), Src: []string{establishedstate}, Dst: initstate},
{Name: pb.ChaincodeMessage_READY.String(), Src: []string{establishedstate}, Dst: readystate},
{Name: pb.ChaincodeMessage_TRANSACTION.String(), Src: []string{readystate}, Dst: transactionstate},
{Name: pb.ChaincodeMessage_PUT_STATE.String(), Src: []string{transactionstate}, Dst: busyxactstate},
{Name: pb.ChaincodeMessage_DEL_STATE.String(), Src: []string{transactionstate}, Dst: busyxactstate},
{Name: pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), Src: []string{transactionstate}, Dst: busyxactstate},
{Name: pb.ChaincodeMessage_PUT_STATE.String(), Src: []string{initstate}, Dst: busyinitstate},
{Name: pb.ChaincodeMessage_DEL_STATE.String(), Src: []string{initstate}, Dst: busyinitstate},
{Name: pb.ChaincodeMessage_INVOKE_CHAINCODE.String(), Src: []string{initstate}, Dst: busyinitstate},
{Name: pb.ChaincodeMessage_COMPLETED.String(), Src: []string{initstate, readystate, transactionstate}, Dst: readystate},
{Name: pb.ChaincodeMessage_GET_STATE.String(), Src: []string{readystate}, Dst: readystate},
{Name: pb.ChaincodeMessage_GET_STATE.String(), Src: []string{initstate}, Dst: initstate},
{Name: pb.ChaincodeMessage_GET_STATE.String(), Src: []string{busyinitstate}, Dst: busyinitstate},
{Name: pb.ChaincodeMessage_GET_STATE.String(), Src: []string{transactionstate}, Dst: transactionstate},
{Name: pb.ChaincodeMessage_GET_STATE.String(), Src: []string{busyxactstate}, Dst: busyxactstate},
{Name: pb.ChaincodeMessage_ERROR.String(), Src: []string{initstate}, Dst: endstate},
{Name: pb.ChaincodeMessage_ERROR.String(), Src: []string{transactionstate}, Dst: readystate},
{Name: pb.ChaincodeMessage_ERROR.String(), Src: []string{busyinitstate}, Dst: initstate},
{Name: pb.ChaincodeMessage_ERROR.String(), Src: []string{busyxactstate}, Dst: transactionstate},
{Name: pb.ChaincodeMessage_RESPONSE.String(), Src: []string{busyinitstate}, Dst: initstate},
{Name: pb.ChaincodeMessage_RESPONSE.String(), Src: []string{busyxactstate}, Dst: transactionstate},
},
fsm.Callbacks{
"before_" + pb.ChaincodeMessage_REGISTER.String(): func(e *fsm.Event) { v.beforeRegisterEvent(e, v.FSM.Current()) },
"before_" + pb.ChaincodeMessage_COMPLETED.String(): func(e *fsm.Event) { v.beforeCompletedEvent(e, v.FSM.Current()) },
"before_" + pb.ChaincodeMessage_INIT.String(): func(e *fsm.Event) { v.beforeInitState(e, v.FSM.Current()) },
"after_" + pb.ChaincodeMessage_GET_STATE.String(): func(e *fsm.Event) { v.afterGetState(e, v.FSM.Current()) },
"after_" + pb.ChaincodeMessage_PUT_STATE.String(): func(e *fsm.Event) { v.afterPutState(e, v.FSM.Current()) },
"after_" + pb.ChaincodeMessage_DEL_STATE.String(): func(e *fsm.Event) { v.afterDelState(e, v.FSM.Current()) },
"after_" + pb.ChaincodeMessage_INVOKE_CHAINCODE.String(): func(e *fsm.Event) { v.afterInvokeChaincode(e, v.FSM.Current()) },
"enter_" + establishedstate: func(e *fsm.Event) { v.enterEstablishedState(e, v.FSM.Current()) },
"enter_" + initstate: func(e *fsm.Event) { v.enterInitState(e, v.FSM.Current()) },
"enter_" + readystate: func(e *fsm.Event) { v.enterReadyState(e, v.FSM.Current()) },
"enter_" + busyinitstate: func(e *fsm.Event) { v.enterBusyState(e, v.FSM.Current()) },
"enter_" + busyxactstate: func(e *fsm.Event) { v.enterBusyState(e, v.FSM.Current()) },
"enter_" + transactionstate: func(e *fsm.Event) { v.enterTransactionState(e, v.FSM.Current()) },
"enter_" + endstate: func(e *fsm.Event) { v.enterEndState(e, v.FSM.Current()) },
},
)
return v
}
func (handler *Handler) createUUIDEntry(uuid string) bool {
if handler.uuidMap == nil {
return false
}
handler.Lock()
defer handler.Unlock()
if handler.uuidMap[uuid] {
return false
}
handler.uuidMap[uuid] = true
return handler.uuidMap[uuid]
}
func (handler *Handler) deleteUUIDEntry(uuid string) {
handler.Lock()
if handler.uuidMap != nil {
delete(handler.uuidMap, uuid)
}
handler.Unlock()
}
// markIsTransaction marks a UUID as a transaction or a query; true = transaction, false = query
func (handler *Handler) markIsTransaction(uuid string, isTrans bool) bool {
if handler.isTransaction == nil {
return false
}
handler.Lock()
defer handler.Unlock()
handler.isTransaction[uuid] = isTrans
return true
}
func (handler *Handler) deleteIsTransaction(uuid string) {
handler.Lock()
if handler.isTransaction != nil {
delete(handler.isTransaction, uuid)
}
handler.Unlock()
}
func (handler *Handler) notifyDuringStartup(val bool) {
//if USER_RUNS_CC readyNotify will be nil
if handler.readyNotify != nil {
chaincodeLogger.Debug("Notifying during startup")
handler.readyNotify <- val
} else {
chaincodeLogger.Debug("nothing to notify (dev mode ?)")
}
}
// beforeRegisterEvent is invoked when chaincode tries to register.
func (handler *Handler) beforeRegisterEvent(e *fsm.Event, state string) {
chaincodeLogger.Debug("Received %s in state %s", e.Event, state)
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
chaincodeID := &pb.ChaincodeID{}
err := proto.Unmarshal(msg.Payload, chaincodeID)
if err != nil {
e.Cancel(fmt.Errorf("Error in received %s, could NOT unmarshal registration info: %s", pb.ChaincodeMessage_REGISTER, err))
return
}
// Now register with the chaincodeSupport
handler.ChaincodeID = chaincodeID
err = handler.chaincodeSupport.registerHandler(handler)
if err != nil {
e.Cancel(err)
handler.notifyDuringStartup(false)
return
}
chaincodeLogger.Debug("Got %s for chaincodeID = %s, sending back %s", e.Event, chaincodeID, pb.ChaincodeMessage_REGISTERED)
if err := handler.ChatStream.Send(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTERED}); err != nil {
e.Cancel(fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_REGISTERED, err))
handler.notifyDuringStartup(false)
return
}
}
func (handler *Handler) notify(msg *pb.ChaincodeMessage) {
handler.Lock()
defer handler.Unlock()
notfy := handler.responseNotifiers[msg.Uuid]
if notfy == nil {
chaincodeLogger.Debug("notifier Uuid:%s does not exist", msg.Uuid)
} else {
chaincodeLogger.Debug("notifying Uuid:%s", msg.Uuid)
notfy <- msg
}
}
// beforeCompletedEvent is invoked when chaincode has completed execution of init, invoke or query.
func (handler *Handler) beforeCompletedEvent(e *fsm.Event, state string) {
chaincodeLogger.Debug("Received %s in state %s", e.Event, state)
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
// Notify on channel once into READY state
chaincodeLogger.Debug("beforeCompleted uuid:%s - not in ready state will notify when in readystate", msg.Uuid)
return
}
// beforeInitState is invoked before an init message is sent to the chaincode.
func (handler *Handler) beforeInitState(e *fsm.Event, state string) {
chaincodeLogger.Debug("Before state %s.. notifying waiter that we are up", state)
handler.notifyDuringStartup(true)
}
// afterGetState handles a GET_STATE request from the chaincode.
func (handler *Handler) afterGetState(e *fsm.Event, state string) {
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
chaincodeLogger.Debug("Received %s, invoking get state from ledger", pb.ChaincodeMessage_GET_STATE)
// Query ledger for state
defer handler.handleGetState(msg)
chaincodeLogger.Debug("Exiting GET_STATE")
}
// Handles query to ledger to get state
func (handler *Handler) handleGetState(msg *pb.ChaincodeMessage) {
// The defer followed by triggering a go routine dance is needed to ensure that the previous state transition
// is completed before the next one is triggered. The previous state transition is deemed complete only when
// the afterGetState function is exited. Interesting bug fix!!
go func() {
// Check if this is the unique state request from this chaincode uuid
uniqueReq := handler.createUUIDEntry(msg.Uuid)
if !uniqueReq {
// Drop this request
chaincodeLogger.Debug("Another state request pending for this Uuid. Cannot process.")
return
}
key := string(msg.Payload)
ledgerObj, ledgerErr := ledger.GetLedger()
if ledgerErr != nil {
// Send error msg back to chaincode. GetState will not trigger event
payload := []byte(ledgerErr.Error())
chaincodeLogger.Debug("Failed to get chaincode state. Sending %s", pb.ChaincodeMessage_ERROR)
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
handler.ChatStream.Send(errMsg)
return
}
// Invoke ledger to get state
chaincodeID := handler.ChaincodeID.Name
// ToDo: Eventually, once consensus is plugged in, we need to set bool to true for ledger
res, err := ledgerObj.GetState(chaincodeID, key, false)
chaincodeLogger.Debug("Got state %s from ledger", string(res))
if err != nil {
// Send error msg back to chaincode. GetState will not trigger event
payload := []byte(err.Error())
chaincodeLogger.Debug("Failed to get chaincode state. Sending %s", pb.ChaincodeMessage_ERROR)
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
handler.ChatStream.Send(errMsg)
} else {
// Send response msg back to chaincode. GetState will not trigger event
chaincodeLogger.Debug("Got state. Sending %s", pb.ChaincodeMessage_RESPONSE)
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
responseMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Payload: res, Uuid: msg.Uuid}
handler.ChatStream.Send(responseMsg)
}
}()
}
// afterPutState handles a PUT_STATE request from the chaincode.
func (handler *Handler) afterPutState(e *fsm.Event, state string) {
_, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
chaincodeLogger.Debug("Received %s in state %s, invoking put state to ledger", pb.ChaincodeMessage_PUT_STATE, state)
// Put state into ledger handled within enterBusyState
}
// afterDelState handles a DEL_STATE request from the chaincode.
func (handler *Handler) afterDelState(e *fsm.Event, state string) {
_, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
chaincodeLogger.Debug("Received %s, invoking delete state from ledger", pb.ChaincodeMessage_DEL_STATE)
// Delete state from ledger handled within enterBusyState
}
// afterInvokeChaincode handles an INVOKE_CHAINCODE request from the chaincode.
func (handler *Handler) afterInvokeChaincode(e *fsm.Event, state string) {
_, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
chaincodeLogger.Debug("Received %s in state %s, invoking another chaincode", pb.ChaincodeMessage_INVOKE_CHAINCODE, state)
// Invoke another chaincode handled within enterBusyState
}
// Handles request to ledger to put state
func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
go func() {
chaincodeLogger.Debug("state is %s", state)
msg, _ := e.Args[0].(*pb.ChaincodeMessage)
// First check if this UUID is a transaction; error otherwise
if !handler.isTransaction[msg.Uuid] {
payload := []byte(fmt.Sprintf("Cannot handle %s in query context", msg.Type.String()))
chaincodeLogger.Debug("Cannot handle %s in query context. Sending %s", msg.Type.String(), pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
handler.ChatStream.Send(errMsg)
return
}
// Check if this is the unique request from this chaincode uuid
uniqueReq := handler.createUUIDEntry(msg.Uuid)
if !uniqueReq {
// Drop this request
chaincodeLogger.Debug("Another request pending for this Uuid. Cannot process.")
return
}
ledgerObj, ledgerErr := ledger.GetLedger()
if ledgerErr != nil {
// Send error msg back to chaincode and trigger event
payload := []byte(ledgerErr.Error())
chaincodeLogger.Debug("Failed to handle %s. Sending %s", msg.Type.String(), pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
chaincodeID := handler.ChaincodeID.Name
var err error
var res []byte
if msg.Type.String() == pb.ChaincodeMessage_PUT_STATE.String() {
putStateInfo := &pb.PutStateInfo{}
unmarshalErr := proto.Unmarshal(msg.Payload, putStateInfo)
if unmarshalErr != nil {
payload := []byte(unmarshalErr.Error())
chaincodeLogger.Debug("Unable to decipher payload. Sending %s", pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// Invoke ledger to put state
err = ledgerObj.SetState(chaincodeID, putStateInfo.Key, putStateInfo.Value)
} else if msg.Type.String() == pb.ChaincodeMessage_DEL_STATE.String() {
// Invoke ledger to delete state
key := string(msg.Payload)
err = ledgerObj.DeleteState(chaincodeID, key)
} else if msg.Type.String() == pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {
chaincodeSpec := &pb.ChaincodeSpec{}
unmarshalErr := proto.Unmarshal(msg.Payload, chaincodeSpec)
if unmarshalErr != nil {
payload := []byte(unmarshalErr.Error())
chaincodeLogger.Debug("Unable to decipher payload. Sending %s", pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// Get the chaincodeID to invoke
newChaincodeID := chaincodeSpec.ChaincodeID.Name
// Create the transaction object
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: chaincodeSpec}
transaction, _ := pb.NewChaincodeExecute(chaincodeInvocationSpec, msg.Uuid, pb.Transaction_CHAINCODE_EXECUTE)
// Launch the new chaincode if not already running
_, chaincodeInput, launchErr := handler.chaincodeSupport.LaunchChaincode(context.Background(), transaction)
if launchErr != nil {
payload := []byte(launchErr.Error())
chaincodeLogger.Debug("Failed to launch invoked chaincode. Sending %s", pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// TODO: Need to handle timeout correctly
timeout := time.Duration(30000) * time.Millisecond
ccMsg, _ := createTransactionMessage(transaction.Uuid, chaincodeInput)
// Execute the chaincode
response, execErr := handler.chaincodeSupport.Execute(context.Background(), newChaincodeID, ccMsg, timeout)
err = execErr
res = response.Payload
}
if err != nil {
// Send error msg back to chaincode and trigger event
payload := []byte(err.Error())
chaincodeLogger.Debug("Failed to handle %s. Sending %s", msg.Type.String(), pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_ERROR.String(), errMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event ERROR: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// Send response msg back to chaincode.
chaincodeLogger.Debug("Completed %s. Sending %s", msg.Type.String(), pb.ChaincodeMessage_RESPONSE)
responseMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Payload: res, Uuid: msg.Uuid}
// Send FSM event to trigger state change
eventErr := handler.FSM.Event(pb.ChaincodeMessage_RESPONSE.String(), responseMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event RESPONSE: %s", eventErr)
}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(responseMsg)
}()
}
func (handler *Handler) enterEstablishedState(e *fsm.Event, state string) {
handler.notifyDuringStartup(true)
chaincodeLogger.Debug("Entered state; notified %s", state)
}
func (handler *Handler) enterInitState(e *fsm.Event, state string) {
chaincodeLogger.Debug("Entered state %s", state)
ccMsg, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
//very first time entering init state from established, send message to chaincode
if ccMsg.Type == pb.ChaincodeMessage_INIT {
// Mark isTransaction to allow put/del state and invoke other chaincodes
handler.markIsTransaction(ccMsg.Uuid, true)
if err := handler.ChatStream.Send(ccMsg); err != nil {
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(fmt.Sprintf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)), Uuid: ccMsg.Uuid}
handler.notify(errMsg)
}
}
}
func (handler *Handler) enterReadyState(e *fsm.Event, state string) {
// Now notify
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
handler.deleteIsTransaction(msg.Uuid)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
handler.notify(msg)
chaincodeLogger.Debug("Entered state %s", state)
}
func (handler *Handler) enterTransactionState(e *fsm.Event, state string) {
chaincodeLogger.Debug("Entered state %s", state)
}
func (handler *Handler) enterEndState(e *fsm.Event, state string) {
defer handler.deregister()
// Now notify
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
handler.deleteIsTransaction(msg.Uuid)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
return
}
handler.notify(msg)
chaincodeLogger.Debug("Entered state %s", state)
e.Cancel(fmt.Errorf("Entered end state"))
}
//if initArgs is set (should be for "deploy" only) move to Init
//else move to ready
func (handler *Handler) initOrReady(uuid string, f *string, initArgs []string) (chan *pb.ChaincodeMessage, error) {
var event string
var notfy chan *pb.ChaincodeMessage
var ccMsg *pb.ChaincodeMessage
if f != nil || initArgs != nil {
chaincodeLogger.Debug("sending INIT")
var f2 string
if f != nil {
f2 = *f
}
funcArgsMsg := &pb.ChaincodeInput{Function: f2, Args: initArgs}
payload, err := proto.Marshal(funcArgsMsg)
if err != nil {
return nil, err
}
notfy, err = handler.createNotifier(uuid)
if err != nil {
return nil, err
}
ccMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_INIT, Payload: payload, Uuid: uuid}
/** we need to send when actually in init state (see enterInitState)
if err = handler.ChatStream.Send(ccMsg); err != nil {
notfy <- &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(fmt.Sprintf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)), Uuid: uuid}
return notfy, fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)
}
*/
event = pb.ChaincodeMessage_INIT.String()
} else {
chaincodeLogger.Debug("sending READY")
var payload []byte
ccMsg = &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_READY, Payload: payload, Uuid: uuid}
if err := handler.ChatStream.Send(ccMsg); err != nil {
notfy <- &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(fmt.Sprintf("Error sending %s: %s", pb.ChaincodeMessage_READY, err)), Uuid: uuid}
return notfy, fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_READY, err)
}
event = pb.ChaincodeMessage_READY.String()
}
eventErr := handler.FSM.Event(event, ccMsg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event %s : %s\n", ccMsg.Type.String(), eventErr)
}
return notfy, eventErr
}
// Handles request to query another chaincode
func (handler *Handler) handleQueryChaincode(msg *pb.ChaincodeMessage) {
go func() {
// Check if this is the unique request from this chaincode uuid
uniqueReq := handler.createUUIDEntry(msg.Uuid)
if !uniqueReq {
// Drop this request
chaincodeLogger.Debug("Another request pending for this Uuid. Cannot process.")
return
}
chaincodeSpec := &pb.ChaincodeSpec{}
unmarshalErr := proto.Unmarshal(msg.Payload, chaincodeSpec)
if unmarshalErr != nil {
payload := []byte(unmarshalErr.Error())
chaincodeLogger.Debug("Unable to decipher payload. Sending %s", pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// Get the chaincodeID to invoke
newChaincodeID := chaincodeSpec.ChaincodeID.Name
// Create the transaction object
chaincodeInvocationSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: chaincodeSpec}
transaction, _ := pb.NewChaincodeExecute(chaincodeInvocationSpec, msg.Uuid, pb.Transaction_CHAINCODE_QUERY)
// Launch the new chaincode if not already running
_, chaincodeInput, launchErr := handler.chaincodeSupport.LaunchChaincode(context.Background(), transaction)
if launchErr != nil {
payload := []byte(launchErr.Error())
chaincodeLogger.Debug("Failed to launch invoked chaincode. Sending %s", pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// TODO: Need to handle timeout correctly
timeout := time.Duration(30000) * time.Millisecond
ccMsg, _ := createQueryMessage(transaction.Uuid, chaincodeInput)
// Query the chaincode
response, execErr := handler.chaincodeSupport.Execute(context.Background(), newChaincodeID, ccMsg, timeout)
if execErr != nil {
// Send error msg back to chaincode and trigger event
payload := []byte(execErr.Error())
chaincodeLogger.Debug("Failed to handle %s. Sending %s", msg.Type.String(), pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(errMsg)
return
}
// Send response msg back to chaincode.
chaincodeLogger.Debug("Completed %s. Sending %s", msg.Type.String(), pb.ChaincodeMessage_RESPONSE)
responseMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_RESPONSE, Payload: response.Payload, Uuid: msg.Uuid}
// Remove uuid from current set
handler.deleteUUIDEntry(msg.Uuid)
handler.ChatStream.Send(responseMsg)
}()
}
// HandleMessage implementation of MessageHandler interface. Peer's handling of Chaincode messages.
func (handler *Handler) HandleMessage(msg *pb.ChaincodeMessage) error {
chaincodeLogger.Debug("Handling ChaincodeMessage of type: %s in state %s", msg.Type, handler.FSM.Current())
//QUERY_COMPLETED message can happen ONLY for Transaction_QUERY (stateless)
if msg.Type == pb.ChaincodeMessage_QUERY_COMPLETED {
chaincodeLogger.Debug("HandleMessage- QUERY_COMPLETED for uuid:%s. Notify", msg.Uuid)
handler.deleteIsTransaction(msg.Uuid)
handler.notify(msg)
return nil
} else if msg.Type == pb.ChaincodeMessage_QUERY_ERROR {
chaincodeLogger.Debug("HandleMessage- QUERY_ERROR (%s) for uuid:%s. Notify", string(msg.Payload), msg.Uuid)
handler.deleteIsTransaction(msg.Uuid)
handler.notify(msg)
return nil
} else if msg.Type == pb.ChaincodeMessage_INVOKE_QUERY {
// Received request to query another chaincode from shim
chaincodeLogger.Debug("HandleMessage- Received request to query another chaincode")
handler.handleQueryChaincode(msg)
return nil
}
if handler.FSM.Cannot(msg.Type.String()) {
// Check if this is a request from validator in query context
if msg.Type.String() == pb.ChaincodeMessage_PUT_STATE.String() || msg.Type.String() == pb.ChaincodeMessage_DEL_STATE.String() || msg.Type.String() == pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {
// Check if this UUID is a transaction
if !handler.isTransaction[msg.Uuid] {
payload := []byte(fmt.Sprintf("Cannot handle %s in query context", msg.Type.String()))
chaincodeLogger.Debug("Cannot handle %s in query context. Sending %s", msg.Type.String(), pb.ChaincodeMessage_ERROR)
errMsg := &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: payload, Uuid: msg.Uuid}
handler.ChatStream.Send(errMsg)
return fmt.Errorf("Cannot handle %s in query context", msg.Type.String())
}
}
// Other errors
return fmt.Errorf("Chaincode handler validator FSM cannot handle message (%s) with payload size (%d) while in state: %s", msg.Type.String(), len(msg.Payload), handler.FSM.Current())
}
chaincodeLogger.Debug("Received message %s from shim", msg.Type.String())
if msg.Type.String() == pb.ChaincodeMessage_ERROR.String() {
chaincodeLogger.Debug("Got error: %s", string(msg.Payload))
}
eventErr := handler.FSM.Event(msg.Type.String(), msg)
filteredErr := filterError(eventErr)
if filteredErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event %s: %s", msg.Type.String(), filteredErr)
}
return filteredErr
}
// Filter the Errors to allow NoTransitionError and CanceledError to not propogate for cases where embedded Err == nil
func filterError(errFromFSMEvent error) error {
if errFromFSMEvent != nil {
if noTransitionErr, ok := errFromFSMEvent.(*fsm.NoTransitionError); ok {
if noTransitionErr.Err != nil {
// Only allow NoTransitionError's, all others are considered true error.
return errFromFSMEvent
}
chaincodeLogger.Debug("Ignoring NoTransitionError: %s", noTransitionErr)
}
if canceledErr, ok := errFromFSMEvent.(*fsm.CanceledError); ok {
if canceledErr.Err != nil {
// Only allow NoTransitionError's, all others are considered true error.
return canceledErr
//t.Error("expected only 'NoTransitionError'")
}
chaincodeLogger.Debug("Ignoring CanceledError: %s", canceledErr)
}
}
return nil
}
func (handler *Handler) deleteNotifier(uuid string) {
handler.Lock()
if handler.responseNotifiers != nil {
delete(handler.responseNotifiers, uuid)
}
handler.Unlock()
}
func (handler *Handler) createNotifier(uuid string) (chan *pb.ChaincodeMessage, error) {
if handler.responseNotifiers == nil {
return nil, fmt.Errorf("cannot create notifier for Uuid:%s", uuid)
}
handler.Lock()
defer handler.Unlock()
if handler.responseNotifiers[uuid] != nil {
return nil, fmt.Errorf("Uuid:%s exists", uuid)
}
handler.responseNotifiers[uuid] = make(chan *pb.ChaincodeMessage, 1)
return handler.responseNotifiers[uuid], nil
}
func (handler *Handler) sendExecuteMessage(msg *pb.ChaincodeMessage) (chan *pb.ChaincodeMessage, error) {
notfy, err := handler.createNotifier(msg.Uuid)
if err != nil {
return nil, err
}
// Mark UUID as either transaction or query
chaincodeLogger.Debug("Inside sendExecuteMessage. Message %s, Uuid %s", msg.Type.String(), msg.Uuid)
if msg.Type.String() == pb.ChaincodeMessage_QUERY.String() {
handler.markIsTransaction(msg.Uuid, false)
} else {
handler.markIsTransaction(msg.Uuid, true)
}
// Trigger FSM event if it is a transaction
if msg.Type.String() == pb.ChaincodeMessage_TRANSACTION.String() {
eventErr := handler.FSM.Event(msg.Type.String(), msg)
if eventErr != nil {
chaincodeLogger.Debug("Failed to trigger FSM event TRANSACTION: %s", eventErr)
}
}
// Send the message to shim
if err := handler.ChatStream.Send(msg); err != nil {
handler.deleteNotifier(msg.Uuid)
return nil, fmt.Errorf("SendMessage error sending %s(%s)", msg.Uuid, err)
}
return notfy, nil
}
func (handler *Handler) isRunning() bool {
switch handler.FSM.Current() {
case createdstate:
fallthrough
case establishedstate:
fallthrough
case initstate:
return false
default:
return true
}
}
/****************
func (handler *Handler) initEvent() (chan *pb.ChaincodeMessage, error) {
if handler.responseNotifiers == nil {
return nil,fmt.Errorf("SendMessage called before registration for Uuid:%s", msg.Uuid)
}
var notfy chan *pb.ChaincodeMessage
handler.Lock()
if handler.responseNotifiers[msg.Uuid] != nil {
handler.Unlock()
return nil, fmt.Errorf("SendMessage Uuid:%s exists", msg.Uuid)
}
//note the explicit use of buffer 1. We won't block if the receiver times outi and does not wait
//for our response
handler.responseNotifiers[msg.Uuid] = make(chan *pb.ChaincodeMessage, 1)
handler.Unlock()
if err := c.ChatStream.Send(msg); err != nil {
deleteNotifier(msg.Uuid)
return nil, fmt.Errorf("SendMessage error sending %s(%s)", msg.Uuid, err)
}
return notfy, nil
}
*******************/