Exemplo n.º 1
0
 async def asyncSetUp(self):
     self.index_config = OperationIndexConfig(
         kind='operation',
         datasource='tzkt',
         contracts=[
             ContractConfig(address='KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9')
         ],
         handlers=[
             OperationHandlerConfig(
                 callback='',
                 pattern=[
                     OperationHandlerTransactionPatternConfig(
                         type='transaction',
                         destination=ContractConfig(
                             address='KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9'
                         ),
                         entrypoint='collect',
                     )
                 ],
             )
         ],
     )
     self.index_config.state = State(index_name='test',
                                     index_type=IndexType.operation,
                                     hash='')
     self.index_config.handlers[0].pattern[
         0].parameter_type_cls = CollectParameter
     self.dipdup_mock = MagicMock(spec=DipDup)
     self.datasource = TzktDatasource('tzkt.test', self.dipdup_mock)
     await self.datasource.add_index('test', self.index_config)
Exemplo n.º 2
0
    async def test_convert_operation(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)

        for operation_json in operations_message['data']:
            operation = TzktDatasource.convert_operation(operation_json)
            self.assertIsInstance(operation, OperationData)
Exemplo n.º 3
0
    async def test_on_operation_match(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)
        operations = [
            TzktDatasource.convert_operation(op)
            for op in operations_message['data']
        ]
        matched_operation = operations[0]

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            callback_mock = AsyncMock()
            storage_type_mock = MagicMock()
            storage_type_mock.__fields__ = MagicMock()

            self.index_config.handlers[0].callback_fn = callback_mock
            self.index_config.handlers[0].pattern[
                0].storage_type_cls = storage_type_mock

            await self.datasource.on_operation_match(
                self.index_config, self.index_config.handlers[0],
                [matched_operation], operations)

            self.assertIsInstance(callback_mock.await_args[0][0],
                                  HandlerContext)
            self.assertIsInstance(callback_mock.await_args[0][1], Transaction)
            self.assertIsInstance(callback_mock.await_args[0][1].parameter,
                                  CollectParameter)
            self.assertIsInstance(callback_mock.await_args[0][1].data,
                                  OperationData)
Exemplo n.º 4
0
    async def test_process(self):
        callback_mock = self.dipdup_mock.spawn_operation_handler_callback

        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)

        operations = [
            TzktDatasource.convert_operation(operation_json)
            for operation_json in operations_message['data']
        ]
        for operation in operations:
            await self.matcher.add(operation)

        await self.matcher.process()

        self.assertIsInstance(callback_mock.await_args[0][0],
                              OperationIndexConfig)
        self.assertEqual(
            callback_mock.await_args[0][1],
            OperationHandlerConfig(
                callback='',
                pattern=[
                    OperationHandlerTransactionPatternConfig(
                        type='transaction',
                        destination=ContractConfig(
                            address='KT1AFA2mwNUMNd4SsujE1YYp29vd8BZejyKW'),
                        entrypoint='hDAO_batch',
                    )
                ],
            ),
        )
        self.assertIsInstance(callback_mock.await_args[0][2], list)
        self.assertIsInstance(callback_mock.await_args[0][2][0], OperationData)
        self.assertIsInstance(callback_mock.await_args[0][3], list)
        self.assertIsInstance(callback_mock.await_args[0][3][0], OperationData)
Exemplo n.º 5
0
    async def _create_datasources(self) -> None:
        datasource: DatasourceT
        for name, datasource_config in self._config.datasources.items():
            if name in self._datasources:
                continue

            if isinstance(datasource_config, TzktDatasourceConfig):
                datasource = TzktDatasource(
                    url=datasource_config.url,
                    cache=self._config.cache_enabled,
                )
            elif isinstance(datasource_config, BcdDatasourceConfig):
                datasource = BcdDatasource(
                    url=datasource_config.url,
                    network=datasource_config.network,
                    cache=self._config.cache_enabled,
                )
            elif isinstance(datasource_config, CoinbaseDatasourceConfig):
                datasource = CoinbaseDatasource(
                    cache=self._config.cache_enabled, )
            else:
                raise NotImplementedError

            self._datasources[name] = datasource
            self._datasources_by_config[datasource_config] = datasource
