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

     1  import unittest
     2  from unittest.mock import Mock
     3  
     4  from sawtooth_validator.consensus import handlers
     5  from sawtooth_validator.consensus.proxy import ConsensusProxy
     6  
     7  
     8  class FinalizeBlockResult:
     9      def __init__(self, block, remaining_batches, last_batch, injected_batches):
    10          self.block = block
    11          self.remaining_batches = remaining_batches
    12          self.last_batch = last_batch
    13          self.injected_batches = injected_batches
    14  
    15  
    16  class TestHandlers(unittest.TestCase):
    17  
    18      def setUp(self):
    19          self.mock_proxy = Mock()
    20  
    21      def test_consensus_register_handler(self):
    22          mock_chain_head = Mock()
    23          mock_chain_head.identifier = "dead"
    24          mock_chain_head.previous_block_id = "beef"
    25          mock_chain_head.signer_public_key = "abcd"
    26          mock_chain_head.block_num = 12
    27          mock_chain_head.consensus = b"deadbeef"
    28          self.mock_proxy.register.return_value = mock_chain_head, []
    29          handler = handlers.ConsensusRegisterHandler(self.mock_proxy)
    30          request_class = handler.request_class
    31          request = request_class()
    32          request.name = "test"
    33          request.version = "test"
    34          result = handler.handle(None, request.SerializeToString())
    35          response = result.message_out
    36          self.assertEqual(response.status, handler.response_class.OK)
    37          self.mock_proxy.register.assert_called_with()
    38  
    39      def test_consensus_send_to_handler(self):
    40          handler = handlers.ConsensusSendToHandler(self.mock_proxy)
    41          request_class = handler.request_class
    42          request = request_class()
    43          request.peer_id = b"test"
    44          request.message.message_type = "test"
    45          request.message.content = b"test"
    46          request.message.name = "test"
    47          request.message.version = "test"
    48          result = handler.handle(None, request.SerializeToString())
    49          response = result.message_out
    50          self.assertEqual(response.status, handler.response_class.OK)
    51          self.mock_proxy.send_to.assert_called_with(
    52              request.peer_id,
    53              request.message.SerializeToString())
    54  
    55      def test_consensus_broadcast_handler(self):
    56          handler = handlers.ConsensusBroadcastHandler(self.mock_proxy)
    57          request_class = handler.request_class
    58          request = request_class()
    59          request.message.message_type = "test"
    60          request.message.content = b"test"
    61          request.message.name = "test"
    62          request.message.version = "test"
    63          result = handler.handle(None, request.SerializeToString())
    64          response = result.message_out
    65          self.assertEqual(response.status, handler.response_class.OK)
    66          self.mock_proxy.broadcast.assert_called_with(
    67              request.message.SerializeToString())
    68  
    69      def test_consensus_initialize_block_handler(self):
    70          handler = handlers.ConsensusInitializeBlockHandler(self.mock_proxy)
    71          request_class = handler.request_class
    72          request = request_class()
    73          request.previous_id = b"test"
    74          result = handler.handle(None, request.SerializeToString())
    75          response = result.message_out
    76          self.assertEqual(response.status, handler.response_class.OK)
    77          self.mock_proxy.initialize_block.assert_called_with(
    78              request.previous_id)
    79  
    80      def test_consensus_summarize_block_handler(self):
    81          self.mock_proxy.summarize_block.return_value = b"1234"
    82          handler = handlers.ConsensusSummarizeBlockHandler(self.mock_proxy)
    83          request_class = handler.request_class
    84          request = request_class()
    85          result = handler.handle(None, request.SerializeToString())
    86          response = result.message_out
    87          self.assertEqual(response.status, handler.response_class.OK)
    88          self.mock_proxy.summarize_block.assert_called_with()
    89  
    90      def test_consensus_finalize_block_handler(self):
    91          self.mock_proxy.finalize_block.return_value = b"1234"
    92          handler = handlers.ConsensusFinalizeBlockHandler(self.mock_proxy)
    93          request_class = handler.request_class
    94          request = request_class()
    95          request.data = b"test"
    96          result = handler.handle(None, request.SerializeToString())
    97          response = result.message_out
    98          self.assertEqual(response.status, handler.response_class.OK)
    99          self.mock_proxy.finalize_block.assert_called_with(
   100              request.data)
   101  
   102      def test_consensus_cancel_block_handler(self):
   103          handler = handlers.ConsensusCancelBlockHandler(self.mock_proxy)
   104          request_class = handler.request_class
   105          request = request_class()
   106          result = handler.handle(None, request.SerializeToString())
   107          response = result.message_out
   108          self.assertEqual(response.status, handler.response_class.OK)
   109          self.mock_proxy.cancel_block.assert_called_with()
   110  
   111      def test_consensus_check_blocks_handler(self):
   112          handler = handlers.ConsensusCheckBlocksHandler(self.mock_proxy)
   113          request_class = handler.request_class
   114          request = request_class()
   115          request.block_ids.extend([b"test"])
   116          result = handler.handle(None, request.SerializeToString())
   117          response = result.message_out
   118          self.assertEqual(response.status, handler.response_class.OK)
   119          self.mock_proxy.check_blocks.assert_called_with(
   120              request.block_ids)
   121  
   122      def test_consensus_commit_block_handler(self):
   123          handler = handlers.ConsensusCommitBlockHandler(self.mock_proxy)
   124          request_class = handler.request_class
   125          request = request_class()
   126          request.block_id = b"test"
   127          result = handler.handle(None, request.SerializeToString())
   128          response = result.message_out
   129          self.assertEqual(response.status, handler.response_class.OK)
   130          self.mock_proxy.commit_block.assert_called_with(
   131              request.block_id)
   132  
   133      def test_consensus_ignore_block_handler(self):
   134          handler = handlers.ConsensusIgnoreBlockHandler(self.mock_proxy)
   135          request_class = handler.request_class
   136          request = request_class()
   137          request.block_id = b"test"
   138          result = handler.handle(None, request.SerializeToString())
   139          response = result.message_out
   140          self.assertEqual(response.status, handler.response_class.OK)
   141          self.mock_proxy.ignore_block.assert_called_with(
   142              request.block_id)
   143  
   144      def test_consensus_fail_block_handler(self):
   145          handler = handlers.ConsensusFailBlockHandler(self.mock_proxy)
   146          request_class = handler.request_class
   147          request = request_class()
   148          request.block_id = b"test"
   149          result = handler.handle(None, request.SerializeToString())
   150          response = result.message_out
   151          self.assertEqual(response.status, handler.response_class.OK)
   152          self.mock_proxy.fail_block.assert_called_with(
   153              request.block_id)
   154  
   155      def test_consensus_blocks_get_handler(self):
   156          self.mock_proxy.blocks_get.return_value = [
   157              Mock(
   158                  identifier='abcd',
   159                  previous_block_id='abcd',
   160                  header_signature='abcd',
   161                  signer_public_key='abcd',
   162                  block_num=1,
   163                  consensus=b'consensus')]
   164          handler = handlers.ConsensusBlocksGetHandler(self.mock_proxy)
   165          request_class = handler.request_class
   166          request = request_class()
   167          request.block_ids.extend([b"test"])
   168          result = handler.handle(None, request.SerializeToString())
   169          response = result.message_out
   170          self.assertEqual(response.status, handler.response_class.OK)
   171          self.mock_proxy.blocks_get.assert_called_with(
   172              request.block_ids)
   173  
   174      def test_consensus_chain_head_get_handler(self):
   175          self.mock_proxy.chain_head_get.return_value = Mock(
   176              identifier='abcd',
   177              previous_block_id='abcd',
   178              header_signature='abcd',
   179              signer_public_key='abcd',
   180              block_num=1,
   181              consensus=b'consensus')
   182          handler = handlers.ConsensusChainHeadGetHandler(self.mock_proxy)
   183          request_class = handler.request_class
   184          request = request_class()
   185          result = handler.handle(None, request.SerializeToString())
   186          response = result.message_out
   187          self.assertEqual(response.status, handler.response_class.OK)
   188          self.mock_proxy.chain_head_get.assert_called_with()
   189  
   190      def test_consensus_settings_get_handler(self):
   191          self.mock_proxy.settings_get.return_value = [('key', 'value')]
   192          handler = handlers.ConsensusSettingsGetHandler(self.mock_proxy)
   193          request_class = handler.request_class
   194          request = request_class()
   195          request.block_id = b"test"
   196          request.keys.extend(["test"])
   197          result = handler.handle(None, request.SerializeToString())
   198          response = result.message_out
   199          self.assertEqual(response.status, handler.response_class.OK)
   200          self.mock_proxy.settings_get.assert_called_with(
   201              request.block_id, request.keys)
   202  
   203      def test_consensus_state_get_handler(self):
   204          self.mock_proxy.state_get.return_value = [('address', b'data')]
   205          handler = handlers.ConsensusStateGetHandler(self.mock_proxy)
   206          request_class = handler.request_class
   207          request = request_class()
   208          request.block_id = b"test"
   209          request.addresses.extend(["test"])
   210          result = handler.handle(None, request.SerializeToString())
   211          response = result.message_out
   212          self.assertEqual(response.status, handler.response_class.OK)
   213          self.mock_proxy.state_get.assert_called_with(
   214              request.block_id, request.addresses)
   215  
   216  
   217  class TestProxy(unittest.TestCase):
   218  
   219      def setUp(self):
   220          self._mock_block_cache = {}
   221          self._mock_block_publisher = Mock()
   222          self._mock_chain_controller = Mock()
   223          self._mock_gossip = Mock()
   224          self._mock_identity_signer = Mock()
   225          self._mock_settings_view_factory = Mock()
   226          self._mock_state_view_factory = Mock()
   227          self._proxy = ConsensusProxy(
   228              block_cache=self._mock_block_cache,
   229              chain_controller=self._mock_chain_controller,
   230              block_publisher=self._mock_block_publisher,
   231              gossip=self._mock_gossip,
   232              identity_signer=self._mock_identity_signer,
   233              settings_view_factory=self._mock_settings_view_factory,
   234              state_view_factory=self._mock_state_view_factory)
   235  
   236      def test_send_to(self):
   237          self._proxy.send_to(peer_id=b'peer_id', message=b'message')
   238  
   239      def test_broadcast(self):
   240          self._proxy.broadcast(message=b'message')
   241  
   242      # Using block publisher
   243      def test_initialize_block(self):
   244          self._proxy.initialize_block(None)
   245          self._mock_block_publisher.initialize_block.assert_called_with(
   246              self._mock_chain_controller.chain_head)
   247  
   248          self._mock_block_cache["34"] = "a block"
   249          self._proxy.initialize_block(previous_id=bytes([0x34]))
   250          self._mock_block_publisher\
   251              .initialize_block.assert_called_with("a block")
   252  
   253      def test_summarize_block(self):
   254          self._mock_block_publisher.summarize_block.return_value =\
   255              b"summary"
   256  
   257          summary = self._proxy.summarize_block()
   258          self._mock_block_publisher.summarize_block.assert_called_with()
   259          self.assertEqual(summary, b"summary")
   260  
   261      def test_finalize_block(self):
   262          self._mock_block_publisher.finalize_block.return_value =\
   263              FinalizeBlockResult(
   264                  block=None,
   265                  remaining_batches=None,
   266                  last_batch=None,
   267                  injected_batches=None)
   268          self._mock_block_publisher.publish_block.return_value = "00"
   269  
   270          data = bytes([0x56])
   271          self._proxy.finalize_block(data)
   272          self._mock_block_publisher.finalize_block.assert_called_with(
   273              consensus=data)
   274          self._mock_block_publisher.publish_block.assert_called_with(None, None)
   275  
   276      def test_cancel_block(self):
   277          self._proxy.cancel_block()
   278          self._mock_block_publisher.cancel_block.assert_called_with()
   279  
   280      # Using chain controller
   281      def test_check_blocks(self):
   282          block_ids = [bytes([0x56]), bytes([0x78])]
   283          self._mock_block_cache["56"] = "block0"
   284          self._mock_block_cache["78"] = "block1"
   285          self._proxy.check_blocks(block_ids)
   286          self._mock_chain_controller\
   287              .submit_blocks_for_verification\
   288              .assert_called_with(["block0", "block1"])
   289  
   290      def test_commit_block(self):
   291          self._mock_block_cache["34"] = "a block"
   292          self._proxy.commit_block(block_id=bytes([0x34]))
   293          self._mock_chain_controller\
   294              .commit_block\
   295              .assert_called_with("a block")
   296  
   297      def test_ignore_block(self):
   298          self._mock_block_cache["34"] = "a block"
   299          self._proxy.ignore_block(block_id=bytes([0x34]))
   300          self._mock_chain_controller\
   301              .ignore_block\
   302              .assert_called_with("a block")
   303  
   304      def test_fail_block(self):
   305          self._mock_block_cache["34"] = "a block"
   306          self._proxy.fail_block(block_id=bytes([0x34]))
   307          self._mock_chain_controller\
   308              .fail_block\
   309              .assert_called_with("a block")
   310  
   311      # Using blockstore and state database
   312      def test_blocks_get(self):
   313          block_1 = Mock(
   314              identifier=b'id-1',
   315              previous_block_id=b'prev-1',
   316              header_signature=b'sign-1',
   317              block_num=1,
   318              consensus=b'consensus')
   319  
   320          self._mock_block_cache[b'block1'.hex()] = block_1
   321  
   322          block_2 = Mock(
   323              identifier=b'id-2',
   324              previous_block_id=b'prev-2',
   325              header_signature=b'sign-2',
   326              block_num=2,
   327              consensus=b'consensus')
   328  
   329          self._mock_block_cache[b'block2'.hex()] = block_2
   330  
   331          proxy_block_1, proxy_block_2 = self._proxy.blocks_get([
   332              b'block1',
   333              b'block2'])
   334  
   335          self.assertEqual(
   336              block_1,
   337              proxy_block_1)
   338  
   339          self.assertEqual(
   340              block_2,
   341              proxy_block_2)
   342  
   343      def test_chain_head_get(self):
   344          chain_head = Mock(
   345              identifier=b'id-2',
   346              previous_block_id=b'prev-2',
   347              header_signature=b'sign-2',
   348              block_num=2,
   349              consensus=b'consensus')
   350  
   351          self._mock_chain_controller.chain_head = chain_head
   352  
   353          self.assertEqual(
   354              self._proxy.chain_head_get(),
   355              chain_head)
   356  
   357      def test_settings_get(self):
   358          self._mock_block_cache[b'block'.hex()] = MockBlock()
   359  
   360          self.assertEqual(
   361              self._proxy.settings_get(b'block', ['key1', 'key2']),
   362              [
   363                  ('key1', 'mock-key1'),
   364                  ('key2', 'mock-key2'),
   365              ])
   366  
   367      def test_state_get(self):
   368          self._mock_block_cache[b'block'.hex()] = MockBlock()
   369  
   370          self.assertEqual(
   371              self._proxy.state_get(b'block', ['address-1', 'address-2']),
   372              [
   373                  ('address-1', b'mock-address-1'),
   374                  ('address-2', b'mock-address-2'),
   375              ])
   376  
   377  
   378  class MockBlock:
   379      def get_state_view(self, state_view_factory):
   380          return MockStateView()
   381  
   382      def get_settings_view(self, settings_view_factory):
   383          return MockSettingsView()
   384  
   385  
   386  class MockStateView:
   387      def get(self, address):
   388          return 'mock-{}'.format(address).encode()
   389  
   390  
   391  class MockSettingsView:
   392      def get_setting(self, key):
   393          return 'mock-{}'.format(key)