github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/channel/application/organization.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 application
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/configvalues"
    23  	"github.com/hyperledger/fabric/common/configvalues/channel/common/organization"
    24  	mspconfig "github.com/hyperledger/fabric/common/configvalues/msp"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  	pb "github.com/hyperledger/fabric/protos/peer"
    27  
    28  	"github.com/golang/protobuf/proto"
    29  	logging "github.com/op/go-logging"
    30  )
    31  
    32  // Application org config keys
    33  const (
    34  	// AnchorPeersKey is the key name for the AnchorPeers ConfigValue
    35  	AnchorPeersKey = "AnchorPeers"
    36  )
    37  
    38  type applicationOrgConfig struct {
    39  	anchorPeers []*pb.AnchorPeer
    40  }
    41  
    42  // SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler
    43  // In general, it should only be referenced as an Impl for the configtx.Manager
    44  type ApplicationOrgConfig struct {
    45  	*organization.OrgConfig
    46  	pendingConfig *applicationOrgConfig
    47  	config        *applicationOrgConfig
    48  
    49  	mspConfig *mspconfig.MSPConfigHandler
    50  }
    51  
    52  // NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper
    53  func NewApplicationOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *ApplicationOrgConfig {
    54  	return &ApplicationOrgConfig{
    55  		OrgConfig: organization.NewOrgConfig(id, mspConfig),
    56  		config:    &applicationOrgConfig{},
    57  	}
    58  }
    59  
    60  // AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
    61  func (oc *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer {
    62  	return oc.config.anchorPeers
    63  }
    64  
    65  // BeginValueProposals is used to start a new config proposal
    66  func (oc *ApplicationOrgConfig) BeginValueProposals(groups []string) ([]config.ValueProposer, error) {
    67  	logger.Debugf("Beginning a possible new org config")
    68  	if len(groups) != 0 {
    69  		return nil, fmt.Errorf("ApplicationGroup does not support subgroups")
    70  	}
    71  	if oc.pendingConfig != nil {
    72  		logger.Panicf("Programming error, cannot call begin in the middle of a proposal")
    73  	}
    74  	oc.pendingConfig = &applicationOrgConfig{}
    75  	return oc.OrgConfig.BeginValueProposals(groups)
    76  }
    77  
    78  // RollbackProposals is used to abandon a new config proposal
    79  func (oc *ApplicationOrgConfig) RollbackProposals() {
    80  	logger.Debugf("Rolling back proposed org config")
    81  	oc.pendingConfig = nil
    82  	oc.OrgConfig.RollbackProposals()
    83  }
    84  
    85  // CommitProposals is used to commit a new config proposal
    86  func (oc *ApplicationOrgConfig) CommitProposals() {
    87  	logger.Debugf("Committing new org config")
    88  	if oc.pendingConfig == nil {
    89  		logger.Panicf("Programming error, cannot call commit without an existing proposal")
    90  	}
    91  	oc.config = oc.pendingConfig
    92  	oc.pendingConfig = nil
    93  	oc.OrgConfig.CommitProposals()
    94  }
    95  
    96  // ProposeValue is used to add new config to the config proposal
    97  func (oc *ApplicationOrgConfig) ProposeValue(key string, configValue *cb.ConfigValue) error {
    98  	switch key {
    99  	case AnchorPeersKey:
   100  		anchorPeers := &pb.AnchorPeers{}
   101  		if err := proto.Unmarshal(configValue.Value, anchorPeers); err != nil {
   102  			return fmt.Errorf("Unmarshaling error for %s: %s", key, err)
   103  		}
   104  		if logger.IsEnabledFor(logging.DEBUG) {
   105  			logger.Debugf("Setting %s to %v", key, anchorPeers.AnchorPeers)
   106  		}
   107  		oc.pendingConfig.anchorPeers = anchorPeers.AnchorPeers
   108  	default:
   109  		return oc.OrgConfig.ProposeValue(key, configValue)
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  // PreCommit returns nil
   116  func (c *ApplicationOrgConfig) PreCommit() error { return nil }