Exemplo n.º 6
0
    async def test_add(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)

        operations = [
            TzktDatasource.convert_operation(operation_json)
            for operation_json in operations_message['data']
        ]
        await self.matcher.add(operations[0])
        await self.matcher.add(operations[1])

        expected_key = OperationGroup(
            hash='opGZHyGpDt6c8x2mKexrhc8btiMkuyF1EHeL3hQvaNtTxsyzUGu',
            counter=7057537)

        self.assertEqual([expected_key], list(self.matcher._operations.keys()))
        self.assertEqual(2, len(self.matcher._operations[expected_key]))
Exemplo n.º 7
0
    async def test_on_operation_message_data(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)
        operation = TzktDatasource.convert_operation(
            operations_message['data'][-2])

        on_operation_match_mock = AsyncMock()
        self.datasource.on_operation_match = on_operation_match_mock

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            await self.datasource.on_operation_message([operations_message],
                                                       sync=True)

            on_operation_match_mock.assert_awaited_with(
                self.index_config,
                self.index_config.handlers[0],
                [operation],
                ANY,
            )
Exemplo n.º 8
0
    async def test_on_operation_match_with_storage(self):
        with open(join(dirname(__file__), 'operations-storage.json')) as file:
            operations_message = json.load(file)
        self.index_config.handlers[0].pattern[
            0].parameter_type_cls = ProposeParameter

        for op in operations_message['data']:
            op['type'] = 'transaction'
        operations = [
            TzktDatasource.convert_operation(op)
            for op in operations_message['data']
        ]
        matched_operation = operations[0]

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            callback_mock = AsyncMock()

            self.index_config.handlers[0].callback_fn = callback_mock
            self.index_config.handlers[0].pattern[
                0].storage_type_cls = RegistryStorage

            await self.datasource.on_operation_match(
                self.index_config, self.index_config.handlers[0],
                [matched_operation], operations)

            self.assertIsInstance(callback_mock.await_args[0][1].storage,
                                  RegistryStorage)
            self.assertIsInstance(
                callback_mock.await_args[0][1].storage.ledger, list)
            self.assertIsInstance(
                callback_mock.await_args[0][1].storage.proposals[
                    'e710c1a066bbbf73692168e783607996785260cec4d60930579827298493b8b9'],
                Proposals,
            )
