github.com/fnagchunpeng/fabric@v2.1.1+incompatible/core/endorser/support.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package endorser
     8  
     9  import (
    10  	"fmt"
    11  
    12  	pb "github.com/hyperledger/fabric-protos-go/peer"
    13  	"github.com/hyperledger/fabric/common/channelconfig"
    14  	"github.com/hyperledger/fabric/core/aclmgmt"
    15  	"github.com/hyperledger/fabric/core/aclmgmt/resources"
    16  	"github.com/hyperledger/fabric/core/chaincode"
    17  	"github.com/hyperledger/fabric/core/chaincode/lifecycle"
    18  	"github.com/hyperledger/fabric/core/common/ccprovider"
    19  	"github.com/hyperledger/fabric/core/handlers/decoration"
    20  	endorsement "github.com/hyperledger/fabric/core/handlers/endorsement/api/identities"
    21  	"github.com/hyperledger/fabric/core/handlers/library"
    22  	"github.com/hyperledger/fabric/core/ledger"
    23  	"github.com/hyperledger/fabric/core/scc"
    24  	"github.com/hyperledger/fabric/internal/pkg/identity"
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  // PeerOperations contains the peer operatiosn required to support the
    29  // endorser.
    30  type PeerOperations interface {
    31  	GetApplicationConfig(cid string) (channelconfig.Application, bool)
    32  	GetLedger(cid string) ledger.PeerLedger
    33  }
    34  
    35  // SupportImpl provides an implementation of the endorser.Support interface
    36  // issuing calls to various static methods of the peer
    37  type SupportImpl struct {
    38  	*PluginEndorser
    39  	identity.SignerSerializer
    40  	Peer             PeerOperations
    41  	ChaincodeSupport *chaincode.ChaincodeSupport
    42  	ACLProvider      aclmgmt.ACLProvider
    43  	BuiltinSCCs      scc.BuiltinSCCs
    44  }
    45  
    46  func (s *SupportImpl) NewQueryCreator(channel string) (QueryCreator, error) {
    47  	lgr := s.Peer.GetLedger(channel)
    48  	if lgr == nil {
    49  		return nil, errors.Errorf("channel %s doesn't exist", channel)
    50  	}
    51  	return lgr, nil
    52  }
    53  
    54  func (s *SupportImpl) SigningIdentityForRequest(*pb.SignedProposal) (endorsement.SigningIdentity, error) {
    55  	return s.SignerSerializer, nil
    56  }
    57  
    58  // GetTxSimulator returns the transaction simulator for the specified ledger
    59  // a client may obtain more than one such simulator; they are made unique
    60  // by way of the supplied txid
    61  func (s *SupportImpl) GetTxSimulator(ledgername string, txid string) (ledger.TxSimulator, error) {
    62  	lgr := s.Peer.GetLedger(ledgername)
    63  	if lgr == nil {
    64  		return nil, errors.Errorf("Channel does not exist: %s", ledgername)
    65  	}
    66  	return lgr.NewTxSimulator(txid)
    67  }
    68  
    69  // GetHistoryQueryExecutor gives handle to a history query executor for the
    70  // specified ledger
    71  func (s *SupportImpl) GetHistoryQueryExecutor(ledgername string) (ledger.HistoryQueryExecutor, error) {
    72  	lgr := s.Peer.GetLedger(ledgername)
    73  	if lgr == nil {
    74  		return nil, errors.Errorf("Channel does not exist: %s", ledgername)
    75  	}
    76  	return lgr.NewHistoryQueryExecutor()
    77  }
    78  
    79  // GetTransactionByID retrieves a transaction by id
    80  func (s *SupportImpl) GetTransactionByID(chid, txID string) (*pb.ProcessedTransaction, error) {
    81  	lgr := s.Peer.GetLedger(chid)
    82  	if lgr == nil {
    83  		return nil, errors.Errorf("failed to look up the ledger for Channel %s", chid)
    84  	}
    85  	tx, err := lgr.GetTransactionByID(txID)
    86  	if err != nil {
    87  		return nil, errors.WithMessage(err, "GetTransactionByID failed")
    88  	}
    89  	return tx, nil
    90  }
    91  
    92  // GetLedgerHeight returns ledger height for given channelID
    93  func (s *SupportImpl) GetLedgerHeight(channelID string) (uint64, error) {
    94  	lgr := s.Peer.GetLedger(channelID)
    95  	if lgr == nil {
    96  		return 0, errors.Errorf("failed to look up the ledger for Channel %s", channelID)
    97  	}
    98  
    99  	info, err := lgr.GetBlockchainInfo()
   100  	if err != nil {
   101  		return 0, errors.Wrap(err, fmt.Sprintf("failed to obtain information for Channel %s", channelID))
   102  	}
   103  
   104  	return info.Height, nil
   105  }
   106  
   107  // IsSysCC returns true if the name matches a system chaincode's
   108  // system chaincode names are system, chain wide
   109  func (s *SupportImpl) IsSysCC(name string) bool {
   110  	return s.BuiltinSCCs.IsSysCC(name)
   111  }
   112  
   113  // ExecuteInit a deployment proposal and return the chaincode response
   114  func (s *SupportImpl) ExecuteLegacyInit(txParams *ccprovider.TransactionParams, name, version string, input *pb.ChaincodeInput) (*pb.Response, *pb.ChaincodeEvent, error) {
   115  	return s.ChaincodeSupport.ExecuteLegacyInit(txParams, name, version, input)
   116  }
   117  
   118  // Execute a proposal and return the chaincode response
   119  func (s *SupportImpl) Execute(txParams *ccprovider.TransactionParams, name string, input *pb.ChaincodeInput) (*pb.Response, *pb.ChaincodeEvent, error) {
   120  	// decorate the chaincode input
   121  	decorators := library.InitRegistry(library.Config{}).Lookup(library.Decoration).([]decoration.Decorator)
   122  	input.Decorations = make(map[string][]byte)
   123  	input = decoration.Apply(txParams.Proposal, input, decorators...)
   124  	txParams.ProposalDecorations = input.Decorations
   125  
   126  	return s.ChaincodeSupport.Execute(txParams, name, input)
   127  }
   128  
   129  // ChaincodeEndorsementInfo returns info needed to endorse a tx for the chaincode with the supplied name.
   130  func (s *SupportImpl) ChaincodeEndorsementInfo(channelID, chaincodeName string, txsim ledger.QueryExecutor) (*lifecycle.ChaincodeEndorsementInfo, error) {
   131  	return s.ChaincodeSupport.Lifecycle.ChaincodeEndorsementInfo(channelID, chaincodeName, txsim)
   132  }
   133  
   134  // CheckACL checks the ACL for the resource for the Channel using the
   135  // SignedProposal from which an id can be extracted for testing against a policy
   136  func (s *SupportImpl) CheckACL(channelID string, signedProp *pb.SignedProposal) error {
   137  	return s.ACLProvider.CheckACL(resources.Peer_Propose, channelID, signedProp)
   138  }
   139  
   140  // GetApplicationConfig returns the configtxapplication.SharedConfig for the Channel
   141  // and whether the Application config exists
   142  func (s *SupportImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
   143  	return s.Peer.GetApplicationConfig(cid)
   144  }
   145  
   146  // GetDeployedCCInfoProvider returns ledger.DeployedChaincodeInfoProvider
   147  func (s *SupportImpl) GetDeployedCCInfoProvider() ledger.DeployedChaincodeInfoProvider {
   148  	return s.ChaincodeSupport.DeployedCCInfoProvider
   149  }