github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/peer/channel/join.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 channel
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  
    23  	"github.com/hyperledger/fabric/peer/common"
    24  	pcommon "github.com/hyperledger/fabric/protos/common"
    25  	pb "github.com/hyperledger/fabric/protos/peer"
    26  	putils "github.com/hyperledger/fabric/protos/utils"
    27  	"github.com/spf13/cobra"
    28  	"golang.org/x/net/context"
    29  )
    30  
    31  const joinFuncName = "join"
    32  
    33  func joinCmd(cf *ChannelCmdFactory) *cobra.Command {
    34  	// Set the flags on the channel start command.
    35  
    36  	channelJoinCmd := &cobra.Command{
    37  		Use:   "join",
    38  		Short: "Joins the peer to a chain.",
    39  		Long:  `Joins the peer to a chain.`,
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			return join(cmd, args, cf)
    42  		},
    43  	}
    44  	return channelJoinCmd
    45  }
    46  
    47  //GBFileNotFoundErr genesis block file not found
    48  type GBFileNotFoundErr string
    49  
    50  func (e GBFileNotFoundErr) Error() string {
    51  	return fmt.Sprintf("genesis block file not found %s", string(e))
    52  }
    53  
    54  //ProposalFailedErr proposal failed
    55  type ProposalFailedErr string
    56  
    57  func (e ProposalFailedErr) Error() string {
    58  	return fmt.Sprintf("proposal failed (err: %s)", string(e))
    59  }
    60  
    61  func getJoinCCSpec() (*pb.ChaincodeSpec, error) {
    62  	if genesisBlockPath == common.UndefinedParamValue {
    63  		return nil, fmt.Errorf("Must supply genesis block file.\n")
    64  	}
    65  
    66  	gb, err := ioutil.ReadFile(genesisBlockPath)
    67  	if err != nil {
    68  		return nil, GBFileNotFoundErr(err.Error())
    69  	}
    70  
    71  	spec := &pb.ChaincodeSpec{}
    72  
    73  	// Build the spec
    74  	input := &pb.ChaincodeInput{Args: [][]byte{[]byte("JoinChain"), gb}}
    75  
    76  	spec = &pb.ChaincodeSpec{
    77  		Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
    78  		ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
    79  		Input:       input,
    80  	}
    81  
    82  	return spec, nil
    83  }
    84  
    85  func executeJoin(cf *ChannelCmdFactory) (err error) {
    86  	spec, err := getJoinCCSpec()
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	// Build the ChaincodeInvocationSpec message
    92  	invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
    93  
    94  	creator, err := cf.Signer.Serialize()
    95  	if err != nil {
    96  		return fmt.Errorf("Error serializing identity for %s: %s\n", cf.Signer.GetIdentifier(), err)
    97  	}
    98  
    99  	var prop *pb.Proposal
   100  	prop, _, err = putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
   101  	if err != nil {
   102  		return fmt.Errorf("Error creating proposal for join %s\n", err)
   103  	}
   104  
   105  	var signedProp *pb.SignedProposal
   106  	signedProp, err = putils.GetSignedProposal(prop, cf.Signer)
   107  	if err != nil {
   108  		return fmt.Errorf("Error creating signed proposal  %s\n", err)
   109  	}
   110  
   111  	var proposalResp *pb.ProposalResponse
   112  	proposalResp, err = cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
   113  	if err != nil {
   114  		return ProposalFailedErr(err.Error())
   115  	}
   116  
   117  	if proposalResp == nil {
   118  		return ProposalFailedErr("nil proposal response")
   119  	}
   120  
   121  	if proposalResp.Response.Status != 0 && proposalResp.Response.Status != 200 {
   122  		return ProposalFailedErr(fmt.Sprintf("bad proposal response %d", proposalResp.Response.Status))
   123  	}
   124  
   125  	fmt.Printf("Join Result: %s\n", string(proposalResp.Response.Payload))
   126  
   127  	return nil
   128  }
   129  
   130  func join(cmd *cobra.Command, args []string, cf *ChannelCmdFactory) error {
   131  	var err error
   132  	if cf == nil {
   133  		cf, err = InitCmdFactory(true)
   134  		if err != nil {
   135  			return err
   136  		}
   137  	}
   138  	return executeJoin(cf)
   139  }