Skip to content

Commit 0aa8ce8

Browse files
committed
Updated free call logic, signatures and user interface
1 parent fc53dc7 commit 0aa8ce8

5 files changed

Lines changed: 32 additions & 21 deletions

File tree

snet/sdk/concurrency_manager.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import grpc
44
import web3
55

6-
from snet.sdk.service_client import ServiceClient
76
from snet.sdk.utils.utils import RESOURCES_PATH, add_to_path
87

98

109
class ConcurrencyManager:
11-
def __init__(self, concurrent_calls: int):
10+
def __init__(self, concurrent_calls: int=1):
1211
self.__concurrent_calls: int = concurrent_calls
1312
self.__token: str = ''
1413
self.__planned_amount: int = 0
@@ -18,14 +17,18 @@ def __init__(self, concurrent_calls: int):
1817
def concurrent_calls(self) -> int:
1918
return self.__concurrent_calls
2019

20+
@concurrent_calls.setter
21+
def concurrent_calls(self, concurrent_calls: int):
22+
self.__concurrent_calls = concurrent_calls
23+
2124
def get_token(self, service_client, channel, service_call_price):
2225
if len(self.__token) == 0:
2326
self.__token = self.__get_token(service_client, channel, service_call_price)
2427
elif self.__used_amount >= self.__planned_amount:
2528
self.__token = self.__get_token(service_client, channel, service_call_price, new_token=True)
2629
return self.__token
2730

28-
def __get_token(self, service_client: ServiceClient, channel, service_call_price, new_token=False):
31+
def __get_token(self, service_client, channel, service_call_price, new_token=False):
2932
if not new_token:
3033
amount = channel.state["last_signed_amount"]
3134
if amount != 0:
@@ -47,13 +50,13 @@ def __get_token(self, service_client: ServiceClient, channel, service_call_price
4750
self.__planned_amount = token_reply.planned_amount
4851
return token_reply.token
4952

50-
def __get_stub_for_get_token(self, service_client: ServiceClient):
53+
def __get_stub_for_get_token(self, service_client):
5154
grpc_channel = service_client.get_grpc_base_channel()
5255
with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
5356
token_service_pb2_grpc = importlib.import_module("token_service_pb2_grpc")
5457
return token_service_pb2_grpc.TokenServiceStub(grpc_channel)
5558

56-
def __get_token_for_amount(self, service_client: ServiceClient, channel, amount):
59+
def __get_token_for_amount(self, service_client, channel, amount):
5760
nonce = channel.state["nonce"]
5861
stub = self.__get_stub_for_get_token(service_client)
5962
with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):

snet/sdk/payment_strategies/default_payment_strategy.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from snet.sdk.concurrency_manager import ConcurrencyManager
21
from snet.sdk.payment_strategies.freecall_payment_strategy import FreeCallPaymentStrategy
32
from snet.sdk.payment_strategies.paidcall_payment_strategy import PaidCallPaymentStrategy
43
from snet.sdk.payment_strategies.prepaid_payment_strategy import PrePaidPaymentStrategy
@@ -16,13 +15,13 @@ def set_channel(self, channel):
1615
def get_payment_metadata(self, service_client):
1716
free_call_payment_strategy = FreeCallPaymentStrategy()
1817

19-
if free_call_payment_strategy.is_free_call_available(service_client):
18+
if free_call_payment_strategy.get_free_calls_available(service_client) > 0:
2019
metadata = free_call_payment_strategy.get_payment_metadata(service_client)
2120
else:
2221
if service_client.get_concurrency_flag():
2322
concurrent_calls = service_client.get_concurrent_calls()
2423
payment_strategy = PrePaidPaymentStrategy(concurrent_calls)
25-
metadata = payment_strategy.get_payment_metadata(service_client, self.channel)
24+
metadata = payment_strategy.get_payment_metadata(service_client)
2625
else:
2726
payment_strategy = PaidCallPaymentStrategy()
2827
metadata = payment_strategy.get_payment_metadata(service_client)

snet/sdk/payment_strategies/freecall_payment_strategy.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ def __init__(self):
1212
self._user_address = None
1313
self._free_call_token = None
1414
self._token_expiration_block = None
15-
self._free_calls_available = None
1615

17-
def is_free_call_available(self, service_client) -> bool:
16+
def get_free_calls_available(self, service_client) -> int:
1817
if not self._user_address:
1918
self._user_address = service_client.account.signer_address
2019

@@ -31,29 +30,27 @@ def is_free_call_available(self, service_client) -> bool:
3130
with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
3231
state_service_pb2_grpc = importlib.import_module("state_service_pb2_grpc")
3332

