forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 1
/
support.go
54 lines (46 loc) · 1.57 KB
/
support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"github.com/golang/protobuf/proto"
common2 "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/chaincode"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/policies/inquire"
)
var logger = flogging.MustGetLogger("discovery.DiscoverySupport")
type MetadataRetriever interface {
Metadata(channel string, cc string, loadCollections bool) *chaincode.Metadata
}
// DiscoverySupport implements support that is used for service discovery
// that is related to chaincode
type DiscoverySupport struct {
ci MetadataRetriever
}
// NewDiscoverySupport creates a new DiscoverySupport
func NewDiscoverySupport(ci MetadataRetriever) *DiscoverySupport {
s := &DiscoverySupport{
ci: ci,
}
return s
}
func (s *DiscoverySupport) PolicyByChaincode(channel string, cc string) policies.InquireablePolicy {
chaincodeData := s.ci.Metadata(channel, cc, false)
if chaincodeData == nil {
logger.Info("Chaincode", cc, "wasn't found")
return nil
}
pol := &common2.SignaturePolicyEnvelope{}
if err := proto.Unmarshal(chaincodeData.Policy, pol); err != nil {
logger.Warning("Failed unmarshaling policy for chaincode", cc, ":", err)
return nil
}
if len(pol.Identities) == 0 || pol.Rule == nil {
logger.Warningf("Invalid policy, either Identities(%v) or Rule(%v) are empty:", pol.Identities, pol.Rule)
return nil
}
return inquire.NewInquireableSignaturePolicy(pol)
}