github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/chaincode/chaincodeexec.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 chaincode
    18  
    19  import (
    20  	"golang.org/x/net/context"
    21  
    22  	"fmt"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/hyperledger/fabric/common/util"
    26  	"github.com/hyperledger/fabric/core/chaincode/shim"
    27  	"github.com/hyperledger/fabric/core/common/ccprovider"
    28  	pb "github.com/hyperledger/fabric/protos/peer"
    29  )
    30  
    31  //create a chaincode invocation spec
    32  func createCIS(ccname string, args [][]byte) (*pb.ChaincodeInvocationSpec, error) {
    33  	var err error
    34  	spec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: ccname}, Input: &pb.ChaincodeInput{Args: args}}}
    35  	if nil != err {
    36  		return nil, err
    37  	}
    38  	return spec, nil
    39  }
    40  
    41  // GetCDSFromLSCC gets chaincode deployment spec from LSCC
    42  func GetCDSFromLSCC(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) ([]byte, error) {
    43  	version := util.GetSysCCVersion()
    44  	cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
    45  	res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
    46  	if err != nil {
    47  		return nil, fmt.Errorf("Execute getdepspec(%s, %s) of LSCC error: %s", chainID, chaincodeID, err)
    48  	}
    49  	if res.Status != shim.OK {
    50  		return nil, fmt.Errorf("Get ChaincodeDeploymentSpec for %s/%s from LSCC error: %s", chaincodeID, chainID, res.Message)
    51  	}
    52  
    53  	return res.Payload, nil
    54  }
    55  
    56  // GetChaincodeDataFromLSCC gets chaincode data from LSCC given name
    57  func GetChaincodeDataFromLSCC(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (*ccprovider.ChaincodeData, error) {
    58  	version := util.GetSysCCVersion()
    59  	cccid := ccprovider.NewCCContext(chainID, "lscc", version, txid, true, signedProp, prop)
    60  	res, _, err := ExecuteChaincode(ctxt, cccid, [][]byte{[]byte("getccdata"), []byte(chainID), []byte(chaincodeID)})
    61  	if err == nil {
    62  		if res.Status != shim.OK {
    63  			return nil, fmt.Errorf("%s", res.Message)
    64  		}
    65  		cd := &ccprovider.ChaincodeData{}
    66  		err = proto.Unmarshal(res.Payload, cd)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		return cd, nil
    71  	}
    72  
    73  	return nil, err
    74  }
    75  
    76  // ExecuteChaincode executes a given chaincode given chaincode name and arguments
    77  func ExecuteChaincode(ctxt context.Context, cccid *ccprovider.CCContext, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
    78  	var spec *pb.ChaincodeInvocationSpec
    79  	var err error
    80  	var res *pb.Response
    81  	var ccevent *pb.ChaincodeEvent
    82  
    83  	spec, err = createCIS(cccid.Name, args)
    84  	res, ccevent, err = Execute(ctxt, cccid, spec)
    85  	if err != nil {
    86  		chaincodeLogger.Errorf("Error executing chaincode: %s", err)
    87  		return nil, nil, fmt.Errorf("Error executing chaincode: %s", err)
    88  	}
    89  
    90  	return res, ccevent, err
    91  }