Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

sign promissory notes in TTC and SRV #4137

Merged
merged 7 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions golem/task/tasksession.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pydispatch import dispatcher

import golem
from golem.config.active import EthereumConfig
from golem.core import common
from golem.core import golem_async
from golem.core.keysauth import KeysAuth
Expand Down Expand Up @@ -447,6 +448,14 @@ def _offer_chosen(
ctd['task_id'], self.address).__dict__
)
ttc.generate_ethsig(self.my_private_key)
if ttc.concent_enabled:
ttc.sign_promissory_note(private_key=self.my_private_key)
ttc.sign_concent_promissory_note(
deposit_contract_address=getattr(
EthereumConfig, 'deposit_contract_address'),
private_key=self.my_private_key
)

self.send(ttc)
history.add(
msg=msg_utils.copy_and_sign(
Expand All @@ -458,10 +467,10 @@ def _offer_chosen(
remote_role=Actor.Provider,
)

# pylint: disable=too-many-return-statements
# pylint: disable=too-many-return-statements, too-many-branches
@handle_attr_error
@history.provider_history
def _react_to_task_to_compute(self, msg):
def _react_to_task_to_compute(self, msg: message.tasks.TaskToCompute):
ctd: Optional[message.tasks.ComputeTaskDef] = msg.compute_task_def
want_to_compute_task = msg.want_to_compute_task
if ctd is None or want_to_compute_task is None:
Expand Down Expand Up @@ -552,6 +561,21 @@ def _cannot_compute(reason):
_cannot_compute(reasons.TooShortDeposit)
return

if not (msg.verify_promissory_note() and
msg.verify_concent_promissory_note(
deposit_contract_address=getattr(
EthereumConfig, 'deposit_contract_address')
)):
_cannot_compute(reasons.PromissoryNoteMissing)
logger.debug(
f"Requestor failed to provide correct promissory"
f"note signatures to compute with the Concent:"
f"promissory_note_sig: {msg.promissory_note_sig}, "
f"concent_promissory_note_sig: "
f"{msg.concent_promissory_note_sig}."
)
return

try:
self._check_ctd_params(ctd)
self._set_env_params(
Expand All @@ -567,6 +591,8 @@ def _cannot_compute(reason):
_cannot_compute(None)
return

# pylint: enable=too-many-return-statements, too-many-branches

def _check_resource_size(self, resource_size):
max_resource_size_kib = self.task_server.config_desc.max_resource_size
max_resource_size = int(max_resource_size_kib) * 1024
Expand Down Expand Up @@ -740,6 +766,11 @@ def ask_for_verification(_):
srv = message.concents.SubtaskResultsVerify(
subtask_results_rejected=msg
)
srv.sign_concent_promissory_note(
deposit_contract_address=getattr(
EthereumConfig, 'deposit_contract_address'),
private_key=self.my_private_key,
)

self.concent_service.submit_task_message(
subtask_id=msg.subtask_id,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ eth-keys==0.2.0b3
eth-tester==0.1.0-beta.24
eth-utils==1.0.3
ethereum==1.6.1
Golem-Messages==3.3.1
Golem-Messages==3.4.0
Golem-Smart-Contracts-Interface==1.7.0
greenlet==0.4.15
h2==3.0.1
Expand Down
2 changes: 1 addition & 1 deletion requirements_to-freeze.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ docker==3.5.0
enforce==0.3.4
eth-utils==1.0.3
ethereum==1.6.1
Golem-Messages==3.3.1
Golem-Messages==3.4.0
Golem-Smart-Contracts-Interface==1.7.0
html2text==2018.1.9
humanize==0.5.1
Expand Down
72 changes: 56 additions & 16 deletions tests/golem/task/test_concent_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from twisted.internet.defer import Deferred

from golem import testutils
from golem.config.active import EthereumConfig
from golem.core import keysauth
from golem.network import history
from golem.task import tasksession
Expand All @@ -38,10 +39,24 @@ def setUp(self):
super().setUp()
self.keys = cryptography.ECCx(None)
self.different_keys = cryptography.ECCx(None)
self.msg = factories.tasks.TaskToComputeFactory()
self.msg._fake_sign()
self.requestor_keys = cryptography.ECCx(None)
self.msg: message.tasks.TaskToCompute = \
factories.tasks.TaskToComputeFactory(
requestor_ethereum_public_key=encode_hex(
self.requestor_keys.raw_pubkey),
)
self.msg.concent_enabled = True
self.msg.want_to_compute_task.sign_message(self.keys.raw_privkey) # noqa pylint: disable=no-member
self.msg.generate_ethsig(self.requestor_keys.raw_privkey)
self.msg.sign_promissory_note(self.requestor_keys.raw_privkey)
self.msg.sign_concent_promissory_note(
deposit_contract_address=getattr(
EthereumConfig, 'deposit_contract_address'),
private_key=self.requestor_keys.raw_privkey
)
self.msg.sign_message(self.requestor_keys.raw_privkey) # noqa go home pylint, you're drunk pylint: disable=no-value-for-parameter
self.task_session = tasksession.TaskSession(mock.MagicMock())
self.task_session.concent_service.enabled = True
self.task_session.task_computer.has_assigned_task.return_value = False
self.task_session.task_server.keys_auth.ecc.raw_pubkey = \
self.keys.raw_pubkey
Expand Down Expand Up @@ -73,18 +88,15 @@ def assert_rejected(
reason,
)

def test_requestor_concented(self, send_mock, *_):
self.task_session._react_to_task_to_compute(self.msg)
self.assert_accepted(send_mock)

def test_requestor_failed_to_concent(self, send_mock, *_):
self.task_session.concent_service.enabled = True
self.msg.concent_enabled = False
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(send_mock)

def test_requestor_concented(self, send_mock, *_):
self.task_session.concent_service.enabled = True
self.msg.concent_enabled = True
self.task_session._react_to_task_to_compute(self.msg)
self.assert_accepted(send_mock)

def test_provider_doesnt_want_concent(self, send_mock, *_):
self.task_session.concent_service.enabled = False
self.msg.concent_enabled = False
Expand All @@ -96,16 +108,13 @@ def test_provider_doesnt_want_concent_but_requestor_insists(
send_mock,
*_):
self.task_session.concent_service.enabled = False
self.msg.concent_enabled = True
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(
send_mock,
reason=cannot_reasons.ConcentDisabled,
)

def test_requestor_low_balance(self, send_mock, *_):
self.task_session.concent_service.enabled = True
self.msg.concent_enabled = True
self.task_session.task_server.client.transaction_system\
.get_available_gnt.return_value = self.msg.price * 9
self.task_session._react_to_task_to_compute(self.msg)
Expand All @@ -129,8 +138,6 @@ def test_requestor_low_balance_no_concent(
)

def test_requestor_low_deposit(self, send_mock, *_):
self.task_session.concent_service.enabled = True
self.msg.concent_enabled = True
self.task_session.task_server.client.transaction_system\
.concent_balance.return_value = int((self.msg.price * 10) * 1.5)
self.task_session._react_to_task_to_compute(self.msg)
Expand All @@ -140,8 +147,6 @@ def test_requestor_low_deposit(self, send_mock, *_):
)

def test_requestor_short_deposit(self, send_mock, *_):
self.task_session.concent_service.enabled = True
self.msg.concent_enabled = True
self.task_session.task_server.client.transaction_system\
.concent_timelock.return_value = 1
self.task_session._react_to_task_to_compute(self.msg)
Expand Down Expand Up @@ -178,6 +183,41 @@ def test_want_to_compute_task_signed_by_different_key_than_it_contains(
send_mock.assert_not_called()
task_session_dropped.assert_called_once()

def test_no_promissory_note_sig(self, send_mock, *_):
self.msg.promissory_note_sig = None
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(
send_mock,
reason=cannot_reasons.PromissoryNoteMissing,
)

def test_no_concent_promissory_note_sig(self, send_mock, *_):
self.msg.concent_promissory_note_sig = None
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(
send_mock,
reason=cannot_reasons.PromissoryNoteMissing,
)

def test_bad_promissory_note_sig(self, send_mock, *_):
self.msg.sign_promissory_note(self.different_keys.raw_privkey)
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(
send_mock,
reason=cannot_reasons.PromissoryNoteMissing,
)

def test_bad_concent_promissory_note_sig(self, send_mock, *_):
self.msg.sign_concent_promissory_note(
deposit_contract_address=getattr(
EthereumConfig, 'deposit_contract_address'),
private_key=self.different_keys.raw_privkey
)
self.task_session._react_to_task_to_compute(self.msg)
self.assert_rejected(
send_mock,
reason=cannot_reasons.PromissoryNoteMissing,
)

@mock.patch(
'golem.task.tasksession.TaskSession.verify_owners',
Expand Down
Loading