/
test_file_commands.py
618 lines (540 loc) · 30.8 KB
/
test_file_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
from unittest import skipIf
import asyncio
import os
from binascii import hexlify
from lbry.schema import Claim
from lbry.testcase import CommandTestCase
from lbry.extras.daemon.components import TorrentSession, BACKGROUND_DOWNLOADER_COMPONENT
from lbry.wallet import Transaction
class FileCommands(CommandTestCase):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.skip_libtorrent = False
async def initialize_torrent(self, tx_to_update=None):
if not hasattr(self, 'seeder_session'):
self.seeder_session = TorrentSession(self.loop, None)
self.addCleanup(self.seeder_session.stop)
await self.seeder_session.bind(port=4040)
btih = await self.seeder_session.add_fake_torrent()
address = await self.account.receiving.get_or_create_usable_address()
if not tx_to_update:
claim = Claim()
claim.stream.update(bt_infohash=btih)
tx = await Transaction.claim_create(
'torrent', claim, 1, address, [self.account], self.account
)
else:
claim = tx_to_update.outputs[0].claim
claim.stream.update(bt_infohash=btih)
tx = await Transaction.claim_update(
tx_to_update.outputs[0], claim, 1, address, [self.account], self.account
)
await tx.sign([self.account])
await self.broadcast(tx)
await self.confirm_tx(tx.id)
self.client_session = self.daemon.file_manager.source_managers['torrent'].torrent_session
self.client_session._session.add_dht_node(('localhost', 4040))
self.client_session.wait_start = False # fixme: this is super slow on tests
return tx, btih
@skipIf(TorrentSession is None, "libtorrent not installed")
async def test_download_torrent(self):
tx, btih = await self.initialize_torrent()
self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
# second call, see its there and move on
self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, btih)
self.assertIn(btih, self.client_session._handles)
tx, new_btih = await self.initialize_torrent(tx)
self.assertNotEqual(btih, new_btih)
# claim now points to another torrent, update to it
self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, new_btih)
self.assertIn(new_btih, self.client_session._handles)
self.assertNotIn(btih, self.client_session._handles)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
self.assertNotIn(new_btih, self.client_session._handles)
async def create_streams_in_range(self, *args, **kwargs):
self.stream_claim_ids = []
for i in range(*args, **kwargs):
t = await self.stream_create(f'Stream_{i}', '0.00001')
self.stream_claim_ids.append(t['outputs'][0]['claim_id'])
async def test_file_reflect(self):
tx = await self.stream_create('mirror', '0.01')
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
self.assertEqual([], await self.daemon.jsonrpc_file_reflect(sd_hash=sd_hash))
all_except_sd = [
blob_hash for blob_hash in self.server.blob_manager.completed_blob_hashes if blob_hash != sd_hash
]
await self.reflector.blob_manager.delete_blobs(all_except_sd)
self.assertEqual(all_except_sd, await self.daemon.jsonrpc_file_reflect(sd_hash=sd_hash))
async def test_file_management(self):
await self.stream_create('foo', '0.01')
await self.stream_create('foo2', '0.01')
file1, file2 = await self.file_list('claim_name')
self.assertEqual(file1['claim_name'], 'foo')
self.assertEqual(file2['claim_name'], 'foo2')
self.assertItemCount(await self.daemon.jsonrpc_file_list(claim_id=[file1['claim_id'], file2['claim_id']]), 2)
self.assertItemCount(await self.daemon.jsonrpc_file_list(claim_id=file1['claim_id']), 1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(outpoint=[file1['outpoint'], file2['outpoint']]), 2)
self.assertItemCount(await self.daemon.jsonrpc_file_list(outpoint=file1['outpoint']), 1)
await self.daemon.jsonrpc_file_delete(claim_name='foo')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await self.daemon.jsonrpc_file_delete(claim_name='foo2')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
await self.daemon.jsonrpc_get('lbry://foo')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
async def test_announces(self):
# announces on publish
self.assertEqual(await self.daemon.storage.get_blobs_to_announce(), [])
await self.stream_create('foo', '0.01')
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
self.assertSetEqual(
set(await self.daemon.storage.get_blobs_to_announce()),
{stream.sd_hash, stream.descriptor.blobs[0].blob_hash}
)
self.assertTrue(await self.daemon.jsonrpc_file_delete(delete_all=True))
# announces on download
self.assertEqual(await self.daemon.storage.get_blobs_to_announce(), [])
stream = await self.daemon.jsonrpc_get('foo')
self.assertSetEqual(
set(await self.daemon.storage.get_blobs_to_announce()),
{stream.sd_hash, stream.descriptor.blobs[0].blob_hash}
)
async def _purge_file(self, claim_name, full_path):
self.assertTrue(
await self.daemon.jsonrpc_file_delete(claim_name=claim_name, delete_from_download_dir=True)
)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
self.assertFalse(os.path.isfile(full_path))
async def test_publish_with_illegal_chars(self):
def check_prefix_suffix(name, prefix, suffix):
self.assertTrue(name.startswith(prefix))
self.assertTrue(name.endswith(suffix))
# Stream a file with file name containing invalid chars
claim_name = 'lolwindows'
prefix, suffix = 'derp?', '.ext.'
san_prefix, san_suffix = 'derp', '.ext'
tx = await self.stream_create(claim_name, '0.01', prefix=prefix, suffix=suffix)
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
claim_id = self.get_claim_id(tx)
# Assert that file list and source contains the local unsanitized name, but suggested name is sanitized
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name)).full_path
stream_file_name = os.path.basename(full_path)
source_file_name = tx['outputs'][0]['value']['source']['name']
file_list_name = stream.file_name
suggested_file_name = stream.descriptor.suggested_file_name
self.assertTrue(os.path.isfile(full_path))
check_prefix_suffix(stream_file_name, prefix, suffix)
self.assertEqual(stream_file_name, source_file_name)
self.assertEqual(stream_file_name, file_list_name)
check_prefix_suffix(suggested_file_name, san_prefix, san_suffix)
await self._purge_file(claim_name, full_path)
# Re-download deleted file and assert that the file name is sanitized
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
stream_file_name = os.path.basename(full_path)
stream = (await self.daemon.jsonrpc_file_list())["items"][0]
file_list_name = stream.file_name
suggested_file_name = stream.descriptor.suggested_file_name
self.assertTrue(os.path.isfile(full_path))
check_prefix_suffix(stream_file_name, san_prefix, san_suffix)
self.assertEqual(stream_file_name, file_list_name)
self.assertEqual(stream_file_name, suggested_file_name)
await self._purge_file(claim_name, full_path)
# Assert that the downloaded file name is not sanitized when user provides custom file name
custom_name = 'cust*m_name'
full_path = (await self.daemon.jsonrpc_get(
'lbry://' + claim_name, file_name=custom_name, save_file=True)).full_path
file_name_on_disk = os.path.basename(full_path)
self.assertTrue(os.path.isfile(full_path))
self.assertEqual(custom_name, file_name_on_disk)
# Update the stream and assert the file name is not sanitized, but the suggested file name is
prefix, suffix = 'derpyderp?', '.ext.'
san_prefix, san_suffix = 'derpyderp', '.ext'
tx = await self.stream_update(claim_id, data=b'amazing content', prefix=prefix, suffix=suffix)
full_path = (await self.daemon.jsonrpc_get('lbry://' + claim_name, save_file=True)).full_path
updated_stream = (await self.daemon.jsonrpc_file_list())["items"][0]
stream_file_name = os.path.basename(full_path)
source_file_name = tx['outputs'][0]['value']['source']['name']
file_list_name = updated_stream.file_name
suggested_file_name = updated_stream.descriptor.suggested_file_name
self.assertTrue(os.path.isfile(full_path))
check_prefix_suffix(stream_file_name, prefix, suffix)
self.assertEqual(stream_file_name, source_file_name)
self.assertEqual(stream_file_name, file_list_name)
check_prefix_suffix(suggested_file_name, san_prefix, san_suffix)
async def test_file_list_fields(self):
await self.stream_create('foo', '0.01')
file_list = await self.file_list()
self.assertEqual(
file_list[0]['timestamp'],
self.ledger.headers.estimated_timestamp(file_list[0]['height'])
)
self.assertEqual(file_list[0]['confirmations'], -1)
await self.daemon.jsonrpc_resolve('foo')
file_list = await self.file_list()
self.assertEqual(
file_list[0]['timestamp'],
self.ledger.headers.estimated_timestamp(file_list[0]['height'])
)
self.assertEqual(file_list[0]['confirmations'], 1)
async def test_get_doesnt_touch_user_written_files_between_calls(self):
await self.stream_create('foo', '0.01', data=bytes([0] * (2 << 23)))
self.assertTrue(await self.daemon.jsonrpc_file_delete(claim_name='foo'))
first_path = (await self.daemon.jsonrpc_get('lbry://foo', save_file=True)).full_path
await self.wait_files_to_complete()
self.assertTrue(await self.daemon.jsonrpc_file_delete(claim_name='foo'))
with open(first_path, 'wb') as f:
f.write(b' ')
f.flush()
second_path = await self.daemon.jsonrpc_get('lbry://foo', save_file=True)
await self.wait_files_to_complete()
self.assertNotEqual(first_path, second_path)
async def test_file_list_updated_metadata_on_resolve(self):
await self.stream_create('foo', '0.01')
txo = (await self.daemon.resolve(self.wallet.accounts, ['lbry://foo']))['lbry://foo']
claim = txo.claim
await self.daemon.jsonrpc_file_delete(claim_name='foo')
txid = await self.blockchain_claim_name('bar', hexlify(claim.to_bytes()).decode(), '0.01')
await self.daemon.jsonrpc_get('lbry://bar')
claim.stream.description = "fix typos, fix the world"
await self.blockchain_update_name(txid, hexlify(claim.to_bytes()).decode(), '0.01')
await self.daemon.jsonrpc_resolve('lbry://bar')
file_list = (await self.daemon.jsonrpc_file_list())['items']
self.assertEqual(file_list[0].stream_claim_info.claim.stream.description, claim.stream.description)
async def test_sourceless_content(self):
# claim has no source, then it has one
tx = await self.stream_create('foo', '0.01', data=None)
claim_id = self.get_claim_id(tx)
await self.daemon.jsonrpc_file_delete(claim_name='foo')
response = await self.out(self.daemon.jsonrpc_get('lbry://foo'))
self.assertIn('error', response)
self.assertIn('nothing to download', response['error'])
# source is set (there isn't a way to clear the source field, so we stop here for now)
await self.stream_update(claim_id, data=b'surpriiiiiiiise')
response = await self.out(self.daemon.jsonrpc_get('lbry://foo'))
self.assertNotIn('error', response)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
async def test_file_list_paginated_output(self):
await self.create_streams_in_range(0, 20)
page = await self.file_list(page_size=20)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids)
page = await self.file_list(page_size=6)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids[:6])
page = await self.file_list(page_size=6, page=2)
page_claim_ids = [item['claim_id'] for item in page]
self.assertListEqual(page_claim_ids, self.stream_claim_ids[6:12])
out_of_bounds = await self.file_list(page=5, page_size=6)
self.assertEqual(out_of_bounds, [])
complete = await self.daemon.jsonrpc_file_list()
self.assertEqual(complete['total_pages'], 1)
self.assertEqual(complete['total_items'], 20)
page = await self.daemon.jsonrpc_file_list(page_size=10, page=1)
self.assertEqual(page['total_pages'], 2)
self.assertEqual(page['total_items'], 20)
self.assertEqual(page['page'], 1)
full = await self.out(self.daemon.jsonrpc_file_list(page_size=20, page=1))
page1 = await self.file_list(page=1, page_size=10)
page2 = await self.file_list(page=2, page_size=10)
self.assertEqual(page1 + page2, full['items'])
async def test_download_different_timeouts(self):
tx = await self.stream_create('foo', '0.01')
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
await self.daemon.jsonrpc_file_delete(claim_name='foo')
all_except_sd = [
blob_hash for blob_hash in self.server.blob_manager.completed_blob_hashes if blob_hash != sd_hash
]
await self.server.blob_manager.delete_blobs(all_except_sd)
resp = await self.daemon.jsonrpc_get('lbry://foo', timeout=2, save_file=True)
self.assertIn('error', resp)
self.assertEqual('Failed to download data blobs for sd hash %s within timeout.' % sd_hash, resp['error'])
self.assertTrue(await self.daemon.jsonrpc_file_delete(claim_name='foo'), "data timeout didn't create a file")
await self.server.blob_manager.delete_blobs([sd_hash])
resp = await self.daemon.jsonrpc_get('lbry://foo', timeout=2, save_file=True)
self.assertIn('error', resp)
self.assertEqual('Failed to download sd blob %s within timeout.' % sd_hash, resp['error'])
async def wait_files_to_complete(self):
while await self.file_list(status='running'):
await asyncio.sleep(0.01)
async def test_filename_conflicts_management_on_resume_download(self):
await self.stream_create('foo', '0.01', data=bytes([0] * (1 << 23)))
file_info = (await self.file_list())[0]
original_path = os.path.join(self.daemon.conf.download_dir, file_info['file_name'])
await self.daemon.jsonrpc_file_delete(claim_name='foo')
await self.daemon.jsonrpc_get('lbry://foo')
with open(original_path, 'wb') as handle:
handle.write(b'some other stuff was there instead')
self.daemon.file_manager.stop()
await self.daemon.file_manager.start()
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5) # if this hangs, file didn't get set completed
# check that internal state got through up to the file list API
stream = self.daemon.file_manager.get_filtered(stream_hash=file_info['stream_hash'])[0]
file_info = (await self.file_list())[0]
self.assertEqual(stream.file_name, file_info['file_name'])
# checks if what the API shows is what he have at the very internal level.
self.assertEqual(stream.full_path, file_info['download_path'])
async def test_incomplete_downloads_erases_output_file_on_stop(self):
tx = await self.stream_create('foo', '0.01', data=b'deadbeef' * 1000000)
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
file_info = (await self.file_list())[0]
await self.daemon.jsonrpc_file_delete(claim_name='foo')
blobs = await self.server_storage.get_blobs_for_stream(
await self.server_storage.get_stream_hash_for_sd_hash(sd_hash)
)
all_except_sd_and_head = [
blob.blob_hash for blob in blobs[1:-1]
]
await self.server.blob_manager.delete_blobs(all_except_sd_and_head)
path = os.path.join(self.daemon.conf.download_dir, file_info['file_name'])
self.assertFalse(os.path.isfile(path))
resp = await self.out(self.daemon.jsonrpc_get('lbry://foo', timeout=2))
self.assertNotIn('error', resp)
self.assertTrue(os.path.isfile(path))
self.daemon.file_manager.stop()
await asyncio.sleep(0.01, loop=self.loop) # FIXME: this sleep should not be needed
self.assertFalse(os.path.isfile(path))
async def test_incomplete_downloads_retry(self):
tx = await self.stream_create('foo', '0.01', data=b'deadbeef' * 1000000)
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
await self.daemon.jsonrpc_file_delete(claim_name='foo')
blobs = await self.server_storage.get_blobs_for_stream(
await self.server_storage.get_stream_hash_for_sd_hash(sd_hash)
)
all_except_sd_and_head = [
blob.blob_hash for blob in blobs[1:-1]
]
# backup server blobs
for blob_hash in all_except_sd_and_head:
blob = self.server_blob_manager.get_blob(blob_hash)
os.rename(blob.file_path, blob.file_path + '__')
# erase all except sd blob
await self.server.blob_manager.delete_blobs(all_except_sd_and_head)
# start the download
resp = await self.out(self.daemon.jsonrpc_get('lbry://foo', timeout=2))
self.assertNotIn('error', resp)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual('running', (await self.file_list())[0]['status'])
await self.daemon.jsonrpc_file_set_status('stop', claim_name='foo')
# recover blobs
for blob_hash in all_except_sd_and_head:
blob = self.server_blob_manager.get_blob(blob_hash)
os.rename(blob.file_path + '__', blob.file_path)
self.server_blob_manager.blobs.clear()
await self.server_blob_manager.blob_completed(self.server_blob_manager.get_blob(blob_hash))
await self.daemon.jsonrpc_file_set_status('start', claim_name='foo')
await asyncio.wait_for(self.wait_files_to_complete(), timeout=5)
file_info = (await self.file_list())[0]
self.assertEqual(file_info['blobs_completed'], file_info['blobs_in_stream'])
self.assertEqual('finished', file_info['status'])
async def test_paid_download(self):
target_address = await self.blockchain.get_raw_change_address()
# FAIL: beyond available balance
await self.stream_create(
'expensive', '0.01', data=b'pay me if you can',
fee_currency='LBC', fee_amount='11.0',
fee_address=target_address, claim_address=target_address
)
await self.daemon.jsonrpc_file_delete(claim_name='expensive')
response = await self.out(self.daemon.jsonrpc_get('lbry://expensive'))
self.assertEqual(response['error'], 'Not enough funds to cover this transaction.')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
# FAIL: beyond maximum key fee
await self.stream_create(
'maxkey', '0.01', data=b'no pay me, no',
fee_currency='LBC', fee_amount='111.0',
fee_address=target_address, claim_address=target_address
)
await self.daemon.jsonrpc_file_delete(claim_name='maxkey')
response = await self.out(self.daemon.jsonrpc_get('lbry://maxkey'))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
self.assertEqual(
response['error'], 'Purchase price of 111.0 LBC exceeds maximum configured price of 100.0 LBC (50.0 USD).'
)
# PASS: purchase is successful
await self.stream_create(
'icanpay', '0.01', data=b'I got the power!',
fee_currency='LBC', fee_amount='1.0',
fee_address=target_address, claim_address=target_address
)
await self.daemon.jsonrpc_file_delete(claim_name='icanpay')
await self.assertBalance(self.account, '9.925679')
response = await self.daemon.jsonrpc_get('lbry://icanpay')
raw_content_fee = response.content_fee.raw
await self.ledger.wait(response.content_fee)
await self.assertBalance(self.account, '8.925538')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
# check that the fee was received
starting_balance = float(await self.blockchain.get_balance())
await self.generate(1)
block_reward_and_claim_fee = 2.0
self.assertEqual(
float(await self.blockchain.get_balance()),
starting_balance + block_reward_and_claim_fee
)
# restart the daemon and make sure the fee is still there
self.daemon.file_manager.stop()
await self.daemon.file_manager.start()
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].content_fee.raw, raw_content_fee)
await self.daemon.jsonrpc_file_delete(claim_name='icanpay')
# PASS: no fee address --> use the claim address to pay
tx = await self.stream_create(
'nofeeaddress', '0.01', data=b'free stuff?',
)
await self.__raw_value_update_no_fee_address(
tx, fee_amount='2.0', fee_currency='LBC', claim_address=target_address
)
await self.daemon.jsonrpc_file_delete(claim_name='nofeeaddress')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
response = await self.out(self.daemon.jsonrpc_get('lbry://nofeeaddress'))
self.assertIsNone((await self.daemon.jsonrpc_file_list())['items'][0].stream_claim_info.claim.stream.fee.address)
self.assertIsNotNone(response['content_fee'])
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertEqual(response['content_fee']['outputs'][0]['amount'], '2.0')
self.assertEqual(response['content_fee']['outputs'][0]['address'], target_address)
async def test_null_max_key_fee(self):
target_address = await self.blockchain.get_raw_change_address()
self.daemon.conf.max_key_fee = None
await self.stream_create(
'somename', '0.5', data=b'Yes, please',
fee_currency='LBC', fee_amount='1.0',
fee_address=target_address, claim_address=target_address
)
self.assertTrue(await self.daemon.jsonrpc_file_delete(claim_name='somename'))
# Assert the fee and bid are subtracted
await self.assertBalance(self.account, '9.483893')
response = await self.daemon.jsonrpc_get('lbry://somename')
await self.ledger.wait(response.content_fee)
await self.assertBalance(self.account, '8.483752')
# Assert the file downloads
await asyncio.wait_for(self.wait_files_to_complete(), timeout=1)
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
# Assert the transaction is recorded to the blockchain
starting_balance = float(await self.blockchain.get_balance())
await self.generate(1)
block_reward_and_claim_fee = 2.0
self.assertEqual(
float(await self.blockchain.get_balance()), starting_balance + block_reward_and_claim_fee
)
async def test_null_fee(self):
target_address = await self.blockchain.get_raw_change_address()
tx = await self.stream_create(
'nullfee', '0.01', data=b'no pay me, no',
fee_currency='LBC', fee_address=target_address, fee_amount='1.0'
)
await self.__raw_value_update_no_fee_amount(tx, target_address)
await self.daemon.jsonrpc_file_delete(claim_name='nullfee')
response = await self.daemon.jsonrpc_get('lbry://nullfee')
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
self.assertIsNone(response.content_fee)
self.assertTrue(response.stream_claim_info.claim.stream.has_fee)
self.assertDictEqual(
response.stream_claim_info.claim.stream.to_dict()['fee'],
{'currency': 'LBC', 'address': target_address}
)
await self.daemon.jsonrpc_file_delete(claim_name='nullfee')
async def __raw_value_update_no_fee_address(self, tx, claim_address, **kwargs):
tx = await self.daemon.jsonrpc_stream_update(
self.get_claim_id(tx), preview=True, claim_address=claim_address, **kwargs
)
tx.outputs[0].claim.stream.fee.address_bytes = b''
tx.outputs[0].script.generate()
await tx.sign([self.account])
await self.broadcast(tx)
await self.confirm_tx(tx.id)
async def __raw_value_update_no_fee_amount(self, tx, claim_address):
tx = await self.daemon.jsonrpc_stream_update(
self.get_claim_id(tx), preview=True, fee_currency='LBC', fee_amount='1.0', fee_address=claim_address,
claim_address=claim_address
)
tx.outputs[0].claim.stream.fee.message.ClearField('amount')
tx.outputs[0].script.generate()
await tx.sign([self.account])
await self.broadcast(tx)
await self.confirm_tx(tx.id)
class DiskSpaceManagement(CommandTestCase):
async def get_referenced_blobs(self, tx):
sd_hash = tx['outputs'][0]['value']['source']['sd_hash']
stream_hash = await self.daemon.storage.get_stream_hash_for_sd_hash(sd_hash)
return tx['outputs'][0]['value']['source']['sd_hash'], set(await self.blob_list(
stream_hash=stream_hash
))
async def test_file_management(self):
status = await self.status()
self.assertIn('disk_space', status)
self.assertEqual('0', status['disk_space']['space_used'])
self.assertEqual(True, status['disk_space']['running'])
sd_hash1, blobs1 = await self.get_referenced_blobs(
await self.stream_create('foo1', '0.01', data=('0' * 2 * 1024 * 1024).encode())
)
sd_hash2, blobs2 = await self.get_referenced_blobs(
await self.stream_create('foo2', '0.01', data=('0' * 3 * 1024 * 1024).encode())
)
sd_hash3, blobs3 = await self.get_referenced_blobs(
await self.stream_create('foo3', '0.01', data=('0' * 3 * 1024 * 1024).encode())
)
sd_hash4, blobs4 = await self.get_referenced_blobs(
await self.stream_create('foo4', '0.01', data=('0' * 2 * 1024 * 1024).encode())
)
await self.daemon.storage.update_blob_ownership(sd_hash1, False)
await self.daemon.storage.update_blob_ownership(sd_hash3, False)
await self.daemon.storage.update_blob_ownership(sd_hash4, False)
self.assertEqual('10', (await self.status())['disk_space']['space_used'])
self.assertEqual(blobs1 | blobs2 | blobs3 | blobs4, set(await self.blob_list()))
await self.blob_clean()
self.assertEqual('10', (await self.status())['disk_space']['space_used'])
self.assertEqual(blobs1 | blobs2 | blobs3 | blobs4, set(await self.blob_list()))
self.daemon.conf.blob_storage_limit = 6
await self.blob_clean()
self.assertEqual('5', (await self.status())['disk_space']['space_used'])
blobs = set(await self.blob_list())
self.assertFalse(blobs1.issubset(blobs))
self.assertTrue(blobs2.issubset(blobs))
self.assertFalse(blobs3.issubset(blobs))
self.assertTrue(blobs4.issubset(blobs))
class TestProactiveDownloaderComponent(CommandTestCase):
async def assertBlobs(self, *sd_hashes):
# checks that we have ony the finished blobs needed for the the referenced streams
seen = set(sd_hashes)
for sd_hash in sd_hashes:
self.assertTrue(self.daemon.blob_manager.get_blob(sd_hash).get_is_verified())
blobs = await self.daemon.storage.get_blobs_for_stream(
await self.daemon.storage.get_stream_hash_for_sd_hash(sd_hash)
)
for blob in blobs[:-1]:
self.assertTrue(self.daemon.blob_manager.get_blob(blob.blob_hash).get_is_verified())
seen.update(blob.blob_hash for blob in blobs if blob.blob_hash)
self.assertEqual(seen, self.daemon.blob_manager.completed_blob_hashes)
async def test_ensure_download(self):
unrelated_claim_id = self.get_claim_id(await self.stream_create('something_else', '0.01'))
channel_id = self.get_claim_id(await self.channel_create('@cool'))
content1 = await self.stream_create('content1', '0.01', channel_id=channel_id)
content1 = content1['outputs'][0]['value']['source']['sd_hash']
content2 = await self.stream_create('content2', '0.01', channel_id=channel_id)
content2 = content2['outputs'][0]['value']['source']['sd_hash']
await self.stream_create('paid', '0.01', channel_id=channel_id, fee_amount=42, fee_currency='USD')
await self.stream_repost(unrelated_claim_id, 'repost')
await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertEqual(0, len(await self.file_list()))
self.assertEqual(0, len((await self.daemon.jsonrpc_blob_list())['items']))
proactive_downloader = self.daemon.component_manager.get_component(BACKGROUND_DOWNLOADER_COMPONENT)
await proactive_downloader.ensure_download(channel_id, 1)
await self.assertBlobs(content1)
await proactive_downloader.ensure_download(channel_id, 2)
await self.assertBlobs(content1, content2)
# ignores paid content
await proactive_downloader.ensure_download(channel_id, 3)
await self.assertBlobs(content1, content2)
# ignores reposts
await proactive_downloader.ensure_download(channel_id, 4)
await self.assertBlobs(content1, content2)
await self.daemon.jsonrpc_file_delete(delete_all=True)
self.assertEqual(0, len(await self.file_list()))
await self.daemon.blob_manager.delete_blobs(list(self.daemon.blob_manager.completed_blob_hashes), True)
self.assertEqual(0, len((await self.daemon.jsonrpc_blob_list())['items']))