diff --git a/golem_messages/factories/tasks.py b/golem_messages/factories/tasks.py index b166903..965055a 100644 --- a/golem_messages/factories/tasks.py +++ b/golem_messages/factories/tasks.py @@ -1,11 +1,9 @@ # pylint: disable=too-few-public-methods,unnecessary-lambda import calendar import datetime -import os import time import typing -from eth_utils import encode_hex import factory.fuzzy import faker @@ -27,6 +25,11 @@ class Meta: node_name = factory.Faker('name') task_id = factory.Faker('uuid4') + provider_public_key = factory.LazyFunction( + lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey)) + provider_ethereum_public_key = factory.SelfAttribute( + 'provider_public_key' + ) class CTDBlenderExtraDataFactory(factory.DictFactory): @@ -63,19 +66,14 @@ class Meta: requestor_id = factory.SelfAttribute( 'requestor_public_key') - provider_id = factory.SelfAttribute( - 'provider_public_key' + provider_id = factory.LazyAttribute( + lambda task_to_compute: task_to_compute.want_to_compute_task.provider_public_key ) compute_task_def = factory.SubFactory(ComputeTaskDefFactory) - provider_public_key = factory.LazyFunction( - lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey)) - provider_ethereum_public_key = factory.SelfAttribute( - 'provider_public_key' - ) requestor_public_key = factory.LazyFunction( lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey)) - compute_task_def = factory.SubFactory(ComputeTaskDefFactory) + want_to_compute_task = factory.SubFactory(WantToComputeTaskFactory) package_hash = factory.LazyFunction(lambda: 'sha1:' + faker.Faker().sha1()) size = factory.Faker('random_int', min=1 << 20, max=10 << 20) price = factory.Faker('random_int', min=1 << 20, max=10 << 20) @@ -186,7 +184,6 @@ class Meta: node_name = factory.Faker('name') address = factory.Faker('ipv4') port = factory.Faker('pyint') - eth_account = factory.LazyFunction(lambda: encode_hex(os.urandom(20))) key_id = factory.Faker('binary', length=64) task_to_compute = factory.SubFactory(TaskToComputeFactory) package_hash = factory.LazyFunction(lambda: 'sha1:' + faker.Faker().sha1()) diff --git a/golem_messages/message/tasks.py b/golem_messages/message/tasks.py index 43cc255..a4be734 100644 --- a/golem_messages/message/tasks.py +++ b/golem_messages/message/tasks.py @@ -242,8 +242,16 @@ class WantToComputeTask(ConcentEnabled, base.Message): # `golem-messages` should be intentionally agnostic # with regards to the contents of this field. + 'provider_public_key', # key used for msg signing and encryption + 'provider_ethereum_public_key', # used for transactions on blockchain ] + base.Message.__slots__ + @property + def provider_ethereum_address(self): + return to_checksum_address( + sha3(decode_hex(self.provider_ethereum_public_key))[12:].hex(), + ) + @library.register(TASK_MSG_BASE + 2) class TaskToCompute(ConcentEnabled, TaskMessage): @@ -256,9 +264,8 @@ class TaskToCompute(ConcentEnabled, TaskMessage): 'requestor_public_key', # key used for msg signing and encryption 'requestor_ethereum_public_key', # used for transactions on blockchain 'provider_id', # a.k.a. node id - 'provider_public_key', # key used for msg signing and encryption - 'provider_ethereum_public_key', # used for transactions on blockchain 'compute_task_def', + 'want_to_compute_task', 'package_hash', # the hash of the package (resources) zip file 'size', # the size of the resources zip file 'concent_enabled', @@ -273,12 +280,19 @@ def requestor_ethereum_address(self): sha3(decode_hex(self.requestor_ethereum_public_key))[12:].hex() ) + @property + def provider_public_key(self): + return self.want_to_compute_task.provider_public_key + + @property + def provider_ethereum_public_key(self): + return self.want_to_compute_task.provider_ethereum_public_key + @property def provider_ethereum_address(self): - return to_checksum_address( - sha3(decode_hex(self.provider_ethereum_public_key))[12:].hex(), - ) + return self.want_to_compute_task.provider_ethereum_address + @base.verify_slot('want_to_compute_task', WantToComputeTask) def deserialize_slot(self, key, value): value = super().deserialize_slot(key, value) if key == 'compute_task_def': @@ -432,7 +446,6 @@ class ReportComputedTask(TaskMessage): 'port', 'key_id', 'extra_data', - 'eth_account', 'task_to_compute', 'size', 'package_hash', # sha1 hash of the package file (the zip file) diff --git a/tests/message/test_messages.py b/tests/message/test_messages.py index 057f12f..d9cae09 100644 --- a/tests/message/test_messages.py +++ b/tests/message/test_messages.py @@ -5,10 +5,13 @@ import unittest.mock as mock import uuid +from golem_messages import cryptography from golem_messages import datastructures from golem_messages import factories from golem_messages import message from golem_messages import shortcuts +from golem_messages.utils import encode_hex + class InitializationTestCase(unittest.TestCase): def test_default_slots(self): @@ -51,6 +54,8 @@ def test_message_want_to_compute_task(self): max_memory_size = random.randint(1, 2**10) num_cores = random.randint(1, 2**5) concent_enabled = random.random() > 0.5 + provider_public_key = provider_ethereum_public_key = ( + encode_hex(cryptography.ECCx(None).raw_pubkey)) msg = message.WantToComputeTask( node_name=node_id, task_id=task_id, @@ -59,7 +64,10 @@ def test_message_want_to_compute_task(self): max_resource_size=max_resource_size, max_memory_size=max_memory_size, num_cores=num_cores, - concent_enabled=concent_enabled) + concent_enabled=concent_enabled, + provider_public_key=provider_public_key, + provider_ethereum_public_key=provider_ethereum_public_key, + ) expected = [ ['node_name', node_id], ['task_id', task_id], @@ -70,6 +78,8 @@ def test_message_want_to_compute_task(self): ['price', price], ['concent_enabled', concent_enabled], ['extra_data', None], + ['provider_public_key', provider_public_key], + ['provider_ethereum_public_key', provider_ethereum_public_key], ] self.assertEqual(expected, msg.slots()) diff --git a/tests/message/test_tasks.py b/tests/message/test_tasks.py index 1de0638..c348e05 100644 --- a/tests/message/test_tasks.py +++ b/tests/message/test_tasks.py @@ -33,12 +33,33 @@ def test_concent_enabled_true(self): wtct = message.tasks.WantToComputeTask(concent_enabled=True) self.assertTrue(wtct.concent_enabled) + def test_extra_data(self): extra_data_content = {'some': 'content'} wtct = message.tasks.WantToComputeTask(extra_data=extra_data_content) wtct2 = helpers.dump_and_load(wtct) self.assertEqual(wtct2.extra_data, extra_data_content) + def test_provider_ethereum_address_checksum(self): + msg = factories.tasks.WantToComputeTaskFactory() + self.assertTrue(msg.provider_ethereum_public_key) + self.assertTrue(is_checksum_address(msg.provider_ethereum_address)) + + def test_ethereum_address_provider(self): + msg = factories.tasks.WantToComputeTaskFactory() + provider_public_key = decode_hex(msg.provider_ethereum_public_key) + + self.assertEqual(msg.provider_ethereum_address, + to_checksum_address( + '0x' + sha3(provider_public_key)[12:].hex())) + + def test_ethereum_address(self): + msg = factories.tasks.WantToComputeTaskFactory() + serialized = shortcuts.dump(msg, None, None) + msg_l = shortcuts.load(serialized, None, None) + self.assertEqual(len(msg_l.provider_ethereum_address), 2 + (20*2)) + + class ComputeTaskDefTestCase(unittest.TestCase): @mock.patch('golem_messages.message.tasks.ComputeTaskDef.validate_task_id') def test_task_id_validator(self, v_mock): @@ -165,31 +186,32 @@ def test_concent_enabled_none_false(self): ttc = message.tasks.TaskToCompute(concent_enabled=None) self.assertFalse(ttc.concent_enabled) - def test_ethereum_address(self): + def test_ethereum_address_requestor(self): msg = factories.tasks.TaskToComputeFactory() + requestor_public_key = decode_hex(msg.requestor_ethereum_public_key) serialized = shortcuts.dump(msg, None, None) msg_l = shortcuts.load(serialized, None, None) - for addr_slot in ( - 'requestor_ethereum_address', - 'provider_ethereum_address'): - address = getattr(msg_l, addr_slot) - self.assertEqual(len(address), 2 + (20*2)) + self.assertEqual(len(msg_l.requestor_ethereum_address), 2 + (20*2)) + self.assertEqual(msg.requestor_ethereum_address, + to_checksum_address( + '0x' + sha3(requestor_public_key)[12:].hex())) def test_ethereum_address_provider(self): msg = factories.tasks.TaskToComputeFactory() provider_public_key = decode_hex(msg.provider_ethereum_public_key) - + serialized = shortcuts.dump(msg, None, None) + msg_l = shortcuts.load(serialized, None, None) + self.assertEqual(len(msg_l.provider_ethereum_address), 2 + (20*2)) + self.assertEqual(msg_l.provider_ethereum_address, + msg_l.want_to_compute_task.provider_ethereum_address) self.assertEqual(msg.provider_ethereum_address, to_checksum_address( '0x' + sha3(provider_public_key)[12:].hex())) - def test_ethereum_address_requestor(self): + def test_public_key_provider(self): msg = factories.tasks.TaskToComputeFactory() - requestor_public_key = decode_hex(msg.requestor_ethereum_public_key) - - self.assertEqual(msg.requestor_ethereum_address, - to_checksum_address( - '0x' + sha3(requestor_public_key)[12:].hex())) + self.assertEqual(msg.provider_ethereum_public_key, + msg.want_to_compute_task.provider_ethereum_public_key) def test_task_id(self): self.assertEqual(self.msg.task_id, @@ -256,11 +278,6 @@ def test_requestor_ethereum_address_checksum(self): self.assertTrue(ttc.requestor_ethereum_public_key) self.assertTrue(is_checksum_address(ttc.requestor_ethereum_address)) - def test_provider_ethereum_address_checksum(self): - ttc = factories.tasks.TaskToComputeFactory() - self.assertTrue(ttc.provider_ethereum_public_key) - self.assertTrue(is_checksum_address(ttc.provider_ethereum_address)) - # pylint:disable=protected-access @@ -554,8 +571,10 @@ def setUp(self): def get_ttc(self, **kwargs): return factories.tasks.TaskToComputeFactory( - provider_public_key=encode_hex(self.provider_keys.raw_pubkey), requestor_public_key=encode_hex(self.requestor_keys.raw_pubkey), + want_to_compute_task=factories.tasks.WantToComputeTaskFactory( + provider_public_key=encode_hex(self.provider_keys.raw_pubkey), + ), **kwargs, )