github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/asset_management_interactive/app2/app2.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 main
    18  
    19  import (
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/hyperledger/fabric/core/crypto"
    24  	pb "github.com/hyperledger/fabric/protos"
    25  	"github.com/op/go-logging"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  var (
    30  	// Logging
    31  	appLogger = logging.MustGetLogger("app")
    32  
    33  	// NVP related objects
    34  	peerClientConn *grpc.ClientConn
    35  	serverClient   pb.PeerClient
    36  
    37  	// Bob is the administrator
    38  	bob     crypto.Client
    39  	bobCert crypto.CertificateHandler
    40  
    41  	// Charlie, Dave, and Edwina are owners
    42  	charlie     crypto.Client
    43  	charlieCert crypto.CertificateHandler
    44  
    45  	dave     crypto.Client
    46  	daveCert crypto.CertificateHandler
    47  
    48  	edwina     crypto.Client
    49  	edwinaCert crypto.CertificateHandler
    50  
    51  	assets  map[string]string
    52  	lotNums []string
    53  )
    54  
    55  func assignOwnership() (err error) {
    56  	appLogger.Debug("------------- Bob wants to assign the asset owners to all of the assets...")
    57  
    58  	i := 0
    59  	var ownerCert crypto.CertificateHandler
    60  
    61  	for _, lotNum := range lotNums {
    62  		assetName := assets[lotNum]
    63  
    64  		if i%3 == 0 {
    65  			appLogger.Debugf("Assigning ownership of asset '[%s]: [%s]' to '[%s]'", lotNum, assetName, "Charlie")
    66  			ownerCert = charlieCert
    67  		} else if i%3 == 1 {
    68  			appLogger.Debugf("Assigning ownership of asset '[%s]: [%s]' to '[%s]'", lotNum, assetName, "Dave")
    69  			ownerCert = daveCert
    70  		} else {
    71  			appLogger.Debugf("Assigning ownership of asset '[%s]: [%s]' to '[%s]'", lotNum, assetName, "Edwina")
    72  			ownerCert = edwinaCert
    73  		}
    74  
    75  		resp, err := assignOwnershipInternal(bob, bobCert, assetName, ownerCert)
    76  		if err != nil {
    77  			appLogger.Errorf("Failed assigning ownership [%s]", err)
    78  			return err
    79  		}
    80  		appLogger.Debugf("Resp [%s]", resp.String())
    81  
    82  		i++
    83  	}
    84  
    85  	appLogger.Debug("Wait 60 seconds...")
    86  	time.Sleep(60 * time.Second)
    87  
    88  	appLogger.Debug("------------- Done!")
    89  	return
    90  }
    91  
    92  func testAssetManagementChaincode() (err error) {
    93  	// Assign
    94  	err = assignOwnership()
    95  	if err != nil {
    96  		appLogger.Errorf("Failed assigning ownership [%s]", err)
    97  		return
    98  	}
    99  
   100  	appLogger.Debug("Assigned ownership!")
   101  
   102  	closeCryptoClient(bob)
   103  	closeCryptoClient(charlie)
   104  	closeCryptoClient(dave)
   105  	closeCryptoClient(edwina)
   106  
   107  	return
   108  }
   109  
   110  func main() {
   111  	if len(os.Args) != 2 {
   112  		appLogger.Errorf("Error -- A ChaincodeName must be specified.")
   113  		os.Exit(-1)
   114  	}
   115  
   116  	chaincodeName = os.Args[1]
   117  
   118  	// Initialize a non-validating peer whose role is to submit
   119  	// transactions to the fabric network.
   120  	// A 'core.yaml' file is assumed to be available in the working directory.
   121  	if err := initNVP(); err != nil {
   122  		appLogger.Debugf("Failed initiliazing NVP [%s]", err)
   123  		os.Exit(-1)
   124  	}
   125  
   126  	// Enable fabric 'confidentiality'
   127  	confidentiality(true)
   128  
   129  	// Exercise the 'asset_management' chaincode
   130  	if err := testAssetManagementChaincode(); err != nil {
   131  		appLogger.Debugf("Failed testing asset management chaincode [%s]", err)
   132  		os.Exit(-2)
   133  	}
   134  }