@@ -229,6 +229,13 @@ func (f InstantiationPolicyViolatedErr) Error() string {
229
229
return "chaincode instantiation policy violated"
230
230
}
231
231
232
+ //InstantiationPolicyMissing when no existing instantiation policy is found when upgrading CC
233
+ type InstantiationPolicyMissing string
234
+
235
+ func (f InstantiationPolicyMissing ) Error () string {
236
+ return "instantiation policy missing"
237
+ }
238
+
232
239
//-------------- helper functions ------------------
233
240
//create the chaincode on the given chain
234
241
func (lscc * LifeCycleSysCC ) createChaincode (stub shim.ChaincodeStubInterface , cd * ccprovider.ChaincodeData ) error {
@@ -473,16 +480,28 @@ func (lscc *LifeCycleSysCC) executeInstall(stub shim.ChaincodeStubInterface, ccb
473
480
474
481
// getInstantiationPolicy retrieves the instantiation policy from a SignedCDSPackage
475
482
func (lscc * LifeCycleSysCC ) getInstantiationPolicy (stub shim.ChaincodeStubInterface , ccpack ccprovider.CCPackage ) ([]byte , error ) {
476
- //if ccpack is a SignedCDSPackage, evaluate submitter against instantiation policy
483
+ var ip []byte
484
+ // if ccpack is a SignedCDSPackage, return its IP, otherwise use a default IP
477
485
sccpack , isSccpack := ccpack .(* ccprovider.SignedCDSPackage )
478
486
if isSccpack {
479
- ip : = sccpack .GetInstantiationPolicy ()
487
+ ip = sccpack .GetInstantiationPolicy ()
480
488
if ip == nil {
481
489
return nil , fmt .Errorf ("Instantiation policy cannot be null for a SignedCCDeploymentSpec" )
482
490
}
483
- return ip , nil
491
+ } else {
492
+ // the default instantiation policy requires the peer's msp admin
493
+ // it assumes that the peer's MSP does not change over time
494
+ mspid , err := mspmgmt .GetLocalMSP ().GetIdentifier ()
495
+ if err != nil {
496
+ return nil , fmt .Errorf ("Error creating default instantiation policy: could not retrieve local MSP identifier %s" , err )
497
+ }
498
+ ipEnvelope := cauthdsl .SignedByMspAdmin (mspid )
499
+ ip , err = proto .Marshal (ipEnvelope )
500
+ if err != nil {
501
+ return nil , fmt .Errorf ("Marshalling instantiation policy failed: [%s]" , err )
502
+ }
484
503
}
485
- return nil , nil
504
+ return ip , nil
486
505
}
487
506
488
507
// checkInstantiationPolicy evaluates an instantiation policy against a signed proposal
@@ -573,11 +592,9 @@ func (lscc *LifeCycleSysCC) executeDeploy(stub shim.ChaincodeStubInterface, chai
573
592
if err != nil {
574
593
return nil , err
575
594
}
576
- if cd .InstantiationPolicy != nil {
577
- err = lscc .checkInstantiationPolicy (stub , chainname , cd .InstantiationPolicy )
578
- if err != nil {
579
- return nil , err
580
- }
595
+ err = lscc .checkInstantiationPolicy (stub , chainname , cd .InstantiationPolicy )
596
+ if err != nil {
597
+ return nil , err
581
598
}
582
599
583
600
err = lscc .createChaincode (stub , cd )
@@ -625,11 +642,12 @@ func (lscc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
625
642
}
626
643
627
644
//do not upgrade if instantiation policy is violated
628
- if cd .InstantiationPolicy != nil {
629
- err = lscc .checkInstantiationPolicy (stub , chainName , cd .InstantiationPolicy )
630
- if err != nil {
631
- return nil , err
632
- }
645
+ if cd .InstantiationPolicy == nil {
646
+ return nil , InstantiationPolicyMissing ("" )
647
+ }
648
+ err = lscc .checkInstantiationPolicy (stub , chainName , cd .InstantiationPolicy )
649
+ if err != nil {
650
+ return nil , err
633
651
}
634
652
635
653
ccpack , err := ccprovider .GetChaincodeFromFS (chaincodeName , cds .ChaincodeSpec .ChaincodeId .Version )
@@ -650,11 +668,9 @@ func (lscc *LifeCycleSysCC) executeUpgrade(stub shim.ChaincodeStubInterface, cha
650
668
if err != nil {
651
669
return nil , err
652
670
}
653
- if cd .InstantiationPolicy != nil {
654
- err = lscc .checkInstantiationPolicy (stub , chainName , cd .InstantiationPolicy )
655
- if err != nil {
656
- return nil , err
657
- }
671
+ err = lscc .checkInstantiationPolicy (stub , chainName , cd .InstantiationPolicy )
672
+ if err != nil {
673
+ return nil , err
658
674
}
659
675
660
676
err = lscc .upgradeChaincode (stub , cd )
0 commit comments