Skip to content

Commit

Permalink
moved mpe_channel_command, mpe_service_metadata and contract to share…
Browse files Browse the repository at this point in the history
…d package
  • Loading branch information
vforvalerio87 committed Jun 25, 2019
1 parent 22ed5db commit ac4fa63
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 27 deletions.
22 changes: 12 additions & 10 deletions packages/sdk/snet/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from urllib.parse import urljoin

import web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.gas_strategies.time_based import medium_gas_price_strategy
from rfc3986 import urlparse
import ipfsapi
from web3.datastructures import AttributeDict
Expand All @@ -14,6 +14,8 @@
from snet.sdk.payment_channel_management_strategies.default import PaymentChannelManagementStrategy

from snet.snet_cli.utils import get_contract_object
from snet.snet_cli.utils_ipfs import bytesuri_to_hash, get_from_ipfs_and_checkhash
from snet.snet_cli.mpe_service_metadata import mpe_service_metadata_from_json


class SnetSDK:
Expand All @@ -28,7 +30,7 @@ def __init__(
eth_rpc_endpoint = self._config.get("eth_rpc_endpoint", "https://mainnet.infura.io")
provider = web3.HTTPProvider(eth_rpc_endpoint)
self.web3 = web3.Web3(provider)
self.web3.eth.setGasPriceStrategy(rpc_gas_price_strategy)
self.web3.eth.setGasPriceStrategy(medium_gas_price_strategy)

self.mpe_contract = MPEContract(self.web3)

Expand All @@ -43,20 +45,20 @@ def __init__(
self.account = Account(self.web3, config, self.mpe_contract)


def create_service_client(self, org_id, service_id, service_stub, group_name="default_group", payment_channel_management_strategy=PaymentChannelManagementStrategy, options=None):
def create_service_client(self, org_id, service_id, service_stub, group_name=None, payment_channel_management_strategy=PaymentChannelManagementStrategy, options=None):
if options is None:
options = dict()

service_metadata = self.service_metadata(org_id, service_id)
try:
group = next(filter(lambda group: group["group_name"] == group_name, service_metadata.groups))
except StopIteration:
raise ValueError("Group[name: {}] not found for orgId: {} and serviceId: {}".format(group_name, org_id, service_id))
service_metadata = self.get_service_metadata(org_id, service_id)
group = service_metadata.get_group(group_name)
strategy = payment_channel_management_strategy(self)
service_client = ServiceClient(self, service_metadata, group, service_stub, strategy, options)
return service_client

def service_metadata(self, org_id, service_id):

def get_service_metadata(self, org_id, service_id):
(found, registration_id, metadata_uri, tags) = self.registry_contract.functions.getServiceRegistrationById(bytes(org_id, "utf-8"), bytes(service_id, "utf-8")).call()
metadata = AttributeDict(json.loads(self.ipfs_client.cat(metadata_uri.rstrip(b"\0").decode('ascii')[7:])))
metadata_hash = bytesuri_to_hash(metadata_uri)
metadata_json = get_from_ipfs_and_checkhash(self.ipfs_client, metadata_hash)
metadata = mpe_service_metadata_from_json(metadata_json)
return metadata
2 changes: 1 addition & 1 deletion packages/sdk/snet/sdk/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _get_gas_price(self):
def _send_signed_transaction(self, contract_fn, *args):
transaction = contract_fn(*args).buildTransaction({
"chainId": int(self.web3.version.network),
"gas": DEFAULT_GAS,
"gas": DEFAULT_GAS,
"gasPrice": self._get_gas_price(),
"nonce": self._get_nonce()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def select_channel(self, service_client):
service_client.load_open_channels()
service_client.update_channel_states()
payment_channels = service_client.payment_channels
service_call_price = service_client.metadata.pricing["price_in_cogs"]
service_call_price = service_client.metadata["pricing"]["price_in_cogs"]
mpe_balance = account.escrow_balance()
default_expiration = service_client.default_channel_expiration()

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/snet/sdk/service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, sdk, metadata, group, service_stub, payment_channel_managemen
self.group = group
self.metadata = metadata
self.payment_channel_management_strategy = payment_channel_management_strategy
self.expiry_threshold = self.metadata.payment_expiration_threshold
self.expiry_threshold = self.metadata["payment_expiration_threshold"]
self._base_grpc_channel = self._get_grpc_channel()
self.grpc_channel = grpc.intercept_channel(self._base_grpc_channel, generic_client_interceptor.create(self._intercept_call))
self.payment_channel_state_service_client = self._generate_payment_channel_state_service_client()
Expand All @@ -46,7 +46,7 @@ def _generate_grpc_stub(self, service_stub):
def _get_grpc_channel(self):
endpoint = self.options.get("endpoint", None)
if endpoint is None:
endpoint = next(filter(lambda endpoint: endpoint["group_name"] == self.group["group_name"], self.metadata["endpoints"]))["endpoint"]
endpoint = self.metadata.get_endpoints_for_group(self.group["group_name"])[0]
endpoint_object = urlparse(endpoint)
if endpoint_object.port is not None:
channel_endpoint = endpoint_object.hostname + ":" + str(endpoint_object.port)
Expand All @@ -63,7 +63,7 @@ def _get_grpc_channel(self):

def _get_service_call_metadata(self):
channel = self.payment_channel_management_strategy.select_channel(self)
amount = channel.state["last_signed_amount"] + int(self.metadata.pricing["price_in_cogs"])
amount = channel.state["last_signed_amount"] + int(self.metadata["pricing"]["price_in_cogs"])
message = web3.Web3.soliditySha3(
["address", "uint256", "uint256", "uint256"],
[self.sdk.mpe_contract.contract.address, channel.channel_id, channel.state["nonce"], amount]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from web3.utils.events import get_event_data

from snet_cli.mpe_service_command import MPEServiceCommand
from snet_cli.mpe_service_metadata import load_mpe_service_metadata
from snet_cli.utils_ipfs import safe_extract_proto_from_ipfs
from snet_cli.utils_agi2cogs import cogs2stragi

from snet.snet_cli.utils_ipfs import safe_extract_proto_from_ipfs
from snet.snet_cli.mpe_service_metadata import load_mpe_service_metadata
from snet.snet_cli.utils import compile_proto, get_contract_def, abi_get_element_by_name, abi_decode_struct_to_dict


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/snet_cli/snet_cli/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from snet_cli.identity import get_identity_types
from snet_cli.mpe_account_command import MPEAccountCommand
from snet_cli.mpe_service_command import MPEServiceCommand
from snet_cli.mpe_channel_command import MPEChannelCommand
from snet_cli.mpe_client_command import MPEClientCommand
from snet_cli.mpe_treasurer_command import MPETreasurerCommand
from snet_cli.sdk_command import SDKCommand
from snet_cli.utils_agi2cogs import stragi2cogs
from snet_cli.config import Config, get_session_keys, get_session_network_keys_removable

from snet.snet_cli.mpe_channel_command import MPEChannelCommand
from snet.snet_cli.utils import type_converter, get_contract_def, RESOURCES_PATH


Expand Down
2 changes: 1 addition & 1 deletion packages/snet_cli/snet_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from snet_cli.utils_config import get_contract_address, get_field_from_args_or_session, read_default_contract_address
from snet_cli.identity import RpcIdentityProvider, MnemonicIdentityProvider, TrezorIdentityProvider, \
LedgerIdentityProvider, KeyIdentityProvider, KeyStoreIdentityProvider
from snet_cli.contract import Contract
from snet_cli.identity import get_kws_for_identity_type

from snet.snet_cli.contract import Contract
from snet.snet_cli.utils import DefaultAttributeObject, get_web3, serializable, type_converter, get_contract_def, get_cli_version, bytes32_to_str


Expand Down
2 changes: 1 addition & 1 deletion packages/snet_cli/snet_cli/mpe_client_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import grpc
from eth_account.messages import defunct_hash_message

from snet_cli.mpe_channel_command import MPEChannelCommand
from snet_cli.utils_agi2cogs import cogs2stragi

from snet.snet_cli.utils import open_grpc_channel, rgetattr, compile_proto, RESOURCES_PATH
from snet.snet_cli.mpe_channel_command import MPEChannelCommand
from snet.snet_cli.utils_proto import import_protobuf_from_dir, switch_to_json_payload_encoding


Expand Down
6 changes: 3 additions & 3 deletions packages/snet_cli/snet_cli/mpe_service_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from grpc_health.v1 import health_pb2_grpc as heartb_pb2_grpc

from snet_cli.commands import BlockchainCommand
import snet_cli.utils_ipfs as utils_ipfs
from snet_cli.mpe_service_metadata import MPEServiceMetadata, load_mpe_service_metadata, mpe_service_metadata_from_json
from snet_cli.utils_ipfs import hash_to_bytesuri, bytesuri_to_hash, get_from_ipfs_and_checkhash, safe_extract_proto_from_ipfs

import snet.snet_cli.utils_ipfs as utils_ipfs
from snet.snet_cli.utils_ipfs import hash_to_bytesuri, bytesuri_to_hash, get_from_ipfs_and_checkhash, safe_extract_proto_from_ipfs
from snet.snet_cli.mpe_service_metadata import MPEServiceMetadata, load_mpe_service_metadata, mpe_service_metadata_from_json
from snet.snet_cli.utils import type_converter, bytes32_to_str, open_grpc_channel

class MPEServiceCommand(BlockchainCommand):
Expand Down
3 changes: 1 addition & 2 deletions packages/snet_cli/snet_cli/sdk_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from tempfile import TemporaryDirectory

from snet_cli.mpe_service_command import MPEServiceCommand
from snet_cli.utils_ipfs import bytesuri_to_hash, get_from_ipfs_and_checkhash, safe_extract_proto_from_ipfs
from snet_cli.mpe_service_metadata import mpe_service_metadata_from_json

from snet.snet_cli.utils_ipfs import bytesuri_to_hash, get_from_ipfs_and_checkhash, safe_extract_proto_from_ipfs
from snet.snet_cli.utils import type_converter, bytes32_to_str, compile_proto

class SDKCommand(MPEServiceCommand):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import time
import web3

from snet_cli.mpe_channel_command import MPEChannelCommand
from snet_cli.config import Config

from snet.snet_cli.mpe_channel_command import MPEChannelCommand
from snet.snet_cli.utils import compile_proto, DefaultAttributeObject


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from snet_cli.mpe_service_metadata import MPEServiceMetadata

from snet.snet_cli.mpe_service_metadata import MPEServiceMetadata

class TestStringMethods(unittest.TestCase):

Expand Down

0 comments on commit ac4fa63

Please sign in to comment.