github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/integration/sawtooth_integration/tests/test_poet_smoke.py (about)

     1  # Copyright 2017 Intel Corporation
     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 unittest
    17  import subprocess
    18  import shlex
    19  import time
    20  import logging
    21  
    22  from sawtooth_integration.tests.integration_tools import wait_for_rest_apis
    23  from sawtooth_integration.tests.intkey_client import IntkeyClient
    24  
    25  LOGGER = logging.getLogger(__name__)
    26  LOGGER.setLevel(logging.DEBUG)
    27  
    28  # This needs to be coordinated with the test's yaml file.
    29  VALIDATOR_COUNT = 3
    30  BATCH_COUNT = 20
    31  WAIT = 120
    32  
    33  
    34  class TestPoetSmoke(unittest.TestCase):
    35      def setUp(self):
    36          endpoints = ['rest-api-{}:8008'.format(i)
    37                       for i in range(VALIDATOR_COUNT)]
    38  
    39          wait_for_rest_apis(endpoints)
    40  
    41          self.clients = [IntkeyClient('http://' + endpoint, WAIT)
    42                          for endpoint in endpoints]
    43  
    44      def test_poet_smoke(self):
    45          '''
    46          $VALIDATOR_COUNT validators are started, each with config,
    47          intkey, and validator registry transaction processors. After
    48          waiting for the validators to register, do the following:
    49  
    50          1) Send a batch of intkey 'set' transactions to one validator.
    51  
    52          2) Send one batch of intkey 'inc' transactions to each validator
    53             one after the other $BATCH_COUNT times.
    54  
    55          3) Loop through the validators, sending each one $BATCH_COUNT
    56             batches of intkey 'inc' transactions.
    57  
    58          4) Assert that the validators are in consensus with each.
    59          '''
    60          populate, increment = _make_txns()
    61  
    62          self.assert_consensus()
    63  
    64          LOGGER.info('Sending populate txns')
    65          self.clients[0].send_txns(populate)
    66  
    67          # send txns to each validator in succession
    68          for i in range(BATCH_COUNT):
    69              for client in self.clients:
    70                  LOGGER.info('Sending batch %s @ %s', i, client.url)
    71                  client.send_txns(increment)
    72  
    73          # send txns to one validator at a time
    74          for client in self.clients:
    75              for i in range(BATCH_COUNT):
    76                  LOGGER.info('Sending batch %s @ %s', i, client.url)
    77                  client.send_txns(increment)
    78  
    79          # wait for validators to catch up
    80  
    81          self.assert_consensus()
    82  
    83      # if the validators aren't in consensus, wait and try again
    84      def assert_consensus(self):
    85          start_time = time.time()
    86  
    87          while time.time() < start_time + WAIT:
    88              try:
    89                  self._assert_consensus()
    90                  return
    91              except AssertionError:
    92                  LOGGER.info(
    93                      'Blocks not yet in consensus after %d seconds' %
    94                      time.time() - start_time)
    95  
    96          raise AssertionError(
    97              'Validators were not in consensus after %d seconds' % WAIT)
    98  
    99      def _assert_consensus(self):
   100          tolerance = self.clients[0].calculate_tolerance()
   101  
   102          LOGGER.info('Verifying consensus @ tolerance %s', tolerance)
   103  
   104          # for convenience, list the blocks
   105          for client in self.clients:
   106              url = client.url
   107              LOGGER.info('Blocks @ %s', url)
   108              subprocess.run(
   109                  shlex.split(
   110                      'sawtooth block list --url {}'.format(url)),
   111                  check=True)
   112  
   113          list_of_sig_lists = [
   114              client.recent_block_signatures(tolerance)
   115              for client in self.clients
   116          ]
   117  
   118          sig_list_0 = list_of_sig_lists[0]
   119  
   120          for sig_list in list_of_sig_lists[1:]:
   121              self.assertTrue(
   122                  any(sig in sig_list for sig in sig_list_0),
   123                  'Validators are not in consensus')
   124  
   125  
   126  def _make_txns():
   127      fruits = 'fig', 'quince', 'medlar', 'cornel', 'pomegranate'
   128      populate = [('set', fruit, 10000) for fruit in fruits]
   129      increment = [('inc', fruit, 1) for fruit in fruits]
   130      return populate, increment