Skip to content

Commit 69c407a

Browse files
author
Srinivasan Muralidharan
committed
FAB-2166 - check version in lccc and CLI
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 <[email protected]>
1 parent 7158ab3 commit 69c407a

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

core/scc/lccc/lccc.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ const (
6363

6464
//characters used in chaincodenamespace
6565
specialChars = "/:[]${}"
66-
67-
// chaincode version when deploy
68-
startVersion = "0"
6966
)
7067

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

178+
//EmptyVersionErr trying to upgrade to same version of Chaincode
179+
type EmptyVersionErr string
180+
181+
func (f EmptyVersionErr) Error() string {
182+
return fmt.Sprintf("version not provided for chaincode %s", string(f))
183+
}
184+
181185
//-------------- helper functions ------------------
182186
//create the chaincode on the given chain
183187
func (lccc *LifeCycleSysCC) createChaincode(stub shim.ChaincodeStubInterface, chainname string, ccname string, version string, cccode []byte, policy []byte, escc []byte, vscc []byte) (*ccprovider.ChaincodeData, error) {
@@ -301,13 +305,11 @@ func (lccc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
301305
return ExistsErr(cds.ChaincodeSpec.ChaincodeID.Name)
302306
}
303307

304-
//user default startversion if none specified
305-
ver := startVersion
306-
if cds.ChaincodeSpec.ChaincodeID.Version != "" {
307-
ver = cds.ChaincodeSpec.ChaincodeID.Version
308+
if cds.ChaincodeSpec.ChaincodeID.Version == "" {
309+
return EmptyVersionErr(cds.ChaincodeSpec.ChaincodeID.Name)
308310
}
309311

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

312314
return err
313315
}

core/scc/lccc/lccc_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,35 @@ func TestInvalidChaincodeName(t *testing.T) {
140140
}
141141
}
142142

143+
//TestEmptyChaincodeVersion tests the deploy function without a version name
144+
func TestEmptyChaincodeVersion(t *testing.T) {
145+
scc := new(LifeCycleSysCC)
146+
stub := shim.NewMockStub("lccc", scc)
147+
148+
if res := stub.MockInit("1", nil); res.Status != shim.OK {
149+
fmt.Println("Init failed", string(res.Message))
150+
t.FailNow()
151+
}
152+
153+
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")})
154+
defer os.Remove(lccctestpath + "/chaincodes/example02.0")
155+
156+
//change version to empty
157+
cds.ChaincodeSpec.ChaincodeID.Version = ""
158+
159+
var b []byte
160+
if b, err = proto.Marshal(cds); err != nil || b == nil {
161+
t.FailNow()
162+
}
163+
164+
args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
165+
res := stub.MockInvoke("1", args)
166+
if string(res.Message) != EmptyVersionErr("example02").Error() {
167+
t.Logf("Get error: %s", res.Message)
168+
t.FailNow()
169+
}
170+
}
171+
143172
//TestRedeploy tests the redeploying will fail function(and fail with "exists" error)
144173
func TestRedeploy(t *testing.T) {
145174
scc := new(LifeCycleSysCC)

peer/chaincode/common.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
134134
return fmt.Errorf("Must supply value for %s name parameter.\n", chainFuncName)
135135
}
136136

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

peer/chaincode/install.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828

2929
var chaincodeInstallCmd *cobra.Command
3030

31+
const install_cmdname = "install"
32+
3133
// installCmd returns the cobra command for Chaincode Deploy
3234
func installCmd(cf *ChaincodeCmdFactory) *cobra.Command {
3335
chaincodeInstallCmd = &cobra.Command{

0 commit comments

Comments
 (0)