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

Merge pull request #120 from oceanprotocol/bugfix/#114-consuming-asset-fails

Improved integration test error detection and fixed some bugs.
This commit is contained in:
Pedro Gutiérrez 2019-01-25 14:21:05 +01:00 committed by GitHub
commit 6f2db5b9b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 146 additions and 88 deletions

View File

@ -29,7 +29,7 @@ script:
- npm run report-coverage - npm run report-coverage
- npm run build - npm run build
- npm run doc - npm run doc
- ./src/examples/fire_lazers.sh - npm run integration
after_script: after_script:
- greenkeeper-lockfile-upload - greenkeeper-lockfile-upload

View File

@ -8,6 +8,7 @@
"test": "mocha", "test": "mocha",
"test:watch": "mocha -w --watch-extensions js,ts,json", "test:watch": "mocha -w --watch-extensions js,ts,json",
"test:cover": "nyc mocha", "test:cover": "nyc mocha",
"integration": "./src/examples/fire_lazers.sh",
"clean": "rm -rf ./dist/ ./doc/ ./coverage ./.nyc_output", "clean": "rm -rf ./dist/ ./doc/ ./coverage ./.nyc_output",
"lint": "tslint -c tslint.json 'src/**/*.ts' 'test/**/*.ts'", "lint": "tslint -c tslint.json 'src/**/*.ts' 'test/**/*.ts'",
"start": "npm link @oceanprotocol/keeper-contracts @oceanprotocol/secret-store-client && npm run build:watch", "start": "npm link @oceanprotocol/keeper-contracts @oceanprotocol/secret-store-client && npm run build:watch",

View File

@ -42,12 +42,15 @@ export default class Brizo {
consumerAddress, consumerAddress,
} }
return WebServiceConnectorProvider try {
.getConnector() return await WebServiceConnectorProvider
.post( .getConnector()
this.getPurchaseEndpoint(), .post(
decodeURI(JSON.stringify(args)), this.getPurchaseEndpoint(),
) decodeURI(JSON.stringify(args)),
)
} catch (e) {
throw new Error("HTTP request failed")
}
} }
} }

View File

@ -5,8 +5,9 @@ import Service from "../ddo/Service"
import DID from "../ocean/DID" import DID from "../ocean/DID"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid" import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0] const publisher: Account = (await ocean.getAccounts())[0]
@ -68,6 +69,6 @@ import config from "./config"
const paid = await serviceAgreement.payAsset(did.getId(), metaData.base.price, consumer) const paid = await serviceAgreement.payAsset(did.getId(), metaData.base.price, consumer)
Logger.log(`Asset paid: ${paid}`) Logger.log(`Asset paid: ${paid}`)
}
process.exit(0) runner(exec)
})()

View File

@ -4,8 +4,9 @@ import MetaDataBase from "../ddo/MetaDataBase"
import Service from "../ddo/Service" import Service from "../ddo/Service"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid" import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0] const publisher: Account = (await ocean.getAccounts())[0]
@ -63,6 +64,6 @@ import config from "./config"
consumer, consumer,
publisher) publisher)
Logger.log("ServiceAgreement Id:", serviceAgreement.getId()) Logger.log("ServiceAgreement Id:", serviceAgreement.getId())
}
process.exit(0) runner(exec)
})()

View File

@ -1,12 +1,13 @@
import {Logger, Ocean} from "../squid" import {Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const accounts = await ocean.getAccounts() const accounts = await ocean.getAccounts()
Logger.log(JSON.stringify(accounts, null, 2)) Logger.log(JSON.stringify(accounts, null, 2))
}
process.exit(0) runner(exec)
})()

View File

@ -1,12 +1,13 @@
import {Logger, Ocean} from "../squid" import {Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const accounts = await ocean.getAccounts() const accounts = await ocean.getAccounts()
Logger.log(await accounts[0].getBalance()) Logger.log(await accounts[0].getBalance())
}
process.exit(0) runner(exec)
})()

View File

@ -1,12 +1,13 @@
import {Logger, Ocean} from "../squid" import {Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const accounts = await ocean.getAccounts() const accounts = await ocean.getAccounts()
Logger.log(JSON.stringify(accounts, null, 2)) Logger.log(JSON.stringify(accounts, null, 2))
}
process.exit(0) runner(exec)
})()

View File

