1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

add execution of compute jobs

This commit is contained in:
Matthias Kretschmann 2020-01-09 12:20:19 +01:00
parent ca67de1254
commit ae7768253e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 133 additions and 60 deletions

View File

@ -1,4 +1,4 @@
import { File } from '../ddo/MetaData' import { File, MetaData } from '../ddo/MetaData'
import Account from '../ocean/Account' import Account from '../ocean/Account'
import { noZeroX } from '../utils' import { noZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
@ -35,13 +35,8 @@ export class Brizo extends Instantiable {
return `${this.url}${apiPath}/publish` return `${this.url}${apiPath}/publish`
} }
public getComputeEndpoint( public getComputeEndpoint() {
pubKey: string, return `${this.url}${apiPath}/exec`
serviceIndex: number,
_notUsed: string,
container: string
) {
return `${this.url}${apiPath}/compute`
} }
public async initializeServiceAgreement( public async initializeServiceAgreement(
@ -78,12 +73,7 @@ export class Brizo extends Instantiable {
destination: string, destination: string,
index: number = -1 index: number = -1
): Promise<string> { ): Promise<string> {
const signature = const signature = await this.createSignature(account, agreementId)
(await account.getToken()) ||
(await this.ocean.utils.signature.signText(
noZeroX(agreementId),
account.getId()
))
const filesPromises = files const filesPromises = files
.filter((_, i) => index === -1 || i === index) .filter((_, i) => index === -1 || i === index)
.map(async ({ index: i }) => { .map(async ({ index: i }) => {
@ -105,6 +95,59 @@ export class Brizo extends Instantiable {
return destination return destination
} }
public async executeService(
agreementId: string,
serviceEndpoint: string,
account: Account,
algorithmDid: string,
algorithm: string,
algorithmMeta?: MetaData
): Promise<string> {
const signature = await this.createSignature(account, agreementId)
let executeUrl = serviceEndpoint
executeUrl += `&signature=${signature}`
executeUrl += `&serviceAgreementId=${noZeroX(agreementId)}`
executeUrl += `&consumerAddress=${account.getId()}`
executeUrl += `&algorithmDID=${algorithmDid}`
executeUrl += `&algorithm=${algorithm}`
executeUrl += `&algorithmMeta=${algorithmMeta}`
const result: { workflowId: string } = await this.ocean.utils.fetch
.post(executeUrl, '')
.then((response: any) => {
if (response.ok) {
return response.json()
}
this.logger.error(
'Executing compute job failed:',
response.status,
response.statusText
)
return null
})
.catch(error => {
this.logger.error('Error executing compute job')
this.logger.error(error)
throw error
})
return result.workflowId
}
public async createSignature(account: Account, agreementId: string): Promise<string> {
const signature =
(await account.getToken()) ||
(await this.ocean.utils.signature.signText(
noZeroX(agreementId),
account.getId()
))
return signature
}
public async encrypt( public async encrypt(
did: string, did: string,
signature: string, signature: string,

View File

@ -1,25 +0,0 @@
export interface Provider {
type: string
description: string
environment: {
cluster: {
type: string
url: string
}
supportedContainers: {
image: string
tag: string
checksum: string
}[]
supportedServers: {
serverId: string
serverType: string
price: string
cpu: string
gpu: string
memory: string
disk: string
maxExecutionTime: number
}[]
}
}

View File

@ -1,14 +1,7 @@
import { MetaData } from './MetaData' import { MetaData } from './MetaData'
import { ServiceAgreementTemplate } from './ServiceAgreementTemplate' import { ServiceAgreementTemplate } from './ServiceAgreementTemplate'
import { Provider } from './ComputingProvider'
export type ServiceType = export type ServiceType = 'authorization' | 'metadata' | 'access' | 'compute'
| 'authorization'
| 'metadata'
| 'access'
| 'compute'
| 'computing'
| 'fitchainCompute'
export interface ServiceCommon { export interface ServiceCommon {
type: ServiceType type: ServiceType
@ -47,15 +40,45 @@ export interface ServiceAccess extends ServiceCommon {
} }
} }
export interface ServiceComputing extends ServiceCommon { export interface ServiceCompute extends ServiceCommon {
type: 'computing' type: 'compute'
templateId?: string templateId?: string
provider?: Provider attributes: {
serviceAgreementTemplate?: ServiceAgreementTemplate main: {
creator: string
datePublished: string
price: string
timeout: number
provider?: ServiceComputeProvider
serviceAgreementTemplate?: ServiceAgreementTemplate
}
}
} }
export interface ServiceCompute extends ServiceCommon { export interface ServiceComputeProvider {
templateId?: string type: string
description: string
environment: {
cluster: {
type: string
url: string
}
supportedContainers: {
image: string
tag: string
checksum: string
}[]
supportedServers: {
serverId: string
serverType: string
price: string
cpu: string
gpu: string
memory: string
disk: string
maxExecutionTime: number
}[]
}
} }
export type Service< export type Service<
@ -64,8 +87,6 @@ export type Service<
? ServiceAuthorization ? ServiceAuthorization
: T extends 'metadata' : T extends 'metadata'
? ServiceMetadata ? ServiceMetadata
: T extends 'computing'
? ServiceComputing
: T extends 'access' : T extends 'access'
? ServiceAccess ? ServiceAccess
: T extends 'compute' : T extends 'compute'

View File

@ -206,6 +206,7 @@ export class OceanAssets extends Instantiable {
useSecretStore?: boolean useSecretStore?: boolean
): Promise<string> ): Promise<string>
/* eslint-disable no-dupe-class-members */
public async consume( public async consume(
agreementId: string, agreementId: string,
did: string, did: string,
@ -276,19 +277,20 @@ export class OceanAssets extends Instantiable {
} }
return true return true
} }
/* eslint-enable no-dupe-class-members */
/** /**
* Start the purchase/order of an asset's service. Starts by signing the service agreement * Start the purchase/order of an asset's service. Starts by signing the service agreement
* then sends the request to the publisher via the service endpoint (Brizo http service). * then sends the request to the publisher via the service endpoint (Brizo http service).
* @param {string} did Decentralized ID. * @param {string} did Decentralized ID.
* @param {number} index Service index. * @param {number} index Service index.
* @param {Account} consumer Consumer account. * @param {Account} consumerAccount Consumer account.
* @return {Promise<string>} Returns Agreement ID * @return {Promise<string>} Returns Agreement ID
*/ */
public order( public order(
did: string, did: string,
index: number, index: number,
consumer: Account consumerAccount: Account
): SubscribablePromise<OrderProgressStep, string> { ): SubscribablePromise<OrderProgressStep, string> {
return new SubscribablePromise(async observer => { return new SubscribablePromise(async observer => {
const oceanAgreements = this.ocean.agreements const oceanAgreements = this.ocean.agreements
@ -321,7 +323,7 @@ export class OceanAssets extends Instantiable {
const paid = await oceanAgreements.conditions.lockReward( const paid = await oceanAgreements.conditions.lockReward(
agreementId, agreementId,
attributes.main.price, attributes.main.price,
consumer consumerAccount
) )
observer.next(OrderProgressStep.LockedPayment) observer.next(OrderProgressStep.LockedPayment)
@ -347,8 +349,8 @@ export class OceanAssets extends Instantiable {
agreementId, agreementId,
index, index,
undefined, undefined,
consumer, consumerAccount,
consumer consumerAccount
) )
this.logger.log('Agreement created') this.logger.log('Agreement created')
@ -362,6 +364,38 @@ export class OceanAssets extends Instantiable {
}) })
} }
/**
* Start the execution of a compute job.
* @param {string} agreementId ID of the agreement.
* @param {DDO} computeDdo DDO of the compute asset.
* @param {Account} consumerAccount Consumer account.
* @param {string} algorithmDid The asset DID (of type `algorithm`) which consist of `did:op:` and the `assetId` hex str (without `0x` prefix).
* @param {string} algorithm The text of the algorithm to run in the compute job (e.g. a jupyter notebook)
* @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDID` is specified.
* @return {Promise<string>} Returns Workflow ID
*/
public async execute(
agreementId: string,
computeDdo: DDO,
consumerAccount: Account,
algorithmDid: string,
algorithm: string,
algorithmMeta?: MetaData
): Promise<string> {
const { serviceEndpoint } = computeDdo.findServiceByType('compute')
const workflowId = await this.ocean.brizo.executeService(
agreementId,
serviceEndpoint,
consumerAccount,
algorithmDid,
algorithm,
algorithmMeta
)
return workflowId
}
/** /**
* Returns the owner of a asset. * Returns the owner of a asset.
* @param {string} did Decentralized ID. * @param {string} did Decentralized ID.