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 build
- npm run doc
- ./src/examples/fire_lazers.sh
- npm run integration
after_script:
- greenkeeper-lockfile-upload

View File

@ -8,6 +8,7 @@
"test": "mocha",
"test:watch": "mocha -w --watch-extensions js,ts,json",
"test:cover": "nyc mocha",
"integration": "./src/examples/fire_lazers.sh",
"clean": "rm -rf ./dist/ ./doc/ ./coverage ./.nyc_output",
"lint": "tslint -c tslint.json 'src/**/*.ts' 'test/**/*.ts'",
"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,
}
return WebServiceConnectorProvider
.getConnector()
.post(
this.getPurchaseEndpoint(),
decodeURI(JSON.stringify(args)),
)
try {
return await WebServiceConnectorProvider
.getConnector()
.post(
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 {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
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)
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 {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0]
@ -63,6 +64,6 @@ import config from "./config"
consumer,
publisher)
Logger.log("ServiceAgreement Id:", serviceAgreement.getId())
}
process.exit(0)
})()
runner(exec)

View File

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

View File

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

View File

@ -1,12 +1,13 @@
import {Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const accounts = await ocean.getAccounts()
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 {Account, Logger, Ocean, ServiceAgreement} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
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)
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 {Account, Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[1]
@ -57,15 +58,19 @@ import config from "./config"
const service: Service = ddo.findServiceByType("Access")
await ocean
.initializeServiceAgreement(
ddo.id,
service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId,
serviceAgreementSignatureResult.serviceAgreementSignature,
(files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
consumer,
)
try {
await ocean
.initializeServiceAgreement(
ddo.id,
service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId,
serviceAgreementSignatureResult.serviceAgreementSignature,
(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 {Account, Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0]
@ -41,6 +42,6 @@ import config from "./config"
const ddo: DDO = await ocean.registerAsset(metaData, publisher)
Logger.log(ddo.id)
}
process.exit(0)
})()
runner(exec)

View File

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

View File

@ -1,8 +1,9 @@
import DDO from "../ddo/DDO"
import {Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const result: DDO[] = await ocean.searchAssetsByText("Office Humidity")
@ -14,6 +15,6 @@ import config from "./config"
})
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 {Account, Logger, Ocean} from "../squid"
import config from "./config"
import {runner} from "./runner"
(async () => {
async function exec() {
const ocean: Ocean = await Ocean.getInstance(config)
const publisher: Account = (await ocean.getAccounts())[0]
@ -77,6 +78,6 @@ import config from "./config"
Logger.log("ServiceAgreement Id:", serviceAgreementResult.serviceAgreementId)
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 EventListener from "./EventListener"
import Web3Provider from "./Web3Provider"
// import Logger from "../utils/Logger"
export default class Event {
@ -40,6 +40,9 @@ export default class Event {
private async handler(callback: any) {
const contract = await GenericContract.getInstance(this.contractName)
if (this.lastBlock > await Web3Provider.getWeb3().eth.getBlockNumber()) {
return
}
try {
const events = await contract.getEventData(this.eventName, {
filter: this.filter,
@ -52,7 +55,7 @@ export default class Event {
callback(events)
}
} catch (err) {
// Logger.log("err")
Logger.log(err)
}
}
}

View File

@ -63,8 +63,12 @@ export default abstract class ContractBase {
value: args[i],
}
})
Logger.error("-".repeat(40))
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
}
}

View File

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

View File

@ -245,11 +245,10 @@ export default class Ocean {
const event: ContractEvent = EventListener.subscribe(
accessService.serviceAgreementContract.contractName,
accessService.serviceAgreementContract.events[0].name, {
serviceAgreementId,
serviceAgreementId: `0x${serviceAgreementId}`,
})
event.listenOnce(async (data) => {
const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.serviceAgreementId)
await sa.payAsset(
d.getId(),
@ -263,7 +262,6 @@ export default class Ocean {
serviceAgreementId,
serviceAgreementSignature,
}
} catch (err) {
Logger.error("Signing ServiceAgreement failed!", err)
}
@ -296,24 +294,26 @@ export default class Ocean {
const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName,
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 () => {
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 = []
for (const cUrl of contentUrls) {
let url: string = serviceUrl + `?url=${cUrl}`
url = url + `&serviceAgreementId=${serviceAgreementId}`
url = url + `&consumerAddress=${consumer.getId()}`
files.push(url)
}
for (const cUrl of contentUrls) {
let url: string = serviceUrl + `?url=${cUrl}`
url = url + `&serviceAgreementId=${serviceAgreementId}`
url = url + `&consumerAddress=${consumer.getId()}`
files.push(url)
}
cb(files)
cb(files)
resolve(files)
})
})
await BrizoProvider
@ -324,6 +324,8 @@ export default class Ocean {
serviceDefinitionId,
serviceAgreementSignature,
consumer.getId())
await filesPromise
}
/**

View File

@ -58,15 +58,21 @@ export default class ServiceAgreementTemplate extends OceanBase {
return false
}
const receipt = await serviceAgreement
.setupAgreementTemplate(
this.template.id,
await this.getMethodReflections(),
dependencyMatrix,
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
fulfillmentIndices,
this.template.fulfillmentOperator,
templateOwnerAddress)
let receipt
try {
receipt = await serviceAgreement
.setupAgreementTemplate(
this.template.id,
await this.getMethodReflections(),
dependencyMatrix,
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
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

View File

@ -1,11 +1,12 @@
import fetch, { BodyInit, RequestInit, Response } from "node-fetch"
import Logger from "./Logger"
/**
* Provides a common interface to web services.
*/
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, {
method: "POST",
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, {
method: "GET",
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, {
method: "PUT",
body: payload,
@ -35,6 +36,12 @@ export default class WebServiceConnector {
}
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
}
}