@ -5,8 +5,9 @@ import Service from "../ddo/Service"
import DID from "../ocean/DID" import DID from "../ocean/DID"
import {Account, Logger, Ocean, ServiceAgreement} from "../squid" import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0] const publisher: Account = (await ocean.getAccounts())[0]
@ -71,6 +72,6 @@ import config from "./config"
const accessGranted = await serviceAgreement.grantAccess(did.getId(), did.getId(), publisher) const accessGranted = await serviceAgreement.grantAccess(did.getId(), did.getId(), publisher)
Logger.log(`Asset access granted: ${accessGranted}`) Logger.log(`Asset access granted: ${accessGranted}`)
}
process.exit(0) runner(exec)
})()

View File

@ -4,8 +4,9 @@ import MetaDataBase from "../ddo/MetaDataBase"
import Service from "../ddo/Service" import Service from "../ddo/Service"
import {Account, Logger, Ocean} from "../squid" import {Account, Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[1] const publisher: Account = (await ocean.getAccounts())[1]
@ -57,15 +58,19 @@ import config from "./config"
const service: Service = ddo.findServiceByType("Access") const service: Service = ddo.findServiceByType("Access")
await ocean try {
.initializeServiceAgreement( await ocean
ddo.id, .initializeServiceAgreement(
service.serviceDefinitionId, ddo.id,
serviceAgreementSignatureResult.serviceAgreementId, service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementSignature, serviceAgreementSignatureResult.serviceAgreementId,
(files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`), serviceAgreementSignatureResult.serviceAgreementSignature,
consumer, (files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
) consumer,
)
} catch (e) {
throw new Error(`"Error on initializeServiceAgreement: ${e}`)
}
}
process.exit(0) runner(exec)
})()

View File

@ -3,8 +3,9 @@ import MetaData from "../ddo/MetaData"
import MetaDataBase from "../ddo/MetaDataBase" import MetaDataBase from "../ddo/MetaDataBase"
import {Account, Logger, Ocean} from "../squid" import {Account, Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0] const publisher: Account = (await ocean.getAccounts())[0]
@ -41,6 +42,6 @@ import config from "./config"
const ddo: DDO = await ocean.registerAsset(metaData, publisher) const ddo: DDO = await ocean.registerAsset(metaData, publisher)
Logger.log(ddo.id) Logger.log(ddo.id)
}
process.exit(0) runner(exec)
})()

View File

@ -1,7 +1,8 @@
import {Account, Logger, Ocean, ServiceAgreementTemplate, Templates} from "../squid" import {Account, Logger, Ocean, ServiceAgreementTemplate, Templates} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const templateOwner: Account = (await ocean.getAccounts())[0] const templateOwner: Account = (await ocean.getAccounts())[0]
@ -11,6 +12,6 @@ import config from "./config"
Logger.log("ServiceAgreementTemplate registered:", serviceAgreementRegistered, Logger.log("ServiceAgreementTemplate registered:", serviceAgreementRegistered,
"templateId:", serviceAgreementTemplate.getId()) "templateId:", serviceAgreementTemplate.getId())
}
process.exit(0) runner(exec)
})()

View File

@ -1,8 +1,9 @@
import DDO from "../ddo/DDO" import DDO from "../ddo/DDO"
import {Logger, Ocean} from "../squid" import {Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const result: DDO[] = await ocean.searchAssetsByText("Office Humidity") const result: DDO[] = await ocean.searchAssetsByText("Office Humidity")
@ -14,6 +15,6 @@ import config from "./config"
}) })
Logger.log(names.length, names) Logger.log(names.length, names)
}
process.exit(0) runner(exec)
})()

View File

@ -6,8 +6,9 @@ import MetaDataBase from "../ddo/MetaDataBase"
import Service from "../ddo/Service" import Service from "../ddo/Service"
import {Account, Logger, Ocean} from "../squid" import {Account, Logger, Ocean} from "../squid"
import config from "./config" import config from "./config"
import {runner} from "./runner"
(async () => { async function exec() {
const ocean: Ocean = await Ocean.getInstance(config) const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0] const publisher: Account = (await ocean.getAccounts())[0]
@ -77,6 +78,6 @@ import config from "./config"
Logger.log("ServiceAgreement Id:", serviceAgreementResult.serviceAgreementId) Logger.log("ServiceAgreement Id:", serviceAgreementResult.serviceAgreementId)
Logger.log("ServiceAgreement Signature:", serviceAgreementResult.serviceAgreementSignature) Logger.log("ServiceAgreement Signature:", serviceAgreementResult.serviceAgreementSignature)
}
process.exit(0) runner(exec)
})()

