github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bddtests/steps/endorser_util.py (about)

     1  # Copyright IBM Corp. 2016 All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  #
    15  
    16  import os
    17  import json
    18  from contexthelper import ContextHelper
    19  import bdd_test_util
    20  import bdd_grpc_util
    21  import bootstrap_util
    22  from peer import chaincode_pb2
    23  from peer import transaction_pb2
    24  from peer import proposal_pb2
    25  from peer import peer_pb2_grpc
    26  from msp import identities_pb2
    27  
    28  from common import common_pb2 as common_dot_common_pb2
    29  
    30  from OpenSSL import crypto
    31  
    32  def getChaincodeSpec(ccType, path, name, args):
    33  	# make chaincode spec for chaincode to be deployed
    34  	ccSpec = chaincode_pb2.ChaincodeSpec(type=chaincode_pb2.ChaincodeSpec.Type.Value(ccType),
    35  		chaincode_id = chaincode_pb2.ChaincodeID(path=path, name=name, version="test"),
    36  		input = chaincode_pb2.ChaincodeInput(args = args))
    37  	return ccSpec
    38  
    39  def getChaincodeSpecUsingTemplate(template_chaincode_spec, args):
    40      # make chaincode spec for chaincode to be deployed
    41      ccSpec = chaincode_pb2.ChaincodeSpec()
    42      input = chaincode_pb2.ChaincodeInput(args = args)
    43      ccSpec.CopyFrom(template_chaincode_spec)
    44      ccSpec.input.CopyFrom(input)
    45      return ccSpec
    46  
    47  
    48  
    49  def createPropsalId():
    50  	return 'TODO proposal Id'
    51  
    52  def createInvokeProposalForBDD(context, ccSpec, chainID, signersCert, Mspid, type):
    53      import binascii
    54  
    55      "Returns a deployment proposal of chaincode type"
    56      lc_chaincode_invocation_spec = chaincode_pb2.ChaincodeInvocationSpec(chaincode_spec = ccSpec)
    57  
    58      # Create
    59      ccHdrExt = proposal_pb2.ChaincodeHeaderExtension(chaincode_id=ccSpec.chaincode_id)
    60  
    61      ccProposalPayload = proposal_pb2.ChaincodeProposalPayload(input=lc_chaincode_invocation_spec.SerializeToString())
    62  
    63      bootstrapHelper = ContextHelper.GetHelper(context=context).getBootrapHelper(chainId=chainID)
    64  
    65      serializedIdentity = identities_pb2.SerializedIdentity(mspid=Mspid, id_bytes=crypto.dump_certificate(crypto.FILETYPE_PEM, signersCert))
    66  
    67      nonce = bootstrap_util.BootstrapHelper.getNonce()
    68  
    69      sigHdr = bootstrapHelper.makeSignatureHeader(serializedIdentity.SerializeToString(), nonce)
    70  
    71      # Calculate the transaction ID
    72      tx_id = binascii.hexlify(bootstrap_util.computeCryptoHash(nonce + serializedIdentity.SerializeToString()))
    73  
    74      chainHdr = bootstrapHelper.makeChainHeader(type=common_dot_common_pb2.HeaderType.Value(type),
    75                                                 txID=tx_id, extension=ccHdrExt.SerializeToString())
    76  
    77      header = common_dot_common_pb2.Header(channel_header=chainHdr.SerializeToString(), signature_header=sigHdr.SerializeToString())
    78  
    79      # make proposal
    80      proposal = proposal_pb2.Proposal(header=header.SerializeToString(), payload=ccProposalPayload.SerializeToString())
    81  
    82  
    83      return proposal
    84  
    85  def createSignedTx(user, signed_proposal, proposal_responses):
    86      assert len(proposal_responses) > 0, "Expected at least 1 ProposalResponse"
    87      #Unpack signed proposal
    88      proposal = proposal_pb2.Proposal()
    89      proposal.ParseFromString(signed_proposal.proposal_bytes)
    90      header = common_dot_common_pb2.Header()
    91      header.ParseFromString(proposal.header)
    92      ccProposalPayload = proposal_pb2.ChaincodeProposalPayload()
    93      ccProposalPayload.ParseFromString(proposal.payload)
    94      # Be sure to clear the TransientMap
    95      ccProposalPayload.TransientMap.clear()
    96  
    97      endorsements = [p.endorsement for p in proposal_responses]
    98      ccEndorsedAction = transaction_pb2.ChaincodeEndorsedAction(proposal_response_payload=proposal_responses[0].payload, endorsements=endorsements)
    99  
   100      ccActionPayload = transaction_pb2.ChaincodeActionPayload(chaincode_proposal_payload=ccProposalPayload.SerializeToString(), action=ccEndorsedAction)
   101  
   102      transaction = transaction_pb2.Transaction()
   103      action = transaction.actions.add()
   104      action.header = header.signature_header
   105      action.payload = ccActionPayload.SerializeToString()
   106      payload = common_dot_common_pb2.Payload(header=header, data=transaction.SerializeToString())
   107      payloadBytes = payload.SerializeToString()
   108      signature = user.sign(payloadBytes)
   109      envelope = common_dot_common_pb2.Envelope(payload=payloadBytes, signature=signature)
   110      return envelope
   111  
   112  
   113  
   114  def signProposal(proposal, entity, signersCert):
   115  	import binascii
   116  	# Sign the proposal
   117  	proposalBytes = proposal.SerializeToString()
   118  	signature = entity.sign(proposalBytes)
   119  	#Verify the signature
   120  	entity.verifySignature(signature=signature, signersCert=signersCert, data=proposalBytes)
   121  	# print("Proposal Bytes signature= \n{0}\n\n".format(binascii.hexlify(bytearray(signature))))
   122  	signedProposal = proposal_pb2.SignedProposal(proposal_bytes=proposalBytes, signature=signature)
   123  	return signedProposal
   124  
   125  
   126  def createDeploymentChaincodeSpecForBDD(ccDeploymentSpec, chainID):
   127      lc_chaincode_spec = getChaincodeSpec(ccType="GOLANG", path="", name="lscc",
   128                                           args=['deploy', chainID, ccDeploymentSpec.SerializeToString()])
   129      return lc_chaincode_spec
   130  
   131  def createInstallChaincodeSpecForBDD(ccDeploymentSpec, chainID):
   132      lc_chaincode_spec = getChaincodeSpec(ccType="GOLANG", path="", name="lscc",
   133                                           args=['install', ccDeploymentSpec.SerializeToString()])
   134      return lc_chaincode_spec
   135  
   136  def getEndorserStubs(context, composeServices, directory, nodeAdminTuple):
   137      stubs = []
   138      user = directory.getUser(nodeAdminTuple.user)
   139      signingOrg = directory.getOrganization(nodeAdminTuple.organization)
   140  
   141      for composeService in composeServices:
   142          ipAddress, port = bdd_test_util.getPortHostMapping(context.compose_containers, composeService, 7051)
   143          # natForPeerSigner = directory.findNodeAdminTuple(userName="{0}Signer".format(composeService), contextName=composeService, orgName="peerOrg0")
   144          # signerCert = directory.getCertAsPEM(natForPeerSigner)
   145          root_certificates = directory.getTrustedRootsForPeerNetworkAsPEM()
   146          channel = bdd_grpc_util.getGRPCChannel(ipAddress=ipAddress, port=port, root_certificates=root_certificates,
   147                                                 ssl_target_name_override=composeService)
   148          newEndorserStub = peer_pb2_grpc.EndorserStub(channel)
   149          stubs.append(newEndorserStub)
   150      return stubs
   151  
   152  def getExample02ChaincodeSpec():
   153      return getChaincodeSpec("GOLANG", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "example02", ["init","a","100","b","200"])
   154  
   155  
   156  
   157  def _createDeploymentSpecAsFile(ccSpec, outputPath):
   158      '''peer chaincode package -n myCC -c '{"Args":["init","a","100","b","200"]}' -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 --logging-level=DEBUG test.file'''
   159      myEnv = os.environ.copy()
   160      myEnv['CORE_PEER_MSPCONFIGPATH'] = "./../sampleconfig/msp"
   161      nameArgs = ["-n", ccSpec.chaincode_id.name]
   162      ctorArgs = ["-c", json.dumps({'Args' : [item for item in ccSpec.input.args]})]
   163      pathArgs = ["-p", ccSpec.chaincode_id.path]
   164      versionArgs = ["-v", ccSpec.chaincode_id.version]
   165      output, error, returncode = \
   166          bdd_test_util.cli_call(["peer","chaincode","package"] + nameArgs + ctorArgs + pathArgs + versionArgs + [outputPath], expect_success=True, env=myEnv)
   167      return output
   168  
   169  
   170  def createDeploymentSpec(context, ccSpec):
   171      contextHelper = ContextHelper.GetHelper(context=context)
   172      contextHelper.getBootrapHelper(chainId="test")
   173      cacheDeploymentSpec = contextHelper.isConfigEnabled("cache-deployment-spec")
   174      fileName = "deploymentspec-{0}-{1}-{2}".format(chaincode_pb2.ChaincodeSpec.Type.Name(ccSpec.type), ccSpec.chaincode_id.path, ccSpec.chaincode_id.name)
   175      outputPath, fileExists = contextHelper.getTmpPathForName(name=fileName,
   176                                                               copyFromCache=cacheDeploymentSpec)
   177      if not fileExists:
   178          _createDeploymentSpecAsFile(ccSpec=ccSpec, outputPath=outputPath)
   179          if cacheDeploymentSpec:
   180              contextHelper.copyToCache(fileName)
   181      ccDeploymentSpec = chaincode_pb2.ChaincodeDeploymentSpec()
   182      with open(outputPath, 'rb') as f:
   183          ccDeploymentSpec.ParseFromString(f.read())
   184      return ccDeploymentSpec