github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_genesis_consensus/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  import unittest
    16  from unittest.mock import Mock
    17  
    18  from sawtooth_validator.journal.block_wrapper import NULL_BLOCK_IDENTIFIER
    19  from sawtooth_validator.journal.block_wrapper import BlockWrapper
    20  from sawtooth_validator.journal.consensus.genesis.genesis_consensus import \
    21      BlockPublisher
    22  from sawtooth_validator.journal.consensus.genesis.genesis_consensus import \
    23      BlockVerifier
    24  
    25  from sawtooth_validator.protobuf.block_pb2 import Block
    26  from sawtooth_validator.protobuf.block_pb2 import BlockHeader
    27  
    28  
    29  class TestGenesisBlockPublisher(unittest.TestCase):
    30      def test_initialize_block(self):
    31          """
    32          Create a block header and initialize it using the genesis consensus
    33          BlockPublisher.
    34  
    35          This test should verify that the appropriate header fields are set and
    36          the consensus module returns that the block should be built.
    37          """
    38          block_cache = Mock()
    39          state_view_factory = Mock()
    40          batch_publisher = Mock()
    41          data_dir = 'mock_dir'
    42          config_dir = 'mock_dir'
    43          validator_id = 'validator_001'
    44          block_publisher = BlockPublisher(block_cache,
    45                                           state_view_factory,
    46                                           batch_publisher,
    47                                           data_dir,
    48                                           config_dir,
    49                                           validator_id)
    50  
    51          block_header = BlockHeader(
    52              previous_block_id=NULL_BLOCK_IDENTIFIER)
    53  
    54          result = block_publisher.initialize_block(block_header)
    55          self.assertTrue(result)
    56          self.assertEqual(b'Genesis', block_header.consensus)
    57  
    58      def test_check_publish_valid(self):
    59          """
    60          Create a block header with the NULL_BLOCK_IDENTIFIER as previous id and
    61          check that it can be published.
    62  
    63          This test should verify that only blocks with the NULL_BLOCK_IDENTIFIER
    64          as previous should be allowed to be published with this consensus
    65          module.
    66          """
    67          block_cache = Mock()
    68          state_view_factory = Mock()
    69          batch_publisher = Mock()
    70          data_dir = 'mock_dir'
    71          config_dir = 'mock_dir'
    72          validator_id = 'validator_001'
    73          block_publisher = BlockPublisher(block_cache,
    74                                           state_view_factory,
    75                                           batch_publisher,
    76                                           data_dir,
    77                                           config_dir,
    78                                           validator_id)
    79  
    80          block_header = BlockHeader(
    81              consensus=b'Genesis',
    82              previous_block_id=NULL_BLOCK_IDENTIFIER)
    83  
    84          result = block_publisher.check_publish_block(block_header)
    85          self.assertTrue(result)
    86  
    87      def test_check_publish_invalid(self):
    88          """
    89          Create a block header with some other block id as the previous id and
    90          check that it can be published.
    91  
    92          This test should verify that only blocks with the NULL_BLOCK_IDENTIFIER
    93          as previous should be allowed to be published with this consensus
    94          module.
    95          """
    96          block_cache = Mock()
    97          state_view_factory = Mock()
    98          batch_publisher = Mock()
    99          data_dir = 'mock_dir'
   100          config_dir = 'mock_dir'
   101          validator_id = 'validator_001'
   102          block_publisher = BlockPublisher(block_cache,
   103                                           state_view_factory,
   104                                           batch_publisher,
   105                                           data_dir,
   106                                           config_dir,
   107                                           validator_id)
   108  
   109          block_header = BlockHeader(
   110              consensus=b'Genesis',
   111              previous_block_id='some_other_id')
   112  
   113          result = block_publisher.check_publish_block(block_header)
   114          self.assertFalse(result)
   115  
   116      def test_finalize_block_valid(self):
   117          """
   118          Create a block header with the NULL_BLOCK_IDENTIFIER as the previous id
   119          and check that it can be properly finalized.
   120  
   121          This test should verify that only blocks with the NULL_BLOCK_IDENTIFIER
   122          as previous should be allowed to be finalized with this consensus
   123          module.
   124          """
   125          block_cache = Mock()
   126          state_view_factory = Mock()
   127          batch_publisher = Mock()
   128          data_dir = 'mock_dir'
   129          config_dir = 'mock_dir'
   130          validator_id = 'validator_001'
   131          block_publisher = BlockPublisher(block_cache,
   132                                           state_view_factory,
   133                                           batch_publisher,
   134                                           data_dir,
   135                                           config_dir,
   136                                           validator_id)
   137  
   138          block_header = BlockHeader(
   139              consensus=b'Genesis',
   140              previous_block_id=NULL_BLOCK_IDENTIFIER)
   141  
   142          result = block_publisher.finalize_block(block_header)
   143          self.assertTrue(result)
   144  
   145      def test_finalize_block_invalid(self):
   146          """
   147          Create a block header with some other block id as the previous id
   148          and check that it can be properly finalized.
   149  
   150          This test should verify that only blocks with the NULL_BLOCK_IDENTIFIER
   151          as previous should be allowed to be finalized with this consensus
   152          module.
   153          """
   154          block_cache = Mock()
   155          state_view_factory = Mock()
   156          batch_publisher = Mock()
   157          data_dir = 'mock_dir'
   158          config_dir = 'mock_dir'
   159          validator_id = 'validator_001'
   160          block_publisher = BlockPublisher(block_cache,
   161                                           state_view_factory,
   162                                           batch_publisher,
   163                                           data_dir,
   164                                           config_dir,
   165                                           validator_id)
   166  
   167          block_header = BlockHeader(
   168              consensus=b'Genesis',
   169              previous_block_id='some_other_id')
   170  
   171          result = block_publisher.finalize_block(block_header)
   172          self.assertFalse(result)
   173  
   174  
   175  class TestGenesisBlockVerifier(unittest.TestCase):
   176      def test_verify_genesis_block(self):
   177          """This test should verify that a block with the NULL_BLOCK_IDENTIFIER
   178          should be considered a valid block using this consensus module.
   179          """
   180          block_cache = Mock()
   181          state_view_factory = Mock()
   182          data_dir = 'mock_dir'
   183          config_dir = 'mock_dir'
   184          validator_id = 'validator_001'
   185          block_verifier = BlockVerifier(block_cache,
   186                                         state_view_factory,
   187                                         data_dir,
   188                                         config_dir,
   189                                         validator_id)
   190  
   191          block = Block(header=BlockHeader(
   192              previous_block_id=NULL_BLOCK_IDENTIFIER).SerializeToString())
   193          block_wrapper = BlockWrapper(block)
   194  
   195          result = block_verifier.verify_block(block_wrapper)
   196          self.assertTrue(result)
   197  
   198      def test_reject_non_genesis_block(self):
   199          """This test should show that a block with any previous blocks should
   200          fail verification.
   201          """
   202          block_cache = Mock()
   203          state_view_factory = Mock()
   204          data_dir = 'mock_dir'
   205          config_dir = 'mock_dir'
   206          validator_id = 'validator_001'
   207          block_verifier = BlockVerifier(block_cache,
   208                                         state_view_factory,
   209                                         data_dir,
   210                                         config_dir,
   211                                         validator_id)
   212  
   213          block = Block(header=BlockHeader(
   214              previous_block_id='some_prev_block_id').SerializeToString())
   215          block_wrapper = BlockWrapper(block)
   216  
   217          result = block_verifier.verify_block(block_wrapper)
   218          self.assertFalse(result)