示例#1
0
 def test_get_a_block_calls_requests(self, mock_requests_post):
     mock_requests_post.return_value = Mock(status_code=200)
     d = Demux()
     d._get_block(555)
     # assertions
     mock_requests_post.assert_called_with(
         'https://node2.eosphere.io/v1/chain/get_block',
         json={'block_num_or_id': 555})
示例#2
0
 def test_register_multiple_action_functions(self, mock_get_info_head_block,
                                             mock_get_block):
     """
     Tests that multiple action functions are registered to a specific account and name, including actions with no account or name specified
     """
     mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
     mock_get_block.return_value = fake_block2
     # mock callback functions
     mock_start_block = Mock()
     mock_action1 = Mock()
     mock_action2 = Mock()
     mock_action3 = Mock()
     mock_commit_block = Mock()
     # parent mock for checking call order
     m = Mock()
     m.mock_action1, m.mock_action2, m.mock_action3 = mock_action1, mock_action2, mock_action3
     m.mock_calls
     # register the mock callback functions
     d = Demux(start_block_fn=mock_start_block,
               commit_block_fn=mock_commit_block)
     d.register_action(mock_action1, "account21", "name21")
     d.register_action(mock_action2, "account22", "name22")
     d.register_action(mock_action3)
     # process the mock fake block 2
     d.process_block(200)
     # assertions
     assert mock_get_block.call_count == 1
     assert mock_start_block.call_count == 1
     assert mock_action1.call_count == 1
     assert mock_action2.call_count == 1
     assert mock_action3.call_count == 2
     assert mock_commit_block.call_count == 1
     # action_dict assertions
     assert d._action_dict[('account21', 'name21',
                            'updates')] == [mock_action1]
     assert d._action_dict[('account22', 'name22',
                            'updates')] == [mock_action2]
     assert d._action_dict[(None, None, 'updates')] == [mock_action3]
     # action functions call order assertions
     m.assert_has_calls([
         call.mock_action3(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action1(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action3(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[1],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action2(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[1],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0])
     ])
    def test_single_mock_block_processing(self, mock_get_info_head_block,
                                          mock_get_block):  #put get_info first
        """
        Tests block processing on a mocked block
        """
        mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
        # get_block returns fake_block1
        mock_get_block.return_value = fake_block1
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        # register the mock callback functions
        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        d.register_action(mock_action)
        # process the mock block fake_block1
        d.process_block(100)

        # assertions
        mock_get_block.assert_called_once()
        mock_get_block.assert_called_with(100)
        mock_start_block.assert_called_once()
        assert mock_action.call_count == 1
        mock_commit_block.assert_called_once()
    def test_continuous_block_processing(self, mock_sleep,
                                         mock_get_info_head_block,
                                         mock_get_block):
        """
        Test that continuous polling the block chain for new blocks works correctly
        """
        # Internal implementation of get_info() which keeps head_block as var,
        mock_get_info_head_block.side_effect = [{
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            10000,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            10000,
            'last_irreversible_block_num':
            9900
        }]
        # get block iterates through blocks each time it is called
        mock_get_block.side_effect = [block_9999, block_10000]

        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        # register the mock callback functions
        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        d.register_action(mock_action)
        # process the mock blocks 9999
        with pytest.raises(StopIteration) as excinfo:
            d.process_blocks(9999)

        # assertions
        assert mock_get_block.call_count == 2
        assert mock_get_block.call_args_list == [call(9999), call(10000)]
        assert mock_start_block.call_count == 2
        assert mock_action.call_count == 28
        assert mock_commit_block.call_count == 2
        assert mock_sleep.call_count == 1
    def test_cannot_process_past_last_irreversible_block(
            self, mock_get_info_irr_block, mock_get_block):
        """
        Tests when the end block is more than one block greater than the last irreversible block, an assertion is raised and no block is processed
        """
        with pytest.raises(AssertionError) as excinfo:

            mock_get_info_irr_block.return_value = {
                'last_irreversible_block_num': 99
            }

            mock_start_block = Mock()
            mock_action = Mock()
            mock_commit_block = Mock()

            # register the mock callback functions
            d = Demux(start_block_fn=mock_start_block,
                      commit_block_fn=mock_commit_block)
            d.register_action(mock_action)
            # attempts to process the mock blocks 9999998 to 10000000
            d.process_blocks(100, 101, irreversible_only=True)

            mock_get_block.assert_not_called()
            mock_start_block.assert_not_called()
            mock_action.assert_not_called()
            mock_commit_block.assert_not_called()

        assert 'ERROR: End block is past last irreversible block.' in str(
            excinfo.value)
    def test_multiple_mock_block_processing(self, mock_get_info_head_block,
                                            mock_get_block):
        """
        Ensures multiple block are processed given a start and end block
        """
        mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
        # get block iterates through blocks each time it is called
        mock_get_block.side_effect = [fake_block1, fake_block2]
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        # register the mock callback functions
        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        d.register_action(mock_action)
        # process the mock blocks 9999 to 10000
        d.process_blocks(100, 102)

        # assertions
        assert mock_get_block.call_count == 2
        assert mock_get_block.call_args_list == [call(100), call(101)]
        assert mock_start_block.call_count == 2
        assert mock_action.call_count == 3
        assert mock_commit_block.call_count == 2
    def test_irreversible_blocks_only(self, mock_sleep,
                                      mock_get_info_head_block,
                                      mock_get_block):
        """
        Test that rollbacks are dealt with correctly when continously polling the block chain
        """
        mock_get_info_head_block.side_effect = [{
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            9999,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            10000,
            'last_irreversible_block_num':
            9900
        }, {
            'head_block_num':
            10000,
            'last_irreversible_block_num':
            9900
        }]
        # get block iterates through blocks each time it is called
        mock_get_block.side_effect = [block_9999, block_10000]
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        # register the mock callback functions
        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        d.register_action(mock_action)
        # process the mock blocks 9999
        with pytest.raises(StopIteration) as excinfo:
            d.process_blocks(9999)

        # assertions
        assert mock_get_block.call_count == 2
        assert mock_get_block.call_args_list == [call(9999), call(10000)]
        assert mock_start_block.call_count == 2
        assert mock_action.call_count == 28
        assert mock_commit_block.call_count == 2
        assert mock_sleep.call_count == 1
    def test_register_all_callback_functions(self):
        """
        Ensure that all callback functions are registered by demux
        """
        def start():
            print('Block started.')

        def action():
            print('Block action=')

        def commit():
            print('Block committed.')

        d = Demux(start_block_fn=start, commit_block_fn=commit)

        assert d._start_block_fn == start
        #        assert action_fn == action
        assert d._commit_block_fn == commit
    def test_no_commit_block_function(self, mock_get_info_head_block,
                                      mock_get_block):
        """
        When users do not want to use the commit_block function
        """
        mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
        mock_get_block.return_value = fake_block1
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        d = Demux(start_block_fn=mock_start_block)
        d.register_action(mock_action)
        d.process_block(9999)

        mock_start_block.assert_called_once()
        mock_action.assert_called()
        mock_commit_block.assert_not_called()
    def test_start_and_commit_callback_functions_called(
            self, mock_get_info_head_block, mock_get_block):
        """
        Ensure the start_block and commit_block functions are called when processing a block
        """
        mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
        mock_get_block.return_value = fake_block1
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        d.register_action(mock_action)
        d.process_block(9999)
        mock_start_block.assert_called_once()
        mock_action.assert_called()
        mock_commit_block.assert_called_once()
    def test_multiple_block_processing_with_start_and_end_block(
            self, mock_get_info_head_block, mock_get_block):
        """
        Ensure we can process multiple blocks with a start and end block
        """
        mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
        mock_get_block.return_value = fake_block1
        # set up the action_dict
        #initialise_action_dict()
        # mock callback functions
        mock_start_block = Mock()
        mock_action = Mock()
        mock_commit_block = Mock()

        d = Demux(start_block_fn=mock_start_block,
                  commit_block_fn=mock_commit_block)
        #register_start_commit(mock_start_block, mock_commit_block)
        d.register_action(mock_action)
        # process multiple blocks (in this case there are 9)
        d.process_blocks(9990, 9999)

        # assert the callback functions were called for each block only
        assert mock_start_block.call_count == 9
        assert mock_commit_block.call_count == 9
 def test_block_with_no_transactions(self, mock_get_info_head_block,
                                     mock_get_block):
     """
     Ensure we can process a block with no transactions251
     """
     mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
     # get_block returns manual block_1
     mock_get_block.return_value = block_1
     # mock callback functions
     mock_start_block = Mock()
     mock_action = Mock()
     mock_commit_block = Mock()
     # register the mock callback functions
     d = Demux(start_block_fn=mock_start_block,
               commit_block_fn=mock_commit_block)
     d.register_action(mock_action)
     # process the mock block_1
     d.process_block(1)
     # assertions
     mock_get_block.assert_called_once()
     mock_get_block.assert_called_with(1)
     mock_start_block.assert_called_once()
     assert mock_action.call_count == 0
     mock_commit_block.assert_called_once()
示例#13
0
 def test_update_effect_action_functions_stored_and_called_correctly(
         self, mock_get_info_head_block, mock_get_block):
     """
     Test that update and effect action functions are stored and called correctly for multiple blocks
     """
     mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
     mock_get_block.side_effect = [fake_block1, fake_block2]
     # mock callback functions
     mock_start_block = Mock()
     mock_action1 = Mock()
     mock_action2 = Mock()
     mock_action3 = Mock()
     mock_action4 = Mock()
     mock_commit_block = Mock()
     # register the mock callback functions
     d = Demux(start_block_fn=mock_start_block,
               commit_block_fn=mock_commit_block)
     d.register_action(mock_action1, "account11", "name11", is_effect=True)
     d.register_action(mock_action2, "account21", "name21")
     d.register_action(mock_action3, is_effect=True)
     d.register_action(mock_action4, "account22", "name22", is_effect=True)
     # process the mock fake blocks 1 and 2
     # action_dict assertions that updates/effects stored correctly
     d.process_blocks(100, 102, include_effects=True)
     assert d._action_dict[('account11', 'name11',
                            'effects')] == [mock_action1]
     assert d._action_dict[('account21', 'name21',
                            'updates')] == [mock_action2]
     assert d._action_dict[(None, None, 'effects')] == [mock_action3]
     assert d._action_dict[('account22', 'name22',
                            'effects')] == [mock_action4]
     # assertions that function calls correct
     assert mock_get_block.call_count == 2
     assert mock_get_block.call_args_list == [call(100), call(101)]
     assert mock_start_block.call_count == 2
     assert mock_action1.call_count == 1
     assert mock_action2.call_count == 0
     assert mock_action3.call_count == 3
     assert mock_action4.call_count == 1
     assert mock_commit_block.call_count == 2
示例#14
0
 def test_invalid_post_raises_exception(self, mock_requests_post):
     mock_requests_post.return_value = Mock(status_code=500)
     d = Demux()
     with pytest.raises(UnknownBlockError):
         d._get_block(9999999999)
示例#15
0
 def test_multiblock_register_multiple_action_functions(
         self, mock_get_info_head_block, mock_get_block):
     """
     Tests that multiple action functions are registered and called correctly when processing multiple blocks
     """
     mock_get_info_head_block.return_value = {'head_block_num': 99999999999}
     mock_get_block.side_effect = [fake_block1, fake_block2]
     # mock callback functions
     mock_start_block = Mock()
     mock_action1 = Mock()
     mock_action2 = Mock()
     mock_action3 = Mock()
     mock_action4 = Mock()
     mock_commit_block = Mock()
     # parent mock for checking call order
     m = Mock()
     m.mock_action1, m.mock_action2, m.mock_action3, m.mock_action4 = mock_action1, mock_action2, mock_action3, mock_action4
     m.mock_calls
     # register the mock callback functions
     d = Demux(start_block_fn=mock_start_block,
               commit_block_fn=mock_commit_block)
     d.register_action(mock_action1, "account11", "name11")
     d.register_action(mock_action2, "account21", "name21")
     d.register_action(mock_action3)
     d.register_action(mock_action4, "account22", "name22")
     # process the mock fake blocks 1 and 2
     d.process_blocks(100, 102)
     # assertions
     assert mock_get_block.call_count == 2
     assert mock_get_block.call_args_list == [call(100), call(101)]
     assert mock_start_block.call_count == 2
     assert mock_action1.call_count == 1
     assert mock_action2.call_count == 1
     assert mock_action3.call_count == 3
     assert mock_action4.call_count == 1
     assert mock_commit_block.call_count == 2
     # action_dict assertions
     assert d._action_dict[('account11', 'name11',
                            'updates')] == [mock_action1]
     assert d._action_dict[('account21', 'name21',
                            'updates')] == [mock_action2]
     assert d._action_dict[(None, None, 'updates')] == [mock_action3]
     assert d._action_dict[('account22', 'name22',
                            'updates')] == [mock_action4]
     # action functions call order assertions (order is: action3-account11, action1-account11, action3-account21, action2-account21, action3-account22, action4-account22
     m.assert_has_calls([
         call.mock_action3(fake_block1.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block1,
                           transaction=fake_block1.get('transactions')[0]),
         call.mock_action1(fake_block1.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block1,
                           transaction=fake_block1.get('transactions')[0]),
         call.mock_action3(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action2(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[0],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action3(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[1],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0]),
         call.mock_action4(fake_block2.get('transactions')[0]['trx']
                           ['transaction'].get('actions')[1],
                           block=fake_block2,
                           transaction=fake_block2.get('transactions')[0])
     ])
示例#16
0
def action3(action, block, transaction):
    print("Action3 for random account and name")


def effect_action(action, **kwargs):
    print("Effect function")


# Callback commit block function (OPTIONAL)
# Commit block when entire process is
# Actual: Commit the FB transaction
def commit_block(block):
    print("Block " + str(block['block_num']) + " commited.")


d = Demux(start_block_fn=start_block, commit_block_fn=commit_block)
# Tells demux there are callback functions and registers them
d.register_action(action, "eosio.unregd", "add")
d.register_action(action2, "eosio.unregd", "add")
d.register_action(action1)
d.register_action(action3, "randomAccount", "randomName")
d.register_action(effect_action, is_effect=True)

# Input block number to be stored
#block_num = input('Enter a block number: ')
# Iterates through the transactions and actions of the block number
#d.process_block(block_num)
#d.process_block(block_num, include_effects=True)
#print("action_dict=", d.action_dict)
#print("head_block=", d._head_block)
示例#17
0
 def test_info_calls_requests(self, mock_requests_get):
     d = Demux()
     d._get_info()
     # assertions
     mock_requests_get.assert_called_with(
         'https://node2.eosphere.io/v1/chain/get_info')