From 002e38d8b0f285c653967ddcec25762aa30d884a Mon Sep 17 00:00:00 2001 From: mihaisc Date: Mon, 18 Jan 2021 14:03:56 +0200 Subject: [PATCH] Refactor `OnChainMetadataCache` (#551) * update naming, add helper update Signed-off-by: mihaisc * fix test * fix Signed-off-by: mihaisc * fix test Signed-off-by: mihaisc * fix test Signed-off-by: mihaisc --- ...ainMetaDataCache.ts => OnChainMetaData.ts} | 2 +- src/ocean/Assets.ts | 39 ++++++++++++------- src/ocean/Ocean.ts | 10 ++--- src/ocean/Versions.ts | 2 +- test/integration/ComputeFlow.test.ts | 2 +- test/integration/Marketplaceflow.test.ts | 4 +- test/unit/metadatacache/MetadataCache.test.ts | 2 +- test/unit/ocean/Assets.test.ts | 2 +- 8 files changed, 38 insertions(+), 25 deletions(-) rename src/metadatacache/{OnChainMetaDataCache.ts => OnChainMetaData.ts} (99%) diff --git a/src/metadatacache/OnChainMetaDataCache.ts b/src/metadatacache/OnChainMetaData.ts similarity index 99% rename from src/metadatacache/OnChainMetaDataCache.ts rename to src/metadatacache/OnChainMetaData.ts index adbe5844e..61282de0c 100644 --- a/src/metadatacache/OnChainMetaDataCache.ts +++ b/src/metadatacache/OnChainMetaData.ts @@ -13,7 +13,7 @@ import { LZMA } from 'lzma/src/lzma-c' * Provides an interface with Metadata Cache. * Metadata Cache provides an off-chain database store for metadata about data assets. */ -export class OnChainMetadataCache { +export class OnChainMetadata { public GASLIMIT_DEFAULT = 1000000 public DDOContractAddress: string public DDOContractABI: AbiItem | AbiItem[] diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 2fa2c7c87..fffe1bcca 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -1,7 +1,7 @@ import { SearchQuery, QueryResult } from '../metadatacache/MetadataCache' import { DDO } from '../ddo/DDO' import { Metadata } from '../ddo/interfaces/Metadata' -import { Service, ServiceAccess, ServiceComputePrivacy } from '../ddo/interfaces/Service' +import { Service, ServiceAccess } from '../ddo/interfaces/Service' import { EditableMetadata } from '../ddo/interfaces/EditableMetadata' import Account from './Account' import DID from './DID' @@ -12,7 +12,7 @@ import BigNumber from 'bignumber.js' import { Provider } from '../provider/Provider' import { isAddress } from 'web3-utils' import { MetadataMain } from '../ddo/interfaces' -import { DataTokens } from '../lib' +import { TransactionReceipt } from 'web3-core' export enum CreateProgressStep { CreatingDataToken, @@ -91,7 +91,7 @@ export class Assets extends Instantiable { if (!dtAddress) { this.logger.log('Creating datatoken') observer.next(CreateProgressStep.CreatingDataToken) - // const metadataCacheUri = this.ocean.metadatacache.getURI() + // const metadataCacheUri = this.ocean.metadataCache.getURI() // const jsonBlob = { t: 1, url: metadataCacheUri } dtAddress = await datatokens.create('', publisher.getId(), cap, name, symbol) @@ -188,8 +188,8 @@ export class Assets extends Instantiable { } this.logger.log('Storing DDO') observer.next(CreateProgressStep.StoringDdo) - // const storedDdo = await this.ocean.metadatacache.storeDDO(ddo) - const storeTx = await this.ocean.OnChainMetadataCache.publish( + // const storedDdo = await this.ocean.metadataCache.storeDDO(ddo) + const storeTx = await this.ocean.onChainMetadata.publish( ddo.id, ddo, publisher.getId() @@ -207,7 +207,7 @@ export class Assets extends Instantiable { * @return {Promise} List of DIDs. */ public async ownerAssets(owner: string): Promise { - return this.ocean.metadatacache.getOwnerAssets(owner) + return this.ocean.metadataCache.getOwnerAssets(owner) } /** @@ -216,7 +216,7 @@ export class Assets extends Instantiable { * @return {Promise} */ public async resolve(did: string): Promise { - return this.ocean.metadatacache.retrieveDDO(did) + return this.ocean.metadataCache.retrieveDDO(did) } public async resolveByDTAddress( @@ -236,17 +236,17 @@ export class Assets extends Instantiable { }, text: dtAddress } as SearchQuery - return (await this.ocean.metadatacache.queryMetadata(searchQuery)).results + return (await this.ocean.metadataCache.queryMetadata(searchQuery)).results } /** Metadata updates - * Don't forget to call ocean.OnChainMetadataCache.update after using this functions - * ie: ocean.OnChainMetadataCache.update(ddo.id,ddo,account.getId()) + * Don't forget to call ocean.OnChainmetadataCache.update after using this functions + * ie: ocean.OnChainmetadataCache.update(ddo.id,ddo,account.getId()) */ /** * Edit Metadata for a DID. - * @param {ddo} DDO if empty, will trigger a retrieve + * @param {ddo} DDO * @param {newMetadata} EditableMetadata Metadata fields & new values. * @return {Promise} the new DDO */ @@ -266,6 +266,19 @@ export class Assets extends Instantiable { return ddo } + /** + * Update Metadata on chain. + * @param {ddo} DDO + * @param {String} consumerAccount + * @return {Promise} exchangeId + */ + public async updateMetadata( + ddo: DDO, + consumerAccount: string + ): Promise { + return await this.ocean.onChainMetadata.update(ddo.id, ddo, consumerAccount) + } + /** * Edit Service Timeouts * @param {ddo} DDO if empty, will trigger a retrieve @@ -312,7 +325,7 @@ export class Assets extends Instantiable { * @return {Promise} */ public async query(query: SearchQuery): Promise { - return this.ocean.metadatacache.queryMetadata(query) + return this.ocean.metadataCache.queryMetadata(query) } /** @@ -321,7 +334,7 @@ export class Assets extends Instantiable { * @return {Promise} */ public async search(text: string): Promise { - return this.ocean.metadatacache.queryMetadata({ + return this.ocean.metadataCache.queryMetadata({ text, page: 1, offset: 100, diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 9c933ac91..1ee5ba377 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -3,7 +3,7 @@ import { Assets } from './Assets' import { Versions } from './Versions' import { OceanUtils } from './utils/Utils' import { MetadataCache } from '../metadatacache/MetadataCache' -import { OnChainMetadataCache } from '../metadatacache/OnChainMetaDataCache' +import { OnChainMetadata } from '../metadatacache/OnChainMetaData' import { Provider } from '../provider/Provider' import { DataTokens } from '../datatokens/Datatokens' import { Network } from '../datatokens/Network' @@ -37,7 +37,7 @@ export class Ocean extends Instantiable { instance.utils = await OceanUtils.getInstance(instanceConfig) instance.provider = new Provider(instanceConfig) - instance.metadatacache = new MetadataCache( + instance.metadataCache = new MetadataCache( instanceConfig.config.metadataCacheUri, instanceConfig.logger ) @@ -71,7 +71,7 @@ export class Ocean extends Instantiable { instance.datatokens, instanceConfig.config.startBlock ) - instance.OnChainMetadataCache = new OnChainMetadataCache( + instance.onChainMetadata = new OnChainMetadata( instanceConfig.config.web3Provider, instanceConfig.logger, instanceConfig.config.metadataContractAddress, @@ -104,12 +104,12 @@ export class Ocean extends Instantiable { * MetadataCache instance. * @type {MetadataCache} */ - public metadatacache: MetadataCache + public metadataCache: MetadataCache /** * OnChainMetadataCache instance. * @type {OnChainMetadataCache} */ - public OnChainMetadataCache: OnChainMetadataCache + public onChainMetadata: OnChainMetadata /** * Ocean account submodule * @type {Accounts} diff --git a/src/ocean/Versions.ts b/src/ocean/Versions.ts index b1594eb06..65459ae46 100644 --- a/src/ocean/Versions.ts +++ b/src/ocean/Versions.ts @@ -51,7 +51,7 @@ export class Versions extends Instantiable { // MetadataCache try { - const { software: name, version } = await this.ocean.metadatacache.getVersionInfo() + const { software: name, version } = await this.ocean.metadataCache.getVersionInfo() versions.metadataCache = { name, status: OceanPlatformTechStatus.Working, diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 0b4ee3fc6..18e5ac7eb 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -474,7 +474,7 @@ describe('Compute flow', () => { newComputePrivacy ) assert(newDdo !== null) - const txid = await ocean.OnChainMetadataCache.update(newDdo.id, newDdo, alice.getId()) + const txid = await ocean.onChainMetadata.update(newDdo.id, newDdo, alice.getId()) assert(txid !== null) await sleep(60000) const metaData = await ocean.assets.getServiceByType(ddo.id, 'compute') diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index c1bf62692..1e408bebc 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -214,7 +214,7 @@ describe('Marketplace flow', () => { } const newDdo = await ocean.assets.editMetadata(ddo, newMetaData) assert(newDdo !== null) - const txid = await ocean.OnChainMetadataCache.update(newDdo.id, newDdo, alice.getId()) + const txid = await ocean.onChainMetadata.update(newDdo.id, newDdo, alice.getId()) assert(txid !== null) await sleep(60000) const metaData = await ocean.assets.getServiceByType(ddo.id, 'metadata') @@ -233,7 +233,7 @@ describe('Marketplace flow', () => { const newTimeout = 123 const newDdo = await ocean.assets.editServiceTimeout(ddo, serviceIndex, newTimeout) assert(newDdo !== null) - const txid = await ocean.OnChainMetadataCache.update(newDdo.id, newDdo, alice.getId()) + const txid = await ocean.onChainMetadata.update(newDdo.id, newDdo, alice.getId()) assert(txid !== null) await sleep(60000) const metaData = await ocean.assets.getServiceByType(ddo.id, 'access') diff --git a/test/unit/metadatacache/MetadataCache.test.ts b/test/unit/metadatacache/MetadataCache.test.ts index 86fa1ca32..9e1b2fb20 100644 --- a/test/unit/metadatacache/MetadataCache.test.ts +++ b/test/unit/metadatacache/MetadataCache.test.ts @@ -16,7 +16,7 @@ describe('MetadataCache', () => { beforeEach(async () => { ocean = await Ocean.getInstance(config) - metadataCache = ocean.metadatacache // eslint-disable-line prefer-destructuring + metadataCache = ocean.metadataCache // eslint-disable-line prefer-destructuring }) afterEach(() => { diff --git a/test/unit/ocean/Assets.test.ts b/test/unit/ocean/Assets.test.ts index 1bae29275..380fc2e47 100644 --- a/test/unit/ocean/Assets.test.ts +++ b/test/unit/ocean/Assets.test.ts @@ -15,7 +15,7 @@ describe('Assets', () => { beforeEach(async () => { ocean = await Ocean.getInstance(config) - metadataCache = ocean.metadatacache // eslint-disable-line prefer-destructuring + metadataCache = ocean.metadataCache // eslint-disable-line prefer-destructuring }) afterEach(() => {