Exemplo n.º 1
0
class TestProxy(unittest.TestCase):
    def setUp(self):
        self._mock_block_manager = MockBlockManager()
        self._mock_block_publisher = Mock()
        self._mock_chain_controller = Mock()
        self._mock_gossip = Mock()
        self._mock_identity_signer = Mock()
        self._mock_settings_view_factory = Mock()
        self._mock_state_view_factory = Mock()
        self._proxy = ConsensusProxy(
            block_manager=self._mock_block_manager,
            chain_controller=self._mock_chain_controller,
            block_publisher=self._mock_block_publisher,
            gossip=self._mock_gossip,
            identity_signer=self._mock_identity_signer,
            settings_view_factory=self._mock_settings_view_factory,
            state_view_factory=self._mock_state_view_factory)

    def test_send_to(self):
        self._proxy.send_to(peer_id=b'peer_id', message=b'message')

    def test_broadcast(self):
        self._proxy.broadcast(message=b'message')

    # Using block publisher
    def test_initialize_block(self):
        self._proxy.initialize_block(None)
        self._mock_block_publisher.initialize_block.assert_called_with(
            self._mock_chain_controller.chain_head)

        self._mock_block_manager["34"] = "a block"
        self._proxy.initialize_block(previous_id=bytes([0x34]))
        self._mock_block_publisher\
            .initialize_block.assert_called_with("a block")

    def test_summarize_block(self):
        self._mock_block_publisher.summarize_block.return_value =\
            b"summary"

        summary = self._proxy.summarize_block()
        self._mock_block_publisher.summarize_block.assert_called_with()
        self.assertEqual(summary, b"summary")

    def test_finalize_block(self):
        self._mock_block_publisher.finalize_block.return_value = "00"

        data = bytes([0x56])
        self._proxy.finalize_block(data)
        self._mock_block_publisher.finalize_block.assert_called_with(
            consensus=data)

    def test_cancel_block(self):
        self._proxy.cancel_block()
        self._mock_block_publisher.cancel_block.assert_called_with()

    # Using chain controller
    def test_check_blocks(self):
        block_ids = [bytes([0x56]), bytes([0x78])]
        self._mock_block_manager["56"] = "block0"
        self._mock_block_manager["78"] = "block1"
        self._proxy.check_blocks(block_ids)

        with self.assertRaises(UnknownBlock):
            self._proxy.check_blocks([bytes([0x00])])

    def test_commit_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.commit_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .commit_block\
            .assert_called_with("a block")

    def test_ignore_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.ignore_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .ignore_block\
            .assert_called_with("a block")

    def test_fail_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.fail_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .fail_block\
            .assert_called_with("a block")

    # Using blockstore and state database
    def test_blocks_get(self):
        block_1 = Mock(identifier=b'id-1',
                       previous_block_id=b'prev-1',
                       header_signature=b'sign-1',
                       block_num=1,
                       consensus=b'consensus')

        self._mock_block_manager[b'block1'.hex()] = block_1

        block_2 = Mock(identifier=b'id-2',
                       previous_block_id=b'prev-2',
                       header_signature=b'sign-2',
                       block_num=2,
                       consensus=b'consensus')

        self._mock_block_manager[b'block2'.hex()] = block_2

        proxy_block_1, proxy_block_2 = self._proxy.blocks_get(
            [b'block1', b'block2'])

        self.assertEqual(block_1, proxy_block_1)

        self.assertEqual(block_2, proxy_block_2)

    def test_chain_head_get(self):
        chain_head = Mock(identifier=b'id-2',
                          previous_block_id=b'prev-2',
                          header_signature=b'sign-2',
                          block_num=2,
                          consensus=b'consensus')

        self._mock_chain_controller.chain_head = chain_head

        self.assertEqual(self._proxy.chain_head_get(), chain_head)

    @unittest.skip("Test will have to be rethought due to removal of "
                   "blockwrapper from proxy")
    def test_settings_get(self):
        self._mock_block_manager[b'block'.hex()] = MockBlock()

        self.assertEqual(self._proxy.settings_get(b'block', ['key1', 'key2']),
                         [
                             ('key1', 'mock-key1'),
                             ('key2', 'mock-key2'),
                         ])

    @unittest.skip("Test will have to be rethought due to removal of "
                   "blockwrapper from proxy")
    def test_state_get(self):
        self._mock_block_manager[b'block'.hex()] = MockBlock()

        address_1 = '1' * 70
        address_2 = '2' * 70

        self.assertEqual(
            self._proxy.state_get(b'block', [address_1, address_2]), [
                (address_1, 'mock-{}'.format(address_1).encode()),
                (address_2, 'mock-{}'.format(address_2).encode()),
            ])
