Skip to content

Commit 7294874

Browse files
author
Srinivasan Muralidharan
committed
fab-2220 getcc record not to check for CC existence
https://jira.hyperledger.org/browse/FAB-2220 LCCC exposes the following "GET" api. * getid - gets the name given name (basically "exists") * getccdata - gets the record entry for the chaincode given name * getdepspec - gets the deployment spec for the chaincode given name Of these only the later needs the chaincode to be available on the peer. Other APIs should not look for the existence of the chaincode. This is needed for TXs to be committed on peer even if the chaincode is not available on it Change-Id: I34e336128880ccd3d5f82733a17b6e15fb14ec50 Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent 9f561b8 commit 7294874

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

core/scc/lccc/lccc.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (lccc *LifeCycleSysCC) putChaincodeData(stub shim.ChaincodeStubInterface, c
222222
}
223223

224224
//checks for existence of chaincode on the given chain
225-
func (lccc *LifeCycleSysCC) getChaincode(stub shim.ChaincodeStubInterface, ccname string) (*ccprovider.ChaincodeData, []byte, error) {
225+
func (lccc *LifeCycleSysCC) getChaincode(stub shim.ChaincodeStubInterface, ccname string, checkFS bool) (*ccprovider.ChaincodeData, []byte, error) {
226226
cdbytes, err := stub.GetState(ccname)
227227
if err != nil {
228228
return nil, nil, err
@@ -235,9 +235,11 @@ func (lccc *LifeCycleSysCC) getChaincode(stub shim.ChaincodeStubInterface, ccnam
235235
return nil, nil, MarshallErr(ccname)
236236
}
237237

238-
cd.DepSpec, _, err = ccprovider.GetChaincodeFromFS(ccname, cd.Version)
239-
if err != nil {
240-
return nil, nil, InvalidDeploymentSpecErr(err.Error())
238+
if checkFS {
239+
cd.DepSpec, _, err = ccprovider.GetChaincodeFromFS(ccname, cd.Version)
240+
if err != nil {
241+
return cd, nil, InvalidDeploymentSpecErr(err.Error())
242+
}
241243
}
242244

243245
return cd, cdbytes, nil
@@ -326,7 +328,7 @@ func (lccc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
326328
return err
327329
}
328330

329-
cd, _, err := lccc.getChaincode(stub, cds.ChaincodeSpec.ChaincodeId.Name)
331+
cd, _, err := lccc.getChaincode(stub, cds.ChaincodeSpec.ChaincodeId.Name, true)
330332
if cd != nil {
331333
return ExistsErr(cds.ChaincodeSpec.ChaincodeId.Name)
332334
}
@@ -381,7 +383,7 @@ func (lccc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
381383
}
382384

383385
// check for existence of chaincode
384-
cd, _, err := lccc.getChaincode(stub, chaincodeName)
386+
cd, _, err := lccc.getChaincode(stub, chaincodeName, true)
385387
if cd == nil {
386388
return nil, NotFoundErr(chainName)
387389
}
@@ -529,20 +531,27 @@ func (lccc *LifeCycleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response
529531

530532
chain := string(args[1])
531533
ccname := string(args[2])
532-
//get chaincode given <chain, name>
533534

534-
cd, cdbytes, _ := lccc.getChaincode(stub, ccname)
535+
//check the FS only for deployment spec
536+
//other calls are looking for LCCC entries only
537+
checkFS := false
538+
if function == GETDEPSPEC {
539+
checkFS = true
540+
}
541+
cd, cdbytes, err := lccc.getChaincode(stub, ccname, checkFS)
535542
if cd == nil || cdbytes == nil {
536-
logger.Debugf("ChaincodeID: %s does not exist on channel: %s ", ccname, chain)
543+
logger.Errorf("ChaincodeId: %s does not exist on channel: %s(err:%s)", ccname, chain, err)
537544
return shim.Error(TXNotFoundErr(ccname + "/" + chain).Error())
538545
}
539546

540-
if function == GETCCINFO {
547+
switch function {
548+
case GETCCINFO:
541549
return shim.Success([]byte(cd.Name))
542-
} else if function == GETCCDATA {
550+
case GETCCDATA:
543551
return shim.Success(cdbytes)
552+
default:
553+
return shim.Success(cd.DepSpec)
544554
}
545-
return shim.Success(cd.DepSpec)
546555
}
547556

548557
return shim.Error(InvalidFunctionErr(function).Error())

core/scc/lccc/lccc_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,51 @@ func TestUpgradeNonExistChaincode(t *testing.T) {
453453
}
454454
}
455455

456+
//TestGetAPIsWithoutInstall get functions should return the right responses when chaicode is on
457+
//ledger but not on FS
458+
func TestGetAPIsWithoutInstall(t *testing.T) {
459+
scc := new(LifeCycleSysCC)
460+
stub := shim.NewMockStub("lccc", scc)
461+
462+
if res := stub.MockInit("1", nil); res.Status != shim.OK {
463+
fmt.Println("Init failed", string(res.Message))
464+
t.FailNow()
465+
}
466+
467+
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")}, true)
468+
469+
var b []byte
470+
if b, err = proto.Marshal(cds); err != nil || b == nil {
471+
t.FailNow()
472+
}
473+
474+
args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
475+
if res := stub.MockInvoke("1", args); res.Status != shim.OK {
476+
t.FailNow()
477+
}
478+
479+
//Force remove CC
480+
os.Remove(lccctestpath + "/example02.0")
481+
482+
//GETCCINFO should still work
483+
args = [][]byte{[]byte(GETCCINFO), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
484+
if res := stub.MockInvoke("1", args); res.Status != shim.OK {
485+
t.FailNow()
486+
}
487+
488+
//GETCCDATA should still work
489+
args = [][]byte{[]byte(GETCCDATA), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
490+
if res := stub.MockInvoke("1", args); res.Status != shim.OK {
491+
t.FailNow()
492+
}
493+
494+
//GETDEPSPEC should not work
495+
args = [][]byte{[]byte(GETDEPSPEC), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
496+
if res := stub.MockInvoke("1", args); res.Status == shim.OK {
497+
t.FailNow()
498+
}
499+
}
500+
456501
func TestMain(m *testing.M) {
457502
ccprovider.SetChaincodesPath(lccctestpath)
458503
sysccprovider.RegisterSystemChaincodeProviderFactory(&mocksccProviderFactory{})

0 commit comments

Comments
 (0)