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

Commit

Permalink
Merge pull request #4854 from golemfactory/gr260-2-adjust-requestor-q…
Browse files Browse the repository at this point in the history
…uality

adjust the expected amount after we know the actual payment value
  • Loading branch information
shadeofblue authored Nov 6, 2019
2 parents 7c091ed + 674ddb6 commit fcbe27c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
5 changes: 5 additions & 0 deletions golem/ranking/manager/database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ def update_requestor_assigned_sum(node_id: str, amount: int) -> None:
with db.transaction():
rank, _ = LocalRank.get_or_create(node_id=node_id)
rank.requestor_assigned_sum += amount
if rank.requestor_assigned_sum < 0:
logger.error('LocalRank.requestor_assigned_sum '
'unexpectedly negative, setting to 0. '
'node_id=%r', node_id)
rank.requestor_assigned_sum = 0
rank.save()


Expand Down
39 changes: 35 additions & 4 deletions golem/task/tasksession.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
from golem.network.transport import msg_queue
from golem.network.transport import tcpnetwork
from golem.network.transport.session import BasicSafeSession
from golem.ranking.manager.database_manager import (
update_requestor_assigned_sum
)
from golem.resource.resourcehandshake import ResourceHandshakeSessionMixin
from golem.task import exceptions
from golem.task.helpers import calculate_subtask_payment
Expand Down Expand Up @@ -791,6 +794,29 @@ def after_error():
self.port,
)

def _get_payment_value_and_budget(
self, rct: message.tasks.ReportComputedTask):
task_class = self._get_task_class(
rct.task_to_compute.want_to_compute_task.task_header)
market_strategy = task_class.PROVIDER_MARKET_STRATEGY
payment_value = market_strategy.calculate_payment(rct)
budget = market_strategy.calculate_budget(
rct.task_to_compute.want_to_compute_task)
return payment_value, budget

@staticmethod
def _adjust_requestor_assigned_sum(
requestor_id: str, payment_value: int, budget: int):
# because we have originally updated the requestor's assigned sum
# with the budget value, when we accepted the job
# now that we finally know what the actual payment amount is
# we need to subtract the difference
if payment_value < budget:
update_requestor_assigned_sum(
requestor_id,
payment_value - budget
)

@history.provider_history
def _react_to_subtask_results_accepted(
self, msg: message.tasks.SubtaskResultsAccepted):
Expand Down Expand Up @@ -819,10 +845,10 @@ def _react_to_subtask_results_accepted(
'ForceSubtaskResults',
)

task_class = self._get_task_class(
msg.task_to_compute.want_to_compute_task.task_header)
payment_value = task_class.PROVIDER_MARKET_STRATEGY\
.calculate_payment(msg.report_computed_task)
payment_value, budget = self._get_payment_value_and_budget(
msg.report_computed_task)
self._adjust_requestor_assigned_sum(
msg.requestor_id, payment_value, budget)

self.task_server.subtask_accepted(
sender_node_id=self.key_id,
Expand Down Expand Up @@ -858,6 +884,11 @@ def subtask_rejected():
subtask_id=subtask_id,
)

payment_value, budget = self._get_payment_value_and_budget(
msg.report_computed_task)
self._adjust_requestor_assigned_sum(
msg.requestor_id, payment_value, budget)

if msg.task_to_compute.concent_enabled:
self._handle_srr_with_concent_enabled(msg, subtask_rejected)
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/golem/ranking/manager/test_database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,33 @@ def test_should_throw_exception(self):
"""Should throw exception for WRONG_COMPUTED increase."""
with self.assertRaises(KeyError):
Trust.WRONG_COMPUTED.increase('alpha', 0.3)

def test_update_requestor_assigned_sum(self):
REQUESTOR_ID = 'c001d00d'
self.assertEqual(
dm.get_requestor_assigned_sum(REQUESTOR_ID),
0,
)
dm.update_requestor_assigned_sum(REQUESTOR_ID, 42)
self.assertEqual(
dm.get_requestor_assigned_sum(REQUESTOR_ID),
42,
)

