@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/hyperledger/fabric/core/chaincode"
14
14
"github.com/hyperledger/fabric/core/chaincode/fake"
15
+ "github.com/hyperledger/fabric/core/chaincode/lifecycle"
15
16
"github.com/hyperledger/fabric/core/chaincode/mock"
16
17
"github.com/hyperledger/fabric/core/common/ccprovider"
17
18
pb "github.com/hyperledger/fabric/protos/peer"
@@ -28,7 +29,10 @@ var _ = Describe("ChaincodeSupport", func() {
28
29
fakeApplicationConfig * mock.ApplicationConfig
29
30
fakeApplicationCapabilities * mock.ApplicationCapabilities
30
31
fakeLifecycle * mock.Lifecycle
31
- fakeTxSim * mock.TxSimulator
32
+ fakeSimulator * mock.TxSimulator
33
+
34
+ txParams * ccprovider.TransactionParams
35
+ input * pb.ChaincodeInput
32
36
)
33
37
34
38
BeforeEach (func () {
@@ -42,7 +46,17 @@ var _ = Describe("ChaincodeSupport", func() {
42
46
fakeApplicationConfigRetriever .GetApplicationConfigReturns (fakeApplicationConfig , true )
43
47
44
48
fakeLifecycle = & mock.Lifecycle {}
45
- fakeTxSim = & mock.TxSimulator {}
49
+ fakeSimulator = & mock.TxSimulator {}
50
+ fakeSimulator .GetStateReturns ([]byte ("old-cc-version" ), nil )
51
+
52
+ txParams = & ccprovider.TransactionParams {
53
+ ChannelID : "channel-id" ,
54
+ TXSimulator : fakeSimulator ,
55
+ }
56
+
57
+ input = & pb.ChaincodeInput {
58
+ IsInit : true ,
59
+ }
46
60
47
61
chaincodeSupport = & chaincode.ChaincodeSupport {
48
62
AppConfig : fakeApplicationConfigRetriever ,
@@ -51,54 +65,95 @@ var _ = Describe("ChaincodeSupport", func() {
51
65
})
52
66
53
67
Describe ("CheckInvocation" , func () {
54
- var (
55
- fakeLegacyDefinition * mock.LegacyChaincodeDefinition
56
- )
57
-
58
- BeforeEach (func () {
59
- fakeLegacyDefinition = & mock.LegacyChaincodeDefinition {}
60
-
61
- ccDef := struct {
62
- * ccprovider.ChaincodeData
63
- * mock.LegacyChaincodeDefinition
64
- }{
65
- ChaincodeData : & ccprovider.ChaincodeData {
66
- Name : "definition-name" ,
67
- Version : "cc-version" ,
68
- Id : []byte ("id" ),
69
- },
70
- LegacyChaincodeDefinition : fakeLegacyDefinition ,
71
- }
72
-
73
- fakeLifecycle .ChaincodeDefinitionReturns (ccDef , nil )
74
- })
68
+ Context ("when the definition is a _lifecycle definition" , func () {
69
+ BeforeEach (func () {
70
+ fakeLifecycle .ChaincodeDefinitionReturns (& lifecycle.LegacyDefinition {
71
+ Name : "definition-name" ,
72
+ Version : "cc-version" ,
73
+ RequiresInitField : true ,
74
+ CCIDField : "definition-ccid" ,
75
+ }, nil )
76
+ })
75
77
76
- It ("fetches the definition, performs security checks, and returns the necessary info" , func () {
77
- ccid , ccContext , err := chaincodeSupport .CheckInvocation ("test-channel" , "test-chaincode-name" , fakeTxSim )
78
- Expect (err ).NotTo (HaveOccurred ())
79
- Expect (ccid ).To (Equal ("definition-name:cc-version" ))
80
- Expect (ccContext ).To (Equal (& chaincode.CCContext {
81
- Name : "test-chaincode-name" ,
82
- Version : "cc-version" ,
83
- }))
84
-
85
- Expect (fakeLifecycle .ChaincodeDefinitionCallCount ()).To (Equal (1 ))
86
- channelID , chaincodeName , txSim := fakeLifecycle .ChaincodeDefinitionArgsForCall (0 )
87
- Expect (channelID ).To (Equal ("test-channel" ))
88
- Expect (chaincodeName ).To (Equal ("test-chaincode-name" ))
89
- Expect (txSim ).To (Equal (fakeTxSim ))
90
-
91
- Expect (fakeLegacyDefinition .ExecuteLegacySecurityChecksCallCount ()).To (Equal (1 ))
78
+ It ("fetches the definition, skips the security checks, and does the init checks" , func () {
79
+ ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
80
+ Expect (err ).NotTo (HaveOccurred ())
81
+ Expect (ccid ).To (Equal ("definition-ccid" ))
82
+ Expect (cctype ).To (Equal (pb .ChaincodeMessage_INIT ))
83
+
84
+ Expect (fakeLifecycle .ChaincodeDefinitionCallCount ()).To (Equal (1 ))
85
+ channelID , chaincodeName , txSim := fakeLifecycle .ChaincodeDefinitionArgsForCall (0 )
86
+ Expect (channelID ).To (Equal ("channel-id" ))
87
+ Expect (chaincodeName ).To (Equal ("test-chaincode-name" ))
88
+ Expect (txSim ).To (Equal (fakeSimulator ))
89
+ })
90
+
91
+ Context ("when the definition does not require init" , func () {
92
+ BeforeEach (func () {
93
+ fakeLifecycle .ChaincodeDefinitionReturns (& lifecycle.LegacyDefinition {
94
+ Name : "definition-name" ,
95
+ Version : "cc-version" ,
96
+ RequiresInitField : false ,
97
+ CCIDField : "definition-ccid" ,
98
+ }, nil )
99
+ })
100
+
101
+ It ("returns a normal transaction type" , func () {
102
+ ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
103
+ Expect (err ).NotTo (HaveOccurred ())
104
+ Expect (ccid ).To (Equal ("definition-ccid" ))
105
+ Expect (cctype ).To (Equal (pb .ChaincodeMessage_TRANSACTION ))
106
+ })
107
+ })
92
108
})
93
109
94
- Context ("when the legacy security check fails" , func () {
110
+ Context ("when the definition is a legacy definition" , func () {
111
+ var (
112
+ fakeLegacyDefinition * mock.LegacyChaincodeDefinition
113
+ )
114
+
95
115
BeforeEach (func () {
96
- fakeLegacyDefinition .ExecuteLegacySecurityChecksReturns (fmt .Errorf ("fake-security-error" ))
116
+ fakeLegacyDefinition = & mock.LegacyChaincodeDefinition {}
117
+
118
+ ccDef := struct {
119
+ * ccprovider.ChaincodeData
120
+ * mock.LegacyChaincodeDefinition
121
+ }{
122
+ ChaincodeData : & ccprovider.ChaincodeData {
123
+ Name : "definition-name" ,
124
+ Version : "cc-version" ,
125
+ Id : []byte ("id" ),
126
+ },
127
+ LegacyChaincodeDefinition : fakeLegacyDefinition ,
128
+ }
129
+
130
+ fakeLifecycle .ChaincodeDefinitionReturns (ccDef , nil )
97
131
})
98
132
99
- It ("wraps and returns the error" , func () {
100
- _ , _ , err := chaincodeSupport .CheckInvocation ("test-channel" , "test-chaincode-name" , fakeTxSim )
101
- Expect (err ).To (MatchError ("[channel test-channel] failed the chaincode security checks for test-chaincode-name: fake-security-error" ))
133
+ It ("fetches the definition, performs security checks, and skips the init checks" , func () {
134
+ ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
135
+ Expect (err ).NotTo (HaveOccurred ())
136
+ Expect (ccid ).To (Equal ("definition-name:cc-version" ))
137
+ Expect (cctype ).To (Equal (pb .ChaincodeMessage_TRANSACTION ))
138
+
139
+ Expect (fakeLifecycle .ChaincodeDefinitionCallCount ()).To (Equal (1 ))
140
+ channelID , chaincodeName , txSim := fakeLifecycle .ChaincodeDefinitionArgsForCall (0 )
141
+ Expect (channelID ).To (Equal ("channel-id" ))
142
+ Expect (chaincodeName ).To (Equal ("test-chaincode-name" ))
143
+ Expect (txSim ).To (Equal (fakeSimulator ))
144
+
145
+ Expect (fakeLegacyDefinition .ExecuteLegacySecurityChecksCallCount ()).To (Equal (1 ))
146
+ })
147
+
148
+ Context ("when the legacy security check fails" , func () {
149
+ BeforeEach (func () {
150
+ fakeLegacyDefinition .ExecuteLegacySecurityChecksReturns (fmt .Errorf ("fake-security-error" ))
151
+ })
152
+
153
+ It ("wraps and returns the error" , func () {
154
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
155
+ Expect (err ).To (MatchError ("[channel channel-id] failed the chaincode security checks for test-chaincode-name: fake-security-error" ))
156
+ })
102
157
})
103
158
})
104
159
@@ -108,43 +163,15 @@ var _ = Describe("ChaincodeSupport", func() {
108
163
})
109
164
110
165
It ("wraps and returns the error" , func () {
111
- _ , _ , err := chaincodeSupport .CheckInvocation ("test-channel" , "test-chaincode-name" , fakeTxSim )
112
- Expect (err ).To (MatchError ("[channel test- channel] failed to get chaincode container info for test-chaincode-name: fake-lifecycle-error" ))
166
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
167
+ Expect (err ).To (MatchError ("[channel channel-id ] failed to get chaincode container info for test-chaincode-name: fake-lifecycle-error" ))
113
168
})
114
169
})
115
170
})
116
171
117
172
Describe ("CheckInit" , func () {
118
- var (
119
- txParams * ccprovider.TransactionParams
120
- cccid * chaincode.CCContext
121
- input * pb.ChaincodeInput
122
-
123
- fakeSimulator * mock.TxSimulator
124
- )
125
-
126
- BeforeEach (func () {
127
- fakeSimulator = & mock.TxSimulator {}
128
- fakeSimulator .GetStateReturns ([]byte ("old-cc-version" ), nil )
129
-
130
- txParams = & ccprovider.TransactionParams {
131
- ChannelID : "channel-id" ,
132
- TXSimulator : fakeSimulator ,
133
- }
134
-
135
- cccid = & chaincode.CCContext {
136
- Name : "cc-name" ,
137
- Version : "cc-version" ,
138
- InitRequired : true ,
139
- }
140
-
141
- input = & pb.ChaincodeInput {
142
- IsInit : true ,
143
- }
144
- })
145
-
146
173
It ("indicates that it is init" , func () {
147
- isInit , err := chaincodeSupport .CheckInit (txParams , cccid , input )
174
+ isInit , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
148
175
Expect (err ).NotTo (HaveOccurred ())
149
176
Expect (isInit ).To (BeTrue ())
150
177
@@ -161,12 +188,8 @@ var _ = Describe("ChaincodeSupport", func() {
161
188
})
162
189
163
190
Context ("when the version is not changed" , func () {
164
- BeforeEach (func () {
165
- cccid .Version = "old-cc-version"
166
- })
167
-
168
191
It ("returns an error" , func () {
169
- _ , err := chaincodeSupport .CheckInit (txParams , cccid , input )
192
+ _ , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "old-cc-version" , input )
170
193
Expect (err ).To (MatchError ("chaincode 'cc-name' is already initialized but called as init" ))
171
194
})
172
195
@@ -176,7 +199,7 @@ var _ = Describe("ChaincodeSupport", func() {
176
199
})
177
200
178
201
It ("returns that it is not an init" , func () {
179
- isInit , err := chaincodeSupport .CheckInit (txParams , cccid , input )
202
+ isInit , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "old-cc-version" , input )
180
203
Expect (err ).NotTo (HaveOccurred ())
181
204
Expect (isInit ).To (BeFalse ())
182
205
Expect (fakeSimulator .GetStateCallCount ()).To (Equal (1 ))
@@ -185,27 +208,13 @@ var _ = Describe("ChaincodeSupport", func() {
185
208
})
186
209
})
187
210
188
- Context ("when init is not required" , func () {
189
- BeforeEach (func () {
190
- cccid .InitRequired = false
191
- })
192
-
193
- It ("returns that it is not init" , func () {
194
- isInit , err := chaincodeSupport .CheckInit (txParams , cccid , input )
195
- Expect (err ).NotTo (HaveOccurred ())
196
- Expect (isInit ).To (BeFalse ())
197
- Expect (fakeSimulator .GetStateCallCount ()).To (Equal (0 ))
198
- Expect (fakeSimulator .SetStateCallCount ()).To (Equal (0 ))
199
- })
200
- })
201
-
202
211
Context ("when the new lifecycle is not enabled" , func () {
203
212
BeforeEach (func () {
204
213
fakeApplicationCapabilities .LifecycleV20Returns (false )
205
214
})
206
215
207
216
It ("returns that it is not init" , func () {
208
- isInit , err := chaincodeSupport .CheckInit (txParams , cccid , input )
217
+ isInit , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
209
218
Expect (err ).NotTo (HaveOccurred ())
210
219
Expect (isInit ).To (BeFalse ())
211
220
Expect (fakeSimulator .GetStateCallCount ()).To (Equal (0 ))
@@ -219,7 +228,7 @@ var _ = Describe("ChaincodeSupport", func() {
219
228
})
220
229
221
230
It ("returns it is not an init" , func () {
222
- isInit , err := chaincodeSupport .CheckInit (txParams , cccid , input )
231
+ isInit , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
223
232
Expect (err ).NotTo (HaveOccurred ())
224
233
Expect (isInit ).To (BeFalse ())
225
234
Expect (fakeSimulator .GetStateCallCount ()).To (Equal (0 ))
@@ -233,7 +242,7 @@ var _ = Describe("ChaincodeSupport", func() {
233
242
})
234
243
235
244
It ("returns an error" , func () {
236
- _ , err := chaincodeSupport .CheckInit (txParams , cccid , input )
245
+ _ , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
237
246
Expect (err ).To (MatchError ("could not retrieve application config for channel 'channel-id'" ))
238
247
})
239
248
})
@@ -244,7 +253,7 @@ var _ = Describe("ChaincodeSupport", func() {
244
253
})
245
254
246
255
It ("returns an error" , func () {
247
- _ , err := chaincodeSupport .CheckInit (txParams , cccid , input )
256
+ _ , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
248
257
Expect (err ).To (MatchError ("chaincode 'cc-name' has not been initialized for this version, must call as init first" ))
249
258
})
250
259
})
@@ -255,7 +264,7 @@ var _ = Describe("ChaincodeSupport", func() {
255
264
})
256
265
257
266
It ("wraps and returns the error" , func () {
258
- _ , err := chaincodeSupport .CheckInit (txParams , cccid , input )
267
+ _ , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
259
268
Expect (err ).To (MatchError ("could not get 'initialized' key: get-state-error" ))
260
269
})
261
270
})
@@ -266,7 +275,7 @@ var _ = Describe("ChaincodeSupport", func() {
266
275
})
267
276
268
277
It ("wraps and returns the error" , func () {
269
- _ , err := chaincodeSupport .CheckInit (txParams , cccid , input )
278
+ _ , err := chaincodeSupport .CheckInit (txParams , "cc-name" , "cc-version" , input )
270
279
Expect (err ).To (MatchError ("could not set 'initialized' key: set-state-error" ))
271
280
})
272
281
})
0 commit comments