-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from airalab/dev
refactoring
- Loading branch information
Showing
28 changed files
with
442 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .pinning_manager import PinningManager |
5 changes: 5 additions & 0 deletions
5
connectivity/src/feeders/pinning_services/gateways/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .crust import CrustGateway | ||
from .local import LocalGateway | ||
from .pinata import PinataGateway | ||
from .temporal import TemporalGateway | ||
from .pinning_gateway import PinArgs |
61 changes: 61 additions & 0 deletions
61
connectivity/src/feeders/pinning_services/gateways/crust.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import typing as tp | ||
import logging.config | ||
from crustinterface import Mainnet | ||
|
||
from connectivity.config.logging import LOGGING_CONFIG | ||
from .pinning_gateway import ( | ||
PinningGateway, | ||
PinArgs, | ||
) | ||
|
||
logging.config.dictConfig(LOGGING_CONFIG) | ||
logger = logging.getLogger("sensors-connectivity") | ||
|
||
|
||
class CrustGateway(PinningGateway): | ||
def __init__(self, seed: str) -> None: | ||
self.mainnet = Mainnet(seed=seed) | ||
|
||
|
||
def pin(self, args: PinArgs) -> None: | ||
file_hash: str = args.hash | ||
file_size: int = args.file_size | ||
if self._can_upload(file_size): | ||
try: | ||
logger.info( | ||
f"CrustGateway: Start adding {file_hash} to crust with size :{file_size}" | ||
) | ||
file_stored = self.mainnet.store_file(file_hash, file_size) | ||
logger.info( | ||
f"CrustGateway: File stored in Crust. Extrinsic data is: {file_stored}" | ||
) | ||
except Exception as e: | ||
logger.warning( | ||
f"CrustGateway: error while uploading file to crust: {e}" | ||
) | ||
return None | ||
else: | ||
logger.warning( | ||
f"CrustGateway: Not enough account balance to store the file in Crust Network" | ||
) | ||
|
||
def _can_upload(self, file_size: int) -> bool: | ||
"""Check whether there is enough tokens on balance""" | ||
balance = self._get_balance() | ||
approximately_price = self._get_store_price(file_size) | ||
return balance >= approximately_price | ||
|
||
def _get_balance(self) -> tp.Optional[int]: | ||
try: | ||
balance = self.mainnet.get_balance() | ||
logger.debug(f"CrustGateway: Actual balance in crust network: {balance}") | ||
return balance | ||
except Exception as e: | ||
logger.warning(f"CrustGateway: Error while getting account balance: {e}") | ||
return None | ||
|
||
def _get_store_price(self, file_size: int) -> tp.Optional[int]: | ||
"""Check price in Main net. Price in pCRUs""" | ||
price = self.mainnet.get_appx_store_price(int(file_size)) | ||
logger.debug(f"CrustGateway: Approximate cost to store the file: {price}") | ||
return price |
31 changes: 31 additions & 0 deletions
31
connectivity/src/feeders/pinning_services/gateways/local.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import typing as tp | ||
import logging.config | ||
import ipfshttpclient2 | ||
|
||
from connectivity.config.logging import LOGGING_CONFIG | ||
from .pinning_gateway import ( | ||
PinningGateway, | ||
PinArgs, | ||
) | ||
|
||
logging.config.dictConfig(LOGGING_CONFIG) | ||
logger = logging.getLogger("sensors-connectivity") | ||
|
||
|
||
class LocalGateway(PinningGateway): | ||
def __init__(self, endpoint: str) -> None: | ||
self.ipfs_endpoint = endpoint | ||
|
||
def pin(self, args: PinArgs) -> tp.Optional[tp.Tuple[str, int]]: | ||
file_path: str = args.file_path | ||
try: | ||
with ipfshttpclient2.connect(self.ipfs_endpoint) as client: | ||
response = client.add(file_path) | ||
file_hash = response["Hash"] | ||
file_size = response["Size"] | ||
logger.debug(f"LocalGateway: Hash, size: {file_hash}, {file_size}") | ||
return (file_hash, file_size) | ||
except Exception as e: | ||
logger.warning( | ||
f"LocalGateway: cou;dn't add file or connect to local gateway: {e}" | ||
) |
Oops, something went wrong.