github.com/true-sqn/fabric@v2.1.1+incompatible/core/common/privdata/simplecollection.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package privdata
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/peer"
    11  	"github.com/hyperledger/fabric/common/policies"
    12  	"github.com/hyperledger/fabric/msp"
    13  	"github.com/hyperledger/fabric/protoutil"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // SimpleCollection implements a collection with static properties
    18  // and a public member set
    19  type SimpleCollection struct {
    20  	name         string
    21  	accessPolicy policies.Policy
    22  	memberOrgs   map[string]struct{}
    23  	conf         peer.StaticCollectionConfig
    24  }
    25  
    26  type SimpleCollectionPersistenceConfigs struct {
    27  	blockToLive uint64
    28  }
    29  
    30  // NewSimpleCollection returns a simple collection object based on a given
    31  // StaticCollectionConfig proto that has all the necessary information
    32  func NewSimpleCollection(collectionConfig *peer.StaticCollectionConfig, deserializer msp.IdentityDeserializer) (*SimpleCollection, error) {
    33  	sc := &SimpleCollection{}
    34  	err := sc.Setup(collectionConfig, deserializer)
    35  	return sc, err
    36  }
    37  
    38  // CollectionID returns the collection's ID
    39  func (sc *SimpleCollection) CollectionID() string {
    40  	return sc.name
    41  }
    42  
    43  // MemberOrgs returns the MSP IDs that are part of this collection
    44  func (sc *SimpleCollection) MemberOrgs() map[string]struct{} {
    45  	return sc.memberOrgs
    46  }
    47  
    48  // RequiredPeerCount returns the minimum number of peers
    49  // required to send private data to
    50  func (sc *SimpleCollection) RequiredPeerCount() int {
    51  	return int(sc.conf.RequiredPeerCount)
    52  }
    53  
    54  // MaximumPeerCount returns the maximum number of peers
    55  // to which the private data will be sent
    56  func (sc *SimpleCollection) MaximumPeerCount() int {
    57  	return int(sc.conf.MaximumPeerCount)
    58  }
    59  
    60  // AccessFilter returns the member filter function that evaluates signed data
    61  // against the member access policy of this collection
    62  func (sc *SimpleCollection) AccessFilter() Filter {
    63  	return func(sd protoutil.SignedData) bool {
    64  		if err := sc.accessPolicy.EvaluateSignedData([]*protoutil.SignedData{&sd}); err != nil {
    65  			return false
    66  		}
    67  		return true
    68  	}
    69  }
    70  
    71  // IsMemberOnlyRead returns whether only collection member
    72  // has the read permission
    73  func (sc *SimpleCollection) IsMemberOnlyRead() bool {
    74  	return sc.conf.MemberOnlyRead
    75  }
    76  
    77  // IsMemberOnlyWrite returns whether only collection member
    78  // has the write permission
    79  func (sc *SimpleCollection) IsMemberOnlyWrite() bool {
    80  	return sc.conf.MemberOnlyWrite
    81  }
    82  
    83  // Setup configures a simple collection object based on a given
    84  // StaticCollectionConfig proto that has all the necessary information
    85  func (sc *SimpleCollection) Setup(collectionConfig *peer.StaticCollectionConfig, deserializer msp.IdentityDeserializer) error {
    86  	if collectionConfig == nil {
    87  		return errors.New("Nil config passed to collection setup")
    88  	}
    89  	sc.conf = *collectionConfig
    90  	sc.name = collectionConfig.GetName()
    91  
    92  	// get the access signature policy envelope
    93  	collectionPolicyConfig := collectionConfig.GetMemberOrgsPolicy()
    94  	if collectionPolicyConfig == nil {
    95  		return errors.New("Collection config policy is nil")
    96  	}
    97  	accessPolicyEnvelope := collectionPolicyConfig.GetSignaturePolicy()
    98  	if accessPolicyEnvelope == nil {
    99  		return errors.New("Collection config access policy is nil")
   100  	}
   101  
   102  	err := sc.setupAccessPolicy(collectionPolicyConfig, deserializer)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	// get member org MSP IDs from the envelope, identities that fail to deserialize will not be returned
   108  	sc.memberOrgs = getMemberOrgs(accessPolicyEnvelope.Identities, deserializer)
   109  
   110  	return nil
   111  }
   112  
   113  // setupAccessPolicy configures a simple collection object based on a given
   114  // StaticCollectionConfig proto that has all the necessary information
   115  func (sc *SimpleCollection) setupAccessPolicy(collectionPolicyConfig *peer.CollectionPolicyConfig, deserializer msp.IdentityDeserializer) error {
   116  	var err error
   117  	sc.accessPolicy, err = getPolicy(collectionPolicyConfig, deserializer)
   118  	return err
   119  }
   120  
   121  // BlockToLive return collection's block to live configuration
   122  func (s *SimpleCollectionPersistenceConfigs) BlockToLive() uint64 {
   123  	return s.blockToLive
   124  }