github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/mocks/configtx/configtx.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 configtx
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/config"
    21  	mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
    22  	"github.com/hyperledger/fabric/common/policies"
    23  	"github.com/hyperledger/fabric/msp"
    24  	cb "github.com/hyperledger/fabric/protos/common"
    25  
    26  	"github.com/golang/protobuf/proto"
    27  )
    28  
    29  type Resources struct {
    30  	// PolicyManagerVal is returned as the result of PolicyManager()
    31  	PolicyManagerVal *mockpolicies.Manager
    32  
    33  	// ChannelConfigVal is returned as the result of ChannelConfig()
    34  	ChannelConfigVal config.Channel
    35  
    36  	// OrdererConfigVal is returned as the result of OrdererConfig()
    37  	OrdererConfigVal config.Orderer
    38  
    39  	// ApplicationConfigVal is returned as the result of ApplicationConfig()
    40  	ApplicationConfigVal config.Application
    41  
    42  	// ConsortiumsConfigVal is returned as the result of ConsortiumsConfig()
    43  	ConsortiumsConfigVal config.Consortiums
    44  
    45  	// MSPManagerVal is returned as the result of MSPManager()
    46  	MSPManagerVal msp.MSPManager
    47  }
    48  
    49  // Returns the PolicyManagerVal
    50  func (r *Resources) PolicyManager() policies.Manager {
    51  	return r.PolicyManagerVal
    52  }
    53  
    54  // Returns the ChannelConfigVal
    55  func (r *Resources) ChannelConfig() config.Channel {
    56  	return r.ChannelConfigVal
    57  }
    58  
    59  // Returns the OrdererConfigVal
    60  func (r *Resources) OrdererConfig() (config.Orderer, bool) {
    61  	return r.OrdererConfigVal, r.OrdererConfigVal == nil
    62  }
    63  
    64  // Returns the ApplicationConfigVal
    65  func (r *Resources) ApplicationConfig() (config.Application, bool) {
    66  	return r.ApplicationConfigVal, r.ApplicationConfigVal == nil
    67  }
    68  
    69  func (r *Resources) ConsortiumsConfig() (config.Consortiums, bool) {
    70  	return r.ConsortiumsConfigVal, r.ConsortiumsConfigVal != nil
    71  }
    72  
    73  // Returns the MSPManagerVal
    74  func (r *Resources) MSPManager() msp.MSPManager {
    75  	return r.MSPManagerVal
    76  }
    77  
    78  // Transactional implements the configtxapi.Transactional
    79  type Transactional struct{}
    80  
    81  // PreCommit returns nil
    82  func (t *Transactional) PreCommit(tx interface{}) error { return nil }
    83  
    84  // CommitConfig does nothing
    85  func (t *Transactional) CommitProposals(tx interface{}) {}
    86  
    87  // RollbackConfig does nothing
    88  func (t *Transactional) RollbackProposals(tx interface{}) {}
    89  
    90  // Initializer mocks the configtxapi.Initializer interface
    91  type Initializer struct {
    92  	Resources
    93  
    94  	// PolicyProposerVal is returned by PolicyProposers
    95  	PolicyProposerVal *PolicyProposer
    96  
    97  	// ValueProposerVal is returned by ValueProposers
    98  	ValueProposerVal *ValueProposer
    99  }
   100  
   101  // PolicyProposers returns PolicyProposerVal
   102  func (i *Initializer) PolicyProposer() policies.Proposer {
   103  	return i.PolicyProposerVal
   104  }
   105  
   106  // ValueProposers returns ValueProposerVal
   107  func (i *Initializer) ValueProposer() config.ValueProposer {
   108  	return i.ValueProposerVal
   109  }
   110  
   111  // PolicyProposer mocks the policies.Proposer interface
   112  type PolicyProposer struct {
   113  	Transactional
   114  	LastKey               string
   115  	LastPolicy            *cb.ConfigPolicy
   116  	ErrorForProposePolicy error
   117  }
   118  
   119  // ProposeConfig sets LastKey to key, LastPath to path, and LastPolicy to configPolicy, returning ErrorForProposedConfig
   120  func (pp *PolicyProposer) ProposePolicy(tx interface{}, key string, configPolicy *cb.ConfigPolicy) (proto.Message, error) {
   121  	pp.LastKey = key
   122  	pp.LastPolicy = configPolicy
   123  	return nil, pp.ErrorForProposePolicy
   124  }
   125  
   126  // BeginConfig will be removed in the future
   127  func (pp *PolicyProposer) BeginPolicyProposals(tx interface{}, groups []string) ([]policies.Proposer, error) {
   128  	handlers := make([]policies.Proposer, len(groups))
   129  	for i := range handlers {
   130  		handlers[i] = pp
   131  	}
   132  	return handlers, nil
   133  }
   134  
   135  // Handler mocks the configtxapi.Handler interface
   136  type ValueProposer struct {
   137  	Transactional
   138  	LastKey               string
   139  	LastValue             *cb.ConfigValue
   140  	ErrorForProposeConfig error
   141  	DeserializeReturn     proto.Message
   142  	DeserializeError      error
   143  }
   144  
   145  // BeginConfig returns slices populated by self
   146  func (vp *ValueProposer) BeginValueProposals(tx interface{}, groups []string) (config.ValueDeserializer, []config.ValueProposer, error) {
   147  	handlers := make([]config.ValueProposer, len(groups))
   148  	for i := range handlers {
   149  		handlers[i] = vp
   150  	}
   151  	return vp, handlers, nil
   152  }
   153  
   154  func (vp *ValueProposer) Deserialize(key string, value []byte) (proto.Message, error) {
   155  	return vp.DeserializeReturn, vp.DeserializeError
   156  }
   157  
   158  // Manager is a mock implementation of configtxapi.Manager
   159  type Manager struct {
   160  	Initializer
   161  
   162  	// ChainIDVal is returned as the result of ChainID()
   163  	ChainIDVal string
   164  
   165  	// SequenceVal is returned as the result of Sequence()
   166  	SequenceVal uint64
   167  
   168  	// ApplyVal is returned by Apply
   169  	ApplyVal error
   170  
   171  	// AppliedConfigUpdateEnvelope is set by Apply
   172  	AppliedConfigUpdateEnvelope *cb.ConfigEnvelope
   173  
   174  	// ValidateVal is returned by Validate
   175  	ValidateVal error
   176  
   177  	// ProposeConfigUpdateError is returned as the error value for ProposeConfigUpdate
   178  	ProposeConfigUpdateError error
   179  
   180  	// ProposeConfigUpdateVal is returns as the value for ProposeConfigUpdate
   181  	ProposeConfigUpdateVal *cb.ConfigEnvelope
   182  
   183  	// ConfigEnvelopeVal is returned as the value for ConfigEnvelope()
   184  	ConfigEnvelopeVal *cb.ConfigEnvelope
   185  }
   186  
   187  // ConfigEnvelope returns the ConfigEnvelopeVal
   188  func (cm *Manager) ConfigEnvelope() *cb.ConfigEnvelope {
   189  	return cm.ConfigEnvelopeVal
   190  }
   191  
   192  // ConsensusType returns the ConsensusTypeVal
   193  func (cm *Manager) ChainID() string {
   194  	return cm.ChainIDVal
   195  }
   196  
   197  // BatchSize returns the BatchSizeVal
   198  func (cm *Manager) Sequence() uint64 {
   199  	return cm.SequenceVal
   200  }
   201  
   202  // ProposeConfigUpdate
   203  func (cm *Manager) ProposeConfigUpdate(update *cb.Envelope) (*cb.ConfigEnvelope, error) {
   204  	return cm.ProposeConfigUpdateVal, cm.ProposeConfigUpdateError
   205  }
   206  
   207  // Apply returns ApplyVal
   208  func (cm *Manager) Apply(configEnv *cb.ConfigEnvelope) error {
   209  	cm.AppliedConfigUpdateEnvelope = configEnv
   210  	return cm.ApplyVal
   211  }
   212  
   213  // Validate returns ValidateVal
   214  func (cm *Manager) Validate(configEnv *cb.ConfigEnvelope) error {
   215  	return cm.ValidateVal
   216  }