Skip to content

Commit 6a81ec1

Browse files
committed
[FAB-2632] Default endorsement policy
If no custom endorsement policy was specified, the peer used to set a default endorsement policy requiring a signature from a member of MSP "DEFAULT". However this MSP may not always exist. The solution is to default to the following policy: a signature from a member of any of the application MSPs specified for the channel. Change-Id: Ie3d5876cc8593b5b91babb4ba21f3146338cd3bd Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 86e7525 commit 6a81ec1

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

core/peer/peer.go

+24
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,30 @@ func GetCurrConfigBlock(cid string) *common.Block {
297297
return nil
298298
}
299299

300+
// GetMSPIDs returns the ID of each application MSP defined on this chain
301+
func GetMSPIDs(cid string) []string {
302+
chains.RLock()
303+
defer chains.RUnlock()
304+
if c, ok := chains.list[cid]; ok {
305+
if c == nil || c.cs == nil ||
306+
c.cs.ApplicationConfig() == nil ||
307+
c.cs.ApplicationConfig().Organizations() == nil {
308+
return nil
309+
}
310+
311+
orgs := c.cs.ApplicationConfig().Organizations()
312+
toret := make([]string, len(orgs))
313+
i := 0
314+
for _, org := range orgs {
315+
toret[i] = org.MSPID()
316+
i++
317+
}
318+
319+
return toret
320+
}
321+
return nil
322+
}
323+
300324
// SetCurrConfigBlock sets the current config block of the specified chain
301325
func SetCurrConfigBlock(block *common.Block, cid string) error {
302326
chains.Lock()

core/scc/lccc/lccc.go

+33-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/hyperledger/fabric/core/chaincode/shim"
2727
"github.com/hyperledger/fabric/core/common/ccprovider"
2828
"github.com/hyperledger/fabric/core/common/sysccprovider"
29+
"github.com/hyperledger/fabric/core/peer"
30+
"github.com/hyperledger/fabric/protos/common"
2931
pb "github.com/hyperledger/fabric/protos/peer"
3032
"github.com/hyperledger/fabric/protos/utils"
3133
"github.com/op/go-logging"
@@ -469,6 +471,33 @@ func (lccc *LifeCycleSysCC) Init(stub shim.ChaincodeStubInterface) pb.Response {
469471
return shim.Success(nil)
470472
}
471473

474+
// getDefaultEndorsementPolicy returns the default
475+
// endorsement policy for the specified chain; it
476+
// is used in case the deployer has not specified a
477+
// custom one
478+
func (lccc *LifeCycleSysCC) getDefaultEndorsementPolicy(chain string) []byte {
479+
// we create an array of principals, one principal
480+
// per application MSP defined on this chain
481+
ids := peer.GetMSPIDs(chain)
482+
principals := make([]*common.MSPPrincipal, len(ids))
483+
sigspolicy := make([]*common.SignaturePolicy, len(ids))
484+
for i, id := range ids {
485+
principals[i] = &common.MSPPrincipal{
486+
PrincipalClassification: common.MSPPrincipal_ROLE,
487+
Principal: utils.MarshalOrPanic(&common.MSPRole{Role: common.MSPRole_MEMBER, MspIdentifier: id})}
488+
sigspolicy[i] = cauthdsl.SignedBy(int32(i))
489+
}
490+
491+
// create the policy: it requires exactly 1 signature from any of the principals
492+
p := &common.SignaturePolicyEnvelope{
493+
Version: 0,
494+
Policy: cauthdsl.NOutOf(1, sigspolicy),
495+
Identities: principals,
496+
}
497+
498+
return utils.MarshalOrPanic(p)
499+
}
500+
472501
// Invoke implements lifecycle functions "deploy", "start", "stop", "upgrade".
473502
// Deploy's arguments - {[]byte("deploy"), []byte(<chainname>), <unmarshalled pb.ChaincodeDeploymentSpec>}
474503
//
@@ -515,11 +544,10 @@ func (lccc *LifeCycleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response
515544
// args[4] is the name of escc
516545
// args[5] is the name of vscc
517546
var policy []byte
518-
if len(args) > 3 && args[3] != nil {
547+
if len(args) > 3 && len(args[3]) > 0 {
519548
policy = args[3]
520549
} else {
521-
// FIXME: temporary workaround until all SDKs provide a policy
522-
policy = utils.MarshalOrPanic(cauthdsl.SignedByMspMember("DEFAULT"))
550+
policy = lccc.getDefaultEndorsementPolicy(chainname)
523551
}
524552

525553
var escc []byte
@@ -558,11 +586,10 @@ func (lccc *LifeCycleSysCC) Invoke(stub shim.ChaincodeStubInterface) pb.Response
558586
// args[4] is the name of escc
559587
// args[5] is the name of vscc
560588
var policy []byte
561-
if len(args) > 3 && args[3] != nil {
589+
if len(args) > 3 && len(args[3]) > 0 {
562590
policy = args[3]
563591
} else {
564-
// FIXME: temporary workaround until all SDKs provide a policy
565-
policy = utils.MarshalOrPanic(cauthdsl.SignedByMspMember("DEFAULT"))
592+
policy = lccc.getDefaultEndorsementPolicy(chainname)
566593
}
567594

568595
var escc []byte

peer/chaincode/common.go

-4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error {
173173
return fmt.Errorf("Invalid policy %s", policy)
174174
}
175175
policyMarhsalled = putils.MarshalOrPanic(p)
176-
} else {
177-
// FIXME: we need to get the default from somewhere
178-
p := cauthdsl.SignedByMspMember("DEFAULT")
179-
policyMarhsalled = putils.MarshalOrPanic(p)
180176
}
181177
}
182178

0 commit comments

Comments
 (0)