Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom provider #288

Merged
merged 30 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
17e61d0
allow customProvider on publish
alexcos20 Sep 16, 2020
48cb847
extra check
alexcos20 Sep 16, 2020
960e90f
extra checks
alexcos20 Sep 16, 2020
cdc5dc8
add tests
alexcos20 Sep 16, 2020
c367fbb
Update .travis.yml
alexcos20 Sep 16, 2020
071f706
Merge branch 'feature/startOrder' into feature/customProvider
alexcos20 Sep 16, 2020
3e79a3a
Update .travis.yml
alexcos20 Sep 16, 2020
11b58cb
more customProvider code
alexcos20 Sep 19, 2020
fbc6e80
Merge branch 'main' into feature/customProvider
alexcos20 Sep 19, 2020
ae40119
update travis
alexcos20 Sep 19, 2020
f75e455
allow customProvider on publish
alexcos20 Sep 16, 2020
ed5e3e7
extra check
alexcos20 Sep 16, 2020
554ab76
extra checks
alexcos20 Sep 16, 2020
e71180e
add tests
alexcos20 Sep 16, 2020
796b702
more customProvider code
alexcos20 Sep 19, 2020
671c170
update travis
alexcos20 Sep 19, 2020
5655909
allow customProvider on publish
alexcos20 Sep 16, 2020
20493df
extra check
alexcos20 Sep 16, 2020
d10766d
extra checks
alexcos20 Sep 16, 2020
67f6e16
more customProvider code
alexcos20 Sep 19, 2020
3402236
Merge branch 'feature/customProvider' of https://github.com/oceanprot…
alexcos20 Sep 19, 2020
8665649
use custom provider
alexcos20 Sep 19, 2020
3f01792
fix lint
alexcos20 Sep 19, 2020
62f27b0
fix tests
alexcos20 Sep 19, 2020
023c258
Merge branch 'main' into feature/customProvider
alexcos20 Sep 22, 2020
2d989df
fix namings
alexcos20 Sep 22, 2020
1245377
Update src/provider/Provider.ts
alexcos20 Sep 22, 2020
f150a39
Update src/provider/Provider.ts
alexcos20 Sep 22, 2020
7f6c192
fix namings
alexcos20 Sep 22, 2020
fd64b6c
fix typo
alexcos20 Sep 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ deploy:
api_key: ${NPM_TOKEN}
skip_cleanup: true
on:
tags: true
tags: true
44 changes: 27 additions & 17 deletions src/ocean/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SubscribablePromise, didZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import { WebServiceConnector } from './utils/WebServiceConnector'
import BigNumber from 'bignumber.js'
import { Provider } from '../provider/Provider'
import { isAddress } from 'web3-utils'