Exemplo n.º 2
0
class TestProxy(unittest.TestCase):

    def setUp(self):
        self._mock_block_manager = MockBlockManager()
        self._mock_block_publisher = Mock()
        self._mock_chain_controller = Mock()
        self._mock_gossip = MockGossip()
        self._mock_identity_signer = MockIdentitySigner()
        self._mock_settings_view_factory = Mock()
        self._mock_state_view_factory = Mock()
        self._consensus_registry = MockConsensusRegistry()
        self._consensus_notifier = Mock()
        self._proxy = ConsensusProxy(
            block_manager=self._mock_block_manager,
            chain_controller=self._mock_chain_controller,
            block_publisher=self._mock_block_publisher,
            gossip=self._mock_gossip,
            identity_signer=self._mock_identity_signer,
            settings_view_factory=self._mock_settings_view_factory,
            state_view_factory=self._mock_state_view_factory,
            consensus_registry=self._consensus_registry,
            consensus_notifier=self._consensus_notifier)

    def test_send_to(self):
        self._proxy.send_to(
            peer_id=b'peer_id',
            content=b'message',
            message_type='message_type',
            connection_id=b'')

    def test_broadcast(self):
        self._proxy.broadcast(
            content=b'message',
            message_type='message_type',
            connection_id=b'')

    # Using block publisher
    def test_initialize_block(self):
        self._proxy.initialize_block(None)
        self._mock_block_publisher.initialize_block.assert_called_with(
            self._mock_chain_controller.chain_head)

        self._mock_block_manager["34"] = "a block"
        self._proxy.initialize_block(previous_id=bytes([0x34]))
        self._mock_block_publisher\
            .initialize_block.assert_called_with("a block")

    def test_summarize_block(self):
        self._mock_block_publisher.summarize_block.return_value =\
            b"summary"

        summary = self._proxy.summarize_block()
        self._mock_block_publisher.summarize_block.assert_called_with()
        self.assertEqual(summary, b"summary")

    def test_finalize_block(self):
        self._mock_block_publisher.finalize_block.return_value = "00"

        data = bytes([0x56])
        self._proxy.finalize_block(data)
        self._mock_block_publisher.finalize_block.assert_called_with(
            consensus=data)

    def test_cancel_block(self):
        self._proxy.cancel_block()
        self._mock_block_publisher.cancel_block.assert_called_with()

    # Using chain controller
    def test_check_blocks(self):
        block_ids = [bytes([0x56]), bytes([0x78])]
        self._mock_block_manager["56"] = "block0"
        self._mock_block_manager["78"] = "block1"
        self._proxy.check_blocks(block_ids)

        with self.assertRaises(UnknownBlock):
            self._proxy.check_blocks([bytes([0x00])])

    def test_commit_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.commit_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .commit_block\
            .assert_called_with("a block")

    def test_ignore_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.ignore_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .ignore_block\
            .assert_called_with("a block")

    def test_fail_block(self):
        self._mock_block_manager["34"] = "a block"
        self._proxy.fail_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .fail_block\
            .assert_called_with("a block")

    # Using blockstore and state database
    def test_blocks_get(self):
        block_1 = Mock(
            identifier=b'id-1',
            previous_block_id=b'prev-1',
            header_signature=b'sign-1',
            block_num=1,
            consensus=b'consensus')

        self._mock_block_manager[b'block1'.hex()] = block_1

        block_2 = Mock(
            identifier=b'id-2',
            previous_block_id=b'prev-2',
            header_signature=b'sign-2',
            block_num=2,
            consensus=b'consensus')

        self._mock_block_manager[b'block2'.hex()] = block_2

        proxy_block_1, proxy_block_2 = self._proxy.blocks_get([
            b'block1',
            b'block2'])

        self.assertEqual(
            block_1,
            proxy_block_1)

        self.assertEqual(
            block_2,
            proxy_block_2)

    def test_chain_head_get(self):
        chain_head = Mock(
            identifier=b'id-2',
            previous_block_id=b'prev-2',
            header_signature=b'sign-2',
            block_num=2,
            consensus=b'consensus')

        self._mock_chain_controller.chain_head = chain_head

        self.assertEqual(
            self._proxy.chain_head_get(),
            chain_head)

    @unittest.skip("Test will have to be rethought due to removal of "
                   "blockwrapper from proxy")
    def test_settings_get(self):
        self._mock_block_manager[b'block'.hex()] = MockBlock()

        self.assertEqual(
            self._proxy.settings_get(b'block', ['key1', 'key2']),
            [
                ('key1', 'mock-key1'),
                ('key2', 'mock-key2'),
            ])

    @unittest.skip("Test will have to be rethought due to removal of "
                   "blockwrapper from proxy")
    def test_state_get(self):
        self._mock_block_manager[b'block'.hex()] = MockBlock()

        address_1 = '1' * 70
        address_2 = '2' * 70

        self.assertEqual(
            self._proxy.state_get(b'block', [address_1, address_2]),
            [
                (address_1, 'mock-{}'.format(address_1).encode()),
                (address_2, 'mock-{}'.format(address_2).encode()),
            ])
