github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/policies/implicitmeta.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package policies
    18  
    19  import (
    20  	"fmt"
    21  
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  )
    26  
    27  type implicitMetaPolicy struct {
    28  	conf        *cb.ImplicitMetaPolicy
    29  	threshold   int
    30  	subPolicies []Policy
    31  }
    32  
    33  // NewPolicy creates a new policy based on the policy bytes
    34  func newImplicitMetaPolicy(data []byte) (*implicitMetaPolicy, error) {
    35  	imp := &cb.ImplicitMetaPolicy{}
    36  	if err := proto.Unmarshal(data, imp); err != nil {
    37  		return nil, fmt.Errorf("Error unmarshaling to ImplicitMetaPolicy: %s", err)
    38  	}
    39  
    40  	return &implicitMetaPolicy{
    41  		conf: imp,
    42  	}, nil
    43  }
    44  
    45  func (imp *implicitMetaPolicy) initialize(config *policyConfig) {
    46  	imp.subPolicies = make([]Policy, len(config.managers))
    47  	i := 0
    48  	for _, manager := range config.managers {
    49  		imp.subPolicies[i], _ = manager.GetPolicy(imp.conf.SubPolicy)
    50  		i++
    51  	}
    52  
    53  	switch imp.conf.Rule {
    54  	case cb.ImplicitMetaPolicy_ANY:
    55  		imp.threshold = 1
    56  	case cb.ImplicitMetaPolicy_ALL:
    57  		imp.threshold = len(imp.subPolicies)
    58  	case cb.ImplicitMetaPolicy_MAJORITY:
    59  		imp.threshold = len(imp.subPolicies)/2 + 1
    60  	}
    61  
    62  	// In the special case that there are no policies, consider 0 to be a majority or any
    63  	if len(imp.subPolicies) == 0 {
    64  		imp.threshold = 0
    65  	}
    66  }
    67  
    68  // Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy
    69  func (imp *implicitMetaPolicy) Evaluate(signatureSet []*cb.SignedData) error {
    70  	remaining := imp.threshold
    71  	for _, policy := range imp.subPolicies {
    72  		if policy.Evaluate(signatureSet) == nil {
    73  			remaining--
    74  			if remaining == 0 {
    75  				return nil
    76  			}
    77  		}
    78  	}
    79  	if remaining == 0 {
    80  		return nil
    81  	}
    82  	return fmt.Errorf("Failed to reach implicit threshold of %d sub-policies, required %d remaining", imp.threshold, remaining)
    83  }