github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/consensus/poet/cli/tests/test_genesis/tests.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  # pylint: disable=invalid-name
    17  
    18  import os
    19  import shutil
    20  import tempfile
    21  import unittest
    22  from unittest.mock import patch
    23  
    24  from sawtooth_signing import create_context
    25  
    26  from sawtooth_poet_cli.main import main
    27  import sawtooth_validator.protobuf.batch_pb2 as batch_pb
    28  import sawtooth_validator.protobuf.transaction_pb2 as txn_pb
    29  import sawtooth_poet_common.protobuf.validator_registry_pb2 as vr_pb
    30  
    31  
    32  class TestValidatorRegistryGenesisTransaction(unittest.TestCase):
    33      def __init__(self, test_name):
    34          super().__init__(test_name)
    35          self._temp_dir = None
    36  
    37      def setUp(self):
    38          self._temp_dir = tempfile.mkdtemp()
    39          self._store = {}
    40  
    41      def tearDown(self):
    42          shutil.rmtree(self._temp_dir)
    43  
    44      @patch('sawtooth_poet_cli.registration.PoetKeyStateStore')
    45      @patch('sawtooth_poet_cli.registration.config.get_data_dir')
    46      @patch('sawtooth_poet_cli.registration.config.get_key_dir')
    47      def test_run_simulator_genesis(self,
    48                                     get_key_dir_fn,
    49                                     get_data_dir_fn,
    50                                     mock_store):
    51          """Test generating a Validator Registry transaction, which is written
    52          to a file.
    53  
    54          This test executes the `poet registration create` command. The
    55          expected output is:
    56  
    57          - a BatchList written to a file at <temp_dir>/poet_genesis.batch
    58          - the serialized sealed signup data is written to the key state store
    59          """
    60          mock_store.return_value = self._store
    61          get_data_dir_fn.return_value = self._temp_dir
    62          get_key_dir_fn.return_value = self._temp_dir
    63  
    64          public_key = self._create_key()
    65  
    66          main('poet',
    67               args=['registration', 'create',
    68                     '-o', os.path.join(self._temp_dir, 'poet-genesis.batch')])
    69  
    70          self._assert_validator_transaction(public_key, 'poet-genesis.batch')
    71  
    72      def _assert_validator_transaction(self, public_key, target_file):
    73          filename = os.path.join(self._temp_dir, target_file)
    74          batch_list = batch_pb.BatchList()
    75          with open(filename, 'rb') as batch_file:
    76              batch_list.ParseFromString(batch_file.read())
    77  
    78          self.assertEqual(1, len(batch_list.batches))
    79  
    80          batch = batch_list.batches[0]
    81          self.assertEqual(1, len(batch.transactions))
    82          batch_header = batch_pb.BatchHeader()
    83          batch_header.ParseFromString(batch.header)
    84  
    85          self.assertEqual(public_key, batch_header.signer_public_key)
    86  
    87          txn = batch.transactions[0]
    88          txn_header = txn_pb.TransactionHeader()
    89          txn_header.ParseFromString(txn.header)
    90  
    91          self.assertEqual(public_key, txn_header.signer_public_key)
    92          self.assertEqual('sawtooth_validator_registry', txn_header.family_name)
    93  
    94          payload = vr_pb.ValidatorRegistryPayload()
    95          payload.ParseFromString(txn.payload)
    96          self._assert_key_state(payload.signup_info.poet_public_key)
    97  
    98      def _assert_key_state(self, poet_public_key):
    99          # Assert that the proper entry was created in the consensus state store
   100          # and that the sealed signup data contains something
   101          self.assertEqual(len(self._store), 1)
   102          self.assertTrue(poet_public_key in self._store)
   103  
   104      def _create_key(self, key_name='validator.priv'):
   105          context = create_context('secp256k1')
   106          private_key = context.new_random_private_key()
   107          priv_file = os.path.join(self._temp_dir, key_name)
   108          with open(priv_file, 'w') as priv_fd:
   109              priv_fd.write(private_key.as_hex())
   110  
   111          return context.get_public_key(private_key).as_hex()