Skip to content

Commit e26c37c

Browse files
committed
[FAB-9722] allow collection updates - lscc side
This CR makes lscc to process collection config package during chaincode upgrade. Change-Id: I439b574d1e655b1803c52b7ba946ef5399ca0009 Signed-off-by: senthil <cendhu@gmail.com>
1 parent 42403c0 commit e26c37c

File tree

2 files changed

+263
-52
lines changed

2 files changed

+263
-52
lines changed

core/scc/lscc/lscc.go

+31-18
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,6 @@ func (lscc *lifeCycleSysCC) putChaincodeCollectionData(stub shim.ChaincodeStubIn
176176

177177
key := privdata.BuildCollectionKVSKey(cd.Name)
178178

179-
existingCollection, err := stub.GetState(key)
180-
if err != nil {
181-
return errors.WithMessage(err, fmt.Sprintf("unable to check whether collection existed earlier for chaincode %s:%s", cd.Name, cd.Version))
182-
}
183-
// currently, collections are immutable. Support for collection upgrade will be added later
184-
if existingCollection != nil {
185-
return errors.Errorf("collection data should not exist for chaincode %s:%s", cd.Name, cd.Version)
186-
}
187-
188179
err = stub.PutState(key, collectionConfigBytes)
189180
if err != nil {
190181
return errors.WithMessage(err, fmt.Sprintf("error putting collection for chaincode %s:%s", cd.Name, cd.Version))
@@ -459,17 +450,21 @@ func (lscc *lifeCycleSysCC) executeDeployOrUpgrade(
459450
policy, escc, vscc, collectionConfigBytes []byte,
460451
function string,
461452
) (*ccprovider.ChaincodeData, error) {
462-
if err := lscc.isValidChaincodeName(cds.ChaincodeSpec.ChaincodeId.Name); err != nil {
453+
454+
chaincodeName := cds.ChaincodeSpec.ChaincodeId.Name
455+
chaincodeVersion := cds.ChaincodeSpec.ChaincodeId.Version
456+
457+
if err := lscc.isValidChaincodeName(chaincodeName); err != nil {
463458
return nil, err
464459
}
465460

466-
if err := lscc.isValidChaincodeVersion(cds.ChaincodeSpec.ChaincodeId.Name, cds.ChaincodeSpec.ChaincodeId.Version); err != nil {
461+
if err := lscc.isValidChaincodeVersion(chaincodeName, chaincodeVersion); err != nil {
467462
return nil, err
468463
}
469464

470-
ccpack, err := lscc.support.GetChaincodeFromLocalStorage(cds.ChaincodeSpec.ChaincodeId.Name, cds.ChaincodeSpec.ChaincodeId.Version)
465+
ccpack, err := lscc.support.GetChaincodeFromLocalStorage(chaincodeName, chaincodeVersion)
471466
if err != nil {
472-
retErrMsg := fmt.Sprintf("cannot get package for chaincode (%s:%s)", cds.ChaincodeSpec.ChaincodeId.Name, cds.ChaincodeSpec.ChaincodeId.Version)
467+
retErrMsg := fmt.Sprintf("cannot get package for chaincode (%s:%s)", chaincodeName, chaincodeVersion)
473468
logger.Errorf("%s-err:%s", retErrMsg, err)
474469
return nil, fmt.Errorf("%s", retErrMsg)
475470
}
@@ -479,7 +474,7 @@ func (lscc *lifeCycleSysCC) executeDeployOrUpgrade(
479474
case DEPLOY:
480475
return lscc.executeDeploy(stub, chainname, cds, policy, escc, vscc, cd, ccpack, collectionConfigBytes)
481476
case UPGRADE:
482-
return lscc.executeUpgrade(stub, chainname, cds, policy, escc, vscc, cd, ccpack)
477+
return lscc.executeUpgrade(stub, chainname, cds, policy, escc, vscc, cd, ccpack, collectionConfigBytes)
483478
default:
484479
logger.Panicf("Programming error, unexpected function '%s'", function)
485480
panic("") // unreachable code
@@ -499,9 +494,10 @@ func (lscc *lifeCycleSysCC) executeDeploy(
499494
collectionConfigBytes []byte,
500495
) (*ccprovider.ChaincodeData, error) {
501496
//just test for existence of the chaincode in the LSCC
502-
_, err := lscc.getCCInstance(stub, cds.ChaincodeSpec.ChaincodeId.Name)
497+
chaincodeName := cds.ChaincodeSpec.ChaincodeId.Name
498+
_, err := lscc.getCCInstance(stub, chaincodeName)
503499
if err == nil {
504-
return nil, ExistsErr(cds.ChaincodeSpec.ChaincodeId.Name)
500+
return nil, ExistsErr(chaincodeName)
505501
}
506502

507503
//retain chaincode specific data and fill channel specific ones
@@ -538,7 +534,8 @@ func (lscc *lifeCycleSysCC) executeDeploy(
538534
}
539535

540536
// executeUpgrade implements the "upgrade" Invoke transaction.
541-
func (lscc *lifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, chainName string, cds *pb.ChaincodeDeploymentSpec, policy []byte, escc []byte, vscc []byte, cdfs *ccprovider.ChaincodeData, ccpackfs ccprovider.CCPackage) (*ccprovider.ChaincodeData, error) {
537+
func (lscc *lifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, chainName string, cds *pb.ChaincodeDeploymentSpec, policy []byte, escc []byte, vscc []byte, cdfs *ccprovider.ChaincodeData, ccpackfs ccprovider.CCPackage, collectionConfigBytes []byte) (*ccprovider.ChaincodeData, error) {
538+
542539
chaincodeName := cds.ChaincodeSpec.ChaincodeId.Name
543540

544541
// check for existence of chaincode instance only (it has to exist on the channel)
@@ -557,7 +554,7 @@ func (lscc *lifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
557554

558555
//do not upgrade if same version
559556
if cdLedger.Version == cds.ChaincodeSpec.ChaincodeId.Version {
560-
return nil, IdenticalVersionErr(cds.ChaincodeSpec.ChaincodeId.Name)
557+
return nil, IdenticalVersionErr(chaincodeName)
561558
}
562559

563560
//do not upgrade if instantiation policy is violated
@@ -593,6 +590,22 @@ func (lscc *lifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
593590
if err != nil {
594591
return nil, err
595592
}
593+
594+
ac, exists := lscc.sccprovider.GetApplicationConfig(chainName)
595+
if !exists {
596+
logger.Panicf("programming error, non-existent appplication config for channel '%s'", chainName)
597+
}
598+
599+
// TODO: Instead of V1_2Validation(), we should be using something like CollectionUpdate().
600+
// For time being, V1_2Validation() is used. Need to submit a separate CR to introduce a
601+
// new capability named CollectionUpdate.
602+
if ac.Capabilities().V1_2Validation() {
603+
err = lscc.putChaincodeCollectionData(stub, cdfs, collectionConfigBytes)
604+
if err != nil {
605+
return nil, err
606+
}
607+
}
608+
596609
lifecycleEvent := &pb.LifecycleEvent{ChaincodeName: chaincodeName}
597610
lifecycleEventBytes := utils.MarshalOrPanic(lifecycleEvent)
598611
stub.SetEvent(UPGRADE, lifecycleEventBytes)

0 commit comments

Comments
 (0)