Skip to content

Commit 679a32e

Browse files
committed
FAB-11201 shim pkg. provider to support both cc pkgs
This CR adds a new shim package provider which retrieves the code package for a given chaincode name/version and updates the peer to use it at startup. It first searches for a ChaincodeInstallPackage with the given name/version and falls back to searching for a ChaincodeDeploymentSpec. FAB-11201 #done Change-Id: I064dc2b364a1c8e1280ec1f2b3ffc21c8b3e2e96 Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent 3d72b63 commit 679a32e

File tree

11 files changed

+534
-13
lines changed

11 files changed

+534
-13
lines changed

core/chaincode/chaincode_support_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func initMockPeer(chainIDs ...string) (*ChaincodeSupport, error) {
167167

168168
peer.MockSetMSPIDGetter(mspGetter)
169169

170-
ccprovider.SetChaincodesPath(ccprovider.GetCCsPath())
170+
ccprovider.SetChaincodesPath(ccprovider.GetChaincodeInstallPathFromViper())
171171
ca, _ := tlsgen.NewCA()
172172
certGenerator := accesscontrol.NewAuthenticator(ca)
173173
config := GlobalConfig()

core/chaincode/exectransaction_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func initPeer(chainIDs ...string) (net.Listener, *ChaincodeSupport, func(), erro
114114
return nil, nil, nil, fmt.Errorf("Error starting peer listener %s", err)
115115
}
116116

117-
ccprovider.SetChaincodesPath(ccprovider.GetCCsPath())
117+
ccprovider.SetChaincodesPath(ccprovider.GetChaincodeInstallPathFromViper())
118118
ca, _ := tlsgen.NewCA()
119119
certGenerator := accesscontrol.NewAuthenticator(ca)
120120
config := GlobalConfig()

core/chaincode/persistence/mock/legacy_package_provider.go

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

core/chaincode/persistence/mock/store_package_provider.go

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package persistence
8+
9+
import (
10+
"github.com/pkg/errors"
11+
)
12+
13+
// StorePackageProvider is the interface needed to retrieve
14+
// the code package from a ChaincodeInstallPackage
15+
type StorePackageProvider interface {
16+
Load(hash []byte) (codePackage []byte, name, version string, err error)
17+
RetrieveHash(name, version string) (hash []byte, err error)
18+
}
19+
20+
// LegacyPackageProvider is the interface needed to retrieve
21+
// the code package from a ChaincodeDeploymentSpec
22+
type LegacyPackageProvider interface {
23+
GetChaincodeCodePackage(name, version string) (codePackage []byte, err error)
24+
}
25+
26+
// PackageProvider holds the necessary dependencies to obtain the code
27+
// package bytes for a chaincode
28+
type PackageProvider struct {
29+
Store StorePackageProvider
30+
LegacyPP LegacyPackageProvider
31+
}
32+
33+
// GetChaincodeCodePackage gets the code package bytes for a chaincode given
34+
// the name and version. It first searches through the persisted
35+
// ChaincodeInstallPackages and then falls back to searching for
36+
// ChaincodeDeploymentSpecs
37+
func (p *PackageProvider) GetChaincodeCodePackage(name, version string) ([]byte, error) {
38+
codePackage, err := p.getCodePackageFromStore(name, version)
39+
if err == nil {
40+
return codePackage, nil
41+
}
42+
if _, ok := err.(*CodePackageNotFoundErr); !ok {
43+
// return the error if the hash cannot be retrieved or the code package
44+
// fails to load from the persistence store
45+
return nil, err
46+
}
47+
48+
codePackage, err = p.getCodePackageFromLegacyPP(name, version)
49+
if err != nil {
50+
logger.Debug(err.Error())
51+
err = errors.Errorf("code package not found for chaincode with name '%s', version '%s'", name, version)
52+
return nil, err
53+
}
54+
return codePackage, nil
55+
}
56+
57+
// GetCodePackageFromStore gets the code package bytes from the package
58+
// provider's Store, which persists ChaincodeInstallPackages
59+
func (p *PackageProvider) getCodePackageFromStore(name, version string) ([]byte, error) {
60+
hash, err := p.Store.RetrieveHash(name, version)
61+
if _, ok := err.(*CodePackageNotFoundErr); ok {
62+
return nil, err
63+
}
64+
if err != nil {
65+
return nil, errors.WithMessage(err, "error retrieving hash")
66+
}
67+
68+
codePackage, _, _, err := p.Store.Load(hash)
69+
if err != nil {
70+
return nil, errors.WithMessage(err, "error loading code package from ChaincodeInstallPackage")
71+
}
72+
return codePackage, nil
73+
}
74+
75+
// GetCodePackageFromLegacyPP gets the code packages bytes from the
76+
// legacy package provider, which persists ChaincodeDeploymentSpecs
77+
func (p *PackageProvider) getCodePackageFromLegacyPP(name, version string) ([]byte, error) {
78+
codePackage, err := p.LegacyPP.GetChaincodeCodePackage(name, version)
79+
if err != nil {
80+
return nil, errors.Wrap(err, "error loading code package from ChaincodeDeploymentSpec")
81+
}
82+
return codePackage, nil
83+
}

0 commit comments

Comments
 (0)