Skip to content

Commit

Permalink
Fix nonce computation (#1429)
Browse files Browse the repository at this point in the history
* Add nonce endpoint. Used it in signing req.

* Fix circular imports.

* added fallback for null nonce.

* Cast to int.

* Tested only the failing test.

* Corrected command

* Run all tests.

* Fixed tests. Added condition for mock requests.

* Updated to milisecs.

* Deleted get_nonce.
  • Loading branch information
mariacarmina authored May 23, 2023
1 parent 81c99ee commit 975a184
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ocean_lib/data_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def set_http_client(http_client: Session) -> None:
@staticmethod
@enforce_types
def sign_message(wallet, msg: str) -> Tuple[str, str]:
nonce = str(datetime.now(timezone.utc).timestamp())
nonce = str(datetime.now(timezone.utc).timestamp() * 1000)
print(f"signing message with nonce {nonce}: {msg}, account={wallet.address}")

if isinstance(wallet, ClefAccount):
Expand Down
16 changes: 12 additions & 4 deletions ocean_lib/data_provider/data_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"""Provider module."""
import json
import logging
from datetime import datetime, timezone
from json import JSONDecodeError
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from unittest.mock import Mock

from enforce_typing import enforce_types
from requests.models import PreparedRequest, Response
Expand Down Expand Up @@ -162,7 +164,8 @@ def download(
for i in indexes:
payload["fileIndex"] = i
payload["nonce"], payload["signature"] = DataServiceProvider.sign_message(
consumer_wallet, did
consumer_wallet,
did,
)
response = DataServiceProvider._http_method(
method, url=download_endpoint, params=payload, stream=True, timeout=3
Expand Down Expand Up @@ -340,8 +343,10 @@ def compute_job_result(
:return: dict of job_id to result urls.
"""

nonce, signature = DataServiceProvider.sign_message(
consumer, f"{consumer.address}{job_id}{str(index)}"
consumer,
f"{consumer.address}{job_id}{str(index)}",
)

req = PreparedRequest()
Expand Down Expand Up @@ -409,8 +414,10 @@ def compute_job_result_logs(
def _send_compute_request(
http_method: str, did: str, job_id: str, service_endpoint: str, consumer
) -> Dict[str, Any]:

nonce, signature = DataServiceProvider.sign_message(
consumer, f"{consumer.address}{job_id}{did}"
consumer,
f"{consumer.address}{job_id}{did}",
)

req = PreparedRequest()
Expand Down Expand Up @@ -470,7 +477,8 @@ def _prepare_compute_payload(
), f"The received dataset does not have a {req_key}."

nonce, signature = DataServiceProvider.sign_message(
consumer, f"{consumer.address}{dataset.did}"
consumer,
f"{consumer.address}{dataset.did}",
)

payload = {
Expand Down
14 changes: 2 additions & 12 deletions ocean_lib/data_provider/test/test_data_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,27 +520,17 @@ def test_initialize_compute_failure():


@pytest.mark.unit
def test_job_result_failure():
def test_job_result_failure(consumer_wallet):
"""Tests compute job result failures."""
service = Mock(spec=Service)
service.service_endpoint = "http://172.15.0.4:8030"
service.id = "abc"

wallet = Mock()
wallet.address = "none"
wallet.private_key = os.getenv("TEST_PRIVATE_KEY1")

http_client = HttpClientEvilMock()
DataSP.set_http_client(http_client)

with pytest.raises(DataProviderException):
DataSP.compute_job_result("0xabc", 0, service, wallet)

http_client = HttpClientEmptyMock()
DataSP.set_http_client(http_client)

with pytest.raises(DataProviderException):
DataSP.compute_job_result("0xabc", 0, service, wallet)
DataSP.compute_job_result("0xabc", 0, service, consumer_wallet)

DataSP.set_http_client(get_requests_session())

Expand Down

0 comments on commit 975a184

Please sign in to comment.