10
src/examples/runner.ts Normal file
View File

@ -0,0 +1,10 @@
import {Logger} from "../squid"
export function runner(fn: () => Promise<any>) {
fn()
.then(() => process.exit(0))
.catch((e) => {
Logger.error(e)
process.exit(1)
})
}

View File

@ -1,7 +1,7 @@
import Logger from "../utils/Logger"
import GenericContract from "./contracts/GenericContract" import GenericContract from "./contracts/GenericContract"
import EventListener from "./EventListener" import EventListener from "./EventListener"
import Web3Provider from "./Web3Provider" import Web3Provider from "./Web3Provider"
// import Logger from "../utils/Logger"
export default class Event { export default class Event {
@ -40,6 +40,9 @@ export default class Event {
private async handler(callback: any) { private async handler(callback: any) {
const contract = await GenericContract.getInstance(this.contractName) const contract = await GenericContract.getInstance(this.contractName)
if (this.lastBlock > await Web3Provider.getWeb3().eth.getBlockNumber()) {
return
}
try { try {
const events = await contract.getEventData(this.eventName, { const events = await contract.getEventData(this.eventName, {
filter: this.filter, filter: this.filter,
@ -52,7 +55,7 @@ export default class Event {
callback(events) callback(events)
} }
} catch (err) { } catch (err) {
// Logger.log("err") Logger.log(err)
} }
} }
} }

View File

@ -63,8 +63,12 @@ export default abstract class ContractBase {
value: args[i], value: args[i],
} }
}) })
Logger.error("-".repeat(40))
Logger.error(`Sending transaction "${name}" on contract "${this.contractName}" failed.`) Logger.error(`Sending transaction "${name}" on contract "${this.contractName}" failed.`)
Logger.error(`Parameters: ${JSON.stringify(mappedArgs, null, 2)} from: ${from}`) Logger.error(`Error: ${err.message}`)
Logger.error(`From: ${from}`)
Logger.error(`Parameters: ${JSON.stringify(mappedArgs, null, 2)}`)
Logger.error("-".repeat(40))
throw err throw err
} }
} }

View File

@ -3,6 +3,7 @@ import * as EthJsUtils from "ethereumjs-util"
import Keeper from "../keeper/Keeper" import Keeper from "../keeper/Keeper"
import Web3Provider from "../keeper/Web3Provider" import Web3Provider from "../keeper/Web3Provider"
import Balance from "../models/Balance" import Balance from "../models/Balance"
import Logger from "../utils/Logger"
import OceanBase from "./OceanBase" import OceanBase from "./OceanBase"
/** /**
@ -57,9 +58,15 @@ export default class Account extends OceanBase {
* @return {Promise<number>} * @return {Promise<number>}
*/ */
public async requestTokens(amount: number): Promise<number> { public async requestTokens(amount: number): Promise<number> {
await (await Keeper.getInstance()) try {
.market await (await Keeper.getInstance())
.requestTokens(amount, this.id) .market
.requestTokens(amount, this.id)
} catch (e) {
Logger.error(e)
throw new Error("Error requesting tokens")
}
return amount return amount
} }

View File

