Exemplo n.º 1
0
    async def test_session_bloat_from_socket_timeout(self):
        await self.account.ensure_address_gap()

        address1, address2 = await self.account.receiving.get_addresses(
            limit=2, only_usable=True)
        sendtxid1 = await self.blockchain.send_to_address(address1, 5)
        sendtxid2 = await self.blockchain.send_to_address(address2, 5)

        await self.blockchain.generate(1)
        await asyncio.wait([
            self.on_transaction_id(sendtxid1),
            self.on_transaction_id(sendtxid2)
        ])

        self.assertEqual(d2l(await self.account.get_balance()), '10.0')

        channel = Claim()
        channel_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), '@bar', channel,
            self.account.ledger.address_to_hash160(address1))
        channel_txo.generate_channel_private_key()
        channel_txo.script.generate()
        channel_tx = await Transaction.create([], [channel_txo],
                                              [self.account], self.account)

        stream = Claim()
        stream.stream.description = "0" * 8000
        stream_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), 'foo', stream,
            self.account.ledger.address_to_hash160(address1))
        stream_tx = await Transaction.create([], [stream_txo], [self.account],
                                             self.account)
        stream_txo.sign(channel_txo)
        await stream_tx.sign([self.account])
        self.paused_session.clear()
        self.resumed_session.clear()

        await self.broadcast(channel_tx)
        await self.broadcast(stream_tx)
        await asyncio.wait_for(self.paused_session.wait(), 2)
        self.assertEqual(1, len(self.session_manager.sessions))

        real_sock = self.client_session.transport._extra.pop('socket')
        mock_sock = Mock(spec=socket.socket)

        for attr in dir(real_sock):
            if not attr.startswith('__'):
                setattr(mock_sock, attr, getattr(real_sock, attr))

        def recv(*a, **kw):
            raise TimeoutError("[Errno 110] Connection timed out")

        mock_sock.recv = recv
        self.client_session.transport._sock = mock_sock
        self.client_session.transport._extra['socket'] = mock_sock
        self.assertFalse(self.resumed_session.is_set())
        self.assertFalse(self.session_manager.session_event.is_set())
        await self.session_manager.session_event.wait()
        self.assertEqual(0, len(self.session_manager.sessions))
Exemplo n.º 2
0
 def get_channel(self, title, amount, name='@foo', key=b'a'):
     claim = Claim()
     claim.channel.title = title
     channel = Output.pay_claim_name_pubkey_hash(amount, name, claim,
                                                 b'abc')
     self._set_channel_key(channel, key)
     return self._make_tx(channel)
Exemplo n.º 3
0
 def get_stream(self, title, amount, name='foo', channel=None):
     claim = Claim()
     claim.stream.title = title
     result = self._make_tx(Output.pay_claim_name_pubkey_hash(amount, name, claim, b'abc'))
     if channel:
         result[0].outputs[0].sign(channel)
         result[0]._reset()
     return result
Exemplo n.º 4
0
 def get_repost(self, claim_id, amount, channel):
     claim = Claim()
     claim.repost.reference.claim_id = claim_id
     result = self._make_tx(
         Output.pay_claim_name_pubkey_hash(amount, 'repost', claim, b'abc'))
     result[0].outputs[0].sign(channel)
     result[0]._reset()
     return result
Exemplo n.º 5
0
    async def test_balance(self):
        address = await self.account.receiving.get_or_create_usable_address()
        hash160 = self.ledger.address_to_hash160(address)

        tx = Transaction(is_verified=True)\
            .add_outputs([Output.pay_pubkey_hash(100, hash160)])
        await self.ledger.db.insert_transaction(tx)
        await self.ledger.db.save_transaction_io(tx, address, hash160,
                                                 '{}:{}:'.format(tx.id, 1))
        self.assertEqual(await self.account.get_balance(), 100)

        tx = Transaction(is_verified=True)\
            .add_outputs([Output.pay_claim_name_pubkey_hash(100, 'foo', b'', hash160)])
        await self.ledger.db.insert_transaction(tx)
        await self.ledger.db.save_transaction_io(tx, address, hash160,
                                                 '{}:{}:'.format(tx.id, 1))
        self.assertEqual(await self.account.get_balance(),
                         100)  # claim names don't count towards balance
        self.assertEqual(await self.account.get_balance(include_claims=True),
                         200)
Exemplo n.º 6
0
def get_claim_transaction(claim_name, claim=b''):
    return get_transaction(
        Output.pay_claim_name_pubkey_hash(CENT, claim_name, claim, NULL_HASH32)
    )
