github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/ccchecker/chaincodes/registershadow.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 chaincodes
    18  
    19  import (
    20  	"fmt"
    21  
    22  	//shadow chaincodes to be registered
    23  	nkpi "github.com/hyperledger/fabric/examples/ccchecker/chaincodes/newkeyperinvoke/shadow"
    24  )
    25  
    26  //all the statically registered shadow chaincodes that can be used
    27  var shadowCCs = map[string]ShadowCCIntf{
    28  	"github.com/hyperledger/fabric/examples/ccchecker/chaincodes/newkeyperinvoke": &nkpi.NewKeyPerInvoke{},
    29  }
    30  
    31  type shadowCCArgs struct {
    32  	initArgs []string
    33  }
    34  
    35  //For each chaincode that's deployed corresdponds a shadowCC. Not all
    36  //shadows in shadowCCs maybe used in a run using the input JSON.  And each
    37  //chaincode maybe used multiple times (either because Concurrency is > 1 or
    38  //the chaincode is used multiple times in the JSON with different CCChecker
    39  //parameters). inUseShadowCCs keeps a set of all shadow CCs in use
    40  var inUseShadowCCs map[ShadowCCIntf]*shadowCCArgs
    41  
    42  //RegisterCCClients registers and maps chaincode clients to their shadows
    43  func RegisterCCClients(ccs []*CCClient) error {
    44  	inUseShadowCCs = make(map[ShadowCCIntf]*shadowCCArgs)
    45  	for _, cc := range ccs {
    46  		scc, ok := shadowCCs[cc.Path]
    47  		if !ok || scc == nil {
    48  			return fmt.Errorf("%s not a registered chaincode", cc.Path)
    49  		}
    50  
    51  		//setup the shadow chaincode to plug into the ccchecker framework
    52  		cc.shadowCC = scc
    53  
    54  		//add cc to the list in shadow cc
    55  		if _, ok := inUseShadowCCs[scc]; !ok {
    56  			inUseShadowCCs[scc] = &shadowCCArgs{cc.InitArgs}
    57  		}
    58  	}
    59  
    60  	//initialize a shadow chaincode just once. A chaincode may be used
    61  	//multiple times in test run
    62  	for scc, sccArgs := range inUseShadowCCs {
    63  		scc.InitShadowCC(sccArgs.initArgs)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  //ListShadowCCs lists all registered shadow ccs in the library
    70  func ListShadowCCs() {
    71  	for key := range shadowCCs {
    72  		fmt.Printf("\t%s\n", key)
    73  	}
    74  }