Skip to content

Commit

Permalink
FAB-2166 - check version in lccc and CLI
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2166

We were defaulting to "0" if a version was not provided. Should
have removed the default when the new model moved from WIP.

Force version to be provided by checking for empty version in LCCC.

CLI enforces as well.

Change-Id: I93922bc37e48555c377c44243adf46ea522b75f1
Signed-off-by: Srinivasan Muralidharan <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Feb 10, 2017
1 parent 7158ab3 commit 69c407a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
18 changes: 10 additions & 8 deletions core/scc/lccc/lccc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ const (

//characters used in chaincodenamespace
specialChars = "/:[]${}"

// chaincode version when deploy
startVersion = "0"
)

//---------- the LCCC -----------------
Expand Down Expand Up @@ -178,6 +175,13 @@ func (f InvalidVersionErr) Error() string {
return fmt.Sprintf("invalid version %s", string(f))
}

//EmptyVersionErr trying to upgrade to same version of Chaincode
type EmptyVersionErr string

func (f EmptyVersionErr) Error() string {
return fmt.Sprintf("version not provided for chaincode %s", string(f))
}

//-------------- helper functions ------------------
//create the chaincode on the given chain
func (lccc *LifeCycleSysCC) createChaincode(stub shim.ChaincodeStubInterface, chainname string, ccname string, version string, cccode []byte, policy []byte, escc []byte, vscc []byte) (*ccprovider.ChaincodeData, error) {
Expand Down Expand Up @@ -301,13 +305,11 @@ func (lccc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
return ExistsErr(cds.ChaincodeSpec.ChaincodeID.Name)
}

//user default startversion if none specified
ver := startVersion
if cds.ChaincodeSpec.ChaincodeID.Version != "" {
ver = cds.ChaincodeSpec.ChaincodeID.Version
if cds.ChaincodeSpec.ChaincodeID.Version == "" {
return EmptyVersionErr(cds.ChaincodeSpec.ChaincodeID.Name)
}

_, err = lccc.createChaincode(stub, chainname, cds.ChaincodeSpec.ChaincodeID.Name, ver, depSpec, policy, escc, vscc)
_, err = lccc.createChaincode(stub, chainname, cds.ChaincodeSpec.ChaincodeID.Name, cds.ChaincodeSpec.ChaincodeID.Version, depSpec, policy, escc, vscc)

return err
}
Expand Down
29 changes: 29 additions & 0 deletions core/scc/lccc/lccc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,35 @@ func TestInvalidChaincodeName(t *testing.T) {
}
}

//TestEmptyChaincodeVersion tests the deploy function without a version name
func TestEmptyChaincodeVersion(t *testing.T) {
scc := new(LifeCycleSysCC)
stub := shim.NewMockStub("lccc", scc)

if res := stub.MockInit("1", nil); res.Status != shim.OK {
fmt.Println("Init failed", string(res.Message))
t.FailNow()
}

cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")})
defer os.Remove(lccctestpath + "/chaincodes/example02.0")

//change version to empty
cds.ChaincodeSpec.ChaincodeID.Version = ""

var b []byte
if b, err = proto.Marshal(cds); err != nil || b == nil {
t.FailNow()
}

args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
res := stub.MockInvoke("1", args)
if string(res.Message) != EmptyVersionErr("example02").Error() {
t.Logf("Get error: %s", res.Message)
t.FailNow()
}
}

//TestRedeploy tests the redeploying will fail function(and fail with "exists" error)
func TestRedeploy(t *testing.T) {
scc := new(LifeCycleSysCC)
Expand Down
6 changes: 3 additions & 3 deletions peer/chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
return fmt.Errorf("Must supply value for %s name parameter.\n", chainFuncName)
}

if cmd != nil && (cmd == chaincodeInstantiateCmd || cmd == chaincodeInstallCmd || cmd == chaincodeUpgradeCmd) {
if chaincodeVersion == "" {
return fmt.Errorf("Chaincode version is not provided")
if cmd.Name() == instantiate_cmdname || cmd.Name() == install_cmdname || cmd.Name() == upgrade_cmdname {
if chaincodeVersion == common.UndefinedParamValue {
return fmt.Errorf("Chaincode version is not provided for %s", cmd.Name())
}
}

Expand Down
2 changes: 2 additions & 0 deletions peer/chaincode/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (

var chaincodeInstallCmd *cobra.Command

const install_cmdname = "install"

// installCmd returns the cobra command for Chaincode Deploy
func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
chaincodeInstallCmd = &cobra.Command{
Expand Down

0 comments on commit 69c407a

Please sign in to comment.