export enum CreateProgressStep {
Expand Down Expand Up @@ -56,6 +57,7 @@ export class Assets extends Instantiable {
* @param {String} cap Maximum cap (Number) - will be converted to wei
* @param {String} name Token name
* @param {String} symbol Token symbol
* @param {String} providerUri
* @return {Promise<DDO>}
*/
public create(
Expand All @@ -65,7 +67,8 @@ export class Assets extends Instantiable {
dtAddress?: string,
cap?: string,
name?: string,
symbol?: string
symbol?: string,
providerUri?: string
): SubscribablePromise<CreateProgressStep, DDO> {
if (!isAddress(dtAddress)) {
this.logger.error(
Expand Down Expand Up @@ -109,7 +112,12 @@ export class Assets extends Instantiable {

this.logger.log('Encrypting files')
observer.next(CreateProgressStep.EncryptingFiles)
const encryptedFiles = await this.ocean.provider.encrypt(
let provider
if (providerUri) {
provider = new Provider(this.instanceConfig)
provider.setBaseUrl(providerUri)
} else provider = this.ocean.provider
const encryptedFiles = await provider.encrypt(
did.getId(),
metadata.main.files,
publisher
Expand Down Expand Up @@ -395,12 +403,13 @@ export class Assets extends Instantiable {
creator: Account,
cost: string,
datePublished: string,
timeout = 0
timeout = 0,
providerUri?: string
): Promise<ServiceAccess> {
return {
type: 'access',
index: 2,
serviceEndpoint: this.ocean.provider.getConsumeEndpoint(),
serviceEndpoint: providerUri || this.ocean.provider.url,
attributes: {
main: {
creator: creator.getId(),
Expand Down Expand Up @@ -428,14 +437,12 @@ export class Assets extends Instantiable {
did: string,
serviceType: string,
consumerAddress: string,
serviceIndex = -1
serviceIndex = -1,
serviceEndpoint: string
): Promise<any> {
const res = await this.ocean.provider.initialize(
did,
serviceIndex,
serviceType,
consumerAddress
)
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
const res = await provider.initialize(did, serviceIndex, serviceType, consumerAddress)
if (res === null) return null
const providerData = JSON.parse(res)
return providerData
Expand All @@ -457,11 +464,12 @@ export class Assets extends Instantiable {
serviceIndex = -1,
mpAddress?: string
): Promise<string> {
let service
if (serviceIndex === -1) {
const service = await this.getServiceByType(did, serviceType)
service = await this.getServiceByType(did, serviceType)
serviceIndex = service.index
} else {
const service = await this.getServiceByIndex(did, serviceIndex)
service = await this.getServiceByIndex(did, serviceIndex)
serviceType = service.type
}
const { datatokens } = this.ocean
Expand All @@ -470,10 +478,11 @@ export class Assets extends Instantiable {
did,
serviceType,
consumerAddress,
serviceIndex
serviceIndex,
service.serviceEndpoint
)
if (!providerData) return null
const service = await this.getServiceByIndex(did, serviceIndex)
service = await this.getServiceByIndex(did, serviceIndex)
const previousOrder = await datatokens.getPreviousValidOrders(
providerData.dataToken,
providerData.numTokens,
Expand Down Expand Up @@ -538,8 +547,9 @@ export class Assets extends Instantiable {
destination = destination
? `${destination}/datafile.${ddo.shortId()}.${service.index}/`
: undefined

await this.ocean.provider.download(
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
await provider.download(
did,
txId,
tokenAddress,
Expand Down
46 changes: 39 additions & 7 deletions src/ocean/Compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SubscribablePromise } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
import { Output } from './interfaces/ComputeOutput'
import { ComputeJob } from './interfaces/ComputeJob'
import { Provider } from '../provider/Provider'

export enum OrderProgressStep {
TransferDataToken
Expand Down Expand Up @@ -87,8 +88,13 @@ export class Compute extends Instantiable {
algorithmDataToken?: string
): Promise<ComputeJob> {
output = this.checkOutput(consumerAccount, output)
const ddo = await this.ocean.assets.resolve(did)
const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service
if (did && txId) {
const computeJobsList = await this.ocean.provider.compute(
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.compute(
'post',
did,
consumerAccount,
Expand Down Expand Up @@ -119,7 +125,12 @@ export class Compute extends Instantiable {
did: string,
jobId: string
): Promise<ComputeJob> {
const computeJobsList = await this.ocean.provider.compute(
const ddo = await this.ocean.assets.resolve(did)
const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.compute(
'put',
did,
consumerAccount,
Expand All @@ -143,7 +154,12 @@ export class Compute extends Instantiable {
did: string,
jobId: string
): Promise<ComputeJob> {
const computeJobsList = await this.ocean.provider.compute(
const ddo = await this.ocean.assets.resolve(did)
const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.compute(
'delete',
did,
consumerAccount,
Expand All @@ -167,7 +183,17 @@ export class Compute extends Instantiable {
did?: string,
jobId?: string
): Promise<ComputeJob[]> {
const computeJobsList = await this.ocean.provider.compute(
let provider
if (did) {
const ddo = await this.ocean.assets.resolve(did)
const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service
provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
} else {
provider = this.ocean.provider
}
const computeJobsList = await provider.compute(
'get',
did,
consumerAccount,
Expand All @@ -191,7 +217,12 @@ export class Compute extends Instantiable {
did: string,
jobId: string
): Promise<ComputeJob> {
const computeJobsList = await this.ocean.provider.compute(
const ddo = await this.ocean.assets.resolve(did)
const service = ddo.findServiceByType('compute')
const { serviceEndpoint } = service
const provider = new Provider(this.instanceConfig)
provider.setBaseUrl(serviceEndpoint)
const computeJobsList = await provider.compute(
'get',
did,
consumerAccount,
Expand Down Expand Up @@ -279,14 +310,15 @@ export class Compute extends Instantiable {
datePublished: string,
providerAttributes: any,
computePrivacy?: ServiceComputePrivacy,
timeout?: number
timeout?: number,
providerUri?: string
): ServiceCompute {
const name = 'dataAssetComputingService'
if (!timeout) timeout = 3600
const service = {
type: 'compute',
index: 3,
serviceEndpoint: this.ocean.provider.getComputeEndpoint(),
serviceEndpoint: providerUri || this.ocean.provider.url,
attributes: {
main: {
name,
Expand Down
40 changes: 36 additions & 4 deletions src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ const apiPath = '/api/v1/services'
*/
export class Provider extends Instantiable {
public nonce: string
private get url() {
return this.config.providerUri
private baseUrl: string
public get url() {
return this.baseUrl
}

constructor(config: InstantiableConfig) {
super()
this.setInstanceConfig(config)
this.baseUrl = this.config.providerUri
this.nonce = '0'
}

public setBaseUrl(url: string): void {
this.baseUrl = url
}

public async createSignature(account: Account, agreementId: string): Promise<string> {
const signature = await this.ocean.utils.signature.signText(
noZeroX(agreementId),
Expand Down Expand Up @@ -264,8 +270,12 @@ export class Provider extends Instantiable {
return `${this.url}${apiPath}/nonce`
}

public getConsumeEndpointPath(): string {
return `${apiPath}/consume`
}

public getConsumeEndpoint(): string {
return `${this.url}${apiPath}/consume`
return `${this.url}` + this.getConsumeEndpointPath()
}

public getEncryptEndpoint(): string {
Expand All @@ -276,11 +286,33 @@ export class Provider extends Instantiable {
return `${this.url}${apiPath}/publish`
}

public getComputeEndpointPath(): string {
return `${apiPath}/compute`
}

public getComputeEndpoint(): string {
return `${this.url}${apiPath}/compute`
return `${this.url}` + this.getComputeEndpointPath()
}

public getDownloadEndpoint(): string {
return `${this.url}${apiPath}/download`
}

/** Check for a valid provider at URL
* @param {String} url
* @return {Promise<boolean>} string
*/
public async isValidProvider(url: string): Promise<boolean> {
try {
const response = await this.ocean.utils.fetch.get(url)
if (response?.ok) {
const params = response.json()
alexcos20 marked this conversation as resolved.
Show resolved Hide resolved
if (params && params['provider-address']) return true
}
return false
} catch (error) {
this.logger.error(`Error validating provider: ${error.message}`)
return false
}
}
}
18 changes: 18 additions & 0 deletions test/integration/Provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Ocean } from '../../src/ocean/Ocean'
import config from './config'
import { assert } from 'console'

describe('Provider tests', () => {
let ocean
it('Initialize Ocean', async () => {
ocean = await Ocean.getInstance(config)
})
it('Alice tests invalid provider', async () => {
const valid = ocean.provider.isValidProvider('http://example.net')
assert(valid === false)
})
it('Alice tests valid provider', async () => {
const valid = ocean.provider.isValidProvider('http://127.0.0.1:8030')
assert(valid === true)
})
})