From 3d978f792d2e79f318202f63a9142893354cfe67 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 15 Jan 2020 19:28:04 +0100 Subject: [PATCH 1/2] template refactoring --- src/keeper/Keeper.ts | 22 +-- .../conditions/ComputeExecutionCondition.ts | 2 +- .../managers/AgreementStoreManager.ts | 44 +++++- .../templates/AgreementTemplate.abstract.ts | 34 +---- .../templates/AgreementTemplateBase.ts | 125 ++++++++++++++++++ .../templates/BaseEscrowTemplate.abstract.ts | 43 ------ ... EscrowAccess.serviceAgreementTemplate.ts} | 2 +- .../EscrowAccessSecretStoreTemplate.ts | 106 ++------------- ...EscrowCompute.serviceAgreementTemplate.ts} | 2 +- .../EscrowComputeExecutionTemplate.ts | 100 ++------------ src/ocean/OceanAgreements.ts | 2 +- 11 files changed, 204 insertions(+), 278 deletions(-) create mode 100644 src/keeper/contracts/templates/AgreementTemplateBase.ts delete mode 100644 src/keeper/contracts/templates/BaseEscrowTemplate.abstract.ts rename src/keeper/contracts/templates/{EscrowAccessSecretStoreTemplate.serviceAgreementTemplate.ts => EscrowAccess.serviceAgreementTemplate.ts} (97%) rename src/keeper/contracts/templates/{EscrowComputeExecutionTemplate.serviceAgreementTemplate.ts => EscrowCompute.serviceAgreementTemplate.ts} (97%) diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index b9a023e..517703c 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -62,14 +62,7 @@ export class Keeper extends Instantiable { accessSecretStoreCondition: AccessSecretStoreCondition.getInstance( config ), - computeExecutionCondition: ComputeExecutionCondition.getInstance(config), - // Templates - escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance( - config - ), - escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate.getInstance( - config - ) + computeExecutionCondition: ComputeExecutionCondition.getInstance(config) }) keeper.connected = true @@ -107,6 +100,19 @@ export class Keeper extends Instantiable { escrowComputeExecutionTemplate: keeper.instances.escrowComputeExecutionTemplate } + // Templates + keeper.instances.escrowAccessSecretStoreTemplate = new EscrowAccessSecretStoreTemplate( + keeper.instances.templateStoreManager, + keeper.instances.agreementStoreManager, + keeper.instances.didRegistry, + keeper.instances.conditions + ) + keeper.instances.escrowComputeExecutionTemplate = new EscrowComputeExecutionTemplate( + keeper.instances.templateStoreManager, + keeper.instances.agreementStoreManager, + keeper.instances.didRegistry, + keeper.instances.conditions + ) // Utils keeper.utils = { eventHandler: new EventHandler(config) diff --git a/src/keeper/contracts/conditions/ComputeExecutionCondition.ts b/src/keeper/contracts/conditions/ComputeExecutionCondition.ts index ba83313..9456c73 100644 --- a/src/keeper/contracts/conditions/ComputeExecutionCondition.ts +++ b/src/keeper/contracts/conditions/ComputeExecutionCondition.ts @@ -1,5 +1,5 @@ import { Condition } from './Condition.abstract' -import { zeroX, didZeroX, didPrefixed } from '../../../utils' +import { zeroX, didZeroX } from '../../../utils' import { InstantiableConfig } from '../../../Instantiable.abstract' export class ComputeExecutionCondition extends Condition { diff --git a/src/keeper/contracts/managers/AgreementStoreManager.ts b/src/keeper/contracts/managers/AgreementStoreManager.ts index 72bedf8..06d8d9f 100644 --- a/src/keeper/contracts/managers/AgreementStoreManager.ts +++ b/src/keeper/contracts/managers/AgreementStoreManager.ts @@ -15,11 +15,11 @@ export class AgreementStoreManager extends ContractBase { public static async getInstance( config: InstantiableConfig ): Promise { - const templateStoreManeger: AgreementStoreManager = new AgreementStoreManager( + const templateStoreManager: AgreementStoreManager = new AgreementStoreManager( 'AgreementStoreManager' ) - await templateStoreManeger.init(config) - return templateStoreManeger + await templateStoreManager.init(config) + return templateStoreManager } public getOwner(): Promise { @@ -44,4 +44,42 @@ export class AgreementStoreManager extends ContractBase { blockNumberUpdated: +blockNumberUpdated } as AgreementData } + + /** + * Create a agreement using EscrowComputeExecutionTemplate. + * @param {string} agreementId Generated agreement ID. + * @param {string} did Asset DID. + * @param {string} templateId Template ID. + * @param {string[]} conditionIds List of conditions IDs. + * @param {number[]} timeLocks Timelocks. + * @param {number[]} timeOuts Timeouts. + * @param {string[]} actors ETH account addresses of provider, consumer, etc. + * @param {string} from Action sender. + * + * @return {any} Transaction receipt. + */ + public async createAgreement( + agreementId: string, + did: string, + templateId: string, + conditionIds: string[], + timeLocks: number[], + timeOuts: number[], + actors: string[], + from?: string + ): Promise { + this.sendFrom( + 'createAgreement', + [ + zeroX(agreementId), + zeroX(did), + zeroX(templateId), + conditionIds.map(zeroX), + timeLocks, + timeOuts, + actors + ], + from + ) + } } diff --git a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts index 6fed9cc..2a2407f 100644 --- a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts +++ b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts @@ -34,38 +34,6 @@ export abstract class AgreementTemplate extends ContractBase { super(contractName) } - public createAgreement( - agreementId: string, - did: string, - conditionIds: string[], - timeLocks: number[], - timeOuts: number[], - ...args: any[] - ) - - public createAgreement( - agreementId: string, - did: string, - conditionIds: string[], - timeLocks: number[], - timeOuts: number[], - extraArgs: any[], - from?: string - ) { - return this.sendFrom( - 'createAgreement', - [ - zeroX(agreementId), - zeroX(did), - conditionIds.map(zeroX), - timeLocks, - timeOuts, - ...extraArgs - ], - from - ) - } - /** * Conditions address list. * @return {Promise} Conditions address. @@ -91,7 +59,7 @@ export abstract class AgreementTemplate extends ContractBase { * @param {string} from Consumer address. * @return {Promise} Condition IDs. */ - public abstract getAgreementIdsFromDDO( + public abstract getConditionIdsFromDDO( agreementId: string, ddo: DDO, consumer: string, diff --git a/src/keeper/contracts/templates/AgreementTemplateBase.ts b/src/keeper/contracts/templates/AgreementTemplateBase.ts new file mode 100644 index 0000000..11a3840 --- /dev/null +++ b/src/keeper/contracts/templates/AgreementTemplateBase.ts @@ -0,0 +1,125 @@ +import { escrowAccessServiceAgreementTemplate } from './EscrowAccess.serviceAgreementTemplate' +import { TemplateStoreManager, AgreementStoreManager } from '../managers' +import DIDRegistry from '../DIDRegistry' +import { LockRewardCondition } from '../conditions/LockRewardCondition' +import { AccessSecretStoreCondition } from '../conditions/AccessSecretStoreCondition' +import { EscrowReward } from '../conditions/EscrowReward' +import { DDO } from '../../../ddo/DDO' +import { generateId, zeroX } from '../../../utils' +import { ComputeExecutionCondition } from '../conditions' + +export interface Conditions { + lockRewardCondition: LockRewardCondition + accessSecretStoreCondition?: AccessSecretStoreCondition + computeExecutionCondition?: ComputeExecutionCondition + escrowReward: EscrowReward +} + +export class AgreementTemplateBase { + public static templateName: string + + public templateManager: TemplateStoreManager + + public agreementStoreManager: AgreementStoreManager + + public didRegistry: DIDRegistry + + public conditions: Conditions + + public constructor( + templateManager: TemplateStoreManager, + agreementStoreManager: AgreementStoreManager, + didRegistry: DIDRegistry, + conditions: Conditions + ) { + this.templateManager = templateManager + this.agreementStoreManager = agreementStoreManager + this.didRegistry = didRegistry + this.conditions = conditions + } + + public async getServiceAgreementTemplate() { + return escrowAccessServiceAgreementTemplate + } + + public async createAgreementFromDDO( + agreementId: string, + ddo: DDO, + consumer: string, + from?: string + ) { + return !!(await this.createFullAgreement( + ddo.shortId(), + ddo.findServiceByType('metadata').attributes.main.price, + consumer, + from, + agreementId + )) + } + + public async getConditionIdsFromDDO( + agreementId: string, + ddo: DDO, + consumer: string, + from?: string + ) { + const conditionIds = await this.createFullAgreementData( + agreementId, + ddo.shortId(), + ddo.findServiceByType('metadata').attributes.main.price, + consumer + ) + return conditionIds + } + + public getId() { + return '' + } + + /** + * Create a agreement using EscrowAccessSecretStoreTemplate using only the most important information. + * @param {string} did Asset DID. + * @param {number} amount Asset price. + * @param {string} from Consumer address. + * + * @return {Promise} Agreement ID. + */ + public async createFullAgreement( + did: string, + amount: number | string, + consumer: string, + from?: string, + agreementId: string = generateId() + ): Promise { + const conditionIds = await this.createFullAgreementData( + agreementId, + did, + amount, + consumer + ) + + const publisher = await this.didRegistry.getDIDOwner(did) + + await this.agreementStoreManager.createAgreement( + agreementId, + did, + this.getId(), + conditionIds, + [0, 0, 0], + [0, 0, 0], + [consumer, publisher], + from + ) + + return zeroX(agreementId) + } + + protected async createFullAgreementData( + agreementId: string, + did: string, + amount: number | string, + consumer: string + ): Promise { + return null + } +} diff --git a/src/keeper/contracts/templates/BaseEscrowTemplate.abstract.ts b/src/keeper/contracts/templates/BaseEscrowTemplate.abstract.ts deleted file mode 100644 index 2d178d7..0000000 --- a/src/keeper/contracts/templates/BaseEscrowTemplate.abstract.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { AgreementTemplate } from './AgreementTemplate.abstract' -import { DDO } from '../../../ddo/DDO' -import { generateId, zeroX } from '../../../utils' -import { InstantiableConfig } from '../../../Instantiable.abstract' - -// import { EscrowComputeExecutionTemplateServiceAgreementTemplate } from './EscrowComputeExecutionTemplate.serviceAgreementTemplate' - -export abstract class BaseEscrowTemplate extends AgreementTemplate { - /** - * Create a agreement using EscrowComputeExecutionTemplate. - * @param {string} agreementId Generated agreement ID. - * @param {string} did Asset DID. - * @param {string[]} conditionIds List of conditions IDs. - * @param {number[]} timeLocks Timelocks. - * @param {number[]} timeOuts Timeouts. - * @param {string} accessConsumer Consumer address. - * @param {string} from Action sender. - * @param {any} Transaction receipt. - */ - public createAgreement( - agreementId: string, - did: string, - conditionIds: string[], - timeLocks: number[], - timeOuts: number[], - accessConsumer: string, - from?: string - ) { - return super.createAgreement( - agreementId, - did, - conditionIds, - timeLocks, - timeOuts, - [accessConsumer], - from - ) - } - - public async getAgreementData(agreementId: string) { - return this.call('getAgreementData', [zeroX(agreementId)]) - } -} diff --git a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.serviceAgreementTemplate.ts b/src/keeper/contracts/templates/EscrowAccess.serviceAgreementTemplate.ts similarity index 97% rename from src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.serviceAgreementTemplate.ts rename to src/keeper/contracts/templates/EscrowAccess.serviceAgreementTemplate.ts index ac26262..651ec6e 100644 --- a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.serviceAgreementTemplate.ts +++ b/src/keeper/contracts/templates/EscrowAccess.serviceAgreementTemplate.ts @@ -1,6 +1,6 @@ import { ServiceAgreementTemplate } from '../../../ddo/ServiceAgreementTemplate' -export const escrowAccessSecretStoreTemplateServiceAgreementTemplate: ServiceAgreementTemplate = { +export const escrowAccessServiceAgreementTemplate: ServiceAgreementTemplate = { contractName: 'EscrowAccessSecretStoreTemplate', events: [ { diff --git a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts index 77b6e4a..a906514 100644 --- a/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts +++ b/src/keeper/contracts/templates/EscrowAccessSecretStoreTemplate.ts @@ -1,108 +1,26 @@ -import { AgreementTemplate } from './AgreementTemplate.abstract' -import { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract' -import { DDO } from '../../../ddo/DDO' -import { generateId, zeroX } from '../../../utils' -import { InstantiableConfig } from '../../../Instantiable.abstract' +import { AgreementTemplateBase } from './AgreementTemplateBase' +import { escrowAccessServiceAgreementTemplate } from './EscrowAccess.serviceAgreementTemplate' -import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from './EscrowAccessSecretStoreTemplate.serviceAgreementTemplate' - -export class EscrowAccessSecretStoreTemplate extends BaseEscrowTemplate { - public static async getInstance( - config: InstantiableConfig - ): Promise { - return AgreementTemplate.getInstance( - config, - 'EscrowAccessSecretStoreTemplate', - EscrowAccessSecretStoreTemplate - ) - } +export class EscrowAccessSecretStoreTemplate extends AgreementTemplateBase { + public static templateName: 'EscrowAccessSecretStoreTemplate' public async getServiceAgreementTemplate() { - return escrowAccessSecretStoreTemplateServiceAgreementTemplate + return escrowAccessServiceAgreementTemplate } - public async createAgreementFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ) { - return !!(await this.createFullAgreement( - ddo.shortId(), - ddo.findServiceByType('metadata').attributes.main.price, - consumer, - from, - agreementId - )) - } - - public async getAgreementIdsFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ) { - const { - accessSecretStoreConditionId, - lockRewardConditionId, - escrowRewardId - } = await this.createFullAgreementData( - agreementId, - ddo.shortId(), - ddo.findServiceByType('metadata').attributes.main.price, - consumer - ) - return [accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId] - } - - /** - * Create a agreement using EscrowAccessSecretStoreTemplate using only the most important information. - * @param {string} did Asset DID. - * @param {number} amount Asset price. - * @param {string} from Consumer address. - * @return {Promise} Agreement ID. - */ - public async createFullAgreement( - did: string, - amount: number | string, - consumer: string, - from?: string, - agreementId: string = generateId() - ): Promise { - const { - accessSecretStoreConditionId, - lockRewardConditionId, - escrowRewardId - } = await this.createFullAgreementData(agreementId, did, amount, consumer) - - await this.createAgreement( - agreementId, - did, - [accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId], - [0, 0, 0], - [0, 0, 0], - consumer, - from - ) - - return zeroX(agreementId) - } - - private async createFullAgreementData( + protected async createFullAgreementData( agreementId: string, did: string, amount: number | string, consumer: string ) { - const { didRegistry, conditions } = this.ocean.keeper - const { - accessSecretStoreCondition, lockRewardCondition, + accessSecretStoreCondition, escrowReward - } = conditions + } = this.conditions - const publisher = await didRegistry.getDIDOwner(did) + const publisher = await this.didRegistry.getDIDOwner(did) const lockRewardConditionId = await lockRewardCondition.generateIdHash( agreementId, @@ -123,10 +41,6 @@ export class EscrowAccessSecretStoreTemplate extends BaseEscrowTemplate { accessSecretStoreConditionId ) - return { - lockRewardConditionId, - accessSecretStoreConditionId, - escrowRewardId - } + return [lockRewardConditionId, accessSecretStoreConditionId, escrowRewardId] } } diff --git a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.serviceAgreementTemplate.ts b/src/keeper/contracts/templates/EscrowCompute.serviceAgreementTemplate.ts similarity index 97% rename from src/keeper/contracts/templates/EscrowComputeExecutionTemplate.serviceAgreementTemplate.ts rename to src/keeper/contracts/templates/EscrowCompute.serviceAgreementTemplate.ts index 149ac2e..732c20d 100644 --- a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.serviceAgreementTemplate.ts +++ b/src/keeper/contracts/templates/EscrowCompute.serviceAgreementTemplate.ts @@ -1,6 +1,6 @@ import { ServiceAgreementTemplate } from '../../../ddo/ServiceAgreementTemplate' -export const escrowComputeExecutionTemplateServiceAgreementTemplate: ServiceAgreementTemplate = { +export const escrowComputeServiceAgreementTemplate: ServiceAgreementTemplate = { contractName: 'EscrowComputeExecutionTemplate', events: [ { diff --git a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts b/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts index b1e3c3e..0bf41b1 100644 --- a/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts +++ b/src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts @@ -1,108 +1,26 @@ -import { AgreementTemplate } from './AgreementTemplate.abstract' -import { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract' -import { DDO } from '../../../ddo/DDO' -import { generateId, zeroX } from '../../../utils' -import { InstantiableConfig } from '../../../Instantiable.abstract' +import { AgreementTemplateBase } from './AgreementTemplateBase' +import { escrowComputeServiceAgreementTemplate } from './EscrowCompute.serviceAgreementTemplate' -import { escrowComputeExecutionTemplateServiceAgreementTemplate } from './EscrowComputeExecutionTemplate.serviceAgreementTemplate' - -export class EscrowComputeExecutionTemplate extends BaseEscrowTemplate { - public static async getInstance( - config: InstantiableConfig - ): Promise { - return AgreementTemplate.getInstance( - config, - 'EscrowComputeExecutionTemplate', - EscrowComputeExecutionTemplate - ) - } +export class EscrowComputeExecutionTemplate extends AgreementTemplateBase { + public static templateName: 'EscrowComputeExecutionTemplate' public async getServiceAgreementTemplate() { - return escrowComputeExecutionTemplateServiceAgreementTemplate + return escrowComputeServiceAgreementTemplate } - public async createAgreementFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ) { - return !!(await this.createFullAgreement( - ddo.shortId(), - ddo.findServiceByType('metadata').attributes.main.price, - consumer, - from, - agreementId - )) - } - - public async getAgreementIdsFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ) { - const { - computeExecutionConditionId, - lockRewardConditionId, - escrowRewardId - } = await this.createFullAgreementData( - agreementId, - ddo.shortId(), - ddo.findServiceByType('metadata').attributes.main.price, - consumer - ) - return [computeExecutionConditionId, lockRewardConditionId, escrowRewardId] - } - - /** - * Create a agreement using EscrowAccess____SecretStoreTemplate using only the most important information. - * @param {string} did Asset DID. - * @param {number} amount Asset price. - * @param {string} from Consumer address. - * @return {Promise} Agreement ID. - */ - public async createFullAgreement( - did: string, - amount: number | string, - consumer: string, - from?: string, - agreementId: string = generateId() - ): Promise { - const { - computeExecutionConditionId, - lockRewardConditionId, - escrowRewardId - } = await this.createFullAgreementData(agreementId, did, amount, consumer) - - await this.createAgreement( - agreementId, - did, - [computeExecutionConditionId, lockRewardConditionId, escrowRewardId], - [0, 0, 0], - [0, 0, 0], - consumer, - from - ) - - return zeroX(agreementId) - } - - private async createFullAgreementData( + protected async createFullAgreementData( agreementId: string, did: string, amount: number | string, consumer: string ) { - const { didRegistry, conditions } = this.ocean.keeper - const { - computeExecutionCondition, lockRewardCondition, + computeExecutionCondition, escrowReward - } = conditions + } = this.conditions - const publisher = await didRegistry.getDIDOwner(did) + const publisher = await this.didRegistry.getDIDOwner(did) const lockRewardConditionId = await lockRewardCondition.generateIdHash( agreementId, diff --git a/src/ocean/OceanAgreements.ts b/src/ocean/OceanAgreements.ts index 2d83d07..5a1bd9e 100644 --- a/src/ocean/OceanAgreements.ts +++ b/src/ocean/OceanAgreements.ts @@ -57,7 +57,7 @@ export class OceanAgreements extends Instantiable { .serviceAgreementTemplate.contractName const agreementConditionsIds = await this.ocean.keeper .getTemplateByName(templateName) - .getAgreementIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId()) + .getConditionIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId()) const signature = await this.ocean.utils.agreements.signServiceAgreement( ddo, From fa24427a151c4d14c2f52061d8559d6d49c4f307 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 15 Jan 2020 20:04:50 +0100 Subject: [PATCH 2/2] bump brizo & events-handler --- .travis.yml | 4 ++-- library.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 027e21e..8901f65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,9 @@ before_script: - git clone https://github.com/oceanprotocol/barge - cd barge - export AQUARIUS_VERSION=v1.0.5 - - export BRIZO_VERSION=v0.8.0 + - export BRIZO_VERSION=v0.8.1 - export KEEPER_VERSION=v0.13.2 - - export EVENTS_HANDLER_VERSION=v0.4.0 + - export EVENTS_HANDLER_VERSION=v0.4.1 - export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" - rm -rf "${HOME}/.ocean/keeper-contracts/artifacts" - bash -x start_ocean.sh --no-commons --no-dashboard 2>&1 > start_ocean.log & diff --git a/library.json b/library.json index 449ee6d..9d0cb91 100644 --- a/library.json +++ b/library.json @@ -11,7 +11,7 @@ }, { "name": "brizo", - "version": "~0.8.0" + "version": "~0.8.1" }, { "name": "aquarius", @@ -19,7 +19,7 @@ }, { "name": "events-handler", - "version": "~0.4.0" + "version": "~0.4.1" } ] }