Exemplo n.º 9
0
class TzktDatasourceTest(IsolatedAsyncioTestCase):
    async def asyncSetUp(self):
        self.index_config = OperationIndexConfig(
            kind='operation',
            datasource='tzkt',
            contracts=[
                ContractConfig(address='KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9')
            ],
            handlers=[
                OperationHandlerConfig(
                    callback='',
                    pattern=[
                        OperationHandlerTransactionPatternConfig(
                            type='transaction',
                            destination=ContractConfig(
                                address='KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9'
                            ),
                            entrypoint='collect',
                        )
                    ],
                )
            ],
        )
        self.index_config.state = State(index_name='test',
                                        index_type=IndexType.operation,
                                        hash='')
        self.index_config.handlers[0].pattern[
            0].parameter_type_cls = CollectParameter
        self.dipdup_mock = MagicMock(spec=DipDup)
        self.datasource = TzktDatasource('tzkt.test', self.dipdup_mock)
        await self.datasource.add_index('test', self.index_config)

    async def test_convert_operation(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)

        for operation_json in operations_message['data']:
            operation = TzktDatasource.convert_operation(operation_json)
            self.assertIsInstance(operation, OperationData)

    async def test_get_client(self):
        client = self.datasource._get_client()
        self.assertIsInstance(client, BaseHubConnection)
        self.assertEqual(self.datasource.on_connect, client.transport._on_open)

    async def test_start(self):
        client = self.datasource._get_client()
        client.start = AsyncMock()

        fetch_operations_mock = AsyncMock()
        self.datasource.fetch_operations = fetch_operations_mock

        get_mock = MagicMock()
        get_mock.return_value.__aenter__.return_value.json.return_value = {
            'level': 1337
        }

        with patch('aiohttp.ClientSession.get', get_mock):
            await self.datasource.start()

        fetch_operations_mock.assert_awaited_with(self.index_config, 1337)
        self.assertEqual(
            {
                self.index_config.contracts[0].address:
                [OperationType.transaction]
            }, self.datasource._transaction_subscriptions)
        client.start.assert_awaited()

    async def test_on_connect_subscribe_to_operations(self):
        send_mock = AsyncMock()
        client = self.datasource._get_client()
        client.send = send_mock
        client.transport.state = ConnectionState.connected
        self.datasource._transaction_subscriptions = {
            self.index_config.contracts[0].address:
            [OperationType.transaction],
        }

        await self.datasource.on_connect()

        send_mock.assert_has_awaits([
            call('SubscribeToOperations',
                 [{
                     'address': self.index_config.contracts[0].address,
                     'types': 'transaction'
                 }]),
        ])
        self.assertEqual(2, len(client.handlers))

    async def test_on_fetch_operations(self):
        self.datasource._transaction_subscriptions = {
            self.index_config.contracts[0].address:
            [OperationType.transaction]
        }
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)
            del operations_message['state']

        stripped_operations_message = operations_message['data']

        on_operation_message_mock = AsyncMock()
        get_mock = MagicMock()
        get_mock.return_value.__aenter__.return_value.json.return_value = stripped_operations_message

        self.datasource.on_operation_message = on_operation_message_mock

        with patch('aiohttp.ClientSession.get', get_mock):
            await self.datasource.fetch_operations(self.index_config, 9999999)

        on_operation_message_mock.assert_awaited_with(
            message=[operations_message],
            sync=True,
        )

    async def test_on_operation_message_state(self):
        fetch_operations_mock = AsyncMock()
        self.datasource.fetch_operations = fetch_operations_mock

        await self.datasource.on_operation_message([{
            'type': 0,
            'state': 123
        }], self.index_config)
        fetch_operations_mock.assert_awaited_with(self.index_config, 123)

    async def test_on_operation_message_data(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)
        operation = TzktDatasource.convert_operation(
            operations_message['data'][-2])

        on_operation_match_mock = AsyncMock()
        self.datasource.on_operation_match = on_operation_match_mock

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            await self.datasource.on_operation_message([operations_message],
                                                       sync=True)

            on_operation_match_mock.assert_awaited_with(
                self.index_config,
                self.index_config.handlers[0],
                [operation],
                ANY,
            )

    async def test_on_operation_match(self):
        with open(join(dirname(__file__), 'operations.json')) as file:
            operations_message = json.load(file)
        operations = [
            TzktDatasource.convert_operation(op)
            for op in operations_message['data']
        ]
        matched_operation = operations[0]

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            callback_mock = AsyncMock()
            storage_type_mock = MagicMock()
            storage_type_mock.__fields__ = MagicMock()

            self.index_config.handlers[0].callback_fn = callback_mock
            self.index_config.handlers[0].pattern[
                0].storage_type_cls = storage_type_mock

            await self.datasource.on_operation_match(
                self.index_config, self.index_config.handlers[0],
                [matched_operation], operations)

            self.assertIsInstance(callback_mock.await_args[0][0],
                                  HandlerContext)
            self.assertIsInstance(callback_mock.await_args[0][1], Transaction)
            self.assertIsInstance(callback_mock.await_args[0][1].parameter,
                                  CollectParameter)
            self.assertIsInstance(callback_mock.await_args[0][1].data,
                                  OperationData)

    async def test_on_operation_match_with_storage(self):
        with open(join(dirname(__file__), 'operations-storage.json')) as file:
            operations_message = json.load(file)
        self.index_config.handlers[0].pattern[
            0].parameter_type_cls = ProposeParameter

        for op in operations_message['data']:
            op['type'] = 'transaction'
        operations = [
            TzktDatasource.convert_operation(op)
            for op in operations_message['data']
        ]
        matched_operation = operations[0]

        async with tortoise_wrapper('sqlite://:memory:'):
            await Tortoise.generate_schemas()

            callback_mock = AsyncMock()

            self.index_config.handlers[0].callback_fn = callback_mock
            self.index_config.handlers[0].pattern[
                0].storage_type_cls = RegistryStorage

            await self.datasource.on_operation_match(
                self.index_config, self.index_config.handlers[0],
                [matched_operation], operations)

            self.assertIsInstance(callback_mock.await_args[0][1].storage,
                                  RegistryStorage)
            self.assertIsInstance(
                callback_mock.await_args[0][1].storage.ledger, list)
            self.assertIsInstance(
                callback_mock.await_args[0][1].storage.proposals[
                    'e710c1a066bbbf73692168e783607996785260cec4d60930579827298493b8b9'],
                Proposals,
            )