Exemplo n.º 7
0
    async def test_creating_updating_and_abandoning_claim_with_channel(self):

        await self.account.ensure_address_gap()

        address1, address2 = await self.account.receiving.get_addresses(
            limit=2, only_usable=True)
        sendtxid1 = await self.blockchain.send_to_address(address1, 5)
        sendtxid2 = await self.blockchain.send_to_address(address2, 5)
        await self.blockchain.generate(1)
        await asyncio.wait([
            self.on_transaction_id(sendtxid1),
            self.on_transaction_id(sendtxid2)
        ])

        self.assertEqual(d2l(await self.account.get_balance()), '10.0')

        channel = Claim()
        channel_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), '@bar', channel,
            self.account.ledger.address_to_hash160(address1))
        channel_txo.generate_channel_private_key()
        channel_txo.script.generate()
        channel_tx = await Transaction.create([], [channel_txo],
                                              [self.account], self.account)

        stream = Claim()
        stream.stream.source.media_type = "video/mp4"
        stream_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), 'foo', stream,
            self.account.ledger.address_to_hash160(address1))
        stream_tx = await Transaction.create([], [stream_txo], [self.account],
                                             self.account)
        stream_txo.sign(channel_txo)
        await stream_tx.sign([self.account])

        await self.broadcast(channel_tx)
        await self.broadcast(stream_tx)
        await asyncio.wait([  # mempool
            self.ledger.wait(channel_tx),
            self.ledger.wait(stream_tx)
        ])
        await self.blockchain.generate(1)
        await asyncio.wait([  # confirmed
            self.ledger.wait(channel_tx),
            self.ledger.wait(stream_tx)
        ])

        self.assertEqual(d2l(await self.account.get_balance()), '7.985786')
        self.assertEqual(
            d2l(await self.account.get_balance(include_claims=True)),
            '9.985786')

        response = await self.ledger.resolve([], ['lbry://@bar/foo'])
        self.assertEqual(response['lbry://@bar/foo'].claim.claim_type,
                         'stream')

        abandon_tx = await Transaction.create(
            [Input.spend(stream_tx.outputs[0])], [], [self.account],
            self.account)
        await self.broadcast(abandon_tx)
        await self.ledger.wait(abandon_tx)
        await self.blockchain.generate(1)
        await self.ledger.wait(abandon_tx)

        response = await self.ledger.resolve([], ['lbry://@bar/foo'])
        self.assertIn('error', response['lbry://@bar/foo'])

        # checks for expected format in inexistent URIs
        response = await self.ledger.resolve([], ['lbry://404', 'lbry://@404'])
        self.assertEqual('lbry://404 did not resolve to a claim',
                         response['lbry://404']['error'])
        self.assertEqual('lbry://@404 did not resolve to a claim',
                         response['lbry://@404']['error'])
Exemplo n.º 8
0
def get_stream(claim_name='foo'):
    stream_txo = Output.pay_claim_name_pubkey_hash(CENT, claim_name, Claim(), b'abc')
    get_tx().add_outputs([stream_txo])
    return stream_txo
Exemplo n.º 9
0
def get_channel(claim_name='@foo'):
    channel_txo = Output.pay_claim_name_pubkey_hash(CENT, claim_name, Claim(), b'abc')
    channel_txo.generate_channel_private_key()
    get_tx().add_outputs([channel_txo])
    return channel_txo
Exemplo n.º 10
0
    async def test_creating_updating_and_abandoning_claim_with_channel(self):

        await self.account.ensure_address_gap()

        address1, address2 = await self.account.receiving.get_addresses(limit=2, only_usable=True)
        notifications = asyncio.create_task(asyncio.wait(
            [asyncio.ensure_future(self.on_address_update(address1)),
             asyncio.ensure_future(self.on_address_update(address2))]
        ))
        await self.send_to_address_and_wait(address1, 5)
        await self.send_to_address_and_wait(address2, 5, 1)
        await notifications

        self.assertEqual(d2l(await self.account.get_balance()), '10.0')

        channel = Claim()
        channel_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), '@bar', channel, self.account.ledger.address_to_hash160(address1)
        )
        channel_txo.set_channel_private_key(
            await self.account.generate_channel_private_key()
        )
        channel_txo.script.generate()
        channel_tx = await Transaction.create([], [channel_txo], [self.account], self.account)

        stream = Claim()
        stream.stream.source.media_type = "video/mp4"
        stream_txo = Output.pay_claim_name_pubkey_hash(
            l2d('1.0'), 'foo', stream, self.account.ledger.address_to_hash160(address1)
        )
        stream_tx = await Transaction.create([], [stream_txo], [self.account], self.account)
        stream_txo.sign(channel_txo)
        await stream_tx.sign([self.account])

        notifications = asyncio.create_task(asyncio.wait(
            [asyncio.ensure_future(self.ledger.wait(channel_tx)), asyncio.ensure_future(self.ledger.wait(stream_tx))]
        ))

        await self.broadcast(channel_tx)
        await self.broadcast(stream_tx)
        await notifications
        notifications = asyncio.create_task(asyncio.wait(
            [asyncio.ensure_future(self.ledger.wait(channel_tx)), asyncio.ensure_future(self.ledger.wait(stream_tx))]
        ))
        await self.generate(1)
        await notifications
        self.assertEqual(d2l(await self.account.get_balance()), '7.985786')
        self.assertEqual(d2l(await self.account.get_balance(include_claims=True)), '9.985786')

        response = await self.ledger.resolve([], ['lbry://@bar/foo'])
        self.assertEqual(response['lbry://@bar/foo'].claim.claim_type, 'stream')

        abandon_tx = await Transaction.create([Input.spend(stream_tx.outputs[0])], [], [self.account], self.account)
        notify = asyncio.create_task(self.ledger.wait(abandon_tx))
        await self.broadcast(abandon_tx)
        await notify
        notify = asyncio.create_task(self.ledger.wait(abandon_tx))
        await self.generate(1)
        await notify

        response = await self.ledger.resolve([], ['lbry://@bar/foo'])
        self.assertIn('error', response['lbry://@bar/foo'])

        # checks for expected format in inexistent URIs
        response = await self.ledger.resolve([], ['lbry://404', 'lbry://@404', 'lbry://@404/404'])
        self.assertEqual('Could not find claim at "lbry://404".', response['lbry://404']['error']['text'])
        self.assertEqual('Could not find channel in "lbry://@404".', response['lbry://@404']['error']['text'])
        self.assertEqual('Could not find channel in "lbry://@404/404".', response['lbry://@404/404']['error']['text'])