示例#1
0
    async def create_notification(self, tx_output, account):
        if tx_output.confirmations is not None:
            if tx_output.confirmations >= self.app.config.confirmations_required:
                notify_code = Notification.NOTIFICATION_TYPES['TX_CONFIRMED']
            else:
                notify_code = Notification.NOTIFICATION_TYPES[
                    'TX_MINED'] + tx_output.confirmations
        else:
            notify_code = Notification.NOTIFICATION_TYPES['TX_IN_POOL']

        if await self.app.notifications_repo.find_one({
                'tx_id': tx_output.tx_id,
                'code': notify_code
        }) is None:
            notification = Notification.from_kwargs(tx_id=tx_output.tx_id,
                                                    url=account.notify_url,
                                                    code=notify_code,
                                                    txn_data={
                                                        'value':
                                                        tx_output.value,
                                                        'address':
                                                        account.address,
                                                        'block_hash':
                                                        tx_output.block_hash,
                                                        'confirmations':
                                                        tx_output.confirmations
                                                    })

            await self.app.notifications_repo.save(notification)
示例#2
0
async def test_already_processed_tx_output(app, list_unspent_response,
                                           transaction_response):
    tx_output = await app.tx_outputs_repo.save(
        TxOutput({
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'blocktime': 1536681241,
            'confirmations': 6,
            'locktime': 1412987,
            'position': 1,
            'tx_hash':
            '5f7d66d9f36dacf150320bedd35d2f601754a31be78ccc7b6be795ed8a518b46',
            'tx_id':
            '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'value': 0.39654376
        }))
    notification = await app.notifications_repo.save(
        Notification({
            'code': 100,
            'created_at': datetime.utcnow(),
            'failed': False,
            'next_send': datetime.utcnow(),
            'attempts': 0,
            'sent': False,
            'tx_id':
            '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
            'txn_data': {
                'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
                'block_hash':
                '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
                'confirmations': 6,
                'value': 0.39654376
            },
            'url': 'localhost:8080/notify',
            'failures': []
        }))

    ElectrumxSessionStub.stub_server_version()
    ElectrumxSessionStub.stub_request(
        'blockchain.scripthash.listunspent',
        ['da22c9488b0d4828b4bc96ad6ff41c274bc9e1632482673cc4c07a0a1a8fcebc'],
        list_unspent_response)
    ElectrumxSessionStub.stub_request('blockchain.transaction.get', [
        '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
        True
    ], transaction_response)

    await SyncTxOutputs(app=app)()

    assert await app.tx_outputs_repo.count() == 1
    assert await app.tx_outputs_repo.find_one() == tx_output
    assert await app.notifications_repo.count() == 1
    assert await app.notifications_repo.find_one() == notification
示例#3
0
async def test_check_next_send(app, notifications_handler, notifications_url):
    notification = Notification({
        'code':
        100,
        'created_at':
        datetime.utcnow(),
        'failed':
        False,
        'next_send':
        datetime.utcnow() + timedelta(seconds=1),
        'attempts':
        0,
        'sent':
        False,
        'tx_id':
        '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
        'txn_data': {
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'confirmations': 6,
            'value': 0.39654376
        },
        'url':
        notifications_url,
        'failures': []
    })
    await app.notifications_repo.save(notification)

    notifications_handler.stub_post_notification(
        data={
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'tx_id':
            '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
            'code': '100',
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'confirmations': '6',
            'value': '0.39654376'
        },
        signature=
        '683024d5df432da32291bd5ded2e38cf9bc803e54566c6154269845ed960c564'
        '342e1b8b9c9131542143d497655d8d98c5b37c20b759907bec0841e1c1017e3e',
        response_status=200,
        response_body='OK')

    await SendNotifications(app=app)()

    assert notifications_handler.requests_received == 0

    assert await app.notifications_repo.count() == 1
    assert await app.notifications_repo.find_one() == notification
示例#4
0
async def notification(app, notifications_url):
    notification = Notification({
        'code': 100,
        'created_at': datetime.utcnow(),
        'failed': False,
        'next_send': datetime.utcnow(),
        'attempts': 0,
        'sent': False,
        'tx_id':
        '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
        'txn_data': {
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'confirmations': 6,
            'value': 0.39654376
        },
        'url': notifications_url,
        'failures': []
    })
    await app.notifications_repo.save(notification)

    return notification
示例#5
0
async def test_mark_failed(app, aiohttp_unused_port):
    port = aiohttp_unused_port()
    url = f'http://localhost:{port}'
    notification = Notification({
        'code':
        100,
        'created_at':
        datetime.utcnow(),
        'failed':
        False,
        'next_send':
        datetime.utcnow(),
        'attempts':
        10,
        'sent':
        False,
        'tx_id':
        '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
        'txn_data': {
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'confirmations': 6,
            'value': 0.39654376
        },
        'url':
        url,
        'failures': [{
            'created_at': datetime.utcnow(),
            'failure_type': 'request_timeout'
        }]
    })
    await app.notifications_repo.save(notification)

    await SendNotifications(app=app)()

    assert await app.notifications_repo.count() == 1
    notification = await app.notifications_repo.find_one()

    assert notification.serialize() == {
        '_id':
        notification.id,
        'code':
        100,
        'created_at':
        datetime.utcnow(),
        'failed':
        True,
        'next_send':
        datetime.utcnow(),
        'attempts':
        10,
        'sent':
        False,
        'tx_id':
        '25705000a564b7852947faa1fea2987143410bb3ada53458b6eead67e7469d60',
        'txn_data': {
            'address': 'mwHHRbLcC394T7vsLQZVh8FsB3QkDNRKK9',
            'block_hash':
            '000000004c3604d758cba41c30c1f2be2ffa3e730a3017ded08078dc422a614e',
            'confirmations': 6,
            'value': 0.39654376
        },
        'url':
        url,
        'failures': [{
            'created_at': datetime.utcnow(),
            'failure_type': 'request_timeout'
        }, {
            'created_at':
            datetime.utcnow(),
            'failure_type':
            'client_error',
            'data':
            f"Cannot connect to host localhost:{port} ssl:None [Connect call failed ('127.0.0.1', {port})]"
        }]
    }