Skip to content

Commit

Permalink
feat: use context manager for the vega wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel1302 committed Sep 13, 2023
1 parent 901cf27 commit 6c69268
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions vega_sim/wallet/vega_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,12 @@ def get_keypairs(self, wallet_name: str) -> dict:

url = f"{self.wallet_url}/api/v2/requests"

self._lock()
response = requests.post(url, headers=headers, json=submission)
response = None
if self._mutex is None:
response = requests.post(url, headers=headers, json=submission)
else:
with self._mutex:
response = requests.post(url, headers=headers, json=submission)
self._release()

try:
Expand Down Expand Up @@ -292,10 +296,15 @@ def submit_transaction(
"id": "request",
}
url = f"{self.wallet_url}/api/v2/requests"


response = None
if self._mutex is None:
response = requests.post(url, headers=headers, json=submission)
else:
with self._mutex:
response = requests.post(url, headers=headers, json=submission)

self._lock()
response = requests.post(url, headers=headers, json=submission)
self._release()
try:
response.raise_for_status()
except Exception as e:
Expand Down Expand Up @@ -324,20 +333,3 @@ def public_key(self, name: str, wallet_name: Optional[str] = None) -> str:
)

return self.pub_keys[wallet_name][name]

def _lock(self):
"""
Lock if mutex is available
"""
if self._mutex is None:
return
self._mutex.acquire()

def _release(self):
"""
Release the lock if mutex is available
"""
if self._mutex is None:
return

self._mutex.release()

0 comments on commit 6c69268

Please sign in to comment.