def register_compute_asset(): # get ocean instance config = ExampleConfig.get_config() ConfigProvider.set_config(config) ocean = Ocean() keeper = Keeper.get_instance() consumer_account = get_account(0) publisher_account = get_account(1) # get descriptor for compute service compute_descriptor = build_compute_descriptor(ocean) # create an asset with compute service ddo = ocean.assets.create(example_metadata.metadata, publisher_account, providers=[config.provider_address], use_secret_store=False, service_descriptors=[compute_descriptor]) event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 15, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': publisher_account.address }, wait=True) assert event, 'There was a problem registering an asset' logging.info(f'Registered asset: did={ddo.did}') # buy an asset access agreement_id = ocean.compute.order(ddo.did, consumer_account) event = keeper.compute_execution_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True) assert event, 'There was a problem creating an agreement' logging.info(f'Created asset agreement: agreement_id={agreement_id}') # supply metadata describing the algorithm to run against the dataset # you can also use an algorithm published as an Ocean asset instead algo_meta = AlgorithmMetadata(example_metadata.algo_metadata) # whether to publish the algorithm results as an Ocean assets output_dict = { 'publishOutput': False, 'publishAlgorithmLog': False, } # start the compute job job_id = ocean.compute.start(agreement_id, consumer_account, algorithm_meta=algo_meta, output=output_dict) logging.info(f'Started compute job: job_id={job_id}') # query operator service for job status status = ocean.compute.status(agreement_id, job_id, consumer_account) logging.info(f'Job status: {status}')
def _make_ocean_agreements(self): return OceanAgreements( self._keeper, self._did_resolver, AssetConsumer, ConfigProvider.get_config() )
def get_contract_dict_by_name(contract_name): """ Retrieve the Contract instance for a given contract name. :param contract_name: str :return: the smart contract's definition from the json abi file, dict """ network_name = Keeper.get_network_name(Keeper.get_network_id()).lower() artifacts_path = ConfigProvider.get_config().keeper_path # file_name = '{}.{}.json'.format(contract_name, network_name) # path = os.path.join(keeper.artifacts_path, file_name) path = ContractHandler._get_contract_file_path(artifacts_path, contract_name, network_name) if not (path and os.path.exists(path)): path = ContractHandler._get_contract_file_path( artifacts_path, contract_name, network_name.lower()) if not (path and os.path.exists(path)): path = ContractHandler._get_contract_file_path( artifacts_path, contract_name, Keeper.DEFAULT_NETWORK_NAME) if not (path and os.path.exists(path)): raise FileNotFoundError(f'Keeper contract {contract_name} file ' f'not found in {artifacts_path} ' f'using network name {network_name}') with open(path) as f: contract_dict = json.loads(f.read()) return contract_dict
def register_asset(): # make ocean instance ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create(Metadata.get_example(), account) sleep(ASYNC_DELAY) logging.info( f'Registered asset: did={ddo.did}, ddo-services={ddo.services}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' )
def _get_contract_file_path(_base_path, _contract_name, _network_name): contract_file_name = '{}.{}.json'.format(_contract_name, _network_name) for name in os.listdir(_base_path): if name.lower() == contract_file_name.lower(): contract_file_name = name return os.path.join(ConfigProvider.get_config().keeper_path, contract_file_name) return None
def register_asset(): # make ocean instance ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) # account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( example_metadata.metadata, account, providers=['0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095']) sleep(ASYNC_DELAY) logging.info( f'Registered asset: did={ddo.did}, ddo-services={ddo.services}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' )
def verify_contracts(): """ Verify that the contracts are deployed correctly in the network. :raise Exception: raise exception if the contracts are not deployed correctly. """ artifacts_path = ConfigProvider.get_config().keeper_path logger.info( f'Keeper contract artifacts (JSON abi files) at: {artifacts_path}') if os.environ.get('KEEPER_NETWORK_NAME'): logger.warning( f'The `KEEPER_NETWORK_NAME` env var is set to ' f'{os.environ.get("KEEPER_NETWORK_NAME")}. ' f'This enables the user to override the method of how the network name ' f'is inferred from network id.') # try to find contract with this network name contract_name = Diagnostics.TEST_CONTRACT_NAME network_id = Keeper.get_network_id() network_name = Keeper.get_network_name(network_id) logger.info(f'Using keeper contracts from network {network_name}, ' f'network id is {network_id}') logger.info( f'Looking for keeper contracts ending with ".{network_name}.json", ' f'e.g. "{contract_name}.{network_name}.json".') existing_contract_names = os.listdir(artifacts_path) try: ContractHandler.get(contract_name) except Exception as e: logger.error(e) logger.error( f'Cannot find the keeper contracts. \n' f'Current network id is {network_id} and network name is {network_name}.' f'Expected to find contracts ending with ".{network_name}.json",' f' e.g. "{contract_name}.{network_name}.json"') raise OceanKeeperContractsNotFound( f'Keeper contracts for keeper network {network_name} were not found ' f'in {artifacts_path}. \n' f'Found the following contracts: \n\t{existing_contract_names}' ) keeper = Keeper.get_instance() contracts = [ keeper.dispenser, keeper.token, keeper.did_registry, keeper.agreement_manager, keeper.template_manager, keeper.condition_manager, keeper.access_secret_store_condition, keeper.sign_condition, keeper.lock_reward_condition, keeper.escrow_access_secretstore_template, keeper.escrow_reward_condition, keeper.hash_lock_condition ] addresses = '\n'.join([f'\t{c.name}: {c.address}' for c in contracts]) logging.info('Finished loading keeper contracts:\n' '%s', addresses)
def __init__(self): self.network_name = Keeper.get_network_name(Keeper.get_network_id()) self.artifacts_path = ConfigProvider.get_config().keeper_path self.accounts = Web3Provider.get_web3().eth.accounts self.dispenser = Dispenser.get_instance() self.token = Token.get_instance() self.did_registry = DIDRegistry.get_instance() self.template_manager = TemplateStoreManager.get_instance() self.escrow_access_secretstore_template = EscrowAccessSecretStoreTemplate.get_instance() self.agreement_manager = AgreementStoreManager.get_instance() self.condition_manager = ConditionStoreManager.get_instance() self.sign_condition = SignCondition.get_instance() self.lock_reward_condition = LockRewardCondition.get_instance() self.escrow_reward_condition = EscrowRewardCondition.get_instance() self.access_secret_store_condition = AccessSecretStoreCondition.get_instance() self.hash_lock_condition = HashLockCondition.get_instance()
def get_web3(): """Return the web3 instance to interact with the ethereum client.""" if Web3Provider._web3 is None: config = ConfigProvider.get_config() provider = config.web3_provider if config.web3_provider else HTTPProvider( config.keeper_url) Web3Provider._web3 = Web3(provider) # Reset attributes to avoid lint issue about no attribute Web3Provider._web3.eth = getattr(Web3Provider._web3, 'eth') Web3Provider._web3.net = getattr(Web3Provider._web3, 'net') Web3Provider._web3.personal = getattr(Web3Provider._web3, 'personal') Web3Provider._web3.version = getattr(Web3Provider._web3, 'version') Web3Provider._web3.txpool = getattr(Web3Provider._web3, 'txpool') Web3Provider._web3.miner = getattr(Web3Provider._web3, 'miner') Web3Provider._web3.admin = getattr(Web3Provider._web3, 'admin') Web3Provider._web3.parity = getattr(Web3Provider._web3, 'parity') Web3Provider._web3.testing = getattr(Web3Provider._web3, 'testing') return Web3Provider._web3
def __init__(self, config=None): """ Initialize Ocean class. >> # Make a new Ocean instance >> ocean = Ocean({...}) This class provides the main top-level functions in ocean protocol: * Publish assets metadata and associated services * Each asset is assigned a unique DID and a DID Document (DDO) * The DDO contains the asset's services including the metadata * The DID is registered on-chain with a URL of the metadata store to retrieve the DDO from >> ddo = ocean.assets.create(metadata, publisher_account) * Discover/Search assets via the current configured metadata store (Aquarius) >> assets_list = ocean.assets.search('search text') * Purchase asset services by choosing a service agreement from the asset's DDO. Purchase goes through the service agreements interface and starts by signing a service agreement then sending the signature to the publisher's Brizo server via the `purchaseEndpoint` in the service definition: >> service_def_id = ddo.get_service(ServiceTypes.ASSET_ACCESS).service_definition_id >> service_agreement_id = ocean.assets.order(did, service_def_id, consumer_account) An instance of Ocean is parameterized by a `Config` instance. :param config: Config instance """ # Configuration information for the market is stored in the Config class # config = Config(filename=config_file, options_dict=config_dict) if config: ConfigProvider.set_config(config) self._config = ConfigProvider.get_config() self._keeper = Keeper.get_instance() self._did_resolver = DIDResolver(self._keeper.did_registry) # Initialize the public sub-modules self.tokens = OceanTokens(self._keeper) self.accounts = OceanAccounts(self._keeper, self._config, self.tokens) self.secret_store = OceanSecretStore(self._config) self.templates = OceanTemplates( self._keeper, ConfigProvider.get_config() ) self.agreements = self._make_ocean_agreements() self.assets = OceanAssets( self._keeper, self._did_resolver, self._make_ocean_agreements(), AssetConsumer, self._config ) self.services = OceanServices() self.ocean_providers = OceanProviders(self._keeper, self._did_resolver, self._config) # Verify keeper contracts Diagnostics.verify_contracts() # Diagnostics.check_deployed_agreement_templates() logger.info('Squid Ocean instance initialized: ') logger.info(f'\tOther accounts: {sorted([a.address for a in self.accounts.list()])}') # logger.info(f'\taquarius: {self._aquarius.url}') logger.info(f'\tDIDRegistry @ {self._keeper.did_registry.address}')
"""Test Token Contract.""" # Copyright 2018 Ocean Protocol Foundation # SPDX-License-Identifier: Apache-2.0 import pytest from squid_py.config_provider import ConfigProvider from squid_py.keeper import Keeper from squid_py.keeper.token import Token from tests.resources.helper_functions import get_consumer_account, get_publisher_account from tests.resources.tiers import e2e_test token = Token('OceanToken') consumer_account = get_consumer_account(ConfigProvider.get_config()) publisher_account = get_publisher_account(ConfigProvider.get_config()) @e2e_test def test_token_contract(): assert token assert isinstance(token, Token) @e2e_test def test_get_balance(): assert isinstance(token.get_token_balance(consumer_account.address), int) @e2e_test def test_get_balance_invalid_address(): with pytest.raises(Exception):
def __init__(self, config=None): """ Initialize Ocean class. >> # Make a new Ocean instance >> ocean = Ocean({...}) This class provides the main top-level functions in ocean protocol: * Publish assets metadata and associated services * Each asset is assigned a unique DID and a DID Document (DDO) * The DDO contains the asset's services including the metadata * The DID is registered on-chain with a URL of the metadata store to retrieve the DDO from >> ddo = ocean.assets.create(metadata, publisher_account) * Discover/Search assets via the current configured metadata store (Aquarius) >> assets_list = ocean.assets.search('search text') * Purchase asset services by choosing a service agreement from the asset's DDO. Purchase goes through the service agreements interface and starts by signing a service agreement then sending the signature to the publisher's Brizo server via the `purchaseEndpoint` in the service definition: >> service_def_id = ddo.get_service(ServiceTypes.ASSET_ACCESS).service_definition_id >> service_agreement_id = ocean.assets.order(did, service_def_id, consumer_account) An instance of Ocean is parameterized by a `Config` instance. :param config: Config instance """ # Configuration information for the market is stored in the Config class # config = Config(filename=config_file, options_dict=config_dict) if not config: config = ConfigProvider.get_config() self._config = config self._web3 = Web3Provider.get_web3(self._config.keeper_url) ContractHandler.set_artifacts_path(self._config.keeper_path) contracts = [ 'DIDRegistry', 'Dispenser', 'TemplateStoreManager', 'OceanToken', 'ConditionStoreManager', 'EscrowAccessSecretStoreTemplate', 'AgreementStoreManager', 'AgreementStoreManager', 'AccessSecretStoreCondition', 'LockRewardCondition', 'HashLockCondition', 'SignCondition', 'EscrowReward' ] self._keeper = Keeper.get_instance(contracts) self._did_resolver = DIDResolver(self._keeper.did_registry) # Initialize the public sub-modules self.tokens = OceanTokens(self._keeper) self.accounts = OceanAccounts(self._keeper, self._config, self.tokens) self.secret_store = OceanSecretStore(self._config) self.templates = OceanTemplates(self._keeper, config) self.agreements = self._make_ocean_agreements() self.assets = OceanAssets(self._keeper, self._did_resolver, self.agreements, AssetConsumer, AssetExecutor, self._config) self.services = OceanServices() self.ocean_providers = OceanProviders(self._keeper, self._did_resolver, self._config) self.auth = OceanAuth(self._keeper, self._config.storage_path) logger.debug('Squid Ocean instance initialized: ') logger.debug( f'\tOther accounts: {sorted([a.address for a in self.accounts.list()])}' ) logger.debug(f'\tDIDRegistry @ {self._keeper.did_registry.address}')
def __init__(self): self.config = Config('config.ini') self.COMMAND_SENT_EVENT = 'CarCommandSent' ConfigProvider.set_config(self.config)
def test_check_permissions_not_registered_did(): consumer_account = get_consumer_account(ConfigProvider.get_config()) assert not access_secret_store_condition.check_permissions( did_to_id(DID.did()), consumer_account.address)
def test_request_tokens(dispenser): account = get_consumer_account(ConfigProvider.get_config()) assert dispenser.request_tokens( 100, account), f'{account.address} do not get 100 tokens.'
def test_request_tokens_with_locked_account(dispenser): account = Account( get_consumer_account(ConfigProvider.get_config()).address, '') with pytest.raises(OceanInvalidTransaction): dispenser.request_tokens(100, account)
def test_buy_asset(consumer_ocean_instance, publisher_ocean_instance): config = ExampleConfig.get_config() ConfigProvider.set_config(config) keeper = Keeper.get_instance() # :TODO: enable the actual SecretStore # SecretStoreProvider.set_secret_store_class(SecretStore) w3 = Web3Provider.get_web3() pub_acc = get_publisher_account(config) # Register ddo ddo = get_registered_ddo(publisher_ocean_instance, pub_acc) assert isinstance(ddo, DDO) # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally cons_ocn = consumer_ocean_instance # restore the http client because we want the actual Brizo server to do the work # not the BrizoMock. # Brizo.set_http_client(requests) consumer_account = get_account_from_config(cons_ocn._config, 'parity.address1', 'parity.password1') downloads_path_elements = len( os.listdir(consumer_ocean_instance._config.downloads_path)) if os.path.exists( consumer_ocean_instance._config.downloads_path) else 0 # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) assert ServiceAgreement.SERVICE_DEFINITION_ID in service.as_dictionary() sa = ServiceAgreement.from_service_dict(service.as_dictionary()) # This will send the purchase request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) agreement_id = cons_ocn.assets.order( ddo.did, sa.service_definition_id, consumer_account, auto_consume=True) event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( agreement_id, 20, log_event(keeper.escrow_access_secretstore_template.AGREEMENT_CREATED_EVENT), (), wait=True ) assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated' event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, 20, log_event(keeper.lock_reward_condition.FULFILLED_EVENT), (), wait=True ) assert event, 'no event for LockRewardCondition.Fulfilled' event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( agreement_id, 10, log_event(keeper.escrow_reward_condition.FULFILLED_EVENT), (), wait=True ) assert event, 'no event for EscrowReward.Fulfilled' assert w3.toHex(event.args['_agreementId']) == agreement_id assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1 # decrypt the contentUrls using the publisher account instead of consumer account. # if the secret store is working and ACL check is enabled, this should fail # since SecretStore decrypt will fail the checkPermissions check try: cons_ocn.assets.consume( agreement_id, ddo.did, service.service_definition_id, pub_acc, config.downloads_path ) except RPCError: print('hooray, secret store is working as expected.')