From 72ba6b6e571db30bafe212274c2953f276bf7b68 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Mon, 5 Apr 2021 19:32:57 +0200 Subject: [PATCH 1/6] Migration to contracts v1.0 --- .github/workflows/pythonpackage.yml | 3 +- examples/buy_asset.py | 8 +- examples/buy_asset_new_flow.py | 4 +- examples/compute_example.py | 4 +- .../agreement_events/accessSecretStore.py | 4 +- .../escrowAccessSecretStoreTemplate.py | 12 +-- nevermined_sdk_py/nevermined/agreements.py | 23 +++--- nevermined_sdk_py/nevermined/assets.py | 8 +- nevermined_sdk_py/nevermined/conditions.py | 53 ++++++++----- nevermined_sdk_py/nevermined/keeper.py | 6 +- nevermined_sdk_py/nevermined/templates.py | 2 +- setup.py | 14 ++-- tests/conftest.py | 2 +- tests/nevermined/test_agreements.py | 75 ++++++++++--------- tests/nevermined/test_assets.py | 23 +++--- tests/nevermined/test_buy_asset.py | 28 +++---- tests/nevermined/test_keeper.py | 6 +- 17 files changed, 147 insertions(+), 128 deletions(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index f823f90..f6a1a0a 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -42,8 +42,7 @@ jobs: rm -rf "${HOME}/.nevermined/nevermined-contracts/artifacts" # start nevermined with the compute stack - export KEEPER_VERSION=v0.6.2 - ./start_nevermined.sh --no-marketplace --local-spree-node --events-handler --compute --spree-embedded-contracts & + ./start_nevermined.sh --latest --no-marketplace --local-spree-node --events-handler --compute --spree-embedded-contracts & # wait for the compute api to be online. # the compute api is the last service to come online diff --git a/examples/buy_asset.py b/examples/buy_asset.py index 39f6790..3841939 100755 --- a/examples/buy_asset.py +++ b/examples/buy_asset.py @@ -104,19 +104,19 @@ def buy_asset(): did, sa.index, consumer_account) logging.info('placed order: %s, %s', did, agreement_id) - event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( + event = keeper.access_template.subscribe_agreement_created( agreement_id, 60, None, (), wait=True ) assert event, "Agreement event is not found, check the keeper node's logs" logging.info(f'Got agreement event, next: lock reward condition') - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs" logging.info('Got lock reward event, next: wait for the access condition..') - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') @@ -137,7 +137,7 @@ def buy_asset(): index=0) logging.info('Success buying asset.') - event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( + event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 30, None, diff --git a/examples/buy_asset_new_flow.py b/examples/buy_asset_new_flow.py index 32c31d1..d3d1875 100644 --- a/examples/buy_asset_new_flow.py +++ b/examples/buy_asset_new_flow.py @@ -96,7 +96,7 @@ def buy_asset_new_flow(): index=0) logging.info('Success buying asset.') - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') @@ -108,7 +108,7 @@ def buy_asset_new_flow(): assert nevermined.agreements.is_access_granted(agreement_id, did, consumer_account.address) - event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( + event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 30, None, diff --git a/examples/compute_example.py b/examples/compute_example.py index 5d1fe46..c6b4b8c 100644 --- a/examples/compute_example.py +++ b/examples/compute_example.py @@ -117,7 +117,7 @@ def compute_example(verbose=False): f"[CONSUMER --> PROVIDER] Requesting an agreement for compute to the data: {agreement_id}" ) - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Reward condition is not found" @@ -137,7 +137,7 @@ def compute_example(verbose=False): ) print("[CONSUMER --> PROVIDER] Requesting execution of the compute workflow") - event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( + event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Escrow Reward condition not found" diff --git a/nevermined_sdk_py/agreement_events/accessSecretStore.py b/nevermined_sdk_py/agreement_events/accessSecretStore.py index 499bc74..f818253 100644 --- a/nevermined_sdk_py/agreement_events/accessSecretStore.py +++ b/nevermined_sdk_py/agreement_events/accessSecretStore.py @@ -46,7 +46,7 @@ def refund_reward(event, agreement_id, did, service_agreement, price, consumer_a assert document_id == asset_id, f'document_id {document_id} <=> asset_id {asset_id} mismatch.' assert price == service_agreement.get_price(), 'price mismatch.' try: - escrow_condition = Keeper.get_instance().escrow_reward_condition + escrow_condition = Keeper.get_instance().escrow_payment_condition tx_hash = escrow_condition.fulfill( agreement_id, price, @@ -63,7 +63,7 @@ def refund_reward(event, agreement_id, did, service_agreement, price, consumer_a ) except Exception as e: logger.error( - f'Error when doing escrow_reward_condition.fulfills (agreementId {agreement_id}): {e}', + f'Error when doing escrow_payment_condition.fulfills (agreementId {agreement_id}): {e}', exc_info=1) raise e diff --git a/nevermined_sdk_py/agreement_events/escrowAccessSecretStoreTemplate.py b/nevermined_sdk_py/agreement_events/escrowAccessSecretStoreTemplate.py index 6c96ae4..79b25ed 100644 --- a/nevermined_sdk_py/agreement_events/escrowAccessSecretStoreTemplate.py +++ b/nevermined_sdk_py/agreement_events/escrowAccessSecretStoreTemplate.py @@ -7,7 +7,7 @@ logger = logging.getLogger(__name__) -def fulfill_lock_reward_condition(event, agreement_id, price, consumer_account, lock_condition_id): +def fulfill_lock_payment_condition(event, agreement_id, price, consumer_account, lock_condition_id): """ Fulfill the lock reward condition. @@ -19,7 +19,7 @@ def fulfill_lock_reward_condition(event, agreement_id, price, consumer_account, """ if not event: logger.warning( - f'`fulfill_lock_reward_condition` got empty event: event listener timed out.') + f'`fulfill_lock_payment_condition` got empty event: event listener timed out.') return keeper = Keeper.get_instance() @@ -31,16 +31,16 @@ def fulfill_lock_reward_condition(event, agreement_id, price, consumer_account, logger.debug(f"about to lock reward (agreement {agreement_id}) after event {event}.") approved = keeper.token.token_approve( - keeper.lock_reward_condition.address, price, consumer_account) + keeper.lock_payment_condition.address, price, consumer_account) logger.info(f'approval of token transfer was {"" if approved else "NOT"} successful') args = ( agreement_id, - keeper.escrow_reward_condition.address, + keeper.escrow_payment_condition.address, price, consumer_account ) - process_fulfill_condition(args, keeper.lock_reward_condition, lock_condition_id, logger, keeper, + process_fulfill_condition(args, keeper.lock_payment_condition, lock_condition_id, logger, keeper, 10) -fulfillLockRewardCondition = fulfill_lock_reward_condition +fulfillLockRewardCondition = fulfill_lock_payment_condition diff --git a/nevermined_sdk_py/nevermined/agreements.py b/nevermined_sdk_py/nevermined/agreements.py index 9568598..f441f77 100644 --- a/nevermined_sdk_py/nevermined/agreements.py +++ b/nevermined_sdk_py/nevermined/agreements.py @@ -72,12 +72,12 @@ def create(self, did, index, agreement_id, consumer_address, account): f'Unrecognized account address {account.address}' agreement_template_approved = self._keeper.template_manager.is_template_approved( - self._keeper.escrow_access_secretstore_template.address) + self._keeper.access_template.address) agreement_exec_template_approved = self._keeper.template_manager.is_template_approved( self._keeper.escrow_compute_execution_template.address) if not agreement_template_approved: msg = (f'The EscrowAccessSecretStoreTemplate contract at address ' - f'{self._keeper.escrow_access_secretstore_template.address} is not ' + f'{self._keeper.access_template.address} is not ' f'approved and cannot be used for creating service agreements.') logger.warning(msg) raise InvalidAgreementTemplate(msg) @@ -92,7 +92,7 @@ def create(self, did, index, agreement_id, consumer_address, account): asset_id = asset.asset_id service_agreement = asset.get_service_by_index(index) if service_agreement.type == ServiceTypes.ASSET_ACCESS: - agreement_template = self._keeper.escrow_access_secretstore_template + agreement_template = self._keeper.access_template elif service_agreement.type == ServiceTypes.CLOUD_COMPUTE: agreement_template = self._keeper.escrow_compute_execution_template else: @@ -124,7 +124,12 @@ def create(self, did, index, agreement_id, consumer_address, account): account ) if success: - self.conditions.lock_reward(agreement_id, service_agreement.get_price(), account) + self.conditions.lock_payment( + agreement_id, + asset_id, + service_agreement.get_amounts_int(), + service_agreement.get_receivers(), + account) return self._is_condition_fulfilled(agreement_id, 'lockReward') def status(self, agreement_id): @@ -157,7 +162,7 @@ def is_access_granted(self, agreement_id, did, consumer_address): :param consumer_address: ethereum account address of consumer, hex str :return: bool True if user has permission """ - agreement_consumer = self._keeper.escrow_access_secretstore_template.get_agreement_consumer( + agreement_consumer = self._keeper.access_template.get_agreement_consumer( agreement_id) if agreement_consumer is None: @@ -170,7 +175,7 @@ def is_access_granted(self, agreement_id, did, consumer_address): return False document_id = did_to_id(did) - return self._keeper.access_secret_store_condition.check_permissions( + return self._keeper.access_condition.check_permissions( document_id, consumer_address ) @@ -222,7 +227,7 @@ def _process_consumer_agreement_events( logger.debug( f'process consumer events for agreement {agreement_id}, blockNumber {from_block + 10}') if agreement_type == ServiceTypes.ASSET_ACCESS: - self._keeper.escrow_access_secretstore_template.subscribe_agreement_created( + self._keeper.access_template.subscribe_agreement_created( agreement_id, 300, fulfillLockRewardCondition, @@ -251,7 +256,7 @@ def do_refund(_event, _agreement_id, _did, _service_agreement, _consumer_account conditions_dict = service_agreement.condition_by_name if agreement_type == ServiceTypes.ASSET_ACCESS: - self._keeper.access_secret_store_condition.subscribe_condition_fulfilled( + self._keeper.access_condition.subscribe_condition_fulfilled( agreement_id, max(conditions_dict['accessSecretStore'].timeout, 300), consume_asset, @@ -290,7 +295,7 @@ def _log_agreement_info(self, asset, service_agreement, agreement_id, agreement_ f'\n agreement signature: {agreement_signature}' f'\n agreement hash: {agreement_hash}' f'\n EscrowAccessSecretStoreTemplate: ' - f'{self._keeper.escrow_access_secretstore_template.address}' + f'{self._keeper.access_template.address}' f'\n publisher ether balance: {publisher_ether_balance}' ) diff --git a/nevermined_sdk_py/nevermined/assets.py b/nevermined_sdk_py/nevermined/assets.py index 09c2335..e60da73 100644 --- a/nevermined_sdk_py/nevermined/assets.py +++ b/nevermined_sdk_py/nevermined/assets.py @@ -168,8 +168,8 @@ def create(self, metadata, publisher_account, did, gateway.get_access_endpoint(self._config), access_service_attributes, - self._keeper.escrow_access_secretstore_template.address, - self._keeper.escrow_reward_condition.address) + self._keeper.access_template.address, + self._keeper.escrow_payment_condition.address) ddo.add_service(access_service) elif service.type == ServiceTypes.METADATA: ddo_service_endpoint = service.service_endpoint.replace('{did}', did) @@ -181,7 +181,7 @@ def create(self, metadata, publisher_account, service.service_endpoint, service.attributes, self._keeper.compute_execution_condition.address, - self._keeper.escrow_reward_condition.address + self._keeper.escrow_payment_condition.address ) ddo.add_service(compute_service) else: @@ -497,7 +497,7 @@ def consumer_assets(self, consumer_address): :param consumer_address: ethereum address of consumer, hes-str :return: list of dids """ - return self._keeper.access_secret_store_condition.get_purchased_assets_by_address( + return self._keeper.access_condition.get_purchased_assets_by_address( consumer_address) def revoke_permissions(self, did, address_to_revoke, account): diff --git a/nevermined_sdk_py/nevermined/conditions.py b/nevermined_sdk_py/nevermined/conditions.py index 166f7e7..5697a06 100644 --- a/nevermined_sdk_py/nevermined/conditions.py +++ b/nevermined_sdk_py/nevermined/conditions.py @@ -9,74 +9,87 @@ class Conditions: def __init__(self, keeper): self._keeper = keeper - def lock_reward(self, agreement_id, amount, account): + def lock_payment(self, agreement_id, asset_id, amounts, receivers, account): """ Lock reward condition. :param agreement_id: id of the agreement, hex str - :param amount: Amount of tokens, int + :param asset_id: asset identifier, str + :param amounts: Amount of tokens, str[] + :param receivers: Tokens receivers, str[] :param account: Account :return: bool """ - self._keeper.token.token_approve(self._keeper.lock_reward_condition.address, amount, + total_price = 0 + for amount in amounts: + total_price = total_price + int(amount) + self._keeper.token.token_approve(self._keeper.lock_payment_condition.address, total_price, account) - tx_hash = self._keeper.lock_reward_condition.fulfill( - agreement_id, self._keeper.escrow_reward_condition.address, amount, account - ) - receipt = self._keeper.lock_reward_condition.get_tx_receipt(tx_hash) + tx_hash = self._keeper.lock_payment_condition.fulfill( + agreement_id, + asset_id, + self._keeper.escrow_payment_condition.address, + amounts, + to_checksum_addresses(receivers), + account) + + receipt = self._keeper.lock_payment_condition.get_tx_receipt(tx_hash) return bool(receipt and receipt.status == 1) - def grant_access(self, agreement_id, did, grantee_address, account): + def grant_access(self, agreement_id, asset_id, grantee_address, account): """ Grant access condition. :param agreement_id: id of the agreement, hex str - :param did: DID, str + :param asset_id: asset id, str :param grantee_address: Address, hex str :param account: Account :return: """ - tx_hash = self._keeper.access_secret_store_condition.fulfill( - agreement_id, add_0x_prefix(did_to_id(did)), grantee_address, account + tx_hash = self._keeper.access_condition.fulfill( + agreement_id, asset_id, grantee_address, account ) - receipt = self._keeper.access_secret_store_condition.get_tx_receipt(tx_hash) + receipt = self._keeper.access_condition.get_tx_receipt(tx_hash) return bool(receipt and receipt.status == 1) - def release_reward(self, agreement_id, amounts, receivers, account): + def release_reward(self, agreement_id, asset_id, amounts, receivers, account): """ Release reward condition. :param agreement_id: id of the agreement, hex str + :param asset_id: asset id, str :param amounts: Amounts of tokens, int[] :param receivers: Token receivers, str[] :param account sending the transaction :return: """ agreement_values = self._keeper.agreement_manager.get_agreement(agreement_id) - consumer, provider = self._keeper.escrow_access_secretstore_template.get_agreement_data( + consumer, provider = self._keeper.access_template.get_agreement_data( agreement_id) owner = agreement_values.owner access_id, lock_id = agreement_values.condition_ids[:2] - tx_hash = self._keeper.escrow_reward_condition.fulfill( + tx_hash = self._keeper.escrow_payment_condition.fulfill( agreement_id, + asset_id, amounts, to_checksum_addresses(receivers), - owner, + self._keeper.escrow_payment_condition.address, lock_id, access_id, account ) - receipt = self._keeper.escrow_reward_condition.get_tx_receipt(tx_hash) + receipt = self._keeper.escrow_payment_condition.get_tx_receipt(tx_hash) return bool(receipt and receipt.status == 1) - def refund_reward(self, agreement_id, amounts, receivers, account): + def refund_reward(self, agreement_id, asset_id, amounts, receivers, account): """ - Refund reaward condition. + Refund reward condition. :param agreement_id: id of the agreement, hex str + :param asset_id: asset id, str :param amounts: Amounts of tokens, int[] :param receivers: Token receivers, str[] :param account sending the transaction :return: """ - return self.release_reward(agreement_id, amounts, receivers, account) + return self.release_reward(agreement_id, asset_id, amounts, receivers, account) diff --git a/nevermined_sdk_py/nevermined/keeper.py b/nevermined_sdk_py/nevermined/keeper.py index 8e4fff5..9b3cf0a 100644 --- a/nevermined_sdk_py/nevermined/keeper.py +++ b/nevermined_sdk_py/nevermined/keeper.py @@ -11,13 +11,13 @@ def get_instance(artifacts_path=None, contract_names=None): def get_condition_name_by_address(self, address): """Return the condition name for a given address.""" - if self.lock_reward_condition.address == address: + if self.lock_payment_condition.address == address: return 'lockReward' - elif self.access_secret_store_condition.address == address: + elif self.access_condition.address == address: return 'accessSecretStore' elif self.compute_execution_condition.address == address: return 'execCompute' - elif self.escrow_reward_condition.address == address: + elif self.escrow_payment_condition.address == address: return 'escrowReward' else: logging.error(f'The current address {address} is not a condition address') diff --git a/nevermined_sdk_py/nevermined/templates.py b/nevermined_sdk_py/nevermined/templates.py index fc260ac..d4ec40b 100644 --- a/nevermined_sdk_py/nevermined/templates.py +++ b/nevermined_sdk_py/nevermined/templates.py @@ -9,7 +9,7 @@ class Templates: def __init__(self, keeper, config): self._keeper = keeper self._config = config - self.access_template_id = self._keeper.escrow_access_secretstore_template.address + self.access_template_id = self._keeper.access_template.address def propose(self, template_address, account): """ diff --git a/setup.py b/setup.py index 1139cbe..34eb9c0 100644 --- a/setup.py +++ b/setup.py @@ -19,11 +19,11 @@ 'coloredlogs', 'pyopenssl', 'PyJWT', # not jwt - 'PyYAML==4.2b4', - 'common-utils-py==0.4.6', - 'contracts-lib-py==0.5.5', - 'nevermined-secret-store==0.1.0', - 'requests~=2.21.0', + 'PyYAML>=5.2', + 'common-utils-py==0.5.0', + 'contracts-lib-py==0.6.2', + 'nevermined-secret-store==0.1.1', + 'requests>=2.21.0', 'deprecated', 'pycryptodomex', 'tqdm', @@ -69,7 +69,7 @@ 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Natural Language :: English', - 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', ], description="🐳 Nevermined Python SDK.", extras_require={ @@ -89,6 +89,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/nevermined-io/sdk-py', - version='0.8.0', + version='0.9.0', zip_safe=False, ) diff --git a/tests/conftest.py b/tests/conftest.py index f66fd81..aaa5bdf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -144,7 +144,7 @@ def workflow_ddo(): return ddo @pytest.fixture -def setup_agreements_enviroment(ddo_sample): +def setup_agreements_environment(ddo_sample): consumer_acc = get_consumer_account() publisher_acc = get_publisher_account() keeper = Keeper.get_instance() diff --git a/tests/nevermined/test_agreements.py b/tests/nevermined/test_agreements.py index 077c4cd..1383f86 100644 --- a/tests/nevermined/test_agreements.py +++ b/tests/nevermined/test_agreements.py @@ -43,48 +43,49 @@ def test_sign_agreement(publisher_instance, consumer_instance, registered_ddo): # Verify condition types (condition contracts) agreement_values = keeper.agreement_manager.get_agreement(agreement_id) assert agreement_values.did == asset_id, '' - cond_types = keeper.escrow_access_secretstore_template.get_condition_types() + cond_types = keeper.access_template.get_condition_types() for i, cond_id in enumerate(agreement_values.condition_ids): cond = keeper.condition_manager.get_condition(cond_id) assert cond.type_ref == cond_types[i] access_cond_id, lock_cond_id, escrow_cond_id = agreement_values.condition_ids - # Fulfill lock_reward_condition is done automatically when create agreement is done correctly + # Fulfill lock_payment_condition is done automatically when create agreement is done correctly assert 2 == keeper.condition_manager.get_condition_state(lock_cond_id), '' assert 1 == keeper.condition_manager.get_condition_state(access_cond_id), '' assert 1 == keeper.condition_manager.get_condition_state(escrow_cond_id), '' - # Fulfill access_secret_store_condition - tx_hash = keeper.access_secret_store_condition.fulfill( + # Fulfill access_condition + tx_hash = keeper.access_condition.fulfill( agreement_id, asset_id, consumer_acc.address, publisher_acc ) - keeper.access_secret_store_condition.get_tx_receipt(tx_hash) + keeper.access_condition.get_tx_receipt(tx_hash) assert 2 == keeper.condition_manager.get_condition_state(access_cond_id), '' - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 10, - log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), + log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) assert event, 'no event for AccessSecretStoreCondition.Fulfilled' - # Fulfill escrow_reward_condition - amounts = list(map(int, service_agreement.get_param_value_by_name('_amounts'))) - receivers = to_checksum_addresses(service_agreement.get_param_value_by_name('_receivers')) + # Fulfill escrow_payment_condition - tx_hash = keeper.escrow_reward_condition.fulfill( - agreement_id, amounts, receivers, - publisher_acc.address, lock_cond_id, + amounts = service_agreement.get_amounts_int() + receivers = service_agreement.get_receivers() + + tx_hash = keeper.escrow_payment_condition.fulfill( + agreement_id, asset_id, amounts, receivers, + keeper.escrow_payment_condition.address, lock_cond_id, access_cond_id, publisher_acc ) - keeper.escrow_reward_condition.get_tx_receipt(tx_hash) + keeper.escrow_payment_condition.get_tx_receipt(tx_hash) assert 2 == keeper.condition_manager.get_condition_state(escrow_cond_id), '' - event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( + event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 10, - log_event(keeper.escrow_reward_condition.FULFILLED_EVENT), + log_event(keeper.escrow_payment_condition.FULFILLED_EVENT), (), wait=True ) @@ -93,7 +94,7 @@ def test_sign_agreement(publisher_instance, consumer_instance, registered_ddo): # @pytest.mark.skip(reason="Failing some times with actions") -def test_agreement_status(setup_agreements_enviroment, agreements): +def test_agreement_status(setup_agreements_environment, agreements): ( keeper, publisher_acc, @@ -104,9 +105,9 @@ def test_agreement_status(setup_agreements_enviroment, agreements): service_agreement, (lock_cond_id, access_cond_id, escrow_cond_id), - ) = setup_agreements_enviroment + ) = setup_agreements_environment - success = keeper.escrow_access_secretstore_template.create_agreement( + success = keeper.access_template.create_agreement( agreement_id, asset_id, [access_cond_id, lock_cond_id, escrow_cond_id], @@ -117,10 +118,10 @@ def test_agreement_status(setup_agreements_enviroment, agreements): ) print('create agreement: ', success) assert success, f'createAgreement failed {success}' - event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( + event = keeper.access_template.subscribe_agreement_created( agreement_id, 10, - log_event(keeper.escrow_access_secretstore_template.AGREEMENT_CREATED_EVENT), + log_event(keeper.access_template.AGREEMENT_CREATED_EVENT), (), wait=True ) @@ -132,12 +133,15 @@ def test_agreement_status(setup_agreements_enviroment, agreements): } } - agreements.conditions.lock_reward(agreement_id, price, consumer_acc) + amounts = service_agreement.get_amounts_int() + receivers = service_agreement.get_receivers() + + agreements.conditions.lock_payment(agreement_id, asset_id, amounts, receivers, consumer_acc) - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 10, - log_event(keeper.lock_reward_condition.FULFILLED_EVENT), + log_event(keeper.lock_payment_condition.FULFILLED_EVENT), (), wait=True ) @@ -148,13 +152,13 @@ def test_agreement_status(setup_agreements_enviroment, agreements): "escrowReward": 1 } } - tx_hash = keeper.access_secret_store_condition.fulfill( + tx_hash = keeper.access_condition.fulfill( agreement_id, asset_id, consumer_acc.address, publisher_acc) - keeper.access_secret_store_condition.get_tx_receipt(tx_hash) - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + keeper.access_condition.get_tx_receipt(tx_hash) + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 20, - log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), + log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) @@ -166,19 +170,16 @@ def test_agreement_status(setup_agreements_enviroment, agreements): } } - amounts = list(map(int, service_agreement.get_param_value_by_name('_amounts'))) - receivers = to_checksum_addresses(service_agreement.get_param_value_by_name('_receivers')) - - tx_hash = keeper.escrow_reward_condition.fulfill( - agreement_id, amounts, to_checksum_addresses(receivers), - publisher_acc.address, lock_cond_id, + tx_hash = keeper.escrow_payment_condition.fulfill( + agreement_id, asset_id, amounts, receivers, + keeper.escrow_payment_condition.address, lock_cond_id, access_cond_id, publisher_acc ) - keeper.escrow_reward_condition.get_tx_receipt(tx_hash) - event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( + keeper.escrow_payment_condition.get_tx_receipt(tx_hash) + event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 10, - log_event(keeper.escrow_reward_condition.FULFILLED_EVENT), + log_event(keeper.escrow_payment_condition.FULFILLED_EVENT), (), wait=True ) diff --git a/tests/nevermined/test_assets.py b/tests/nevermined/test_assets.py index 8b2d57a..00c31be 100644 --- a/tests/nevermined/test_assets.py +++ b/tests/nevermined/test_assets.py @@ -5,9 +5,10 @@ from common_utils_py.agreements.service_agreement import ServiceAgreement from common_utils_py.agreements.service_factory import ServiceDescriptor from common_utils_py.agreements.service_types import ServiceTypes, ServiceTypesIndices -from common_utils_py.did import DID +from common_utils_py.did import DID, did_to_id from contracts_lib_py.exceptions import DIDNotFound from contracts_lib_py.web3_provider import Web3Provider +from eth_utils import add_0x_prefix from tests.resources.helper_functions import log_event @@ -203,11 +204,11 @@ def test_assets_consumed(publisher_instance, consumer_instance, ddo_sample): def grant_access(event, instance, agr_id, did, cons_address, account): instance.agreements.conditions.grant_access( - agr_id, did, cons_address, account) + agr_id, add_0x_prefix(did_to_id(did)), cons_address, account) agreement_id = consumer_instance.assets.order( asset.did, sa.index, acct, acct) - keeper.lock_reward_condition.subscribe_condition_fulfilled( + keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 15, grant_access, @@ -216,10 +217,10 @@ def grant_access(event, instance, agr_id, did, cons_address, account): wait=True ) - keeper.access_secret_store_condition.subscribe_condition_fulfilled( + keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 15, - log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), + log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) @@ -321,7 +322,7 @@ def test_execute_workflow(publisher_instance_no_init, consumer_instance_no_init, agreement_id = consumer_instance_no_init.assets.order(ddo_computing.did, sa.index, consumer, consumer) keeper = Keeper.get_instance() - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Reward condition is not found" @@ -359,7 +360,7 @@ def test_compute_status(publisher_instance_no_init, consumer_instance_no_init, m agreement_id = consumer_instance_no_init.assets.order(ddo_computing.did, sa.index, consumer, consumer) keeper = Keeper.get_instance() - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Reward condition is not found" @@ -400,7 +401,7 @@ def test_compute_logs(publisher_instance_no_init, consumer_instance_no_init, met agreement_id = consumer_instance_no_init.assets.order(ddo_computing.did, sa.index, consumer, consumer) keeper = Keeper.get_instance() - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Reward condition is not found" @@ -434,10 +435,10 @@ def test_agreement_direct(publisher_instance, consumer_instance, metadata): keeper = publisher_instance.keeper - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 10, - log_event(keeper.lock_reward_condition.FULFILLED_EVENT), + log_event(keeper.lock_payment_condition.FULFILLED_EVENT), (), wait=True ) @@ -454,7 +455,7 @@ def test_nfts(publisher_instance, metadata): ddo = publisher_instance.assets.create(metadata, publisher) balance = publisher_instance.assets.balance(publisher.address, ddo.did) - assert balance == 1 + assert balance == 0 balance_consumer = publisher_instance.assets.balance(someone_address, ddo.did) assert balance_consumer == 0 diff --git a/tests/nevermined/test_buy_asset.py b/tests/nevermined/test_buy_asset.py index 27c7c8a..e3fd952 100644 --- a/tests/nevermined/test_buy_asset.py +++ b/tests/nevermined/test_buy_asset.py @@ -47,10 +47,10 @@ def test_buy_asset(publisher_instance_no_init, consumer_instance_no_init): ddo.did, sa.index, consumer_account, consumer_account) event_wait_time = 10 - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, - log_event(keeper.lock_reward_condition.FULFILLED_EVENT), + log_event(keeper.lock_payment_condition.FULFILLED_EVENT), (), wait=True ) @@ -58,11 +58,11 @@ def test_buy_asset(publisher_instance_no_init, consumer_instance_no_init): # give access publisher_instance_no_init.agreements.conditions.grant_access( - agreement_id, ddo.did, consumer_account.address, pub_acc) - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + agreement_id, ddo.asset_id, consumer_account.address, pub_acc) + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, - log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), + log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) @@ -73,7 +73,7 @@ def test_buy_asset(publisher_instance_no_init, consumer_instance_no_init): receivers = service.get_param_value_by_name('_receivers') publisher_instance_no_init.agreements.conditions.release_reward( - agreement_id, amounts, receivers, pub_acc) + agreement_id, ddo.asset_id, amounts, receivers, pub_acc) assert consumer_instance_no_init.assets.access( agreement_id, @@ -163,19 +163,19 @@ def test_buy_asset_no_secret_store(publisher_instance_gateway, consumer_instance ddo.did, sa.index, consumer_account, consumer_account) event_wait_time = 10 - event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( + event = keeper.access_template.subscribe_agreement_created( agreement_id, event_wait_time, - log_event(keeper.escrow_access_secretstore_template.AGREEMENT_CREATED_EVENT), + log_event(keeper.access_template.AGREEMENT_CREATED_EVENT), (), wait=True ) assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated' - event = keeper.lock_reward_condition.subscribe_condition_fulfilled( + event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, - log_event(keeper.lock_reward_condition.FULFILLED_EVENT), + log_event(keeper.lock_payment_condition.FULFILLED_EVENT), (), wait=True ) @@ -183,11 +183,11 @@ def test_buy_asset_no_secret_store(publisher_instance_gateway, consumer_instance # give access publisher_instance_gateway.agreements.conditions.grant_access( - agreement_id, ddo.did, consumer_account.address, pub_acc) - event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( + agreement_id, ddo.asset_id, consumer_account.address, pub_acc) + event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, - log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), + log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) @@ -195,7 +195,7 @@ def test_buy_asset_no_secret_store(publisher_instance_gateway, consumer_instance assert consumer_instance_gateway.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) publisher_instance_gateway.agreements.conditions.release_reward( - agreement_id, sa.get_price(), pub_acc) + agreement_id, ddo.asset_id, sa.get_amounts_int(), sa.get_receivers(), pub_acc) assert consumer_instance_gateway.assets.access( agreement_id, diff --git a/tests/nevermined/test_keeper.py b/tests/nevermined/test_keeper.py index 2a6050c..9da6f72 100644 --- a/tests/nevermined/test_keeper.py +++ b/tests/nevermined/test_keeper.py @@ -3,11 +3,11 @@ def test_get_condition_name_by_address(): keeper = Keeper.get_instance() - name = keeper.get_condition_name_by_address(keeper.lock_reward_condition.address) + name = keeper.get_condition_name_by_address(keeper.lock_payment_condition.address) assert name == 'lockReward' - name = keeper.get_condition_name_by_address(keeper.access_secret_store_condition.address) + name = keeper.get_condition_name_by_address(keeper.access_condition.address) assert name == 'accessSecretStore' - name = keeper.get_condition_name_by_address(keeper.escrow_reward_condition.address) + name = keeper.get_condition_name_by_address(keeper.escrow_payment_condition.address) assert name == 'escrowReward' From 036636782734902c4fbc2f130af60b6dfeeff902 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Mon, 5 Apr 2021 21:03:07 +0200 Subject: [PATCH 2/6] allowing to register a mintable asset --- nevermined_sdk_py/nevermined/assets.py | 7 ++++++- tests/nevermined/test_assets.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/nevermined_sdk_py/nevermined/assets.py b/nevermined_sdk_py/nevermined/assets.py index e60da73..190329a 100644 --- a/nevermined_sdk_py/nevermined/assets.py +++ b/nevermined_sdk_py/nevermined/assets.py @@ -54,7 +54,8 @@ def _get_secret_store(self, account): def create(self, metadata, publisher_account, service_descriptors=None, providers=None, authorization_type=ServiceAuthorizationTypes.PSK_RSA, use_secret_store=False, - activity_id=None, attributes=None, asset_rewards={"_amounts": [], "_receivers": []}): + activity_id=None, attributes=None, asset_rewards={"_amounts": [], "_receivers": []}, + cap=None, royalties=None): """ Register an asset in both the keeper's DIDRegistry (on-chain) and in the Metadata store. @@ -73,6 +74,8 @@ def create(self, metadata, publisher_account, :param activity_id: identifier of the activity creating the new entity :param attributes: attributes associated with the action :param asset_rewards: rewards distribution including the amounts and the receivers + :param cap: max cap of nfts that can be minted for the asset + :param royalties: royalties in the secondary market going to the original creator :return: DDO instance """ assert isinstance(metadata, dict), f'Expected metadata of type dict, got {type(metadata)}' @@ -240,6 +243,8 @@ def create(self, metadata, publisher_account, checksum=Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), url=ddo_service_endpoint, account=publisher_account, + cap=cap, + royalties=royalties, providers=providers, activity_id=activity_id, attributes=attributes diff --git a/tests/nevermined/test_assets.py b/tests/nevermined/test_assets.py index 00c31be..1cf6179 100644 --- a/tests/nevermined/test_assets.py +++ b/tests/nevermined/test_assets.py @@ -452,7 +452,7 @@ def test_agreement_direct(publisher_instance, consumer_instance, metadata): def test_nfts(publisher_instance, metadata): publisher = publisher_instance.main_account someone_address = "0x00a329c0648769A73afAc7F9381E08FB43dBEA72" - ddo = publisher_instance.assets.create(metadata, publisher) + ddo = publisher_instance.assets.create(metadata, publisher, cap=100, royalties=0) balance = publisher_instance.assets.balance(publisher.address, ddo.did) assert balance == 0 @@ -462,7 +462,7 @@ def test_nfts(publisher_instance, metadata): publisher_instance.assets.mint(ddo.did, 10, account=publisher) assert balance + 10 == publisher_instance.assets.balance(publisher.address, ddo.did) assert publisher_instance.assets.transfer_nft(ddo.did, someone_address, 1, publisher) - assert publisher_instance.assets.balance(publisher.address, ddo.did) == 10 + assert publisher_instance.assets.balance(publisher.address, ddo.did) == 9 assert publisher_instance.assets.balance(someone_address, ddo.did) == balance_consumer + 1 publisher_instance.assets.burn(ddo.did, 9, account=publisher) assert balance == publisher_instance.assets.balance(publisher.address, ddo.did) From 12771e25bf4e99bdf563cf046e5b5b2f6e8638a4 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Tue, 6 Apr 2021 08:52:14 +0200 Subject: [PATCH 3/6] setting up 0.9.0-rc0 version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 34eb9c0..cdb86d7 100644 --- a/setup.py +++ b/setup.py @@ -89,6 +89,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/nevermined-io/sdk-py', - version='0.9.0', + version='0.9.0-rc0', zip_safe=False, ) From d52eeeed1651b803b0be41da7f5ee6b2b7394376 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Tue, 6 Apr 2021 08:52:47 +0200 Subject: [PATCH 4/6] Adding v0.9.0-rc0 Changelog updates --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e48bb..8b55554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v0.9.0-rc0](https://github.com/nevermined-io/sdk-py/compare/v0.8.0...v0.9.0-rc0) + +> 6 April 2021 + +- Added integration with Filecoin [`#41`](https://github.com/nevermined-io/sdk-py/pull/41) +- Test with pre-compiled contracts [`#38`](https://github.com/nevermined-io/sdk-py/pull/38) +- Migration to contracts v1.0 [`72ba6b6`](https://github.com/nevermined-io/sdk-py/commit/72ba6b6e571db30bafe212274c2953f276bf7b68) +- Test chaching dockerlib [`a88c672`](https://github.com/nevermined-io/sdk-py/commit/a88c6725e297d47d69c1198301f542578b5494d9) +- Revert "Test chaching dockerlib" [`b241e27`](https://github.com/nevermined-io/sdk-py/commit/b241e274884ce11a3097c8c13cc65b1f7f839987) + #### [v0.8.0](https://github.com/nevermined-io/sdk-py/compare/v0.7.0...v0.8.0) > 11 February 2021 From d794f868cb925b8be2613f0f77db2f3f0d6788ff Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Tue, 6 Apr 2021 14:35:23 +0200 Subject: [PATCH 5/6] removing events handler --- .github/workflows/pythonpackage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index f6a1a0a..1864abb 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -42,7 +42,7 @@ jobs: rm -rf "${HOME}/.nevermined/nevermined-contracts/artifacts" # start nevermined with the compute stack - ./start_nevermined.sh --latest --no-marketplace --local-spree-node --events-handler --compute --spree-embedded-contracts & + ./start_nevermined.sh --latest --no-marketplace --local-spree-node --compute --spree-embedded-contracts & # wait for the compute api to be online. # the compute api is the last service to come online From 9c1a63612f1a38a1991153d18e6ab402827b2488 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Tue, 6 Apr 2021 15:45:26 +0200 Subject: [PATCH 6/6] updating version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index cdb86d7..34eb9c0 100644 --- a/setup.py +++ b/setup.py @@ -89,6 +89,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/nevermined-io/sdk-py', - version='0.9.0-rc0', + version='0.9.0', zip_safe=False, )