Skip to content

Commit

Permalink
[FAB-3617] Add ChaincodePackageExists function
Browse files Browse the repository at this point in the history
Currently, we detect a chaincodepackage's existense by trying reading
its content. This is heavy when the package is large (very often).

This patchset add a ChaincodePackageExists function, to saves the effort by
detecting the existence using os.stat().

Change-Id: I99b16eeaee2bb6e80087e08f3ad136d01c044857
Signed-off-by: Baohua Yang <baohyang@cn.ibm.com>
  • Loading branch information
yeasy committed May 9, 2017
1 parent f533ae6 commit 78ce862
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 12 additions & 0 deletions core/common/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -98,6 +99,17 @@ func GetChaincodePackage(ccname string, ccversion string) ([]byte, error) {
return ccbytes, nil
}

//ChaincodePackageExists returns whether the chaincode package exists in the file system
func ChaincodePackageExists(ccname string, ccversion string) (bool, error) {
path := filepath.Join(chaincodeInstallPath, ccname+"."+ccversion)
_, err := os.Stat(path)
if err == nil {
// chaincodepackage already exists
return true, nil
}
return false, err
}

// GetChaincodeFromFS this is a wrapper for hiding package implementation.
func GetChaincodeFromFS(ccname string, ccversion string) (CCPackage, error) {
//try raw CDS
Expand Down
5 changes: 2 additions & 3 deletions peer/chaincode/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ func install(msg proto.Message, cf *ChaincodeCmdFactory) error {

//generateChaincode creates ChaincodeDeploymentSpec as the package to install
func generateChaincode(cmd *cobra.Command, chaincodeName, chaincodeVersion string) (*pb.ChaincodeDeploymentSpec, error) {
tmppkg, _ := ccprovider.GetChaincodePackage(chaincodeName, chaincodeVersion)
if tmppkg != nil {
return nil, fmt.Errorf("chaincode %s:%s exists", chaincodeName, chaincodeVersion)
if existed, _ := ccprovider.ChaincodePackageExists(chaincodeName, chaincodeVersion); existed {
return nil, fmt.Errorf("chaincode %s:%s already exists", chaincodeName, chaincodeVersion)
}

spec, err := getChaincodeSpec(cmd)
Expand Down

0 comments on commit 78ce862

Please sign in to comment.