Exemplo n.º 1
0
class TestJournal(unittest.TestCase):
    def setUp(self):
        self.gossip = MockNetwork()
        self.txn_executor = MockTransactionExecutor()
        self.block_sender = MockBlockSender()

    def test_publish_block(self):
        """
        Test that the Journal will produce blocks and consume those blocks
        to extend the chain.
        :return:
        """
        # construction and wire the journal to the
        # gossip layer.

        LOGGER.info("test_publish_block")
        block_store = {
            'chain_head_id': 'genesis',
            'genesis':  BlockState(
                block_wrapper=_generate_genesis_block(),
                weight=0,
                status=BlockStatus.Valid)
        }
        journal = None
        try:
            journal = Journal(
                consensus=test_mode_consensus,
                block_store=block_store,
                block_sender=self.block_sender,
                transaction_executor=self.txn_executor,
                squash_handler=None
            )

            self.gossip.on_batch_received = \
                journal.on_batch_received
            self.gossip.on_block_received = \
                journal.on_block_received
            self.gossip.on_block_request = \
                journal.on_block_request

            journal.start()

            # feed it a batch
            batch = Batch()
            journal.on_batch_received(batch)

            # wait for a block message to arrive should be soon
            to = TimeOut(2)
            while (self.block_sender.new_block is None):
                time.sleep(0.1)

            self.assertTrue(self.block_sender.new_block is not None)

            self.gossip.messages.append(self.block_sender.new_block)
            block = self.gossip.messages[0]
            # dispatch the message
            self.gossip.dispatch_messages()

            # wait for the chain_head to be updated.
            to = TimeOut(2)
            while block_store['chain_head_id'] != block.header_signature:
                time.sleep(0.1)

            self.assertTrue(block_store['chain_head_id'] ==
                            block.header_signature)

        finally:
            if journal is not None:
                journal.stop()
Exemplo n.º 2
0
class TestJournal(unittest.TestCase):
    def setUp(self):
        self.gossip = MockNetwork()
        self.txn_executor = MockTransactionExecutor()

    def test_publish_block(self):
        """
        Test that the Journal will produce blocks and consume those blocks
        to extend the chain.
        :return:
        """
        # construction and wire the journal to the
        # gossip layer.

        LOGGER.info("test_publish_block")
        block_store = {}
        journal = None
        try:
            journal = Journal(consensus=test_mode_consensus,
                              block_store=block_store,
                              send_message=self.gossip.send_message,
                              transaction_executor=self.txn_executor,
                              squash_handler=None,
                              first_state_root="000000")

            self.gossip.on_batch_received = \
                journal.on_batch_received
            self.gossip.on_block_received = \
                journal.on_block_received
            self.gossip.on_block_request = \
                journal.on_block_request

            journal.start()

            # feed it a batch
            batch = Batch()
            journal.on_batch_received(batch)

            # wait for a block message to arrive should be soon
            to = TimeOut(2)
            while len(self.gossip.messages) == 0:
                time.sleep(0.1)

            LOGGER.info("Batches: %s", self.gossip.messages)
            self.assertTrue(len(self.gossip.messages) != 0)

            block = self.gossip.messages[0]
            # dispatch the message
            self.gossip.dispatch_messages()

            # wait for the chain_head to be updated.
            to = TimeOut(2)
            while block_store['chain_head_id'] != block.header_signature:
                time.sleep(0.1)

            self.assertTrue(
                block_store['chain_head_id'] == block.header_signature)

        finally:
            if journal is not None:
                journal.stop()