Skip to content

Commit

Permalink
Refactor OnChainMetadataCache (#551)
Browse files Browse the repository at this point in the history
* update naming, add helper update

Signed-off-by: mihaisc <[email protected]>

* fix test

* fix

Signed-off-by: mihaisc <[email protected]>

* fix test

Signed-off-by: mihaisc <[email protected]>

* fix test

Signed-off-by: mihaisc <[email protected]>
  • Loading branch information
mihaisc authored Jan 18, 2021
1 parent 16c21e1 commit 002e38d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
39 changes: 26 additions & 13 deletions src/ocean/Assets.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -207,7 +207,7 @@ export class Assets extends Instantiable {
* @return {Promise<string[]>} List of DIDs.
*/
public async ownerAssets(owner: string): Promise<QueryResult> {
return this.ocean.metadatacache.getOwnerAssets(owner)
return this.ocean.metadataCache.getOwnerAssets(owner)
}

/**
Expand All @@ -216,7 +216,7 @@ export class Assets extends Instantiable {
* @return {Promise<DDO>}
*/
public async resolve(did: string): Promise<DDO> {
return this.ocean.metadatacache.retrieveDDO(did)
return this.ocean.metadataCache.retrieveDDO(did)
}

public async resolveByDTAddress(
Expand All @@ -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<DDO>} the new DDO
*/
Expand All @@ -266,6 +266,19 @@ export class Assets extends Instantiable {
return ddo
}

/**
* Update Metadata on chain.
* @param {ddo} DDO
* @param {String} consumerAccount
* @return {Promise<TransactionReceipt>} exchangeId
*/
public async updateMetadata(
ddo: DDO,
consumerAccount: string
): Promise<TransactionReceipt> {
return await this.ocean.onChainMetadata.update(ddo.id, ddo, consumerAccount)
}

/**
* Edit Service Timeouts
* @param {ddo} DDO if empty, will trigger a retrieve
Expand Down Expand Up @@ -312,7 +325,7 @@ export class Assets extends Instantiable {
* @return {Promise<QueryResult>}
*/
public async query(query: SearchQuery): Promise<QueryResult> {
return this.ocean.metadatacache.queryMetadata(query)
return this.ocean.metadataCache.queryMetadata(query)
}

/**
Expand All @@ -321,7 +334,7 @@ export class Assets extends Instantiable {
* @return {Promise<QueryResult>}
*/
public async search(text: string): Promise<QueryResult> {
return this.ocean.metadatacache.queryMetadata({
return this.ocean.metadataCache.queryMetadata({
text,
page: 1,
offset: 100,
Expand Down
10 changes: 5 additions & 5 deletions src/ocean/Ocean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/ocean/Versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/ComputeFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions test/integration/Marketplaceflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/unit/metadatacache/MetadataCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ocean/Assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 002e38d

Please sign in to comment.