Skip to content

Commit

Permalink
Changed cache directory. Also, now cache is updated only when accessi…
Browse files Browse the repository at this point in the history
…ng channels.
  • Loading branch information
Arondondon committed Sep 27, 2024
1 parent 12a2165 commit 4a749c6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
1 change: 0 additions & 1 deletion snet/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __init__(self, sdk_config: Config, metadata_provider=None):

self.account = Account(self.web3, sdk_config, self.mpe_contract)
self.payment_channel_provider = PaymentChannelProvider(self.web3, self.mpe_contract)
self.payment_channel_provider.update_cache()

def create_service_client(self, org_id: str, service_id: str, group_name=None,
payment_channel_management_strategy=None,
Expand Down
10 changes: 4 additions & 6 deletions snet/sdk/mpe/payment_channel_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


BLOCKS_PER_BATCH = 5000
CHANNELS_DIR = Path.home().joinpath(".snet", "channels")
CHANNELS_DIR = Path.home().joinpath(".snet", "cache", "mpe")


class PaymentChannelProvider(object):
Expand All @@ -28,6 +28,7 @@ def update_cache(self):
last_read_block = self.deployment_block

if not self.channels_file.exists():
print(f"Channels cache is empty. Caching may take some time when first accessing channels.\nCaching in progress...")
self.channels_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.channels_file, "wb") as f:
empty_dict = {
Expand Down Expand Up @@ -109,18 +110,15 @@ def get_past_open_channels(self, account, payment_address, group_id, payment_cha

def open_channel(self, account, amount, expiration, payment_address, group_id, payment_channel_state_service_client):
receipt = self.mpe_contract.open_channel(account, payment_address, group_id, amount, expiration)
self.update_cache()
return self._get_newly_opened_channel(account, payment_address, group_id, receipt, payment_channel_state_service_client)

def deposit_and_open_channel(self, account, amount, expiration, payment_address, group_id, payment_channel_state_service_client):
receipt = self.mpe_contract.deposit_and_open_channel(account, payment_address, group_id, amount, expiration)
self.update_cache()
return self._get_newly_opened_channel(account, payment_address, group_id, receipt, payment_channel_state_service_client)

def _get_newly_opened_channel(self, account, payment_address, group_id, receipt, payment_channel_state_service_client):
open_channels = self.get_past_open_channels(account, payment_address, group_id, payment_channel_state_service_client)
n_channels = len(open_channels)
if n_channels == 0:
if not open_channels:
raise Exception(f"Error while opening channel, please check transaction {receipt.transactionHash.hex()} ")
return open_channels[n_channels - 1]
return open_channels[-1]

0 comments on commit 4a749c6

Please sign in to comment.