Skip to content

Commit 8b66090

Browse files
author
Jason Yellick
committed
FAB-16158 Get chaincode package without CCCI dep
For legacy reasons which will be eliminated in a future CR, it's necessary to retrieve the container info for chaincodes at every invocation. We then pass the container info down to the container package, but this info is effectively read again when retreiving the code package. This is an intermediate step to eliminating the container info retreival call. Change-Id: Iad1a8507be6f87a352750348dfb6ea151180e63c Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 9993b96 commit 8b66090

File tree

8 files changed

+149
-106
lines changed

8 files changed

+149
-106
lines changed

core/chaincode/chaincode_support_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import (
3131
"github.com/hyperledger/fabric/core/aclmgmt/resources"
3232
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
3333
"github.com/hyperledger/fabric/core/chaincode/mock"
34-
persistence "github.com/hyperledger/fabric/core/chaincode/persistence/intf"
34+
"github.com/hyperledger/fabric/core/chaincode/persistence"
35+
pintf "github.com/hyperledger/fabric/core/chaincode/persistence/intf"
3536
"github.com/hyperledger/fabric/core/chaincode/platforms"
3637
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
3738
"github.com/hyperledger/fabric/core/chaincode/shim"
@@ -180,13 +181,13 @@ func initMockPeer(chainIDs ...string) (*peer.Peer, *ChaincodeSupport, func(), er
180181
return &ccprovider.ChaincodeContainerInfo{
181182
Name: name,
182183
Version: "0",
183-
PackageID: persistence.PackageID(name + ":0"),
184+
PackageID: pintf.PackageID(name + ":0"),
184185
}, nil
185186
case "lscc":
186187
return &ccprovider.ChaincodeContainerInfo{
187188
Name: "lscc",
188189
Version: "latest",
189-
PackageID: persistence.PackageID("lscc:latest"),
190+
PackageID: pintf.PackageID("lscc:latest"),
190191
}, nil
191192
default:
192193
return nil, errors.New("oh-bother-no-chaincode-info")
@@ -210,7 +211,10 @@ func initMockPeer(chainIDs ...string) (*peer.Peer, *ChaincodeSupport, func(), er
210211
Client: client,
211212
},
212213
},
213-
PackageProvider: &PackageProviderWrapper{FS: &ccprovider.CCInfoFSImpl{}},
214+
PackageProvider: &persistence.FallbackPackageLocator{
215+
ChaincodePackageLocator: &persistence.ChaincodePackageLocator{},
216+
LegacyCCPackageLocator: &ccprovider.CCInfoFSImpl{},
217+
},
214218
},
215219
}
216220
if !globalConfig.TLSEnabled {
@@ -897,7 +901,7 @@ func TestStartAndWaitSuccess(t *testing.T) {
897901
Type: "GOLANG",
898902
Name: "testcc",
899903
Version: "0",
900-
PackageID: persistence.PackageID("testcc:0"),
904+
PackageID: pintf.PackageID("testcc:0"),
901905
}
902906

903907
//actual test - everythings good

core/chaincode/exectransaction_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ import (
3838
"github.com/hyperledger/fabric/core/aclmgmt"
3939
"github.com/hyperledger/fabric/core/chaincode/mock"
4040
cm "github.com/hyperledger/fabric/core/chaincode/mock"
41-
persistence "github.com/hyperledger/fabric/core/chaincode/persistence/intf"
41+
"github.com/hyperledger/fabric/core/chaincode/persistence"
42+
pintf "github.com/hyperledger/fabric/core/chaincode/persistence/intf"
4243
"github.com/hyperledger/fabric/core/chaincode/platforms"
4344
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
4445
"github.com/hyperledger/fabric/core/chaincode/shim"
@@ -112,13 +113,13 @@ func initPeer(chainIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSuppor
112113
return &ccprovider.ChaincodeContainerInfo{
113114
Name: "lscc",
114115
Version: "latest",
115-
PackageID: persistence.PackageID("lscc:latest"),
116+
PackageID: pintf.PackageID("lscc:latest"),
116117
}, nil
117118
default:
118119
return &ccprovider.ChaincodeContainerInfo{
119120
Name: name,
120121
Version: "0",
121-
PackageID: persistence.PackageID(name + ":0"),
122+
PackageID: pintf.PackageID(name + ":0"),
122123
}, nil
123124
}
124125
}
@@ -163,7 +164,10 @@ func initPeer(chainIDs ...string) (*cm.Lifecycle, net.Listener, *ChaincodeSuppor
163164
Client: client,
164165
},
165166
},
166-
PackageProvider: &PackageProviderWrapper{FS: &ccprovider.CCInfoFSImpl{}},
167+
PackageProvider: &persistence.FallbackPackageLocator{
168+
ChaincodePackageLocator: &persistence.ChaincodePackageLocator{},
169+
LegacyCCPackageLocator: &ccprovider.CCInfoFSImpl{},
170+
},
167171
},
168172
PeerAddress: peerAddress,
169173
}
@@ -742,7 +746,7 @@ func TestChaincodeInvokeChaincode(t *testing.T) {
742746

743747
func stopChaincode(chaincodeCtx *ccprovider.CCContext, chaincodeSupport *ChaincodeSupport) {
744748
chaincodeSupport.Runtime.Stop(&ccprovider.ChaincodeContainerInfo{
745-
PackageID: persistence.PackageID(chaincodeCtx.Name + ":" + chaincodeCtx.Version),
749+
PackageID: pintf.PackageID(chaincodeCtx.Name + ":" + chaincodeCtx.Version),
746750
Name: chaincodeCtx.Name,
747751
Version: chaincodeCtx.Version,
748752
Type: "GOLANG",
@@ -777,7 +781,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
777781

778782
var nextBlockNumber uint64 = 1
779783
defer chaincodeSupport.Runtime.Stop(&ccprovider.ChaincodeContainerInfo{
780-
PackageID: persistence.PackageID(cID1.Name + ":" + cID1.Version),
784+
PackageID: pintf.PackageID(cID1.Name + ":" + cID1.Version),
781785
Name: cID1.Name,
782786
Version: cID1.Version,
783787
Path: cID1.Path,
@@ -808,7 +812,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
808812
}
809813

810814
defer chaincodeSupport.Runtime.Stop(&ccprovider.ChaincodeContainerInfo{
811-
PackageID: persistence.PackageID(cID2.Name + ":" + cID2.Version),
815+
PackageID: pintf.PackageID(cID2.Name + ":" + cID2.Version),
812816
Name: cID2.Name,
813817
Version: cID2.Version,
814818
Path: cID2.Path,
@@ -871,7 +875,7 @@ func TestChaincodeInit(t *testing.T) {
871875
}
872876

873877
defer chaincodeSupport.Runtime.Stop(&ccprovider.ChaincodeContainerInfo{
874-
PackageID: persistence.PackageID(cID.Name + ":" + cID.Version),
878+
PackageID: pintf.PackageID(cID.Name + ":" + cID.Version),
875879
Name: cID.Name,
876880
Version: cID.Version,
877881
Path: cID.Path,
@@ -931,7 +935,7 @@ func TestQueries(t *testing.T) {
931935
}
932936

933937
defer chaincodeSupport.Runtime.Stop(&ccprovider.ChaincodeContainerInfo{
934-
PackageID: persistence.PackageID(cID.Name + ":" + cID.Version),
938+
PackageID: pintf.PackageID(cID.Name + ":" + cID.Version),
935939
Name: cID.Name,
936940
Version: cID.Version,
937941
Path: cID.Path,

core/container/container.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ SPDX-License-Identifier: Apache-2.0
77
package container
88

99
import (
10+
"io"
11+
"io/ioutil"
1012
"sync"
1113

1214
"github.com/hyperledger/fabric/common/flogging"
15+
"github.com/hyperledger/fabric/core/chaincode/persistence"
16+
pintf "github.com/hyperledger/fabric/core/chaincode/persistence/intf"
1317
"github.com/hyperledger/fabric/core/common/ccprovider"
1418
"github.com/hyperledger/fabric/core/container/ccintf"
1519

@@ -54,7 +58,7 @@ func (UninitializedInstance) Wait() (int, error) {
5458

5559
// PackageProvider gets chaincode packages from the filesystem.
5660
type PackageProvider interface {
57-
GetChaincodeCodePackage(ccci *ccprovider.ChaincodeContainerInfo) ([]byte, error)
61+
GetChaincodePackage(packageID pintf.PackageID) (*persistence.ChaincodePackageMetadata, io.ReadCloser, error)
5862
}
5963

6064
type Router struct {
@@ -85,19 +89,27 @@ func (r *Router) getInstance(ccid ccintf.CCID) Instance {
8589
}
8690

8791
func (r *Router) Build(ccci *ccprovider.ChaincodeContainerInfo) error {
88-
codePackage, err := r.PackageProvider.GetChaincodeCodePackage(ccci)
92+
metadata, codeStream, err := r.PackageProvider.GetChaincodePackage(pintf.PackageID(ccci.PackageID))
8993
if err != nil {
9094
return errors.WithMessage(err, "get chaincode package failed")
9195
}
96+
defer codeStream.Close()
97+
98+
ccci2 := &ccprovider.ChaincodeContainerInfo{
99+
Path: metadata.Path,
100+
Type: metadata.Type,
101+
PackageID: ccci.PackageID,
102+
}
103+
codePackage, err := ioutil.ReadAll(codeStream)
92104

93105
var instance Instance
94106
var externalErr error
95107
if r.ExternalVM != nil {
96-
instance, externalErr = r.ExternalVM.Build(ccci, codePackage)
108+
instance, externalErr = r.ExternalVM.Build(ccci2, codePackage)
97109
}
98110

99111
if r.ExternalVM == nil || externalErr != nil {
100-
instance, err = r.DockerVM.Build(ccci, codePackage)
112+
instance, err = r.DockerVM.Build(ccci2, codePackage)
101113
}
102114

103115
if err != nil {
@@ -111,7 +123,7 @@ func (r *Router) Build(ccci *ccprovider.ChaincodeContainerInfo) error {
111123
r.containers = map[ccintf.CCID]Instance{}
112124
}
113125

114-
r.containers[ccintf.CCID(ccci.PackageID)] = instance
126+
r.containers[ccintf.CCID(ccci2.PackageID)] = instance
115127

116128
return nil
117129
}

core/container/container_test.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ SPDX-License-Identifier: Apache-2.0
77
package container_test
88

99
import (
10+
"bytes"
11+
"io/ioutil"
12+
1013
. "github.com/onsi/ginkgo"
1114
. "github.com/onsi/gomega"
1215

16+
"github.com/hyperledger/fabric/core/chaincode/persistence"
1317
"github.com/hyperledger/fabric/core/common/ccprovider"
1418
"github.com/hyperledger/fabric/core/container"
1519
"github.com/hyperledger/fabric/core/container/ccintf"
@@ -32,7 +36,14 @@ var _ = Describe("Container", func() {
3236
fakeExternalVM = &mock.VM{}
3337
fakeInstance = &mock.Instance{}
3438
fakePackageProvider = &mock.PackageProvider{}
35-
fakePackageProvider.GetChaincodeCodePackageReturns([]byte("code-bytes"), nil)
39+
fakePackageProvider.GetChaincodePackageReturns(
40+
&persistence.ChaincodePackageMetadata{
41+
Type: "package-type",
42+
Path: "package-path",
43+
},
44+
ioutil.NopCloser(bytes.NewBuffer([]byte("code-bytes"))),
45+
nil,
46+
)
3647

3748
router = &container.Router{
3849
DockerVM: fakeDockerVM,
@@ -49,7 +60,7 @@ var _ = Describe("Container", func() {
4960
It("passes through to the external impl", func() {
5061
err := router.Build(
5162
&ccprovider.ChaincodeContainerInfo{
52-
PackageID: "stop:name",
63+
PackageID: "package-id",
5364
Type: "type",
5465
Path: "path",
5566
Name: "name",
@@ -61,28 +72,24 @@ var _ = Describe("Container", func() {
6172
Expect(fakeExternalVM.BuildCallCount()).To(Equal(1))
6273
ccci, codePackage := fakeExternalVM.BuildArgsForCall(0)
6374
Expect(ccci).To(Equal(&ccprovider.ChaincodeContainerInfo{
64-
PackageID: "stop:name",
65-
Type: "type",
66-
Path: "path",
67-
Name: "name",
68-
Version: "version",
75+
PackageID: "package-id",
76+
Type: "package-type",
77+
Path: "package-path",
6978
}))
7079
Expect(codePackage).To(Equal([]byte("code-bytes")))
7180
})
7281

7382
Context("when the package provider returns an error", func() {
7483
BeforeEach(func() {
75-
fakePackageProvider.GetChaincodeCodePackageReturns(nil, errors.New("fake-package-error"))
84+
fakePackageProvider.GetChaincodePackageReturns(nil, nil, errors.New("fake-package-error"))
7685
})
7786

7887
It("wraps and returns the error", func() {
7988
err := router.Build(
8089
&ccprovider.ChaincodeContainerInfo{
81-
PackageID: "stop:name",
82-
Type: "type",
83-
Path: "path",
84-
Name: "name",
85-
Version: "version",
90+
PackageID: "package-id",
91+
Type: "package-type",
92+
Path: "package-path",
8693
},
8794
)
8895

@@ -99,23 +106,19 @@ var _ = Describe("Container", func() {
99106
It("falls back to the docker impl", func() {
100107
err := router.Build(
101108
&ccprovider.ChaincodeContainerInfo{
102-
PackageID: "stop:name",
103-
Type: "type",
104-
Path: "path",
105-
Name: "name",
106-
Version: "version",
109+
PackageID: "package-id",
110+
Type: "package-type",
111+
Path: "package-path",
107112
},
108113
)
109114
Expect(err).To(MatchError("failed external (fake-external-error) and docker build: fake-docker-error"))
110115
Expect(fakeExternalVM.BuildCallCount()).To(Equal(1))
111116
Expect(fakeDockerVM.BuildCallCount()).To(Equal(1))
112117
ccci, codePackage := fakeDockerVM.BuildArgsForCall(0)
113118
Expect(ccci).To(Equal(&ccprovider.ChaincodeContainerInfo{
114-
PackageID: "stop:name",
115-
Type: "type",
116-
Path: "path",
117-
Name: "name",
118-
Version: "version",
119+
PackageID: "package-id",
120+
Type: "package-type",
121+
Path: "package-path",
119122
}))
120123
Expect(codePackage).To(Equal([]byte("code-bytes")))
121124
})

core/container/dockercontroller/dockercontroller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,18 @@ func (vm *DockerVM) Build(ccci *ccprovider.ChaincodeContainerInfo, codePackage [
179179
return nil, err
180180
}
181181

182+
// This is an awkward translation, but better here in a future dead path
183+
// than elsewhere. The old enum types are capital, but at least as implemented
184+
// lifecycle tools seem to allow type to be set lower case.
185+
ccType := strings.ToUpper(ccci.Type)
186+
187+
ccciTranslated := *ccci
188+
ccciTranslated.Type = ccType
189+
182190
_, err = vm.Client.InspectImage(imageName)
183191
switch err {
184192
case docker.ErrNoSuchImage:
185-
dockerfileReader, err := vm.PlatformBuilder.GenerateDockerBuild(ccci, codePackage)
193+
dockerfileReader, err := vm.PlatformBuilder.GenerateDockerBuild(&ccciTranslated, codePackage)
186194
if err != nil {
187195
return nil, errors.Wrap(err, "platform builder failed")
188196
}
@@ -198,7 +206,7 @@ func (vm *DockerVM) Build(ccci *ccprovider.ChaincodeContainerInfo, codePackage [
198206
return &ContainerInstance{
199207
DockerVM: vm,
200208
CCID: ccid,
201-
Type: ccci.Type,
209+
Type: ccType,
202210
}, nil
203211
}
204212

core/container/dockercontroller/dockercontroller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestIntegrationPath(t *testing.T) {
6161

6262
assert.Equal(t, &ContainerInstance{
6363
CCID: ccintf.CCID("simple"),
64-
Type: "type",
64+
Type: "TYPE",
6565
DockerVM: &dc,
6666
}, instance)
6767

@@ -432,7 +432,7 @@ func TestBuild(t *testing.T) {
432432
require.Equal(t, 1, fakePlatformBuilder.GenerateDockerBuildCallCount())
433433
ccci, codePackage := fakePlatformBuilder.GenerateDockerBuildArgsForCall(0)
434434
assert.Equal(t, "chaincode-name:chaincode-version", string(ccci.PackageID))
435-
assert.Equal(t, "type", ccci.Type)
435+
assert.Equal(t, "TYPE", ccci.Type)
436436
assert.Equal(t, "path", ccci.Path)
437437
assert.Equal(t, "name", ccci.Name)
438438
assert.Equal(t, "version", ccci.Version)

0 commit comments

Comments
 (0)