Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from nevermined-io/feature/contracts_v1
Browse files Browse the repository at this point in the history
Migration to contracts v1.0
  • Loading branch information
aaitor authored Apr 6, 2021
2 parents 8bf7c39 + 9c1a636 commit 2df5340
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 131 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --compute --spree-embedded-contracts &
# wait for the compute api to be online.
# the compute api is the last service to come online
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/buy_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/buy_asset_new_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/compute_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions nevermined_sdk_py/agreement_events/accessSecretStore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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
23 changes: 14 additions & 9 deletions nevermined_sdk_py/nevermined/agreements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}'
)

Expand Down
15 changes: 10 additions & 5 deletions nevermined_sdk_py/nevermined/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)}'
Expand Down Expand Up @@ -168,8 +171,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)
Expand All @@ -181,7 +184,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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -497,7 +502,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):
Expand Down
53 changes: 33 additions & 20 deletions nevermined_sdk_py/nevermined/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading

0 comments on commit 2df5340

Please sign in to comment.