@@ -23,6 +23,7 @@ import (
23
23
var _ = Describe ("CheckInvocation" , func () {
24
24
var (
25
25
chaincodeSupport * chaincode.ChaincodeSupport
26
+ invokeInfo * lifecycle.ChaincodeEndorsementInfo
26
27
27
28
fakeLifecycle * mock.Lifecycle
28
29
fakeSimulator * mock.TxSimulator
@@ -36,6 +37,13 @@ var _ = Describe("CheckInvocation", func() {
36
37
fakeSimulator = & mock.TxSimulator {}
37
38
fakeSimulator .GetStateReturns ([]byte ("old-cc-version" ), nil )
38
39
40
+ invokeInfo = & lifecycle.ChaincodeEndorsementInfo {
41
+ Version : "definition-version" ,
42
+ ChaincodeID : "definition-ccid" ,
43
+ }
44
+
45
+ fakeLifecycle .ChaincodeEndorsementInfoReturns (invokeInfo , nil )
46
+
39
47
txParams = & ccprovider.TransactionParams {
40
48
ChannelID : "channel-id" ,
41
49
TXSimulator : fakeSimulator ,
@@ -48,168 +56,95 @@ var _ = Describe("CheckInvocation", func() {
48
56
}
49
57
})
50
58
51
- Describe ("CheckInvocation for a non-legacy (_lifecycle) chaincode definition" , func () {
52
- var (
53
- chaincodeDefinition * lifecycle.LegacyDefinition
54
- )
59
+ It ("fetches the info and returns the ccid and type" , func () {
60
+ ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
61
+ Expect (err ).NotTo (HaveOccurred ())
62
+ Expect (ccid ).To (Equal ("definition-ccid" ))
63
+ Expect (cctype ).To (Equal (pb .ChaincodeMessage_TRANSACTION ))
64
+ })
55
65
66
+ Context ("when the invocation is an init" , func () {
56
67
BeforeEach (func () {
57
- chaincodeDefinition = & lifecycle.LegacyDefinition {
58
- Version : "definition-version" ,
59
- ChaincodeIDField : "definition-ccid" ,
60
- }
61
-
62
- fakeLifecycle .ChaincodeDefinitionReturns (chaincodeDefinition , nil )
68
+ input .IsInit = true
63
69
})
64
70
65
- It ("fetches the definition and skips the legacy security checks" , func () {
66
- ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
67
- Expect (err ).NotTo (HaveOccurred ())
68
- Expect (ccid ).To (Equal ("definition-ccid" ))
69
- Expect (cctype ).To (Equal (pb .ChaincodeMessage_TRANSACTION ))
70
-
71
- Expect (fakeLifecycle .ChaincodeDefinitionCallCount ()).To (Equal (1 ))
72
- channelID , chaincodeName , txSim := fakeLifecycle .ChaincodeDefinitionArgsForCall (0 )
73
- Expect (channelID ).To (Equal ("channel-id" ))
74
- Expect (chaincodeName ).To (Equal ("test-chaincode-name" ))
75
- Expect (txSim ).To (Equal (fakeSimulator ))
71
+ It ("returns an error for chaincodes which do not require init" , func () {
72
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
73
+ Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' does not require initialization but called as init" ))
76
74
})
77
75
78
- Context ("when the invocation is an init" , func () {
76
+ Context ("when the chaincode requires init be enforced " , func () {
79
77
BeforeEach (func () {
80
- input . IsInit = true
78
+ invokeInfo . EnforceInit = true
81
79
})
82
80
83
- It ("returns an error for chaincodes which do not require init" , func () {
84
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
85
- Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' does not require initialization but called as init" ))
81
+ It ("enforces init exactly once semantics" , func () {
82
+ ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
83
+ Expect (err ).NotTo (HaveOccurred ())
84
+ Expect (ccid ).To (Equal ("definition-ccid" ))
85
+ Expect (cctype ).To (Equal (pb .ChaincodeMessage_INIT ))
86
+
87
+ Expect (fakeSimulator .GetStateCallCount ()).To (Equal (1 ))
88
+ namespace , key := fakeSimulator .GetStateArgsForCall (0 )
89
+ Expect (namespace ).To (Equal ("test-chaincode-name" ))
90
+ Expect (key ).To (Equal ("\x00 " + string (utf8 .MaxRune ) + "initialized" ))
91
+
92
+ Expect (fakeSimulator .SetStateCallCount ()).To (Equal (1 ))
93
+ namespace , key , value := fakeSimulator .SetStateArgsForCall (0 )
94
+ Expect (namespace ).To (Equal ("test-chaincode-name" ))
95
+ Expect (key ).To (Equal ("\x00 " + string (utf8 .MaxRune ) + "initialized" ))
96
+ Expect (value ).To (Equal ([]byte ("definition-version" )))
86
97
})
87
98
88
- Context ("when the chaincode requires init" , func () {
99
+ Context ("when the invocation is not an init" , func () {
89
100
BeforeEach (func () {
90
- chaincodeDefinition . RequiresInitField = true
101
+ input . IsInit = false
91
102
})
92
103
93
- It ("enforces init exactly once semantics" , func () {
94
- ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
95
- Expect (err ).NotTo (HaveOccurred ())
96
- Expect (ccid ).To (Equal ("definition-ccid" ))
97
- Expect (cctype ).To (Equal (pb .ChaincodeMessage_INIT ))
98
-
99
- Expect (fakeSimulator .GetStateCallCount ()).To (Equal (1 ))
100
- namespace , key := fakeSimulator .GetStateArgsForCall (0 )
101
- Expect (namespace ).To (Equal ("test-chaincode-name" ))
102
- Expect (key ).To (Equal ("\x00 " + string (utf8 .MaxRune ) + "initialized" ))
103
-
104
- Expect (fakeSimulator .SetStateCallCount ()).To (Equal (1 ))
105
- namespace , key , value := fakeSimulator .SetStateArgsForCall (0 )
106
- Expect (namespace ).To (Equal ("test-chaincode-name" ))
107
- Expect (key ).To (Equal ("\x00 " + string (utf8 .MaxRune ) + "initialized" ))
108
- Expect (value ).To (Equal ([]byte ("definition-version" )))
104
+ It ("returns an error" , func () {
105
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
106
+ Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' has not been initialized for this version, must call as init first" ))
109
107
})
108
+ })
110
109
111
- Context ("when the invocation is not an init" , func () {
112
- BeforeEach (func () {
113
- input .IsInit = false
114
- })
115
-
116
- It ("returns an error" , func () {
117
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
118
- Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' has not been initialized for this version, must call as init first" ))
119
- })
110
+ Context ("when the chaincode is already initialized" , func () {
111
+ BeforeEach (func () {
112
+ fakeSimulator .GetStateReturns ([]byte ("definition-version" ), nil )
120
113
})
121
114
122
- Context ("when the chaincode is already initialized" , func () {
123
- BeforeEach (func () {
124
- fakeSimulator .GetStateReturns ([]byte ("definition-version" ), nil )
125
- })
126
-
127
- It ("returns an error" , func () {
128
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
129
- Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' is already initialized but called as init" ))
130
- })
115
+ It ("returns an error" , func () {
116
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
117
+ Expect (err ).To (MatchError ("chaincode 'test-chaincode-name' is already initialized but called as init" ))
131
118
})
119
+ })
132
120
133
- Context ("when the txsimulator cannot get state" , func () {
134
- BeforeEach (func () {
135
- fakeSimulator .GetStateReturns (nil , fmt .Errorf ("get-state-error" ))
136
- })
137
-
138
- It ("wraps and returns the error" , func () {
139
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
140
- Expect (err ).To (MatchError ("could not get 'initialized' key: get-state-error" ))
141
- })
121
+ Context ("when the txsimulator cannot get state" , func () {
122
+ BeforeEach (func () {
123
+ fakeSimulator .GetStateReturns (nil , fmt .Errorf ("get-state-error" ))
142
124
})
143
125
144
- Context ("when the txsimulator cannot set state" , func () {
145
- BeforeEach (func () {
146
- fakeSimulator .SetStateReturns (fmt .Errorf ("set-state-error" ))
147
- })
148
-
149
- It ("wraps and returns the error" , func () {
150
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
151
- Expect (err ).To (MatchError ("could not set 'initialized' key: set-state-error" ))
152
- })
126
+ It ("wraps and returns the error" , func () {
127
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
128
+ Expect (err ).To (MatchError ("could not get 'initialized' key: get-state-error" ))
153
129
})
154
130
})
155
131
156
- })
157
- })
158
-
159
- Describe ("CheckInvocation for a legacy (lscc) chaincode definition" , func () {
160
- var (
161
- fakeLegacyDefinition * mock.LegacyChaincodeDefinition
162
- )
163
-
164
- BeforeEach (func () {
165
- input .IsInit = false
166
- fakeLegacyDefinition = & mock.LegacyChaincodeDefinition {}
167
-
168
- ccDef := struct {
169
- * ccprovider.ChaincodeData
170
- * mock.LegacyChaincodeDefinition
171
- }{
172
- ChaincodeData : & ccprovider.ChaincodeData {
173
- Name : "definition-name" ,
174
- Version : "cc-version" ,
175
- Id : []byte ("id" ),
176
- },
177
- LegacyChaincodeDefinition : fakeLegacyDefinition ,
178
- }
179
-
180
- fakeLifecycle .ChaincodeDefinitionReturns (ccDef , nil )
181
- })
182
-
183
- It ("fetches the definition, performs security checks, and skips the init checks" , func () {
184
- ccid , cctype , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
185
- Expect (err ).NotTo (HaveOccurred ())
186
- Expect (ccid ).To (Equal ("definition-name:cc-version" ))
187
- Expect (cctype ).To (Equal (pb .ChaincodeMessage_TRANSACTION ))
188
-
189
- Expect (fakeLifecycle .ChaincodeDefinitionCallCount ()).To (Equal (1 ))
190
- channelID , chaincodeName , txSim := fakeLifecycle .ChaincodeDefinitionArgsForCall (0 )
191
- Expect (channelID ).To (Equal ("channel-id" ))
192
- Expect (chaincodeName ).To (Equal ("test-chaincode-name" ))
193
- Expect (txSim ).To (Equal (fakeSimulator ))
194
-
195
- Expect (fakeLegacyDefinition .ExecuteLegacySecurityChecksCallCount ()).To (Equal (1 ))
196
- })
197
-
198
- Context ("when the legacy security check fails" , func () {
199
- BeforeEach (func () {
200
- fakeLegacyDefinition .ExecuteLegacySecurityChecksReturns (fmt .Errorf ("fake-security-error" ))
201
- })
132
+ Context ("when the txsimulator cannot set state" , func () {
133
+ BeforeEach (func () {
134
+ fakeSimulator .SetStateReturns (fmt .Errorf ("set-state-error" ))
135
+ })
202
136
203
- It ("wraps and returns the error" , func () {
204
- _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
205
- Expect (err ).To (MatchError ("[channel channel-id] failed the chaincode security checks for test-chaincode-name: fake-security-error" ))
137
+ It ("wraps and returns the error" , func () {
138
+ _ , _ , err := chaincodeSupport .CheckInvocation (txParams , "test-chaincode-name" , input )
139
+ Expect (err ).To (MatchError ("could not set 'initialized' key: set-state-error" ))
140
+ })
206
141
})
207
142
})
208
143
})
209
144
210
145
Context ("when lifecycle returns an error" , func () {
211
146
BeforeEach (func () {
212
- fakeLifecycle .ChaincodeDefinitionReturns (nil , fmt .Errorf ("fake-lifecycle-error" ))
147
+ fakeLifecycle .ChaincodeEndorsementInfoReturns (nil , fmt .Errorf ("fake-lifecycle-error" ))
213
148
})
214
149
215
150
It ("wraps and returns the error" , func () {
0 commit comments