github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bddtests/steps/endorser_impl.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 endorser_util
    17  import bdd_grpc_util
    18  import bootstrap_util
    19  
    20  @when(u'user "{userName}" creates a chaincode spec "{ccSpecAlias}" with name "{chaincodeName}" of type "{ccType}" for chaincode "{chaincodePath}" with args')
    21  def step_impl(context, userName, ccType, chaincodeName, chaincodePath, ccSpecAlias):
    22  	directory = bootstrap_util.getDirectory(context=context)
    23  	user = directory.getUser(userName)
    24  	args =  bootstrap_util.getArgsFromContextForUser(context, userName)
    25  	ccSpec = endorser_util.getChaincodeSpec(ccType=ccType, path=chaincodePath, name=chaincodeName, args=bdd_grpc_util.toStringArray(args))
    26  	print("ccSpec = {0}".format(ccSpec))
    27  	user.setTagValue(ccSpecAlias, ccSpec)
    28  
    29  
    30  @when(u'user "{userName}" creates a deployment spec "{ccDeploymentSpecAlias}" using chaincode spec "{ccSpecAlias}" and devops on peer "{devopsComposeService}"')
    31  def step_impl(context, userName, ccDeploymentSpecAlias, ccSpecAlias, devopsComposeService):
    32      directory = bootstrap_util.getDirectory(context=context)
    33      user = directory.getUser(userName=userName)
    34      assert ccSpecAlias in user.tags, "ChaincodeSpec alias '{0}' not found for user '{1}'".format(ccSpecAlias, userName)
    35      deploymentSpec = None
    36      raise Exception("Not Implemented")
    37      #user.setTagValue(ccDeploymentSpecAlias, deploymentSpec)
    38      user.setTagValue(ccDeploymentSpecAlias, deploymentSpec)
    39  
    40  
    41  @when(u'user "{userName}" using cert alias "{certAlias}" creates a install proposal "{proposalAlias}" for channel "{channelName}" using chaincode spec "{ccSpecAlias}"')
    42  def step_impl(context, userName, certAlias, proposalAlias, channelName, ccSpecAlias):
    43      directory = bootstrap_util.getDirectory(context=context)
    44      user = directory.getUser(userName=userName)
    45      assert ccSpecAlias in user.tags, "ChaincodeSpec alias '{0}' not found for user '{1}'".format(ccSpecAlias, userName)
    46      ccSpec = user.tags[ccSpecAlias]
    47  
    48  
    49      ccDeploymentSpec = endorser_util.createDeploymentSpec(context=context, ccSpec=ccSpec)
    50      lcChaincodeSpec = endorser_util.createInstallChaincodeSpecForBDD(ccDeploymentSpec=ccDeploymentSpec, chainID=str(channelName))
    51      # Find the cert using the cert tuple information saved for the user under certAlias
    52      nodeAdminTuple = user.tags[certAlias]
    53      signersCert = directory.findCertForNodeAdminTuple(nodeAdminTuple)
    54      mspID = nodeAdminTuple.organization
    55  
    56      proposal = endorser_util.createInvokeProposalForBDD(context, ccSpec=lcChaincodeSpec, chainID=channelName,signersCert=signersCert, Mspid=mspID, type="ENDORSER_TRANSACTION")
    57  
    58      signedProposal = endorser_util.signProposal(proposal=proposal, entity=user, signersCert=signersCert)
    59  
    60      # proposal = endorser_util.createDeploymentProposalForBDD(ccDeploymentSpec)
    61      assert not proposalAlias in user.tags, "Proposal alias '{0}' already exists for '{1}'".format(proposalAlias, userName)
    62      user.setTagValue(proposalAlias, signedProposal)
    63  
    64  @when(u'user "{userName}" using cert alias "{certAlias}" creates a instantiate proposal "{proposalAlias}" for channel "{channelName}" using chaincode spec "{ccSpecAlias}"')
    65  def step_impl(context, userName, certAlias, proposalAlias, channelName, ccSpecAlias):
    66          directory = bootstrap_util.getDirectory(context=context)
    67          user = directory.getUser(userName=userName)
    68          assert ccSpecAlias in user.tags, "ChaincodeSpec alias '{0}' not found for user '{1}'".format(ccSpecAlias, userName)
    69          ccSpec = user.tags[ccSpecAlias]
    70  
    71  
    72          ccDeploymentSpec = endorser_util.createDeploymentSpec(context=context, ccSpec=ccSpec)
    73          ccDeploymentSpec.code_package = ""
    74          lcChaincodeSpec = endorser_util.createDeploymentChaincodeSpecForBDD(ccDeploymentSpec=ccDeploymentSpec, chainID=str(channelName))
    75          # Find the cert using the cert tuple information saved for the user under certAlias
    76          nodeAdminTuple = user.tags[certAlias]
    77          signersCert = directory.findCertForNodeAdminTuple(nodeAdminTuple)
    78          mspID = nodeAdminTuple.organization
    79  
    80          proposal = endorser_util.createInvokeProposalForBDD(context, ccSpec=lcChaincodeSpec, chainID=channelName,signersCert=signersCert, Mspid=mspID, type="ENDORSER_TRANSACTION")
    81  
    82          signedProposal = endorser_util.signProposal(proposal=proposal, entity=user, signersCert=signersCert)
    83  
    84          # proposal = endorser_util.createDeploymentProposalForBDD(ccDeploymentSpec)
    85          assert not proposalAlias in user.tags, "Proposal alias '{0}' already exists for '{1}'".format(proposalAlias, userName)
    86          user.setTagValue(proposalAlias, signedProposal)
    87  
    88  @when(u'user "{userName}" using cert alias "{certAlias}" creates a proposal "{proposalAlias}" for channel "{channelName}" using chaincode spec "{ccSpecAlias}"')
    89  def step_impl(context, userName, certAlias, proposalAlias, channelName, ccSpecAlias):
    90      directory = bootstrap_util.getDirectory(context=context)
    91      user = directory.getUser(userName=userName)
    92      assert ccSpecAlias in user.tags, "ChaincodeSpec alias '{0}' not found for user '{1}'".format(ccSpecAlias, userName)
    93      lcChaincodeSpec = user.tags[ccSpecAlias]
    94      # Find the cert using the cert tuple information saved for the user under certAlias
    95      nodeAdminTuple = user.tags[certAlias]
    96      signersCert = directory.findCertForNodeAdminTuple(nodeAdminTuple)
    97      mspID = nodeAdminTuple.organization
    98  
    99      proposal = endorser_util.createInvokeProposalForBDD(context, ccSpec=lcChaincodeSpec, chainID=channelName,signersCert=signersCert, Mspid=mspID, type="ENDORSER_TRANSACTION")
   100  
   101      signedProposal = endorser_util.signProposal(proposal=proposal, entity=user, signersCert=signersCert)
   102  
   103      # proposal = endorser_util.createDeploymentProposalForBDD(ccDeploymentSpec)
   104      assert not proposalAlias in user.tags, "Proposal alias '{0}' already exists for '{1}'".format(proposalAlias, userName)
   105      user.setTagValue(proposalAlias, signedProposal)
   106  
   107  
   108  
   109  @when(u'user "{userName}" using cert alias "{certAlias}" sends proposal "{proposalAlias}" to endorsers with timeout of "{timeout}" seconds with proposal responses "{proposalResponsesAlias}"')
   110  def step_impl(context, userName, certAlias, proposalAlias, timeout, proposalResponsesAlias):
   111      assert 'table' in context, "Expected table of endorsers"
   112      directory = bootstrap_util.getDirectory(context=context)
   113      user = directory.getUser(userName=userName)
   114  
   115      assert proposalAlias in user.tags, "Proposal alias '{0}' not found for user '{1}'".format(proposalAlias, userName)
   116      signedProposal = user.tags[proposalAlias]
   117  
   118      # Send proposal to each specified endorser, waiting 'timeout' seconds for response/error
   119      endorsers = [row['Endorser'] for row in context.table.rows]
   120      nodeAdminTuple = user.tags[certAlias]
   121  
   122      endorserStubs = endorser_util.getEndorserStubs(context, composeServices=endorsers, directory=directory, nodeAdminTuple=nodeAdminTuple)
   123      proposalResponseFutures = [endorserStub.ProcessProposal.future(signedProposal, int(timeout)) for endorserStub in endorserStubs]
   124      resultsDict =  dict(zip(endorsers, [respFuture.result() for respFuture in proposalResponseFutures]))
   125      user.setTagValue(proposalResponsesAlias, resultsDict)
   126      # user.tags[proposalResponsesAlias] = resultsDict
   127  
   128  
   129  @then(u'user "{userName}" expects proposal responses "{proposalResponsesAlias}" with status "{statusCode}" from endorsers')
   130  def step_impl(context, userName, proposalResponsesAlias, statusCode):
   131      assert 'table' in context, "Expected table of endorsers"
   132      directory = bootstrap_util.getDirectory(context=context)
   133      user = directory.getUser(userName=userName)
   134      # Make sure proposalResponseAlias not already defined
   135      assert proposalResponsesAlias in user.tags, "Expected proposal responses at tag '{0}', for user '{1}'".format(proposalResponsesAlias, userName)
   136      proposalRespDict = user.tags[proposalResponsesAlias]
   137  
   138      # Loop through endorser proposal Responses
   139      endorsers = [row['Endorser'] for row in context.table.rows]
   140      print("Endorsers = {0}, rsults keys = {1}".format(endorsers, proposalRespDict.keys()))
   141      for respSatusCode in [proposalRespDict[endorser].response.status for endorser in endorsers]:
   142          assert int(statusCode) == respSatusCode, "Expected proposal response status code of {0} from {1}, received {2}".format(statusCode, endorser, respSatusCode)
   143  
   144  @then(u'user "{userName}" expects proposal responses "{proposalResponsesAlias}" each have the same value from endorsers')
   145  def step_impl(context, userName, proposalResponsesAlias):
   146      directory = bootstrap_util.getDirectory(context=context)
   147      user = directory.getUser(userName=userName)
   148      assert proposalResponsesAlias in user.tags, "Expected proposal responses at tag '{0}', for user '{1}'".format(proposalResponsesAlias, userName)
   149      proposalRespDict = user.tags[proposalResponsesAlias]
   150      assert len(proposalRespDict) > 0, "Expected at least 1 proposal response, found none in proposal responses dictionary"
   151      if len(proposalRespDict) == 1:
   152          pass
   153      else:
   154          endorsers = [row['Endorser'] for row in context.table.rows]
   155          endorserToProposalResponseHashDict = dict(zip(endorsers, [user.computeHash(proposalRespDict[endorser].payload) for endorser in endorsers]))
   156          setOfHashes = set(endorserToProposalResponseHashDict.values())
   157          assert len(setOfHashes) == 1, "Hashes from endorsers did NOT match: {0}".format(endorserToProposalResponseHashDict)
   158  
   159  @when(u'user "{userName}" creates a chaincode invocation spec "{chaincodeInvocationSpecName}" using spec "{templateSpecName}" with input')
   160  def step_impl(context, userName, chaincodeInvocationSpecName, templateSpecName):
   161      directory = bootstrap_util.getDirectory(context=context)
   162      user = directory.getUser(userName)
   163      args =  bootstrap_util.getArgsFromContextForUser(context, userName)
   164      template_chaincode_spec = user.tags[templateSpecName]
   165      cc_invocation_spec =  endorser_util.getChaincodeSpecUsingTemplate(template_chaincode_spec=template_chaincode_spec, args=bdd_grpc_util.toStringArray(args))
   166      user.setTagValue(chaincodeInvocationSpecName, cc_invocation_spec)
   167  
   168  @when(u'the user "{userName}" creates transaction "{transactionAlias}" from proposal "{proposalAlias}" and proposal responses "{proposalResponseAlias}" for channel "{channelId}"')
   169  def step_impl(context, userName, transactionAlias, proposalAlias, proposalResponseAlias, channelId):
   170      directory = bootstrap_util.getDirectory(context=context)
   171      user = directory.getUser(userName)
   172      proposalResponsesDict = user.tags[proposalResponseAlias]
   173      proposal = user.tags[proposalAlias]
   174      signedTx = endorser_util.createSignedTx(user, proposal, proposalResponsesDict.values())
   175      user.setTagValue(transactionAlias, signedTx)