Skip to content

Commit 73b5c72

Browse files
author
Jason Yellick
committed
FAB-16158 Move CCContext to be test only artifact
No one outside of the core/chaincode package referred to the CCContext type, and this type was only used to pass 3 fields as a return from one function to a parameter of another. This CR gets rid of One of these fields (InitRequired) through a slight refactor, and then passes the other two (name and version) as explicit parameters. Despite the parameter lists becoming a little bit long, this still seems to be the cleaner approach. If we would like to consolidate parameters, I would consider a larger unifying structure, though coming up with one that makes sense was not obvious during this reading. Change-Id: I5e8dd26fbec7864fdbe4a8e4cd263327778ebcc2 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent f4259bf commit 73b5c72

File tree

4 files changed

+145
-150
lines changed

4 files changed

+145
-150
lines changed

core/chaincode/chaincode_ginkgo_support_test.go

Lines changed: 110 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/hyperledger/fabric/core/chaincode"
1414
"github.com/hyperledger/fabric/core/chaincode/fake"
15+
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
1516
"github.com/hyperledger/fabric/core/chaincode/mock"
1617
"github.com/hyperledger/fabric/core/common/ccprovider"
1718
pb "github.com/hyperledger/fabric/protos/peer"
@@ -28,7 +29,10 @@ var _ = Describe("ChaincodeSupport", func() {
2829
fakeApplicationConfig *mock.ApplicationConfig
2930
fakeApplicationCapabilities *mock.ApplicationCapabilities
3031
fakeLifecycle *mock.Lifecycle
31-
fakeTxSim *mock.TxSimulator
32+
fakeSimulator *mock.TxSimulator
33+
34+
txParams *ccprovider.TransactionParams
35+
input *pb.ChaincodeInput
3236
)
3337

3438
BeforeEach(func() {
@@ -42,7 +46,17 @@ var _ = Describe("ChaincodeSupport", func() {
4246
fakeApplicationConfigRetriever.GetApplicationConfigReturns(fakeApplicationConfig, true)
4347

4448
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+
}
4660

4761
chaincodeSupport = &chaincode.ChaincodeSupport{
4862
AppConfig: fakeApplicationConfigRetriever,
@@ -51,54 +65,95 @@ var _ = Describe("ChaincodeSupport", func() {
5165
})
5266

5367
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+
})
7577

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+
})
92108
})
93109

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+
95115
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)
97131
})
98132

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+
})
102157
})
103158
})
104159

@@ -108,43 +163,15 @@ var _ = Describe("ChaincodeSupport", func() {
108163
})
109164

110165
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"))
113168
})
114169
})
115170
})
116171

117172
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-
146173
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)
148175
Expect(err).NotTo(HaveOccurred())
149176
Expect(isInit).To(BeTrue())
150177

@@ -161,12 +188,8 @@ var _ = Describe("ChaincodeSupport", func() {
161188
})
162189

163190
Context("when the version is not changed", func() {
164-
BeforeEach(func() {
165-
cccid.Version = "old-cc-version"
166-
})
167-
168191
It("returns an error", func() {
169-
_, err := chaincodeSupport.CheckInit(txParams, cccid, input)
192+
_, err := chaincodeSupport.CheckInit(txParams, "cc-name", "old-cc-version", input)
170193
Expect(err).To(MatchError("chaincode 'cc-name' is already initialized but called as init"))
171194
})
172195

@@ -176,7 +199,7 @@ var _ = Describe("ChaincodeSupport", func() {
176199
})
177200

178201
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)
180203
Expect(err).NotTo(HaveOccurred())
181204
Expect(isInit).To(BeFalse())
182205
Expect(fakeSimulator.GetStateCallCount()).To(Equal(1))
@@ -185,27 +208,13 @@ var _ = Describe("ChaincodeSupport", func() {
185208
})
186209
})
187210

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-
202211
Context("when the new lifecycle is not enabled", func() {
203212
BeforeEach(func() {
204213
fakeApplicationCapabilities.LifecycleV20Returns(false)
205214
})
206215

207216
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)
209218
Expect(err).NotTo(HaveOccurred())
210219
Expect(isInit).To(BeFalse())
211220
Expect(fakeSimulator.GetStateCallCount()).To(Equal(0))
@@ -219,7 +228,7 @@ var _ = Describe("ChaincodeSupport", func() {
219228
})
220229

221230
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)
223232
Expect(err).NotTo(HaveOccurred())
224233
Expect(isInit).To(BeFalse())
225234
Expect(fakeSimulator.GetStateCallCount()).To(Equal(0))
@@ -233,7 +242,7 @@ var _ = Describe("ChaincodeSupport", func() {
233242
})
234243

235244
It("returns an error", func() {
236-
_, err := chaincodeSupport.CheckInit(txParams, cccid, input)
245+
_, err := chaincodeSupport.CheckInit(txParams, "cc-name", "cc-version", input)
237246
Expect(err).To(MatchError("could not retrieve application config for channel 'channel-id'"))
238247
})
239248
})
@@ -244,7 +253,7 @@ var _ = Describe("ChaincodeSupport", func() {
244253
})
245254

246255
It("returns an error", func() {
247-
_, err := chaincodeSupport.CheckInit(txParams, cccid, input)
256+
_, err := chaincodeSupport.CheckInit(txParams, "cc-name", "cc-version", input)
248257
Expect(err).To(MatchError("chaincode 'cc-name' has not been initialized for this version, must call as init first"))
249258
})
250259
})
@@ -255,7 +264,7 @@ var _ = Describe("ChaincodeSupport", func() {
255264
})
256265

257266
It("wraps and returns the error", func() {
258-
_, err := chaincodeSupport.CheckInit(txParams, cccid, input)
267+
_, err := chaincodeSupport.CheckInit(txParams, "cc-name", "cc-version", input)
259268
Expect(err).To(MatchError("could not get 'initialized' key: get-state-error"))
260269
})
261270
})
@@ -266,7 +275,7 @@ var _ = Describe("ChaincodeSupport", func() {
266275
})
267276

268277
It("wraps and returns the error", func() {
269-
_, err := chaincodeSupport.CheckInit(txParams, cccid, input)
278+
_, err := chaincodeSupport.CheckInit(txParams, "cc-name", "cc-version", input)
270279
Expect(err).To(MatchError("could not set 'initialized' key: set-state-error"))
271280
})
272281
})

core/chaincode/chaincode_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ type invoker interface {
5959
chaincode.Invoker
6060
}
6161

62-
//go:generate counterfeiter -o mock/lifecycle.go --fake-name Lifecycle . lifecycle
63-
type lifecycle interface {
62+
//go:generate counterfeiter -o mock/lifecycle.go --fake-name Lifecycle . lifecycle_
63+
type lifecycle_ interface {
6464
chaincode.Lifecycle
6565
}
6666

0 commit comments

Comments
 (0)