Skip to content

Commit c126821

Browse files
author
Jason Yellick
committed
FAB-13988 Retrieve ApplicationConfig earlier
Presently, only one of the invokes requires the application config. However, this is going to change, and, we need access to it earlier to add capability checks. This CR refactors the code to gain access to this config earlier. Change-Id: I9859f97a07191e35356e985df1827d0adc9851ea Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent bc37cdf commit c126821

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

core/chaincode/lifecycle/integration_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ var _ = Describe("Integration", func() {
7070
fakeOrgKVStore = map[string][]byte{}
7171

7272
fakeStub = &mock.ChaincodeStub{}
73+
74+
fakeStub.GetChannelIDReturns("test-channel")
75+
7376
fakeStub.GetStateStub = func(key string) ([]byte, error) {
7477
return fakePublicKVStore[key], nil
7578
}
@@ -139,6 +142,7 @@ var _ = Describe("Integration", func() {
139142
}),
140143
})
141144
response = scc.Invoke(fakeStub)
145+
Expect(response.Message).To(Equal(""))
142146
Expect(response.Status).To(Equal(int32(200)))
143147

144148
// Get channel definitions

core/chaincode/lifecycle/scc.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,28 @@ func (scc *SCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
154154
return shim.Error(fmt.Sprintf("lifecycle scc operations require exactly two arguments but received %d", len(args)))
155155
}
156156

157+
var ac channelconfig.Application
158+
if channelID := stub.GetChannelID(); channelID != "" {
159+
channelConfig := scc.ChannelConfigSource.GetStableChannelConfig(channelID)
160+
if channelConfig == nil {
161+
return shim.Error(fmt.Sprintf("could not get channelconfig for channel '%s'", channelID))
162+
}
163+
var ok bool
164+
ac, ok = channelConfig.ApplicationConfig()
165+
if !ok {
166+
return shim.Error(fmt.Sprintf("could not get application config for channel '%s'", channelID))
167+
}
168+
}
169+
157170
// TODO add ACLs
158171

159172
outputBytes, err := scc.Dispatcher.Dispatch(
160173
args[1],
161174
string(args[0]),
162175
&Invocation{
163-
SCC: scc,
164-
Stub: stub,
176+
ApplicationConfig: ac,
177+
SCC: scc,
178+
Stub: stub,
165179
},
166180
)
167181
if err != nil {
@@ -172,8 +186,9 @@ func (scc *SCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
172186
}
173187

174188
type Invocation struct {
175-
Stub shim.ChaincodeStubInterface
176-
SCC *SCC
189+
ApplicationConfig channelconfig.Application // Note this may be nil
190+
Stub shim.ChaincodeStubInterface
191+
SCC *SCC
177192
}
178193

179194
// InstallChaincode is a SCC function that may be dispatched to which routes to the underlying
@@ -250,16 +265,11 @@ func (i *Invocation) ApproveChaincodeDefinitionForMyOrg(input *lb.ApproveChainco
250265
}
251266

252267
func (i *Invocation) CommitChaincodeDefinition(input *lb.CommitChaincodeDefinitionArgs) (proto.Message, error) {
253-
channelConfig := i.SCC.ChannelConfigSource.GetStableChannelConfig(i.Stub.GetChannelID())
254-
if channelConfig == nil {
255-
return nil, errors.Errorf("could not get channelconfig for channel %s", i.Stub.GetChannelID())
256-
}
257-
ac, ok := channelConfig.ApplicationConfig()
258-
if !ok {
259-
return nil, errors.Errorf("could not get application config for channel %s", i.Stub.GetChannelID())
268+
if i.ApplicationConfig == nil {
269+
return nil, errors.Errorf("no application config for channel '%s'", i.Stub.GetChannelID())
260270
}
261271

262-
orgs := ac.Organizations()
272+
orgs := i.ApplicationConfig.Organizations()
263273
opaqueStates := make([]OpaqueState, 0, len(orgs))
264274
myOrgIndex := -1
265275
for _, org := range orgs {

core/chaincode/lifecycle/scc_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ var _ = Describe("SCC", func() {
104104

105105
BeforeEach(func() {
106106
fakeStub = &mock.ChaincodeStub{}
107+
fakeStub.GetChannelIDReturns("test-channel")
107108
})
108109

109110
Context("when no arguments are provided", func() {
@@ -139,6 +140,8 @@ var _ = Describe("SCC", func() {
139140
)
140141

141142
BeforeEach(func() {
143+
fakeStub.GetChannelIDReturns("")
144+
142145
arg = &lb.InstallChaincodeArgs{
143146
Name: "name",
144147
Version: "version",
@@ -393,6 +396,7 @@ var _ = Describe("SCC", func() {
393396

394397
It("passes the arguments to and returns the results from the backing scc function implementation", func() {
395398
res := scc.Invoke(fakeStub)
399+
Expect(res.Message).To(Equal(""))
396400
Expect(res.Status).To(Equal(int32(200)))
397401
payload := &lb.CommitChaincodeDefinitionResult{}
398402
err = proto.Unmarshal(res.Payload, payload)
@@ -451,7 +455,7 @@ var _ = Describe("SCC", func() {
451455
It("returns an error indicating the lack of agreement", func() {
452456
res := scc.Invoke(fakeStub)
453457
Expect(res.Status).To(Equal(int32(500)))
454-
Expect(res.Message).To(Equal("failed to invoke backing implementation of 'CommitChaincodeDefinition': could not get channelconfig for channel "))
458+
Expect(res.Message).To(Equal("could not get channelconfig for channel 'test-channel'"))
455459
})
456460
})
457461

@@ -463,7 +467,19 @@ var _ = Describe("SCC", func() {
463467
It("returns an error indicating the lack of agreement", func() {
464468
res := scc.Invoke(fakeStub)
465469
Expect(res.Status).To(Equal(int32(500)))
466-
Expect(res.Message).To(Equal("failed to invoke backing implementation of 'CommitChaincodeDefinition': could not get application config for channel "))
470+
Expect(res.Message).To(Equal("could not get application config for channel 'test-channel'"))
471+
})
472+
473+
Context("when there is no application config yet somehow the sentinal is wrong", func() {
474+
BeforeEach(func() {
475+
fakeChannelConfig.ApplicationConfigReturns(nil, true)
476+
})
477+
478+
It("returns an error indicating the lack of agreement", func() {
479+
res := scc.Invoke(fakeStub)
480+
Expect(res.Status).To(Equal(int32(500)))
481+
Expect(res.Message).To(Equal("failed to invoke backing implementation of 'CommitChaincodeDefinition': no application config for channel 'test-channel'"))
482+
})
467483
})
468484
})
469485

0 commit comments

Comments
 (0)