-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
endorser_test.go
1412 lines (1238 loc) · 47.1 KB
/
endorser_test.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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package endorser_test
import (
"context"
"fmt"
"sort"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-lib-go/common/metrics/metricsfakes"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
mspproto "github.com/hyperledger/fabric-protos-go/msp"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
"github.com/hyperledger/fabric/core/endorser"
"github.com/hyperledger/fabric/core/endorser/fake"
"github.com/hyperledger/fabric/core/ledger"
ledgermock "github.com/hyperledger/fabric/core/ledger/mock"
"github.com/hyperledger/fabric/protoutil"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
)
type CcInterest pb.ChaincodeInterest
func (a CcInterest) Len() int { return len(a.Chaincodes) }
func (a CcInterest) Swap(i, j int) {
a.Chaincodes[i], a.Chaincodes[j] = a.Chaincodes[j], a.Chaincodes[i]
}
func (a CcInterest) Less(i, j int) bool {
ai := a.Chaincodes[i]
aj := a.Chaincodes[j]
if ai.Name != aj.Name {
return ai.Name < aj.Name
}
if len(ai.CollectionNames) != len(aj.CollectionNames) {
return len(ai.CollectionNames) < len(aj.CollectionNames)
}
for ii := range ai.CollectionNames {
if ai.CollectionNames[ii] != aj.CollectionNames[ii] {
return ai.CollectionNames[ii] < aj.CollectionNames[ii]
}
}
return false
}
var _ = Describe("Endorser", func() {
var (
fakeProposalDuration *metricsfakes.Histogram
fakeProposalsReceived *metricsfakes.Counter
fakeSuccessfulProposals *metricsfakes.Counter
fakeProposalValidationFailed *metricsfakes.Counter
fakeProposalACLCheckFailed *metricsfakes.Counter
fakeInitFailed *metricsfakes.Counter
fakeEndorsementsFailed *metricsfakes.Counter
fakeDuplicateTxsFailure *metricsfakes.Counter
fakeSimulateFailure *metricsfakes.Counter
fakeLocalIdentity *fake.Identity
fakeLocalMSPIdentityDeserializer *fake.IdentityDeserializer
fakeChannelIdentity *fake.Identity
fakeChannelMSPIdentityDeserializer *fake.IdentityDeserializer
fakeChannelFetcher *fake.ChannelFetcher
fakePrivateDataDistributor *fake.PrivateDataDistributor
fakeSupport *fake.Support
fakeTxSimulator *fake.TxSimulator
fakeHistoryQueryExecutor *fake.HistoryQueryExecutor
signedProposal *pb.SignedProposal
channelID string
chaincodeName string
chaincodeResponse *pb.Response
chaincodeEvent *pb.ChaincodeEvent
chaincodeInput *pb.ChaincodeInput
e *endorser.Endorser
)
BeforeEach(func() {
fakeProposalDuration = &metricsfakes.Histogram{}
fakeProposalDuration.WithReturns(fakeProposalDuration)
fakeProposalACLCheckFailed = &metricsfakes.Counter{}
fakeProposalACLCheckFailed.WithReturns(fakeProposalACLCheckFailed)
fakeInitFailed = &metricsfakes.Counter{}
fakeInitFailed.WithReturns(fakeInitFailed)
fakeEndorsementsFailed = &metricsfakes.Counter{}
fakeEndorsementsFailed.WithReturns(fakeEndorsementsFailed)
fakeDuplicateTxsFailure = &metricsfakes.Counter{}
fakeDuplicateTxsFailure.WithReturns(fakeDuplicateTxsFailure)
fakeProposalsReceived = &metricsfakes.Counter{}
fakeSuccessfulProposals = &metricsfakes.Counter{}
fakeProposalValidationFailed = &metricsfakes.Counter{}
fakeSimulateFailure = &metricsfakes.Counter{}
fakeSimulateFailure.WithReturns(fakeSimulateFailure)
fakeLocalIdentity = &fake.Identity{}
fakeLocalMSPIdentityDeserializer = &fake.IdentityDeserializer{}
fakeLocalMSPIdentityDeserializer.DeserializeIdentityReturns(fakeLocalIdentity, nil)
fakeChannelIdentity = &fake.Identity{}
fakeChannelMSPIdentityDeserializer = &fake.IdentityDeserializer{}
fakeChannelMSPIdentityDeserializer.DeserializeIdentityReturns(fakeChannelIdentity, nil)
fakeChannelFetcher = &fake.ChannelFetcher{}
fakeChannelFetcher.ChannelReturns(&endorser.Channel{
IdentityDeserializer: fakeChannelMSPIdentityDeserializer,
})
fakePrivateDataDistributor = &fake.PrivateDataDistributor{}
channelID = "channel-id"
chaincodeName = "chaincode-name"
chaincodeInput = &pb.ChaincodeInput{
Args: [][]byte{[]byte("arg1"), []byte("arg2"), []byte("arg3")},
}
chaincodeResponse = &pb.Response{
Status: 200,
Payload: []byte("response-payload"),
}
chaincodeEvent = &pb.ChaincodeEvent{
ChaincodeId: "chaincode-id",
TxId: "event-txid",
EventName: "event-name",
Payload: []byte("event-payload"),
}
fakeSupport = &fake.Support{}
fakeSupport.ExecuteReturns(
chaincodeResponse,
chaincodeEvent,
nil,
)
fakeSupport.ChaincodeEndorsementInfoReturns(&lifecycle.ChaincodeEndorsementInfo{
Version: "chaincode-definition-version",
EndorsementPlugin: "plugin-name",
}, nil)
fakeSupport.GetLedgerHeightReturns(7, nil)
fakeSupport.EndorseWithPluginReturns(
&pb.Endorsement{
Endorser: []byte("endorser-identity"),
Signature: []byte("endorser-signature"),
},
[]byte("endorser-modified-payload"),
nil,
)
fakeTxSimulator = &fake.TxSimulator{}
fakeTxSimulator.GetTxSimulationResultsReturns(
&ledger.TxSimulationResults{
PubSimulationResults: &rwset.TxReadWriteSet{},
PvtSimulationResults: &rwset.TxPvtReadWriteSet{},
},
nil,
)
fakeHistoryQueryExecutor = &fake.HistoryQueryExecutor{}
fakeSupport.GetHistoryQueryExecutorReturns(fakeHistoryQueryExecutor, nil)
fakeSupport.GetTxSimulatorReturns(fakeTxSimulator, nil)
fakeSupport.GetTransactionByIDReturns(nil, fmt.Errorf("txid-error"))
e = &endorser.Endorser{
LocalMSP: fakeLocalMSPIdentityDeserializer,
PrivateDataDistributor: fakePrivateDataDistributor,
Metrics: &endorser.Metrics{
ProposalDuration: fakeProposalDuration,
ProposalsReceived: fakeProposalsReceived,
SuccessfulProposals: fakeSuccessfulProposals,
ProposalValidationFailed: fakeProposalValidationFailed,
ProposalACLCheckFailed: fakeProposalACLCheckFailed,
InitFailed: fakeInitFailed,
EndorsementsFailed: fakeEndorsementsFailed,
DuplicateTxsFailure: fakeDuplicateTxsFailure,
SimulationFailure: fakeSimulateFailure,
},
Support: fakeSupport,
ChannelFetcher: fakeChannelFetcher,
}
})
JustBeforeEach(func() {
signedProposal = &pb.SignedProposal{
ProposalBytes: protoutil.MarshalOrPanic(&pb.Proposal{
Header: protoutil.MarshalOrPanic(&cb.Header{
ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{
Type: int32(cb.HeaderType_ENDORSER_TRANSACTION),
ChannelId: channelID,
Extension: protoutil.MarshalOrPanic(&pb.ChaincodeHeaderExtension{
ChaincodeId: &pb.ChaincodeID{
Name: chaincodeName,
},
}),
TxId: "6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015",
}),
SignatureHeader: protoutil.MarshalOrPanic(&cb.SignatureHeader{
Creator: protoutil.MarshalOrPanic(&mspproto.SerializedIdentity{
Mspid: "msp-id",
}),
Nonce: []byte("nonce"),
}),
}),
Payload: protoutil.MarshalOrPanic(&pb.ChaincodeProposalPayload{
Input: protoutil.MarshalOrPanic(&pb.ChaincodeInvocationSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
Input: chaincodeInput,
},
}),
}),
}),
Signature: []byte("signature"),
}
})
It("successfully endorses the proposal", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Endorsement).To(Equal(&pb.Endorsement{
Endorser: []byte("endorser-identity"),
Signature: []byte("endorser-signature"),
}))
Expect(proposalResponse.Timestamp).To(BeNil())
Expect(proposalResponse.Version).To(Equal(int32(1)))
Expect(proposalResponse.Payload).To(Equal([]byte("endorser-modified-payload")))
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 200,
Payload: []byte("response-payload"),
})).To(BeTrue())
Expect(fakeSupport.EndorseWithPluginCallCount()).To(Equal(1))
pluginName, cid, propRespPayloadBytes, sp := fakeSupport.EndorseWithPluginArgsForCall(0)
Expect(sp).To(Equal(signedProposal))
Expect(pluginName).To(Equal("plugin-name"))
Expect(cid).To(Equal("channel-id"))
prp := &pb.ProposalResponsePayload{}
err = proto.Unmarshal(propRespPayloadBytes, prp)
Expect(err).NotTo(HaveOccurred())
Expect(fmt.Sprintf("%x", prp.ProposalHash)).To(Equal("6fa450b00ebef6c7de9f3479148f6d6ff2c645762e17fcaae989ff7b668be001"))
ccAct := &pb.ChaincodeAction{}
err = proto.Unmarshal(prp.Extension, ccAct)
Expect(err).NotTo(HaveOccurred())
Expect(ccAct.Events).To(Equal(protoutil.MarshalOrPanic(chaincodeEvent)))
Expect(proto.Equal(ccAct.Response, &pb.Response{
Status: 200,
Payload: []byte("response-payload"),
})).To(BeTrue())
Expect(fakeSupport.GetHistoryQueryExecutorCallCount()).To(Equal(1))
ledgerName := fakeSupport.GetHistoryQueryExecutorArgsForCall(0)
Expect(ledgerName).To(Equal("channel-id"))
})
Context("when the chaincode endorsement fails", func() {
BeforeEach(func() {
fakeSupport.EndorseWithPluginReturns(nil, nil, fmt.Errorf("fake-endorserment-error"))
})
It("returns the error, but with no payload encoded", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "endorsing with plugin failed: fake-endorserment-error",
}))
})
})
It("checks for duplicate transactions", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTransactionByIDCallCount()).To(Equal(1))
channelID, txid := fakeSupport.GetTransactionByIDArgsForCall(0)
Expect(channelID).To(Equal("channel-id"))
Expect(txid).To(Equal("6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015"))
})
Context("when the txid is duplicated", func() {
BeforeEach(func() {
fakeSupport.GetTransactionByIDReturns(nil, nil)
})
It("wraps and returns an error and responds to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).To(MatchError("duplicate transaction found [6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015]. Creator [0a066d73702d6964]"))
Expect(proposalResponse).To(Equal(&pb.ProposalResponse{
Response: &pb.Response{
Status: 500,
Message: "duplicate transaction found [6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015]. Creator [0a066d73702d6964]",
},
}))
})
})
It("gets a transaction simulator", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTxSimulatorCallCount()).To(Equal(1))
ledgerName, txid := fakeSupport.GetTxSimulatorArgsForCall(0)
Expect(ledgerName).To(Equal("channel-id"))
Expect(txid).To(Equal("6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015"))
})
Context("when getting the tx simulator fails", func() {
BeforeEach(func() {
fakeSupport.GetTxSimulatorReturns(nil, fmt.Errorf("fake-simulator-error"))
})
It("returns a response with the error", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "fake-simulator-error",
}))
})
})
It("gets a history query executor", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetHistoryQueryExecutorCallCount()).To(Equal(1))
ledgerName := fakeSupport.GetHistoryQueryExecutorArgsForCall(0)
Expect(ledgerName).To(Equal("channel-id"))
})
Context("when getting the history query executor fails", func() {
BeforeEach(func() {
fakeSupport.GetHistoryQueryExecutorReturns(nil, fmt.Errorf("fake-history-error"))
})
It("returns a response with the error", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "fake-history-error",
}))
})
})
It("gets the channel context", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeChannelFetcher.ChannelCallCount()).To(Equal(1))
channelID := fakeChannelFetcher.ChannelArgsForCall(0)
Expect(channelID).To(Equal("channel-id"))
})
Context("when the channel context cannot be retrieved", func() {
BeforeEach(func() {
fakeChannelFetcher.ChannelReturns(nil)
})
It("returns a response with the error", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "channel 'channel-id' not found",
}))
})
})
It("checks the submitter's identity", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeChannelMSPIdentityDeserializer.DeserializeIdentityCallCount()).To(Equal(1))
identity := fakeChannelMSPIdentityDeserializer.DeserializeIdentityArgsForCall(0)
Expect(identity).To(Equal(protoutil.MarshalOrPanic(&mspproto.SerializedIdentity{
Mspid: "msp-id",
})))
Expect(fakeLocalMSPIdentityDeserializer.DeserializeIdentityCallCount()).To(Equal(0))
})
Context("when the proposal is not validly signed", func() {
BeforeEach(func() {
fakeChannelMSPIdentityDeserializer.DeserializeIdentityReturns(nil, fmt.Errorf("fake-deserialize-error"))
})
It("wraps and returns an error and responds to the client", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).To(MatchError("error validating proposal: access denied: channel [channel-id] creator org unknown, creator is malformed"))
Expect(proposalResponse).To(Equal(&pb.ProposalResponse{
Response: &pb.Response{
Status: 500,
Message: "error validating proposal: access denied: channel [channel-id] creator org unknown, creator is malformed",
},
}))
})
})
It("checks the ACLs for the identity", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeProposalACLCheckFailed.WithCallCount()).To(Equal(0))
Expect(fakeInitFailed.WithCallCount()).To(Equal(0))
Expect(fakeEndorsementsFailed.WithCallCount()).To(Equal(0))
Expect(fakeDuplicateTxsFailure.WithCallCount()).To(Equal(0))
})
Context("when the acl check fails", func() {
BeforeEach(func() {
fakeSupport.CheckACLReturns(fmt.Errorf("fake-acl-error"))
})
It("wraps and returns an error and responds to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).To(MatchError("fake-acl-error"))
Expect(proposalResponse).To(Equal(&pb.ProposalResponse{
Response: &pb.Response{
Status: 500,
Message: "fake-acl-error",
},
}))
})
Context("when it's for a system chaincode", func() {
BeforeEach(func() {
fakeSupport.IsSysCCReturns(true)
})
It("skips the acl check", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response.Status).To(Equal(int32(200)))
})
})
})
It("gets the chaincode definition", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.ChaincodeEndorsementInfoCallCount()).To(Equal(1))
channelID, chaincodeName, txSim := fakeSupport.ChaincodeEndorsementInfoArgsForCall(0)
Expect(channelID).To(Equal("channel-id"))
Expect(chaincodeName).To(Equal("chaincode-name"))
Expect(txSim).To(Equal(fakeTxSimulator))
})
Context("when the chaincode definition is not found", func() {
BeforeEach(func() {
fakeSupport.ChaincodeEndorsementInfoReturns(nil, fmt.Errorf("fake-definition-error"))
})
It("returns an error in the response", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "make sure the chaincode chaincode-name has been successfully defined on channel channel-id and try again: fake-definition-error",
}))
})
})
It("calls the chaincode", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.ExecuteCallCount()).To(Equal(1))
txParams, chaincodeName, input := fakeSupport.ExecuteArgsForCall(0)
Expect(txParams.ChannelID).To(Equal("channel-id"))
Expect(txParams.SignedProp).To(Equal(signedProposal))
Expect(txParams.TXSimulator).To(Equal(fakeTxSimulator))
Expect(txParams.HistoryQueryExecutor).To(Equal(fakeHistoryQueryExecutor))
Expect(chaincodeName).To(Equal("chaincode-name"))
Expect(proto.Equal(input, &pb.ChaincodeInput{
Args: [][]byte{[]byte("arg1"), []byte("arg2"), []byte("arg3")},
})).To(BeTrue())
})
Context("when calling the chaincode returns an error", func() {
BeforeEach(func() {
fakeSupport.ExecuteReturns(nil, nil, fmt.Errorf("fake-chaincode-execution-error"))
})
It("returns a response with the error and no payload", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: fake-chaincode-execution-error",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
It("distributes private data", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakePrivateDataDistributor.DistributePrivateDataCallCount()).To(Equal(1))
cid, txid, privateData, blkHt := fakePrivateDataDistributor.DistributePrivateDataArgsForCall(0)
Expect(cid).To(Equal("channel-id"))
Expect(txid).To(Equal("6f142589e4ef6a1e62c9c816e2074f70baa9f7cf67c2f0c287d4ef907d6d2015"))
Expect(blkHt).To(Equal(uint64(7)))
// TODO, this deserves a better test, but there was none before and this logic,
// really seems far too jumbled to be in the endorser package. There are separate
// tests of the private data assembly functions in their test file.
Expect(privateData).NotTo(BeNil())
Expect(privateData.EndorsedAt).To(Equal(uint64(7)))
})
Context("when the private data cannot be distributed", func() {
BeforeEach(func() {
fakePrivateDataDistributor.DistributePrivateDataReturns(fmt.Errorf("fake-private-data-error"))
})
It("returns a response with the error and no payload", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: fake-private-data-error",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
It("checks the block height", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetLedgerHeightCallCount()).To(Equal(1))
})
Context("when the block height cannot be determined", func() {
BeforeEach(func() {
fakeSupport.GetLedgerHeightReturns(0, fmt.Errorf("fake-block-height-error"))
})
It("returns a response with the error and no payload", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: failed to obtain ledger height for channel 'channel-id': fake-block-height-error",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
It("records metrics about the proposal processing", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeProposalsReceived.AddCallCount()).To(Equal(1))
Expect(fakeSuccessfulProposals.AddCallCount()).To(Equal(1))
Expect(fakeProposalValidationFailed.AddCallCount()).To(Equal(0))
Expect(fakeProposalDuration.WithCallCount()).To(Equal(1))
Expect(fakeProposalDuration.WithArgsForCall(0)).To(Equal([]string{
"channel", "channel-id",
"chaincode", "chaincode-name",
"success", "true",
}))
})
Context("when the channel id is empty", func() {
BeforeEach(func() {
channelID = ""
})
It("returns a successful proposal response with no endorsement", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Endorsement).To(BeNil())
Expect(proposalResponse.Timestamp).To(BeNil())
Expect(proposalResponse.Version).To(Equal(int32(0)))
Expect(proposalResponse.Payload).To(BeNil())
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 200,
Payload: []byte("response-payload"),
})).To(BeTrue())
})
It("does not attempt to get a history query executor", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetHistoryQueryExecutorCallCount()).To(Equal(0))
})
It("does not attempt to deduplicate the txid", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTransactionByIDCallCount()).To(Equal(0))
})
It("does not attempt to get a tx simulator", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTxSimulatorCallCount()).To(Equal(0))
})
It("uses the local MSP to authorize the creator", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeChannelMSPIdentityDeserializer.DeserializeIdentityCallCount()).To(Equal(0))
Expect(fakeLocalMSPIdentityDeserializer.DeserializeIdentityCallCount()).To(Equal(1))
identity := fakeLocalMSPIdentityDeserializer.DeserializeIdentityArgsForCall(0)
Expect(identity).To(Equal(protoutil.MarshalOrPanic(&mspproto.SerializedIdentity{
Mspid: "msp-id",
})))
})
Context("when the proposal is not validly signed", func() {
BeforeEach(func() {
fakeLocalMSPIdentityDeserializer.DeserializeIdentityReturns(nil, fmt.Errorf("fake-deserialize-error"))
})
It("wraps and returns an error and responds to the client", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).To(MatchError("error validating proposal: access denied: channel [] creator org unknown, creator is malformed"))
Expect(proposalResponse).To(Equal(&pb.ProposalResponse{
Response: &pb.Response{
Status: 500,
Message: "error validating proposal: access denied: channel [] creator org unknown, creator is malformed",
},
}))
})
})
It("records metrics but without a channel ID set", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeProposalsReceived.AddCallCount()).To(Equal(1))
Expect(fakeSuccessfulProposals.AddCallCount()).To(Equal(1))
Expect(fakeProposalValidationFailed.AddCallCount()).To(Equal(0))
Expect(fakeProposalDuration.WithCallCount()).To(Equal(1))
Expect(fakeProposalDuration.WithArgsForCall(0)).To(Equal([]string{
"channel", "",
"chaincode", "chaincode-name",
"success", "true",
}))
Expect(fakeProposalACLCheckFailed.WithCallCount()).To(Equal(0))
Expect(fakeInitFailed.WithCallCount()).To(Equal(0))
Expect(fakeEndorsementsFailed.WithCallCount()).To(Equal(0))
Expect(fakeDuplicateTxsFailure.WithCallCount()).To(Equal(0))
})
Context("when the chaincode response is >= 500", func() {
BeforeEach(func() {
chaincodeResponse.Status = 500
})
It("returns the result, but with the proposal encoded, and no endorsements", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Endorsement).To(BeNil())
Expect(proposalResponse.Timestamp).To(BeNil())
Expect(proposalResponse.Version).To(Equal(int32(0)))
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 500,
Payload: []byte("response-payload"),
})).To(BeTrue())
// This is almost definitely a bug, but, adding a test in case someone is relying on this behavior.
// When the response is >= 500, we return a payload, but not on success. A payload is only meaningful
// if it is endorsed, so it's unclear why we're returning it here.
prp := &pb.ProposalResponsePayload{}
err = proto.Unmarshal(proposalResponse.Payload, prp)
Expect(err).NotTo(HaveOccurred())
Expect(fmt.Sprintf("%x", prp.ProposalHash)).To(Equal("f2c27f04f897dc28fd1b2983e7b22ebc8fbbb3d0617c140d913b33e463886788"))
ccAct := &pb.ChaincodeAction{}
err = proto.Unmarshal(prp.Extension, ccAct)
Expect(err).NotTo(HaveOccurred())
Expect(proto.Equal(ccAct.Response, &pb.Response{
Status: 500,
Payload: []byte("response-payload"),
})).To(BeTrue())
// This is an especially weird bit of the behavior, the chaincode event is nil-ed before creating
// the proposal response. (That probably shouldn't be created)
Expect(ccAct.Events).To(BeNil())
})
})
Context("when the 200 < chaincode response < 500", func() {
BeforeEach(func() {
chaincodeResponse.Status = 499
})
It("returns the result, but with the proposal encoded, and no endorsements", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Endorsement).To(BeNil())
Expect(proposalResponse.Timestamp).To(BeNil())
Expect(proposalResponse.Version).To(Equal(int32(0)))
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 499,
Payload: []byte("response-payload"),
})).To(BeTrue())
Expect(proposalResponse.Payload).To(BeNil())
})
})
})
Context("when the proposal is malformed", func() {
JustBeforeEach(func() {
signedProposal = &pb.SignedProposal{
ProposalBytes: []byte("garbage"),
}
})
It("wraps and returns an error and responds to the client", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(HavePrefix("error unmarshalling Proposal"))
Expect(proposalResponse.Response.Status).To(Equal(int32(500)))
Expect(proposalResponse.Response.Message).To(HavePrefix("error unmarshalling Proposal"))
})
})
Context("when the chaincode response is >= 500", func() {
BeforeEach(func() {
chaincodeResponse.Status = 500
})
It("returns the result, but with the proposal encoded, and no endorsements", func() {
proposalResponse, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Endorsement).To(BeNil())
Expect(proposalResponse.Timestamp).To(BeNil())
Expect(proposalResponse.Version).To(Equal(int32(0)))
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 500,
Payload: []byte("response-payload"),
})).To(BeTrue())
Expect(proposalResponse.Payload).NotTo(BeNil())
})
})
Context("when the chaincode name is qscc", func() {
BeforeEach(func() {
chaincodeName = "qscc"
})
It("skips fetching the tx simulator and history query exucutor", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTxSimulatorCallCount()).To(Equal(0))
Expect(fakeSupport.GetHistoryQueryExecutorCallCount()).To(Equal(0))
})
})
Context("when the chaincode name is cscc", func() {
BeforeEach(func() {
chaincodeName = "cscc"
})
It("skips fetching the tx simulator and history query exucutor", func() {
_, err := e.ProcessProposal(context.Background(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(fakeSupport.GetTxSimulatorCallCount()).To(Equal(0))
Expect(fakeSupport.GetHistoryQueryExecutorCallCount()).To(Equal(0))
})
})
Context("when the chaincode response is >= 400 but < 500", func() {
BeforeEach(func() {
chaincodeResponse.Status = 400
})
It("returns the response with no payload", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Payload).To(BeNil())
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 400,
Payload: []byte("response-payload"),
})).To(BeTrue())
})
})
Context("when we're in the degenerate legacy lifecycle case", func() {
BeforeEach(func() {
chaincodeName = "lscc"
chaincodeInput.Args = [][]byte{
[]byte("deploy"),
nil,
protoutil.MarshalOrPanic(&pb.ChaincodeDeploymentSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: "deploy-name",
Version: "deploy-version",
},
Input: &pb.ChaincodeInput{
Args: [][]byte{[]byte("target-arg")},
},
},
}),
}
fakeTxSimulator.GetTxSimulationResultsReturns(
&ledger.TxSimulationResults{
PubSimulationResults: &rwset.TxReadWriteSet{},
// We don't return private data in this case because lscc forbids it
},
nil,
)
})
It("triggers the legacy init, and returns the response from lscc", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proto.Equal(proposalResponse.Response, &pb.Response{
Status: 200,
Payload: []byte("response-payload"),
})).To(BeTrue())
Expect(fakeSupport.ExecuteLegacyInitCallCount()).To(Equal(1))
_, name, version, input := fakeSupport.ExecuteLegacyInitArgsForCall(0)
Expect(name).To(Equal("deploy-name"))
Expect(version).To(Equal("deploy-version"))
Expect(input.Args).To(Equal([][]byte{[]byte("target-arg")}))
})
Context("when the chaincode spec contains a code package", func() {
BeforeEach(func() {
chaincodeInput.Args = [][]byte{
[]byte("deploy"),
nil,
protoutil.MarshalOrPanic(&pb.ChaincodeDeploymentSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: "deploy-name",
Version: "deploy-version",
},
Input: &pb.ChaincodeInput{
Args: [][]byte{[]byte("target-arg")},
},
},
CodePackage: []byte("some-code"),
}),
}
})
It("returns an error to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: lscc upgrade/deploy should not include a code packages",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
Context("when the simulation uses private data", func() {
BeforeEach(func() {
fakeTxSimulator.GetTxSimulationResultsReturns(
&ledger.TxSimulationResults{
PubSimulationResults: &rwset.TxReadWriteSet{},
PvtSimulationResults: &rwset.TxPvtReadWriteSet{},
},
nil,
)
})
It("returns an error to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: Private data is forbidden to be used in instantiate",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
Context("when retrieving simulation results", func() {
BeforeEach(func() {
fakeTxSimulator.GetTxSimulationResultsReturns(
&ledger.TxSimulationResults{
PubSimulationResults: &rwset.TxReadWriteSet{},
PvtSimulationResults: &rwset.TxPvtReadWriteSet{},
},
errors.New("bad simulation"),
)
})
It("returns an error to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: bad simulation",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
Context("when retrieving public simulation results fails", func() {
BeforeEach(func() {
fakeTxSimulator.GetTxSimulationResultsReturns(
&ledger.TxSimulationResults{},
nil,
)
})
It("returns an error to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: proto: Marshal called with nil",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
Context("when the init fails", func() {
BeforeEach(func() {
fakeSupport.ExecuteLegacyInitReturns(nil, nil, fmt.Errorf("fake-legacy-init-error"))
})
It("returns an error and increments the metric", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: fake-legacy-init-error",
}))
Expect(fakeInitFailed.WithCallCount()).To(Equal(1))
Expect(fakeInitFailed.WithArgsForCall(0)).To(Equal([]string{
"channel", "channel-id",
"chaincode", "deploy-name",
}))
Expect(fakeInitFailed.AddCallCount()).To(Equal(1))
})
})
Context("when the deploying chaincode is the name of a builtin system chaincode", func() {
BeforeEach(func() {
fakeSupport.IsSysCCStub = func(name string) bool {
return name == "deploy-name"
}
})
It("triggers the legacy init, and returns the response from lscc", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response).To(Equal(&pb.Response{
Status: 500,
Message: "error in simulation: attempting to deploy a system chaincode deploy-name/channel-id",
}))
Expect(fakeSimulateFailure.AddCallCount()).To(Equal(1))
})
})
Context("when unmarshalling UnmarshalChaincodeDeploymentSpec fails", func() {
BeforeEach(func() {
chaincodeInput = &pb.ChaincodeInput{
Args: [][]byte{[]byte("deploy"), nil, []byte("arg3")},
}
})
It("returns an error to the client", func() {
proposalResponse, err := e.ProcessProposal(context.TODO(), signedProposal)
Expect(err).NotTo(HaveOccurred())
Expect(proposalResponse.Response.Status).To(Equal(int32(500)))