From 0438beef2ba909c3714e4622e2819083689ceafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Fri, 14 Jun 2019 00:34:53 +0200 Subject: [PATCH] Create Versions module. --- integration/ocean/Versions.test.ts | 24 +++++ package-lock.json | 29 ++---- src/aquarius/Aquarius.ts | 4 + src/brizo/Brizo.ts | 4 + src/keeper/Keeper.ts | 34 ++++--- src/keeper/contracts/ContractBase.ts | 8 +- src/ocean/Ocean.ts | 8 ++ src/ocean/OceanVersions.ts | 141 +++++++++++++++++++++++++++ src/squid.ts | 1 + 9 files changed, 218 insertions(+), 35 deletions(-) create mode 100644 integration/ocean/Versions.test.ts create mode 100644 src/ocean/OceanVersions.ts diff --git a/integration/ocean/Versions.test.ts b/integration/ocean/Versions.test.ts new file mode 100644 index 0000000..3f5d6c9 --- /dev/null +++ b/integration/ocean/Versions.test.ts @@ -0,0 +1,24 @@ +import { assert } from "chai" + +import { config } from "../config" + +import { Ocean, OceanPlatformTechStatus } from "../../src" // @oceanprotocol/squid + +describe("Versions", () => { + + let ocean: Ocean + + before(async () => { + ocean = await Ocean.getInstance(config) + }) + + it("should returns the versions", async () => { + const versions = await ocean.versions.get() + + assert.equal(versions.aquarius.status, OceanPlatformTechStatus.Working) + assert.equal(versions.brizo.status, OceanPlatformTechStatus.Working) + assert.equal(versions.squid.status, OceanPlatformTechStatus.Working) + + assert.deepEqual(versions.status, {ok: true, contracts: true, network: true}) + }) +}) diff --git a/package-lock.json b/package-lock.json index 3a424f6..61b309a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2697,8 +2697,7 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", @@ -2709,8 +2708,7 @@ "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2827,8 +2825,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2840,7 +2837,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2863,14 +2859,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2889,7 +2883,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2983,7 +2976,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3069,8 +3061,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3106,7 +3097,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3126,7 +3116,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3170,14 +3159,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -7607,7 +7594,7 @@ "requires": { "underscore": "1.8.3", "web3-core-helpers": "1.0.0-beta.37", - "websocket": "git://github.com/frozeman/WebSocket-Node.git#browserifyCompatible" + "websocket": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2" } }, "web3-shh": { diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index c1712e0..39609c6 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -34,6 +34,10 @@ export class Aquarius extends Instantiable { this.setInstanceConfig(config) } + async getVersionInfo() { + return await (await this.ocean.utils.fetch.get(this.url)).json() + } + public async getAccessUrl(accessToken: any, payload: any): Promise { const accessUrl: string = await this.ocean.utils.fetch .post(`${accessToken.service_endpoint}/${accessToken.resource_id}`, payload) diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index 7f34a73..68ab3db 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -22,6 +22,10 @@ export class Brizo extends Instantiable { this.setInstanceConfig(config) } + async getVersionInfo() { + return await (await this.ocean.utils.fetch.get(this.url)).json() + } + public getPurchaseEndpoint() { return `${this.url}${apiPath}/access/initialize` } diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index d50fa71..9fb292f 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -1,3 +1,5 @@ +import { ContractBase } from "./contracts/ContractBase" + import DIDRegistry from "./contracts/DIDRegistry" import Dispenser from "./contracts/Dispenser" import OceanToken from "./contracts/Token" @@ -30,9 +32,9 @@ export class Keeper extends Instantiable { // Adding keeper inside Ocean to prevent `Keeper not defined yet` error config.ocean.keeper = keeper - let instances = {} as any + keeper.instances = {} try { - instances = await objectPromiseAll({ + keeper.instances = await objectPromiseAll({ // Main contracts dispenser: Dispenser.getInstance(config), token: OceanToken.getInstance(config), @@ -45,7 +47,7 @@ export class Keeper extends Instantiable { lockRewardCondition: LockRewardCondition.getInstance(config), escrowReward: EscrowReward.getInstance(config), accessSecretStoreCondition: AccessSecretStoreCondition.getInstance(config), - // Conditions + // Templates escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(config), }) @@ -56,22 +58,22 @@ export class Keeper extends Instantiable { } // Main contracts - keeper.dispenser = instances.dispenser - keeper.token = instances.token - keeper.didRegistry = instances.didRegistry + keeper.dispenser = keeper.instances.dispenser + keeper.token = keeper.instances.token + keeper.didRegistry = keeper.instances.didRegistry // Managers - keeper.templateStoreManager = instances.templateStoreManager - keeper.agreementStoreManager = instances.agreementStoreManager - keeper.conditionStoreManager = instances.conditionStoreManager + keeper.templateStoreManager = keeper.instances.templateStoreManager + keeper.agreementStoreManager = keeper.instances.agreementStoreManager + keeper.conditionStoreManager = keeper.instances.conditionStoreManager // Conditions keeper.conditions = { - lockRewardCondition: instances.lockRewardCondition, - escrowReward: instances.escrowReward, - accessSecretStoreCondition: instances.accessSecretStoreCondition, + lockRewardCondition: keeper.instances.lockRewardCondition, + escrowReward: keeper.instances.escrowReward, + accessSecretStoreCondition: keeper.instances.accessSecretStoreCondition, } // Conditions keeper.templates = { - escrowAccessSecretStoreTemplate: instances.escrowAccessSecretStoreTemplate, + escrowAccessSecretStoreTemplate: keeper.instances.escrowAccessSecretStoreTemplate, } // Utils @@ -82,6 +84,8 @@ export class Keeper extends Instantiable { return keeper } + private instances: {[contractRef: string]: ContractBase & any} + /** * Is connected to the correct network or not. * @type {boolean} @@ -207,6 +211,10 @@ export class Keeper extends Instantiable { } }) } + + getAllInstances() { + return this.instances + } } export default Keeper diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index 71a8210..bce3ac0 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -4,13 +4,17 @@ import ContractHandler from "../ContractHandler" import { Instantiable, InstantiableConfig } from "../../Instantiable.abstract" -export default abstract class ContractBase extends Instantiable { +export abstract class ContractBase extends Instantiable { protected static instance = null public contractName: string private contract: Contract = null + get address() { + return this.contract.options.address + } + constructor(contractName) { super() this.contractName = contractName @@ -128,3 +132,5 @@ export default abstract class ContractBase extends Instantiable { return foundMethod } } + +export default ContractBase diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 52d9838..213b3cb 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -4,6 +4,7 @@ import { OceanAssets } from "./OceanAssets" import { OceanAuth } from "./OceanAuth" import { OceanSecretStore } from "./OceanSecretStore" import { OceanTokens } from "./OceanTokens" +import { OceanVersions } from "./OceanVersions" import { OceanUtils } from "./utils/OceanUtils" import { Aquarius } from "../aquarius/Aquarius" @@ -45,6 +46,7 @@ export class Ocean extends Instantiable { instance.agreements = await OceanAgreements.getInstance(instanceConfig) instance.secretStore = await OceanSecretStore.getInstance(instanceConfig) instance.tokens = await OceanTokens.getInstance(instanceConfig) + instance.versions = await OceanVersions.getInstance(instanceConfig) instance.utils = await OceanUtils.getInstance(instanceConfig) @@ -105,6 +107,12 @@ export class Ocean extends Instantiable { */ public tokens: OceanTokens + /** + * Ocean versions submodule + * @type {OceanVersions} + */ + public versions: OceanVersions + /** * Ocean utils submodule * @type {OceanUtils} diff --git a/src/ocean/OceanVersions.ts b/src/ocean/OceanVersions.ts new file mode 100644 index 0000000..359844a --- /dev/null +++ b/src/ocean/OceanVersions.ts @@ -0,0 +1,141 @@ +import * as keeperPackageJson from "@oceanprotocol/keeper-contracts/package.json" + +import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" +import * as packageJson from "../../package.json" + +export enum OceanPlatformTechStatus { + Loading = 'Loading', + Unknown = 'Unknown', + Stopped = 'Stopped', + Working = 'Working', +} + +interface OceanPlatformTech { + name: string + version?: string + status: OceanPlatformTechStatus +} + +interface OceanPlatformKeeperTech extends OceanPlatformTech { + network?: string + keeperVersion?: string + contracts?: {[contractName: string]: string} +} + +export interface OceanPlatformVersions { + squid: OceanPlatformKeeperTech + aquarius: OceanPlatformTech + brizo: OceanPlatformKeeperTech + status: { + ok: boolean + contracts: boolean + network: boolean + } +} + +/** + * Versions submodule of Ocean Protocol. + */ +export class OceanVersions extends Instantiable { + + /** + * Returns the instance of OceanVersions. + * @return {Promise} + */ + public static async getInstance(config: InstantiableConfig): Promise { + const instance = new OceanVersions() + instance.setInstanceConfig(config) + + return instance + } + + public async get(): Promise { + const versions = {} as OceanPlatformVersions + + // Squid + versions.squid = { + name: 'Squid', + version: packageJson.version, + status: OceanPlatformTechStatus.Working, + network: (await this.ocean.keeper.getNetworkName()).toLowerCase(), + keeperVersion: keeperPackageJson.version, + contracts: Object.values(await this.ocean.keeper.getAllInstances()) + .reduce((acc, {contractName, address}) => ({ + ...acc, + [contractName]: address, + }), {}) + } + + // Brizo + try { + const {contracts, 'keeper-version': keeperVersion, network, software: name, version} = + await this.ocean.brizo.getVersionInfo() + versions.brizo = { + name, + status: OceanPlatformTechStatus.Working, + version, + contracts, + network, + keeperVersion, + } + } catch { + versions.brizo = { + name: 'Brizo', + status: OceanPlatformTechStatus.Stopped, + } + } + + // Aquarius + try { + const {software: name, version} = await this.ocean.aquarius.getVersionInfo() + versions.aquarius = { + name, + status: OceanPlatformTechStatus.Working, + version, + } + } catch { + versions.aquarius = { + name: 'Aquarius', + status: OceanPlatformTechStatus.Stopped, + } + } + + // Status + const techs: OceanPlatformKeeperTech[] = Object.values(versions as any) + + const networks = techs + .map(({network}) => network) + .filter(_ => !!_) + .reduce((acc, network) => ({...acc, [network]: true}), {}) + + let contractStatus = true + const contracts = techs + .map(({contracts}) => contracts) + .filter(_ => !!_) + Array.from(contracts.map(Object.keys)) + .reduce((acc, _) => [...acc, ..._], []) + .filter((_, i, list) => list.indexOf(_) === i) + .forEach(name => { + let address + contracts + .map(_ => _[name]) + .forEach(_ => { + if (!address) { + address = _ + return + } + if (address !== _) { + contractStatus = false + } + }) + }) + + versions.status = { + ok: !techs.find(({status}) => status !== OceanPlatformTechStatus.Working), + network: Object.keys(networks).length === 1, + contracts: contractStatus, + } + + return versions + } +} diff --git a/src/squid.ts b/src/squid.ts index 5215517..21e4d82 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -14,6 +14,7 @@ export * from "./ddo/DDO" export * from "./ddo/MetaData" export { OrderProgressStep, CreateProgressStep } from "./ocean/OceanAssets" +export { OceanPlatformTechStatus } from "./ocean/OceanVersions" export { AgreementTemplate } from "./keeper/contracts/templates" export { Condition, ConditionState } from "./keeper/contracts/conditions"