Exemplo n.º 3
0
class TestProxy(unittest.TestCase):

    def setUp(self):
        self._mock_block_cache = {}
        self._mock_block_publisher = Mock()
        self._mock_chain_controller = Mock()
        self._mock_gossip = Mock()
        self._mock_identity_signer = Mock()
        self._mock_settings_view_factory = Mock()
        self._mock_state_view_factory = Mock()
        self._proxy = ConsensusProxy(
            block_cache=self._mock_block_cache,
            chain_controller=self._mock_chain_controller,
            block_publisher=self._mock_block_publisher,
            gossip=self._mock_gossip,
            identity_signer=self._mock_identity_signer,
            settings_view_factory=self._mock_settings_view_factory,
            state_view_factory=self._mock_state_view_factory)

    def test_send_to(self):
        self._proxy.send_to(peer_id=b'peer_id', message=b'message')

    def test_broadcast(self):
        self._proxy.broadcast(message=b'message')

    # Using block publisher
    def test_initialize_block(self):
        self._proxy.initialize_block(None)
        self._mock_block_publisher.initialize_block.assert_called_with(
            self._mock_chain_controller.chain_head)

        self._mock_block_cache["34"] = "a block"
        self._proxy.initialize_block(previous_id=bytes([0x34]))
        self._mock_block_publisher\
            .initialize_block.assert_called_with("a block")

    def test_summarize_block(self):
        self._mock_block_publisher.summarize_block.return_value =\
            b"summary"

        summary = self._proxy.summarize_block()
        self._mock_block_publisher.summarize_block.assert_called_with()
        self.assertEqual(summary, b"summary")

    def test_finalize_block(self):
        self._mock_block_publisher.finalize_block.return_value =\
            FinalizeBlockResult(
                block=None,
                remaining_batches=None,
                last_batch=None,
                injected_batches=None)
        self._mock_block_publisher.publish_block.return_value = "00"

        data = bytes([0x56])
        self._proxy.finalize_block(data)
        self._mock_block_publisher.finalize_block.assert_called_with(
            consensus=data)
        self._mock_block_publisher.publish_block.assert_called_with(None, None)

    def test_cancel_block(self):
        self._proxy.cancel_block()
        self._mock_block_publisher.cancel_block.assert_called_with()

    # Using chain controller
    def test_check_blocks(self):
        block_ids = [bytes([0x56]), bytes([0x78])]
        self._mock_block_cache["56"] = "block0"
        self._mock_block_cache["78"] = "block1"
        self._proxy.check_blocks(block_ids)
        self._mock_chain_controller\
            .submit_blocks_for_verification\
            .assert_called_with(["block0", "block1"])

    def test_commit_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.commit_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .commit_block\
            .assert_called_with("a block")

    def test_ignore_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.ignore_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .ignore_block\
            .assert_called_with("a block")

    def test_fail_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.fail_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .fail_block\
            .assert_called_with("a block")

    # Using blockstore and state database
    def test_blocks_get(self):
        block_1 = Mock(
            identifier=b'id-1',
            previous_block_id=b'prev-1',
            header_signature=b'sign-1',
            block_num=1,
            consensus=b'consensus')

        self._mock_block_cache[b'block1'.hex()] = block_1

        block_2 = Mock(
            identifier=b'id-2',
            previous_block_id=b'prev-2',
            header_signature=b'sign-2',
            block_num=2,
            consensus=b'consensus')

        self._mock_block_cache[b'block2'.hex()] = block_2

        proxy_block_1, proxy_block_2 = self._proxy.blocks_get([
            b'block1',
            b'block2'])

        self.assertEqual(
            block_1,
            proxy_block_1)

        self.assertEqual(
            block_2,
            proxy_block_2)

    def test_chain_head_get(self):
        chain_head = Mock(
            identifier=b'id-2',
            previous_block_id=b'prev-2',
            header_signature=b'sign-2',
            block_num=2,
            consensus=b'consensus')

        self._mock_chain_controller.chain_head = chain_head

        self.assertEqual(
            self._proxy.chain_head_get(),
            chain_head)

    def test_settings_get(self):
        self._mock_block_cache[b'block'.hex()] = MockBlock()

        self.assertEqual(
            self._proxy.settings_get(b'block', ['key1', 'key2']),
            [
                ('key1', 'mock-key1'),
                ('key2', 'mock-key2'),
            ])

    def test_state_get(self):
        self._mock_block_cache[b'block'.hex()] = MockBlock()

        self.assertEqual(
            self._proxy.state_get(b'block', ['address-1', 'address-2']),
            [
                ('address-1', b'mock-address-1'),
                ('address-2', b'mock-address-2'),
            ])
