This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
chaincode_service_test.go
364 lines (281 loc) · 10.9 KB
/
chaincode_service_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
package gateway_test
import (
"context"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes/empty"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/s7techlab/cckit/convert"
cpservice "github.com/s7techlab/cckit/examples/cpaper_asservice"
"github.com/s7techlab/cckit/examples/cpaper_asservice/testdata"
"github.com/s7techlab/cckit/extensions/encryption"
enctest "github.com/s7techlab/cckit/extensions/encryption/testing"
"github.com/s7techlab/cckit/gateway"
idtestdata "github.com/s7techlab/cckit/identity/testdata"
testcc "github.com/s7techlab/cckit/testing"
"github.com/s7techlab/cckit/testing/gomega"
)
func TestService(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gateway suite")
}
const (
Channel = `my_channel`
ChaincodeName = `commercial_paper`
)
var (
ctx = gateway.ContextWithSigner(
context.Background(),
idtestdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP),
)
)
var _ = Describe(`Gateway`, func() {
Context(`Chaincode service without options`, func() {
var (
ccService *gateway.ChaincodeService
ccInstanceService *gateway.ChaincodeInstanceService
cPaperGateway *cpservice.CPaperServiceGateway
mockStub *testcc.MockStub
)
It("Init", func() {
ccImpl, err := cpservice.NewCC()
Expect(err).NotTo(HaveOccurred())
mockStub = testcc.NewMockStub(ChaincodeName, ccImpl)
peer := testcc.NewPeer().WithChannel(Channel, mockStub)
ccService = gateway.NewChaincodeService(peer)
ccInstanceService = ccService.InstanceService(
&gateway.ChaincodeLocator{Channel: Channel, Chaincode: ChaincodeName},
gateway.WithEventResolver(cpservice.EventMappings))
// "sdk" for deal with cpaper chaincode
cPaperGateway = cpservice.NewCPaperServiceGateway(peer, Channel, ChaincodeName)
})
Context(`Direct calls`, func() {
It("Require to provide chaincode locator", func() {
_, err := ccService.Query(ctx, &gateway.ChaincodeQueryRequest{
Input: &gateway.ChaincodeInput{
Args: [][]byte{[]byte(`List`), {}},
},
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(`invalid field Locator: message must exist`))
})
})
Context(`Chaincode gateway`, func() {
It("Allow to get empty commercial paper list", func() {
pp, err := cPaperGateway.List(ctx, &empty.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(pp.Items).To(HaveLen(0))
})
It("Invoke chaincode with 'tx waiter' in context", func() {
ctx = gateway.ContextWithTxWaiter(ctx, "all")
_, err := cPaperGateway.Issue(ctx, testdata.Issue1)
Expect(err).NotTo(HaveOccurred())
})
It("Invoke chaincode with custom identity in context", func() {
signer := idtestdata.Certificates[1].MustIdentity(idtestdata.DefaultMSP)
ctx = gateway.ContextWithDefaultSigner(ctx, signer)
_, err := cPaperGateway.Delete(ctx, testdata.Id1)
Expect(err).NotTo(HaveOccurred())
})
})
Context(`Events`, func() {
It(`allow to get events as LIST (by default from block 0 to current channel height) `, func(done Done) {
events, err := ccInstanceService.Events(ctx, &gateway.ChaincodeInstanceEventsRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(events.Items).To(HaveLen(1)) // 1 event on issue
e := events.Items[0]
var (
eventObj interface{}
eventJson string
)
eventObj, err = convert.FromBytes(e.Event.Payload, &cpservice.IssueCommercialPaper{})
Expect(err).NotTo(HaveOccurred())
eventJson, err = (&jsonpb.Marshaler{EmitDefaults: true, OrigName: true}).
MarshalToString(eventObj.(proto.Message))
Expect(err).NotTo(HaveOccurred())
Expect(e.Event.EventName).To(Equal(`IssueCommercialPaper`))
Expect(e.Payload).NotTo(BeNil())
Expect(e.Payload.Value).To(Equal([]byte(eventJson))) // check event resolving
close(done)
})
It(`allow to get 0 events as LIST from chaincode with incorrect event name filter`, func(done Done) {
events, err := ccInstanceService.Events(ctx, &gateway.ChaincodeInstanceEventsRequest{
EventName: []string{`________IssueCommercialPaper______`},
})
Expect(err).NotTo(HaveOccurred())
Expect(events.Items).To(HaveLen(0)) // 1 event on issue
close(done)
})
It(`allow to get events from block 0 to current channel height AS STREAM`, func(done Done) {
ctxWithCancel, cancel := context.WithCancel(ctx)
stream := gateway.NewChaincodeEventServerStream(ctxWithCancel)
go func() {
req := &gateway.ChaincodeEventsStreamRequest{
Locator: &gateway.ChaincodeLocator{
Channel: Channel,
Chaincode: ChaincodeName,
},
FromBlock: &gateway.BlockLimit{Num: 0},
ToBlock: &gateway.BlockLimit{Num: 0},
}
err := ccService.EventsStream(req, &gateway.ChaincodeEventsServer{ServerStream: stream})
Expect(err).NotTo(HaveOccurred())
}()
var e *gateway.ChaincodeEvent
err := stream.Recv(e)
Expect(err).NotTo(HaveOccurred())
cancel()
close(done)
}, 1)
})
})
Context(`Chaincode service with encrypted chaincode`, func() {
var (
ccService *gateway.ChaincodeService
ccInstance *gateway.ChaincodeInstanceService
cPaperGateway *cpservice.CPaperServiceGateway
cPaperGatewayWithoutEncryption *cpservice.CPaperServiceGateway
encMockStub *enctest.MockStub
encryptOpts []gateway.Opt
)
It("Init", func() {
ccImpl, err := cpservice.NewCCEncrypted()
Expect(err).NotTo(HaveOccurred())
encMockStub = enctest.NewMockStub(testcc.NewMockStub(ChaincodeName, ccImpl))
encryptOpts = []gateway.Opt{
gateway.WithEncryption(encMockStub.EncKey),
// Event resolver should be AFTER encryption / decryption middleware
gateway.WithEventResolver(cpservice.EventMappings),
}
// "sdk" for deal with cpaper chaincode
peer := testcc.NewPeer().WithChannel(Channel, encMockStub.MockStub)
ccService = gateway.NewChaincodeService(peer)
locator := &gateway.ChaincodeLocator{
Channel: Channel,
Chaincode: ChaincodeName,
}
ccInstance = ccService.InstanceService(locator, encryptOpts...)
cPaperGateway = cpservice.NewCPaperServiceGateway(peer, Channel, ChaincodeName, encryptOpts...)
cPaperGatewayWithoutEncryption = cpservice.NewCPaperServiceGateway(
peer, Channel, ChaincodeName,
gateway.WithEventResolver(cpservice.EventMappings))
})
Context(`Chaincode gateway`, func() {
It("Disallow to query chaincode without encryption data", func() {
_, err := cPaperGatewayWithoutEncryption.List(ctx, &empty.Empty{})
Expect(err).To(gomega.ErrorIs(encryption.ErrKeyNotDefinedInTransientMap))
})
It("Allow to get empty commercial paper list", func() {
pp, err := cPaperGateway.List(ctx, &empty.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(pp.Items).To(HaveLen(0))
})
It("Invoke chaincode", func() {
issued, err := cPaperGateway.Issue(ctx, testdata.Issue1)
Expect(err).NotTo(HaveOccurred())
Expect(issued.Issuer).To(Equal(testdata.Issue1.Issuer))
})
It("Query chaincode", func() {
issued, err := cPaperGateway.Get(ctx, testdata.Id1)
Expect(err).NotTo(HaveOccurred())
Expect(issued.Issuer).To(Equal(testdata.Issue1.Issuer))
})
})
Context(`Events`, func() {
It(`allow to get encrypted events as LIST (by default from block 0 to current channel height) `, func(done Done) {
events, err := ccInstance.Events(ctx, &gateway.ChaincodeInstanceEventsRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(events.Items).To(HaveLen(1)) // 1 event on issue
e := events.Items[0]
var (
eventObj interface{}
eventJson string
)
eventObj, err = convert.FromBytes(e.Event.Payload, &cpservice.IssueCommercialPaper{})
Expect(err).NotTo(HaveOccurred())
eventJson, err = (&jsonpb.Marshaler{EmitDefaults: true, OrigName: true}).
MarshalToString(eventObj.(proto.Message))
Expect(err).NotTo(HaveOccurred())
Expect(e.Event.EventName).To(Equal(`IssueCommercialPaper`))
Expect(e.Payload.Value).To(Equal([]byte(eventJson))) // check event resolving
close(done)
})
It(`allow to get encrypted events from block 0 to current channel height AS STREAM`, func(done Done) {
ctxWithCancel, cancel := context.WithCancel(ctx)
stream := gateway.NewChaincodeEventServerStream(ctxWithCancel)
go func() {
req := &gateway.ChaincodeInstanceEventsStreamRequest{
FromBlock: &gateway.BlockLimit{Num: 0},
ToBlock: &gateway.BlockLimit{Num: 0},
}
err := ccInstance.EventsStream(req, &gateway.ChaincodeEventsServer{ServerStream: stream})
Expect(err).NotTo(HaveOccurred())
}()
var e *gateway.ChaincodeEvent
err := stream.Recv(e)
Expect(err).NotTo(HaveOccurred())
cancel()
close(done)
}, 1)
})
})
Context(`Chaincode instance service`, func() {
var (
ccInstanceService *gateway.ChaincodeInstanceService
)
It("Init", func() {
ccImpl, err := cpservice.NewCC()
Expect(err).NotTo(HaveOccurred())
// peer imitation
peer := testcc.NewPeer().WithChannel(Channel, testcc.NewMockStub(ChaincodeName, ccImpl))
ccInstanceService = gateway.NewChaincodeInstanceService(peer, &gateway.ChaincodeLocator{
Channel: Channel,
Chaincode: ChaincodeName,
})
})
Context(`Direct calls`, func() {
It("Allow to get empty commercial paper list", func() {
resp, err := ccInstanceService.Query(ctx, &gateway.ChaincodeInstanceQueryRequest{
Input: &gateway.ChaincodeInput{
Args: [][]byte{[]byte(`List`), {}},
},
})
Expect(err).NotTo(HaveOccurred())
cPaperList := testcc.MustProtoUnmarshal(resp.Payload, &cpservice.CommercialPaperList{}).(*cpservice.CommercialPaperList)
Expect(cPaperList.Items).To(HaveLen(0))
})
It("Invoke chaincode", func() {
_, err := ccInstanceService.Invoke(ctx, &gateway.ChaincodeInstanceInvokeRequest{
Input: &gateway.ChaincodeInput{
Args: [][]byte{[]byte(`Issue`), testcc.MustProtoMarshal(testdata.Issue1)},
}})
Expect(err).NotTo(HaveOccurred())
})
})
Context(`Events delivery`, func() {
//It(`allow to get events as LIST from chaincode instance service (by default from block 0 to current channel height) `, func(done Done) {
// events, err := ccInstanceService.Events(ctx, &gateway.ChaincodeInstanceEventsRequest{
// EventName: []string{`IssueCommercialPaper`},
// })
//
// Expect(err).NotTo(HaveOccurred())
// Expect(events.Items).To(HaveLen(1)) // 1 event on issue
//
// close(done)
//})
It(`allow to get events as chan from chaincode instance service (by default from block 0 to current channel height) `, func(done Done) {
events, closer, err := ccInstanceService.EventsChan(ctx, &gateway.ChaincodeInstanceEventsStreamRequest{
EventName: []string{`IssueCommercialPaper`},
FromBlock: &gateway.BlockLimit{Num: 0},
})
Expect(err).NotTo(HaveOccurred())
event := <-events
Expect(event.Event.EventName).To(Equal(`IssueCommercialPaper`))
_ = closer()
close(done)
})
})
})
})