34-
signature = self.generate_signature(service_client, current_block_number)
33+
signature, _ = self.generate_signature(service_client, current_block_number)
3534
request = state_service_pb2.FreeCallStateRequest(
3635
address=self._user_address,
3736
free_call_token=self._free_call_token,
3837
signature=signature,
3938
current_block=current_block_number
4039
)
4140

42-
channel = service_client._get_grpc_channel()
41+
channel = service_client.get_grpc_base_channel()
4342
stub = state_service_pb2_grpc.FreeCallStateServiceStub(channel)
4443

4544
try:
4645
response = stub.GetFreeCallsAvailable(request)
47-
if response.free_calls_available > 0:
48-
return True
49-
return False
46+
return response.free_calls_available
5047
except grpc.RpcError as e:
5148
if self._user_address:
5249
print(f"Warning: {e.details()}")
53-
return False
50+
return 0
5451

5552
def get_payment_metadata(self, service_client) -> list:
56-
if self.is_free_call_available(service_client):
53+
if self.get_free_calls_available(service_client) <= 0:
5754
raise Exception(f"Free calls limit for address {self._user_address} has expired. Please use another payment strategy")
5855
signature, current_block_number = self.generate_signature(service_client)
5956
metadata = [("snet-free-call-auth-token-bin", self._free_call_token),
@@ -96,7 +93,7 @@ def get_free_call_token_details(self, service_client, current_block_number=None)
9693
with add_to_path(str(RESOURCES_PATH.joinpath("proto"))):
9794
state_service_pb2_grpc = importlib.import_module("state_service_pb2_grpc")
9895

99-
channel = service_client._get_grpc_channel()
96+
channel = service_client.get_grpc_base_channel()
10097
stub = state_service_pb2_grpc.FreeCallStateServiceStub(channel)
10198
response = stub.GetFreeCallToken(request)
10299

snet/sdk/payment_strategies/prepaid_payment_strategy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
class PrePaidPaymentStrategy(PaymentStrategy):
66

7-
def __init__(self, concurrent_calls: int, block_offset: int = 240, call_allowance: int = 1):
7+
def __init__(self, concurrent_calls: int=1, block_offset: int = 240, call_allowance: int = 1):
88
self.concurrency_manager = ConcurrencyManager(concurrent_calls)
99
self.block_offset = block_offset
1010
self.call_allowance = call_allowance
1111

1212
def get_price(self, service_client):
1313
return service_client.get_price() * self.concurrency_manager.concurrent_calls
1414

15+
def set_concurrent_calls(self, concurrent_calls):
16+
self.concurrency_manager.concurrent_calls = concurrent_calls
17+
1518
def get_payment_metadata(self, service_client):
1619
channel = self.select_channel(service_client)
1720
token = self.concurrency_manager.get_token(service_client, channel, self.get_price(service_client))

snet/sdk/service_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from eth_account.messages import defunct_hash_message
1313
from rfc3986 import urlparse
1414

15-
from snet.sdk import generic_client_interceptor
15+
from snet.sdk import generic_client_interceptor, FreeCallPaymentStrategy
1616
from snet.sdk.account import Account
1717
from snet.sdk.mpe.mpe_contract import MPEContract
1818
from snet.sdk.mpe.payment_channel import PaymentChannel
1919
from snet.sdk.mpe.payment_channel_provider import PaymentChannelProvider
20-
from snet.sdk.payment_strategies import default_payment_strategy as strategy
20+
from snet.sdk.payment_strategies.prepaid_payment_strategy import PrePaidPaymentStrategy
2121
from snet.sdk.resources.root_certificate import certificate
2222
from snet.sdk.storage_provider.service_metadata import MPEServiceMetadata
2323
from snet.sdk.custom_typing import ModuleName, ServiceStub
@@ -51,6 +51,8 @@ def __init__(
5151
self.service_metadata = service_metadata
5252
self.group = group
5353
self.payment_strategy = payment_strategy
54+
if isinstance(payment_strategy, PrePaidPaymentStrategy):
55+
self.payment_strategy.set_concurrent_calls(options["concurrent_calls"])
5456
self.options = options
5557
self.mpe_address = mpe_contract.contract.address
5658
self.account = account
@@ -322,3 +324,10 @@ def get_services_and_messages_info_as_pretty_string(self) -> str:
322324
for field_type, field_name in fields:
323325
string_output += f" Field: {field_type} {field_name}\n"
324326
return string_output
327+
328+
def get_free_calls_available(self) -> int:
329+
payment_strategy = self.payment_strategy
330+
if not isinstance(payment_strategy, FreeCallPaymentStrategy):
331+
payment_strategy = FreeCallPaymentStrategy()
332+
333+
return payment_strategy.get_free_calls_available(self)

0 commit comments

Comments
 (0)