@ -245,11 +245,10 @@ export default class Ocean {
const event: ContractEvent = EventListener.subscribe( const event: ContractEvent = EventListener.subscribe(
accessService.serviceAgreementContract.contractName, accessService.serviceAgreementContract.contractName,
accessService.serviceAgreementContract.events[0].name, { accessService.serviceAgreementContract.events[0].name, {
serviceAgreementId, serviceAgreementId: `0x${serviceAgreementId}`,
}) })
event.listenOnce(async (data) => { event.listenOnce(async (data) => {
const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.serviceAgreementId) const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.serviceAgreementId)
await sa.payAsset( await sa.payAsset(
d.getId(), d.getId(),
@ -263,7 +262,6 @@ export default class Ocean {
serviceAgreementId, serviceAgreementId,
serviceAgreementSignature, serviceAgreementSignature,
} }
} catch (err) { } catch (err) {
Logger.error("Signing ServiceAgreement failed!", err) Logger.error("Signing ServiceAgreement failed!", err)
} }
@ -296,24 +294,26 @@ export default class Ocean {
const accessEvent: ContractEvent = EventListener.subscribe( const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName, accessService.conditions[1].contractName,
accessService.conditions[1].events[1].name, {}) accessService.conditions[1].events[1].name, {})
const filesPromise = new Promise((resolve) => {
accessEvent.listenOnce(async () => {
Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.")
const contentUrls = await SecretStoreProvider
.getSecretStore()
.decryptDocument(d.getId(), metadataService.metadata.base.contentUrls[0])
const serviceUrl: string = accessService.serviceEndpoint
Logger.log("Consuming asset files using service url: ", serviceUrl)
const files = []
accessEvent.listenOnce(async () => { for (const cUrl of contentUrls) {
Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.") let url: string = serviceUrl + `?url=${cUrl}`
const contentUrls = await SecretStoreProvider url = url + `&serviceAgreementId=${serviceAgreementId}`
.getSecretStore() url = url + `&consumerAddress=${consumer.getId()}`
.decryptDocument(d.getId(), metadataService.metadata.base.contentUrls[0]) files.push(url)
const serviceUrl: string = accessService.serviceEndpoint }
Logger.log("Consuming asset files using service url: ", serviceUrl)
const files = []
for (const cUrl of contentUrls) { cb(files)
let url: string = serviceUrl + `?url=${cUrl}` resolve(files)
url = url + `&serviceAgreementId=${serviceAgreementId}` })
url = url + `&consumerAddress=${consumer.getId()}`
files.push(url)
}
cb(files)
}) })
await BrizoProvider await BrizoProvider
@ -324,6 +324,8 @@ export default class Ocean {
serviceDefinitionId, serviceDefinitionId,
serviceAgreementSignature, serviceAgreementSignature,
consumer.getId()) consumer.getId())
await filesPromise
} }
/** /**

View File

@ -58,15 +58,21 @@ export default class ServiceAgreementTemplate extends OceanBase {
return false return false
} }
const receipt = await serviceAgreement let receipt
.setupAgreementTemplate( try {
this.template.id, receipt = await serviceAgreement
await this.getMethodReflections(), .setupAgreementTemplate(
dependencyMatrix, this.template.id,
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName), await this.getMethodReflections(),
fulfillmentIndices, dependencyMatrix,
this.template.fulfillmentOperator, Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
templateOwnerAddress) fulfillmentIndices,
this.template.fulfillmentOperator,
templateOwnerAddress)
} catch (e) {
Logger.error(e)
throw new Error(`Is not possible to setup the agreement template`)
}
const {serviceTemplateId, provider} = receipt.events.SetupAgreementTemplate.returnValues const {serviceTemplateId, provider} = receipt.events.SetupAgreementTemplate.returnValues

View File

@ -1,11 +1,12 @@
import fetch, { BodyInit, RequestInit, Response } from "node-fetch" import fetch, { BodyInit, RequestInit, Response } from "node-fetch"
import Logger from "./Logger"
/** /**
* Provides a common interface to web services. * Provides a common interface to web services.
*/ */
export default class WebServiceConnector { export default class WebServiceConnector {
public async post(url: string, payload: BodyInit): Promise<Response> { public post(url: string, payload: BodyInit): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "POST", method: "POST",
body: payload, body: payload,
@ -15,7 +16,7 @@ export default class WebServiceConnector {
}) })
} }
public async get(url: string): Promise<Response> { public get(url: string): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "GET", method: "GET",
headers: { headers: {
@ -24,7 +25,7 @@ export default class WebServiceConnector {
}) })
} }
public async put(url: string, payload: BodyInit): Promise<Response> { public put(url: string, payload: BodyInit): Promise<Response> {
return this.fetch(url, { return this.fetch(url, {
method: "PUT", method: "PUT",
body: payload, body: payload,
@ -35,6 +36,12 @@ export default class WebServiceConnector {
} }
private async fetch(url: string, opts: RequestInit): Promise<Response> { private async fetch(url: string, opts: RequestInit): Promise<Response> {
return fetch(url, opts) const result = await fetch(url, opts)
if (!result.ok) {
Logger.error(`Error requesting [${opts.method}] ${url}`)
Logger.error(`Response message: \n${await result.text()}`)
throw result
}
return result
} }
} }