From 11086b93f16f179faa9ae74a955e85dc908d5c63 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Sun, 10 Mar 2019 23:10:45 -0300 Subject: [PATCH] migrate other fields --- lbrynet/schema/current_schema/claim.py | 29 +++++++++- tests/unit/schema/test_lbryschema.py | 78 ++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/lbrynet/schema/current_schema/claim.py b/lbrynet/schema/current_schema/claim.py index 4980cd0a7..e915fba22 100644 --- a/lbrynet/schema/current_schema/claim.py +++ b/lbrynet/schema/current_schema/claim.py @@ -5,6 +5,7 @@ import google.protobuf.json_format as json_pb # pylint: disable=no-name-in-modu from google.protobuf.message import Message # pylint: disable=no-name-in-module,import-error from lbrynet.schema.proto3 import claim_pb2 as claim_pb +from torba.client.constants import COIN class Schema(Message): @@ -39,7 +40,33 @@ class Claim(Schema): _message_pb.type = Claim.CLAIM_TYPE_CERT _message_pb.channel = claim_pb.Channel(**_channel) elif "stream" in _claim: - pass # fixme + _message_pb.type = Claim.CLAIM_TYPE_STREAM + _stream = _claim.pop("stream") + if "source" in _stream: + _source = _stream.pop("source") + _message_pb.stream.hash = _source.get("source", b'') # fixme: fail if empty? + _message_pb.stream.media_type = _source.pop("contentType") + if "metadata" in _stream: + _metadata = _stream.pop("metadata") + _message_pb.stream.license = _metadata.get("license") + _message_pb.stream.description = _metadata.get("description") + _message_pb.stream.language = _metadata.get("language") + _message_pb.stream.title = _metadata.get("title") + _message_pb.stream.author = _metadata.get("author") + _message_pb.stream.license_url = _metadata.get("licenseUrl") + _message_pb.stream.thumbnail_url = _metadata.get("thumbnail") + if _metadata.get("nsfw"): + _message_pb.stream.tags.append("nsfw") + if "fee" in _metadata: + _message_pb.stream.fee.address = _metadata["fee"]["address"] + _message_pb.stream.fee.currency = { + "LBC": 0, + "USD": 1 + }[_metadata["fee"]["currency"]] + multiplier = COIN if _metadata["fee"]["currency"] == "LBC" else 100 + total = int(_metadata["fee"]["amount"]*multiplier) + _message_pb.stream.fee.amount = total if total >= 0 else 0 + _claim = {} else: raise AttributeError diff --git a/tests/unit/schema/test_lbryschema.py b/tests/unit/schema/test_lbryschema.py index 9d2cdfda2..160893c6d 100644 --- a/tests/unit/schema/test_lbryschema.py +++ b/tests/unit/schema/test_lbryschema.py @@ -7,6 +7,7 @@ from copy import deepcopy import unittest from lbrynet.schema.signature import Signature, NAMED_SECP256K1 +from torba.client.constants import COIN from .test_data import example_003, example_010, example_010_serialized from .test_data import claim_id_1, claim_address_1, claim_address_2 from .test_data import binary_claim, expected_binary_claim_decoded @@ -23,6 +24,7 @@ from lbrynet.schema.uri import URI, URIParseError from lbrynet.schema.decode import smart_decode, migrate_legacy_protobuf from lbrynet.schema.error import DecodeError, InvalidAddress from lbrynet.schema.address import decode_address, encode_address +from lbrynet.schema.proto2 import legacy_claim_pb2 parsed_uri_matches = [ @@ -561,6 +563,82 @@ class TestMigrateLegacyProtobufToCurrentSchema(UnitTest): migrated_cert = migrate_legacy_protobuf(legacy_binary_cert) self.assertEqual(binascii.hexlify(migrated_cert.channel.public_key).decode(), expected_binary_claim_decoded['certificate']['publicKey']) + self.assertFalse(migrated_cert.HasField('stream')) + + def test_unsigned_stream_claim_migration(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertFalse(migrated_claim.HasField('channel')) + self.assertEqual(migrated_claim.stream.hash, binascii.unhexlify(example_010['stream']['source']['source'])) + self.assertEqual(migrated_claim.stream.media_type, example_010['stream']['source']['contentType']) + self.assertEqual(migrated_claim.stream.license, example_010['stream']['metadata']['license']) + self.assertEqual(migrated_claim.stream.description, example_010['stream']['metadata']['description']) + self.assertEqual(migrated_claim.stream.language, example_010['stream']['metadata']['language']) + self.assertEqual(migrated_claim.stream.title, example_010['stream']['metadata']['title']) + self.assertEqual(migrated_claim.stream.author, example_010['stream']['metadata']['author']) + self.assertEqual(migrated_claim.stream.thumbnail_url, example_010['stream']['metadata']['thumbnail']) + self.assertEqual(len(migrated_claim.stream.tags[:]), 0) # it would have if nsfw was True + self.assertEqual(migrated_claim.stream.license_url, "") + + def test_nsfw_migrated_as_tag(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + claim = legacy_claim_pb2.Claim() + claim.ParseFromString(legacy_binary_unsigned_stream_claim) + claim.stream.metadata.nsfw = True + legacy_binary_unsigned_stream_claim = claim.SerializeToString() + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertEqual(migrated_claim.stream.tags[:], ["nsfw"]) + + def test_license_url_migration(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + claim = legacy_claim_pb2.Claim() + claim.ParseFromString(legacy_binary_unsigned_stream_claim) + claim.stream.metadata.licenseUrl = "url/license" + legacy_binary_unsigned_stream_claim = claim.SerializeToString() + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertEqual(migrated_claim.stream.license_url, "url/license") + + def test_LBC_fee_migration(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + claim = legacy_claim_pb2.Claim() + claim.ParseFromString(legacy_binary_unsigned_stream_claim) + claim.stream.metadata.fee.currency = 1 + claim.stream.metadata.fee.version = 0 + claim.stream.metadata.fee.amount = 2.0 + claim.stream.metadata.fee.address = b"bob" + legacy_binary_unsigned_stream_claim = claim.SerializeToString() + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertEqual(migrated_claim.stream.fee.currency, 0) # LBC was 1, migrates to 0 + self.assertEqual(migrated_claim.stream.fee.amount, int(2.0*COIN)) + self.assertEqual(migrated_claim.stream.fee.address, b"bob") + + def test_USD_fee_migration(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + claim = legacy_claim_pb2.Claim() + claim.ParseFromString(legacy_binary_unsigned_stream_claim) + claim.stream.metadata.fee.currency = 3 + claim.stream.metadata.fee.version = 0 + claim.stream.metadata.fee.amount = 2.0 + claim.stream.metadata.fee.address = b"bob" + legacy_binary_unsigned_stream_claim = claim.SerializeToString() + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertEqual(migrated_claim.stream.fee.currency, 1) # USD was 3, migrates to 1 + self.assertEqual(migrated_claim.stream.fee.amount, int(200)) + self.assertEqual(migrated_claim.stream.fee.address, b"bob") + + def test_negative_fee_trolling_becomes_zero(self): + legacy_binary_unsigned_stream_claim = binascii.unhexlify(example_010_serialized) + claim = legacy_claim_pb2.Claim() + claim.ParseFromString(legacy_binary_unsigned_stream_claim) + claim.stream.metadata.fee.currency = 3 + claim.stream.metadata.fee.version = 0 + claim.stream.metadata.fee.amount = -2.0 + claim.stream.metadata.fee.address = b"bob" + legacy_binary_unsigned_stream_claim = claim.SerializeToString() + migrated_claim = migrate_legacy_protobuf(legacy_binary_unsigned_stream_claim) + self.assertEqual(migrated_claim.stream.fee.currency, 1) # USD was 3, migrates to 1 + self.assertEqual(migrated_claim.stream.fee.amount, int(0)) + self.assertEqual(migrated_claim.stream.fee.address, b"bob") if __name__ == '__main__':