Skip to content

Commit

Permalink
Merge branch 'v4main' into feature/add-reuseOrder-method
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanfazakas committed Apr 15, 2022
2 parents 65a0006 + a92ad71 commit eb70d61
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 73 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ 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).

#### [v1.0.0-next.36](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.35...v1.0.0-next.36)

- Fix get compute results [`#1386`](https://github.com/oceanprotocol/ocean.js/pull/1386)

#### [v1.0.0-next.35](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.34...v1.0.0-next.35)

> 14 April 2022
- fix mm signature [`#1420`](https://github.com/oceanprotocol/ocean.js/pull/1420)
- setPaymentCollector apply same checking as contract [`#1396`](https://github.com/oceanprotocol/ocean.js/pull/1396)
- Release 1.0.0-next.35 [`80bf5cc`](https://github.com/oceanprotocol/ocean.js/commit/80bf5cc33fa7f5e3b4cf47805ce53510c563b29b)

#### [v1.0.0-next.34](https://github.com/oceanprotocol/ocean.js/compare/v1.0.0-next.33...v1.0.0-next.34)

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oceanprotocol/lib",
"source": "./src/index.ts",
"version": "1.0.0-next.35",
"version": "1.0.0-next.36",
"description": "JavaScript client library for Ocean Protocol",
"main": "./dist/lib.js",
"umd:main": "dist/lib.umd.js",
Expand Down Expand Up @@ -40,6 +40,7 @@
"test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit",
"test:integration": "npm run mocha -- 'test/integration/**/*.test.ts'",
"test:provider": "npm run mocha -- 'test/integration/Provider.test.ts'",
"test:compute": "npm run mocha -- 'test/integration/ComputeFlow.test.ts'",
"test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration"
},
"repository": {
Expand Down Expand Up @@ -72,7 +73,7 @@
"@types/chai-spies": "^1.0.3",
"@types/crypto-js": "^4.1.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.23",
"@types/node": "^17.0.24",
"@types/node-fetch": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand Down
6 changes: 5 additions & 1 deletion src/@types/Compute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Metadata, MetadataAlgorithm } from './DDO/Metadata'

export type ComputeResultType = 'algorithmLog' | 'output'
export type ComputeResultType =
| 'algorithmLog'
| 'output'
| 'configrationLog'
| 'publishLog'

export interface ComputeEnvironment {
id: string
Expand Down
78 changes: 30 additions & 48 deletions src/provider/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Web3 from 'web3'
import { LoggerInstance, getData, downloadFile, downloadFileBrowser } from '../utils'
import { LoggerInstance, getData } from '../utils'
import {
FileMetadata,
ComputeJob,
Expand All @@ -11,7 +11,6 @@ import {
} from '../@types/'
import { noZeroX } from '../utils/ConversionTypeHelper'
import fetch from 'cross-fetch'
import { DownloadResponse } from '../@types/DownloadResponse'
export interface HttpCallback {
(httpMethod: string, url: string, body: string, header: any): Promise<any>
}
Expand Down Expand Up @@ -66,7 +65,7 @@ export class Provider {
return serviceEndpoints
}

/** Encrypt DDO using the Provider's own symmetric key
/** Gets current nonce
* @param {string} providerUri provider uri address
* @param {string} consumerAddress Publisher address
* @param {AbortSignal} signal abort signal
Expand Down Expand Up @@ -142,12 +141,11 @@ export class Provider {
const path = this.getEndpointURL(serviceEndpoints, 'encrypt')
? this.getEndpointURL(serviceEndpoints, 'encrypt').urlPath
: null

if (!path) return null
try {
const response = await fetch(path, {
method: 'POST',
body: decodeURI(JSON.stringify(data)),
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/octet-stream'
},
Expand Down Expand Up @@ -437,7 +435,7 @@ export class Provider {
const params = await response.json()
return params
}
console.error('Compute start failed:', response.status, response.statusText)
LoggerInstance.error('Compute start failed: ', response.status, response.statusText)
LoggerInstance.error('Payload was:', payload)
return null
} catch (e) {
Expand Down Expand Up @@ -577,23 +575,21 @@ export class Provider {
}
}

/** Get status for a specific jobId/documentId/owner.
* @param {string} jobId
* @param {number} index
* @param {string} providerUri
* @param {string} destination
* @param {Web3} web3
* @param {AbortSignal} signal abort signal
* @return {Promise<ComputeJob | ComputeJob[]>}
/** Get compute result url
* @param {string} providerUri The URI of the provider we want to query
* @param {Web3} web3 Web3 instance
* @param {string} consumerAddress The consumer ethereum address
* @param {string} jobId The ID of a compute job.
* @param {number} index Result index
* @return {Promise<string>}
*/
public async computeResult(
jobId: string,
index: number,
accountId: string,
public async getComputeResultUrl(
providerUri: string,
web3: Web3,
signal?: AbortSignal
): Promise<DownloadResponse | void> {
consumerAddress: string,
jobId: string,
index: number
): Promise<string> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
Expand All @@ -603,38 +599,24 @@ export class Provider {
? this.getEndpointURL(serviceEndpoints, 'computeResult').urlPath
: null

const nonce = await this.getNonce(
providerUri,
accountId,
signal,
providerEndpoints,
serviceEndpoints
)

let signatureMessage = accountId
const nonce = Date.now()
let signatureMessage = consumerAddress
signatureMessage += jobId
signatureMessage += index.toString()
signatureMessage += nonce
const signature = await this.signProviderRequest(web3, accountId, signatureMessage)

let consumeUrl = computeResultUrl
consumeUrl += `?consumerAddress=${accountId}`
consumeUrl += `&jobId=${jobId}`
consumeUrl += `&index=${String(index)}`
consumeUrl += (signature && `&signature=${signature}`) || ''

const signature = await this.signProviderRequest(
web3,
consumerAddress,
signatureMessage
)
if (!computeResultUrl) return null
try {
if (document) {
await downloadFileBrowser(consumeUrl)
} else {
return await downloadFile(consumeUrl, index)
}
} catch (e) {
LoggerInstance.error('Error getting job result')
LoggerInstance.error(e)
throw e
}
let resultUrl = computeResultUrl
resultUrl += `?consumerAddress=${consumerAddress}`
resultUrl += `&jobId=${jobId}`
resultUrl += `&index=${index.toString()}`
resultUrl += `&nonce=${nonce}`
resultUrl += (signature && `&signature=${signature}`) || ''
return resultUrl
}

/** Deletes a compute job.
Expand Down
7 changes: 6 additions & 1 deletion src/utils/DdoHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sha256 from 'crypto-js/sha256'
import Web3 from 'web3'
import LoggerInstance from './Logger'

export function generateDid(erc721Address: string, chainId: number): string {
erc721Address = Web3.utils.toChecksumAddress(erc721Address)
Expand All @@ -8,5 +9,9 @@ export function generateDid(erc721Address: string, chainId: number): string {
}

export function getHash(data: any): string {
return sha256(data).toString()
try {
return sha256(data).toString()
} catch (e) {
LoggerInstance.error('getHash error: ', e.message)
}
}
40 changes: 28 additions & 12 deletions test/integration/ComputeFlow.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert } from 'chai'
import { SHA256 } from 'crypto-js'
import console from 'console'
import { web3, getTestConfig, getAddresses } from '../config'
import {
Config,
Expand All @@ -10,9 +9,10 @@ import {
NftCreateData,
Datatoken,
getHash,
Nft
Nft,
sleep
} from '../../src'
import { ProviderFees, Erc20CreateParams } from '../../src/@types'
import { ProviderFees, Erc20CreateParams, ComputeJob, Asset } from '../../src/@types'

const assetUrl = [
{
Expand Down Expand Up @@ -110,12 +110,15 @@ const algoDdo = {
}
]
}
let providerUrl: string
let consumerAccount: string
let computeJobId: string
let resolvedDDOAsset: Asset

describe('Simple compute tests', async () => {
let config: Config
let addresses: any
let aquarius: Aquarius
let providerUrl: any

before(async () => {
config = await getTestConfig(web3)
Expand All @@ -130,7 +133,7 @@ describe('Simple compute tests', async () => {
const Factory = new NftFactory(addresses.ERC721Factory, web3)
const accounts = await web3.eth.getAccounts()
const publisherAccount = accounts[0]
const consumerAccount = accounts[1]
consumerAccount = accounts[1]
const chain = await web3.eth.getChainId()
const nftParamsAsset: NftCreateData = {
name: 'testNFT',
Expand Down Expand Up @@ -234,7 +237,7 @@ describe('Simple compute tests', async () => {
)

// let's wait
const resolvedDDOAsset = await aquarius.waitForAqua(ddo.id)
resolvedDDOAsset = await aquarius.waitForAqua(ddo.id)
assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius')
const resolvedDDOAlgo = await aquarius.waitForAqua(algoDdo.id)
assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius')
Expand All @@ -246,7 +249,6 @@ describe('Simple compute tests', async () => {
const computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl)
assert(computeEnvs, 'No Compute environments found')
// we choose the first env
console.log(computeEnvs)
const computeEnv = computeEnvs[0].id
const computeConsumerAddress = computeEnvs[0].consumerAddress
// let's have 10 minutesof compute access
Expand All @@ -265,7 +267,6 @@ describe('Simple compute tests', async () => {
computeEnv,
computeValidUntil
)
console.log(initializeDataAlgo)
const providerAlgoFees: ProviderFees = {
providerFeeAddress: initializeDataAlgo.providerFee.providerFeeAddress,
providerFeeToken: initializeDataAlgo.providerFee.providerFeeToken,
Expand Down Expand Up @@ -302,7 +303,6 @@ describe('Simple compute tests', async () => {
computeEnv,
computeValidUntil
)
console.log(initializeData)
const providerDatasetFees: ProviderFees = {
providerFeeAddress: initializeData.providerFee.providerFeeAddress,
providerFeeToken: initializeData.providerFee.providerFeeToken,
Expand Down Expand Up @@ -340,12 +340,28 @@ describe('Simple compute tests', async () => {
}
)
assert(computeJobs, 'Cannot start compute job')
const jobStatus = await ProviderInstance.computeStatus(
computeJobId = computeJobs[0].jobId
})

it('Check compute status', async () => {
const jobStatus = (await ProviderInstance.computeStatus(
providerUrl,
consumerAccount,
computeJobs[0].jobId,
computeJobId,
resolvedDDOAsset.id
)) as ComputeJob
assert(jobStatus, 'Cannot retrieve compute status!')
})

it('Get download compute results url', async () => {
sleep(10000)
const downloadURL = await ProviderInstance.getComputeResultUrl(
providerUrl,
web3,
consumerAccount,
computeJobId,
0
)
assert(jobStatus)
assert(downloadURL, 'Provider getComputeResultUrl failed!')
})
})

0 comments on commit eb70d61

Please sign in to comment.