Exemplo n.º 4
0
class TestProxy(unittest.TestCase):
    def setUp(self):
        self._mock_block_cache = {}
        self._mock_block_publisher = Mock()
        self._mock_chain_controller = Mock()
        self._mock_gossip = Mock()
        self._mock_identity_signer = Mock()
        self._mock_settings_view_factory = Mock()
        self._mock_state_view_factory = Mock()
        self._proxy = ConsensusProxy(
            block_cache=self._mock_block_cache,
            chain_controller=self._mock_chain_controller,
            block_publisher=self._mock_block_publisher,
            gossip=self._mock_gossip,
            identity_signer=self._mock_identity_signer,
            settings_view_factory=self._mock_settings_view_factory,
            state_view_factory=self._mock_state_view_factory)

    def test_send_to(self):
        self._proxy.send_to(peer_id=b'peer_id', message=b'message')

    def test_broadcast(self):
        self._proxy.broadcast(message=b'message')

    # Using block publisher
    def test_initialize_block(self):
        self._proxy.initialize_block(None)
        self._mock_block_publisher.initialize_block.assert_called_with(
            self._mock_chain_controller.chain_head)

        self._mock_block_cache["34"] = "a block"
        self._proxy.initialize_block(previous_id=bytes([0x34]))
        self._mock_block_publisher\
            .initialize_block.assert_called_with("a block")

    def test_summarize_block(self):
        self._mock_block_publisher.summarize_block.return_value =\
            b"summary"

        summary = self._proxy.summarize_block()
        self._mock_block_publisher.summarize_block.assert_called_with()
        self.assertEqual(summary, b"summary")

    def test_finalize_block(self):
        self._mock_block_publisher.finalize_block.return_value =\
            FinalizeBlockResult(
                block=None,
                remaining_batches=None,
                last_batch=None,
                injected_batches=None)
        self._mock_block_publisher.publish_block.return_value = "00"

        data = bytes([0x56])
        self._proxy.finalize_block(data)
        self._mock_block_publisher.finalize_block.assert_called_with(
            consensus=data)
        self._mock_block_publisher.publish_block.assert_called_with(None, None)

    def test_cancel_block(self):
        self._proxy.cancel_block()
        self._mock_block_publisher.cancel_block.assert_called_with()

    # Using chain controller
    def test_check_blocks(self):
        block_ids = [bytes([0x56]), bytes([0x78])]
        self._mock_block_cache["56"] = "block0"
        self._mock_block_cache["78"] = "block1"
        self._proxy.check_blocks(block_ids)
        self._mock_chain_controller\
            .submit_blocks_for_verification\
            .assert_called_with(["block0", "block1"])

    def test_commit_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.commit_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .commit_block\
            .assert_called_with("a block")

    def test_ignore_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.ignore_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .ignore_block\
            .assert_called_with("a block")

    def test_fail_block(self):
        self._mock_block_cache["34"] = "a block"
        self._proxy.fail_block(block_id=bytes([0x34]))
        self._mock_chain_controller\
            .fail_block\
            .assert_called_with("a block")

    # Using blockstore and state database
    def test_blocks_get(self):
        block_1 = Mock(identifier=b'id-1',
                       previous_block_id=b'prev-1',
                       header_signature=b'sign-1',
                       block_num=1,
                       consensus=b'consensus')

        self._mock_block_cache[b'block1'.hex()] = block_1

        block_2 = Mock(identifier=b'id-2',
                       previous_block_id=b'prev-2',
                       header_signature=b'sign-2',
                       block_num=2,
                       consensus=b'consensus')

        self._mock_block_cache[b'block2'.hex()] = block_2

        proxy_block_1, proxy_block_2 = self._proxy.blocks_get(
            [b'block1', b'block2'])

        self.assertEqual(block_1, proxy_block_1)

        self.assertEqual(block_2, proxy_block_2)

    def test_chain_head_get(self):
        chain_head = Mock(identifier=b'id-2',
                          previous_block_id=b'prev-2',
                          header_signature=b'sign-2',
                          block_num=2,
                          consensus=b'consensus')

        self._mock_chain_controller.chain_head = chain_head

        self.assertEqual(self._proxy.chain_head_get(), chain_head)

    def test_settings_get(self):
        self._mock_block_cache[b'block'.hex()] = MockBlock()

        self.assertEqual(self._proxy.settings_get(b'block', ['key1', 'key2']),
                         [
                             ('key1', 'mock-key1'),
                             ('key2', 'mock-key2'),
                         ])

    def test_state_get(self):
        self._mock_block_cache[b'block'.hex()] = MockBlock()

        self.assertEqual(
            self._proxy.state_get(b'block', ['address-1', 'address-2']), [
                ('address-1', b'mock-address-1'),
                ('address-2', b'mock-address-2'),
            ])