Skip to content

Commit 81b05aa

Browse files
author
Jason Yellick
committed
FAB-13511 Wire QueryDefinedNamespaces to SCC
This simply exposes the ability to query all defined namespaces already implemented in lifecycle through the new lifecycle SCC. Change-Id: If7e8e27f09eb1f793f5e390c5cece67e67badd17 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 9a9d2b9 commit 81b05aa

File tree

5 files changed

+319
-49
lines changed

5 files changed

+319
-49
lines changed

core/chaincode/lifecycle/mock/scc_functions.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/chaincode/lifecycle/scc.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const (
4141
// QueryDefinedChaincodeFuncName is the chaincode function name used to 'define' (previously 'instantiate')
4242
// a chaincode in a channel.
4343
QueryDefinedChaincodeFuncName = "QueryDefinedChaincode"
44+
45+
// QueryDefinedNamespaces is the chaincode function name used query which namespaces are currently defined
46+
// and what type those namespaces are.
47+
QueryDefinedNamespacesFuncName = "QueryDefinedNamespaces"
4448
)
4549

4650
// SCCFunctions provides a backing implementation with concrete arguments
@@ -63,6 +67,9 @@ type SCCFunctions interface {
6367

6468
// QueryDefinedChaincode reads a chaincode definition from the public state.
6569
QueryDefinedChaincode(name string, publicState ReadableState) (*DefinedChaincode, error)
70+
71+
// QueryDefinedNamespaces returns all defined namespaces
72+
QueryDefinedNamespaces(publicState RangeableState) (map[string]string, error)
6673
}
6774

6875
//go:generate counterfeiter -o mock/channel_config_source.go --fake-name ChannelConfigSource . ChannelConfigSource
@@ -309,3 +316,19 @@ func (i *Invocation) QueryDefinedChaincode(input *lb.QueryDefinedChaincodeArgs)
309316
Hash: definedChaincode.Hash,
310317
}, nil
311318
}
319+
320+
func (i *Invocation) QueryDefinedNamespaces(input *lb.QueryDefinedNamespacesArgs) (proto.Message, error) {
321+
namespaces, err := i.SCC.Functions.QueryDefinedNamespaces(&ChaincodePublicLedgerShim{ChaincodeStubInterface: i.Stub})
322+
if err != nil {
323+
return nil, err
324+
}
325+
result := map[string]*lb.QueryDefinedNamespacesResult_Namespace{}
326+
for namespace, nType := range namespaces {
327+
result[namespace] = &lb.QueryDefinedNamespacesResult_Namespace{
328+
Type: nType,
329+
}
330+
}
331+
return &lb.QueryDefinedNamespacesResult{
332+
Namespaces: result,
333+
}, nil
334+
}

core/chaincode/lifecycle/scc_test.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ var _ = Describe("SCC", func() {
479479
})
480480
})
481481

482-
Describe("DefineChaincodeForMyOrg", func() {
482+
Describe("QueryDefinedChaincode", func() {
483483
var (
484484
arg *lb.QueryDefinedChaincodeArgs
485485
marshaledArg []byte
@@ -530,6 +530,50 @@ var _ = Describe("SCC", func() {
530530
})
531531
})
532532
})
533+
534+
Describe("QueryDefinedNamespaces", func() {
535+
var (
536+
arg *lb.QueryDefinedNamespacesArgs
537+
marshaledArg []byte
538+
)
539+
540+
BeforeEach(func() {
541+
arg = &lb.QueryDefinedNamespacesArgs{}
542+
543+
var err error
544+
marshaledArg, err = proto.Marshal(arg)
545+
Expect(err).NotTo(HaveOccurred())
546+
547+
fakeStub.GetArgsReturns([][]byte{[]byte("QueryDefinedNamespaces"), marshaledArg})
548+
fakeSCCFuncs.QueryDefinedNamespacesReturns(map[string]string{
549+
"foo": "Chaincode",
550+
"bar": "Token",
551+
}, nil)
552+
})
553+
554+
It("passes the arguments to and returns the results from the backing scc function implementation", func() {
555+
res := scc.Invoke(fakeStub)
556+
Expect(res.Status).To(Equal(int32(200)))
557+
payload := &lb.QueryDefinedNamespacesResult{}
558+
err := proto.Unmarshal(res.Payload, payload)
559+
Expect(err).NotTo(HaveOccurred())
560+
561+
Expect(fakeSCCFuncs.QueryDefinedNamespacesCallCount()).To(Equal(1))
562+
Expect(fakeSCCFuncs.QueryDefinedNamespacesArgsForCall(0)).To(Equal(&lifecycle.ChaincodePublicLedgerShim{ChaincodeStubInterface: fakeStub}))
563+
})
564+
565+
Context("when the underlying function implementation fails", func() {
566+
BeforeEach(func() {
567+
fakeSCCFuncs.QueryDefinedNamespacesReturns(nil, fmt.Errorf("underlying-error"))
568+
})
569+
570+
It("wraps and returns the error", func() {
571+
res := scc.Invoke(fakeStub)
572+
Expect(res.Status).To(Equal(int32(500)))
573+
Expect(res.Message).To(Equal("failed to invoke backing implementation of 'QueryDefinedNamespaces': underlying-error"))
574+
})
575+
})
576+
})
533577
})
534578

535579
})

0 commit comments

Comments
 (0)