def test_update_requestor_assigned_sum_subtract(self):
REQUESTOR_ID = 'c001d00d'
dm.update_requestor_assigned_sum(REQUESTOR_ID, 42)
self.assertEqual(
dm.get_requestor_assigned_sum(REQUESTOR_ID),
42,
)
dm.update_requestor_assigned_sum(REQUESTOR_ID, -22)
self.assertEqual(
dm.get_requestor_assigned_sum(REQUESTOR_ID),
20,
)
dm.update_requestor_assigned_sum(REQUESTOR_ID, -22)
self.assertEqual(
dm.get_requestor_assigned_sum(REQUESTOR_ID),
0,
)
57 changes: 55 additions & 2 deletions tests/golem/task/test_tasksession.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ def setUp(self):
self.conn = Mock()
self.conn.server.client.transaction_system.deposit_contract_address = \
EthereumConfig().deposit_contract_address
app = Mock()
app.builder.TASK_CLASS.\
PROVIDER_MARKET_STRATEGY = ProviderBrassMarketStrategy
self.conn.server.client.apps_manager.get_app_for_env = Mock(
return_value=app
)
self.task_session = TaskSession(self.conn)
self.peer_keys = cryptography.ECCx(None)
self.task_session.key_id = encode_hex(self.peer_keys.raw_pubkey)
Expand Down Expand Up @@ -584,6 +590,9 @@ def _get_srr(self, concent=False,
requestor_keys=requestor_keys,
provider_keys=provider_keys,
concent_enabled=concent,
want_to_compute_task__task_header__subtask_timeout=360,
want_to_compute_task__price=10,
price=1,
),
sign__privkey=provider_keys.raw_privkey,
)
Expand Down Expand Up @@ -646,6 +655,26 @@ def test_result_rejected(self, *_):
sender=ANY,
)

@patch(
'golem.marketplace.brass_marketplace.'
'ProviderBrassMarketStrategy.calculate_budget',
Mock(return_value=100)
)
@patch(
'golem.marketplace.brass_marketplace.'
'ProviderBrassMarketStrategy.calculate_payment',
Mock(return_value=75)
)
def test_budget_vs_payment_difference(self):
srr = self._get_srr()
with patch('golem.task.tasksession.update_requestor_assigned_sum') \
as sum_mock:
self.__call_react_to_srr(srr)
sum_mock.assert_called_with(
srr.requestor_id,
-25
)

def test_result_rejected_with_wrong_key(self, *_):
srr = self._get_srr()
self.task_session.key_id = encode_hex(
Expand Down Expand Up @@ -981,8 +1010,7 @@ def setUp(self):
self.provider_keys = cryptography.ECCx(None)
self.provider_key_id = encode_hex(self.provider_keys.raw_pubkey)

def test_react_to_subtask_results_accepted(self):
# given
def _get_sra(self):
rct = msg_factories.tasks.ReportComputedTaskFactory(**{
'task_to_compute__want_to_compute_task'
'__task_header__subtask_timeout': 360,
Expand All @@ -1008,6 +1036,11 @@ def test_react_to_subtask_results_accepted(self):
self.task_session.key_id = self.requestor_key_id
self.task_server.client.transaction_system.is_income_expected\
.return_value = False
return sra

def test_react_to_subtask_results_accepted(self):
# given
sra = self._get_sra()

dispatch_listener = Mock()
dispatcher.connect(dispatch_listener, signal='golem.message')
Expand Down Expand Up @@ -1037,6 +1070,26 @@ def test_react_to_subtask_results_accepted(self):
sender=ANY,
)

@patch(
'golem.marketplace.brass_marketplace.'
'ProviderBrassMarketStrategy.calculate_budget',
Mock(return_value=100)
)
@patch(
'golem.marketplace.brass_marketplace.'
'ProviderBrassMarketStrategy.calculate_payment',
Mock(return_value=75)
)
def test_budget_vs_payment_difference(self):
sra = self._get_sra()
with patch('golem.task.tasksession.update_requestor_assigned_sum') \
as sum_mock:
self.task_session._react_to_subtask_results_accepted(sra)
sum_mock.assert_called_with(
sra.requestor_id,
-25
)

def test_react_with_wrong_key(self):
# given
key_id = "CDEF"
Expand Down

0 comments on commit fcbe27c

Please # to comment.