コード例 #1
0
    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
コード例 #2
0
    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
コード例 #3
0
    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)
コード例 #4
0
    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
コード例 #5
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
コード例 #6
0
    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
コード例 #7
0
# 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)

# Input a start and end block for multi-block processing
start_block = int(input('Enter start block number: '))
end_block = int(input('Enter end block number: '))
#end_block = None
# Input a start and end block for multi-block processing
#d.process_blocks(start_block)
d.process_blocks(start_block, end_block, include_effects=True)  # only effects
#d.process_blocks(start_block, end_block) # only updates
#print('action_dict=', demux.action_dict)
コード例 #8
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])
     ])