github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/chaincode/ccproviderimpl.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 chaincode
    18  
    19  import (
    20  	"context"
    21  
    22  	"fmt"
    23  
    24  	"github.com/hyperledger/fabric/core/common/ccprovider"
    25  	"github.com/hyperledger/fabric/core/ledger"
    26  	pb "github.com/hyperledger/fabric/protos/peer"
    27  )
    28  
    29  // ccProviderFactory implements the ccprovider.ChaincodeProviderFactory
    30  // interface and returns instances of ccprovider.ChaincodeProvider
    31  type ccProviderFactory struct {
    32  }
    33  
    34  // NewChaincodeProvider returns pointers to ccProviderImpl as an
    35  // implementer of the ccprovider.ChaincodeProvider interface
    36  func (c *ccProviderFactory) NewChaincodeProvider() ccprovider.ChaincodeProvider {
    37  	return &ccProviderImpl{}
    38  }
    39  
    40  // init is called when this package is loaded. This implementation registers the factory
    41  func init() {
    42  	ccprovider.RegisterChaincodeProviderFactory(&ccProviderFactory{})
    43  }
    44  
    45  // ccProviderImpl is an implementation of the ccprovider.ChaincodeProvider interface
    46  type ccProviderImpl struct {
    47  	txsim ledger.TxSimulator
    48  }
    49  
    50  // ccProviderContextImpl contains the state that is passed around to calls to methods of ccProviderImpl
    51  type ccProviderContextImpl struct {
    52  	ctx *ccprovider.CCContext
    53  }
    54  
    55  // GetContext returns a context for the supplied ledger, with the appropriate tx simulator
    56  func (c *ccProviderImpl) GetContext(ledger ledger.PeerLedger) (context.Context, error) {
    57  	var err error
    58  	// get context for the chaincode execution
    59  	c.txsim, err = ledger.NewTxSimulator()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	ctxt := context.WithValue(context.Background(), TXSimulatorKey, c.txsim)
    64  	return ctxt, nil
    65  }
    66  
    67  // GetCCContext returns an interface that encapsulates a
    68  // chaincode context; the interface is required to avoid
    69  // referencing the chaincode package from the interface definition
    70  func (c *ccProviderImpl) GetCCContext(cid, name, version, txid string, syscc bool, signedProp *pb.SignedProposal, prop *pb.Proposal) interface{} {
    71  	ctx := ccprovider.NewCCContext(cid, name, version, txid, syscc, signedProp, prop)
    72  	return &ccProviderContextImpl{ctx: ctx}
    73  }
    74  
    75  // GetCCValidationInfoFromLCCC returns the VSCC and the policy listed in LCCC for the supplied chaincode
    76  func (c *ccProviderImpl) GetCCValidationInfoFromLCCC(ctxt context.Context, txid string, signedProp *pb.SignedProposal, prop *pb.Proposal, chainID string, chaincodeID string) (string, []byte, error) {
    77  	// LCCC does not have any notion about its own
    78  	// endorsing policy - we should never call this
    79  	// function with lccc as the chaincodeID
    80  	if chaincodeID == "lccc" {
    81  		panic("GetCCValidationInfoFromLCCC invoke for LCCC")
    82  	}
    83  
    84  	data, err := GetChaincodeDataFromLCCC(ctxt, txid, signedProp, prop, chainID, chaincodeID)
    85  	if err != nil {
    86  		return "", nil, err
    87  	}
    88  
    89  	if data == nil || data.Vscc == "" || data.Policy == nil {
    90  		return "", nil, fmt.Errorf("Incorrect validation info in LCCC")
    91  	}
    92  
    93  	return data.Vscc, data.Policy, nil
    94  }
    95  
    96  // ExecuteChaincode executes the chaincode specified in the context with the specified arguments
    97  func (c *ccProviderImpl) ExecuteChaincode(ctxt context.Context, cccid interface{}, args [][]byte) (*pb.Response, *pb.ChaincodeEvent, error) {
    98  	return ExecuteChaincode(ctxt, cccid.(*ccProviderContextImpl).ctx, args)
    99  }
   100  
   101  // Execute executes the chaincode given context and spec (invocation or deploy)
   102  func (c *ccProviderImpl) Execute(ctxt context.Context, cccid interface{}, spec interface{}) (*pb.Response, *pb.ChaincodeEvent, error) {
   103  	return Execute(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
   104  }
   105  
   106  // ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
   107  func (c *ccProviderImpl) ExecuteWithErrorFilter(ctxt context.Context, cccid interface{}, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
   108  	return ExecuteWithErrorFilter(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
   109  }
   110  
   111  // ExecuteWithErrorFilder executes the chaincode given context and spec and returns payload
   112  func (c *ccProviderImpl) Stop(ctxt context.Context, cccid interface{}, spec *pb.ChaincodeDeploymentSpec) error {
   113  	if theChaincodeSupport != nil {
   114  		return theChaincodeSupport.Stop(ctxt, cccid.(*ccProviderContextImpl).ctx, spec)
   115  	}
   116  	panic("ChaincodeSupport not initialized")
   117  }
   118  
   119  // ReleaseContext frees up resources held by the context
   120  func (c *ccProviderImpl) ReleaseContext() {
   121  	c.txsim.Done()
   122  }