1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Refactor OnChainMetadataCache (#551)

* update naming, add helper update

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* fix test

* fix

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* fix test

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* fix test

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2021-01-18 14:03:56 +02:00 committed by GitHub
parent 16c21e1ecb
commit 002e38d8b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 25 deletions

View File

@ -13,7 +13,7 @@ import { LZMA } from 'lzma/src/lzma-c'
* Provides an interface with Metadata Cache. * Provides an interface with Metadata Cache.
* Metadata Cache provides an off-chain database store for metadata about data assets. * Metadata Cache provides an off-chain database store for metadata about data assets.
*/ */
export class OnChainMetadataCache { export class OnChainMetadata {
public GASLIMIT_DEFAULT = 1000000 public GASLIMIT_DEFAULT = 1000000
public DDOContractAddress: string public DDOContractAddress: string
public DDOContractABI: AbiItem | AbiItem[] public DDOContractABI: AbiItem | AbiItem[]

View File

@ -1,7 +1,7 @@
import { SearchQuery, QueryResult } from '../metadatacache/MetadataCache' import { SearchQuery, QueryResult } from '../metadatacache/MetadataCache'
import { DDO } from '../ddo/DDO' import { DDO } from '../ddo/DDO'
import { Metadata } from '../ddo/interfaces/Metadata' 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 { EditableMetadata } from '../ddo/interfaces/EditableMetadata'
import Account from './Account' import Account from './Account'
import DID from './DID' import DID from './DID'
@ -12,7 +12,7 @@ import BigNumber from 'bignumber.js'
import { Provider } from '../provider/Provider' import { Provider } from '../provider/Provider'
import { isAddress } from 'web3-utils' import { isAddress } from 'web3-utils'
import { MetadataMain } from '../ddo/interfaces' import { MetadataMain } from '../ddo/interfaces'
import { DataTokens } from '../lib' import { TransactionReceipt } from 'web3-core'
export enum CreateProgressStep { export enum CreateProgressStep {
CreatingDataToken, CreatingDataToken,
@ -91,7 +91,7 @@ export class Assets extends Instantiable {
if (!dtAddress) { if (!dtAddress) {
this.logger.log('Creating datatoken') this.logger.log('Creating datatoken')
observer.next(CreateProgressStep.CreatingDataToken) observer.next(CreateProgressStep.CreatingDataToken)
// const metadataCacheUri = this.ocean.metadatacache.getURI() // const metadataCacheUri = this.ocean.metadataCache.getURI()
// const jsonBlob = { t: 1, url: metadataCacheUri } // const jsonBlob = { t: 1, url: metadataCacheUri }
dtAddress = await datatokens.create('', publisher.getId(), cap, name, symbol) dtAddress = await datatokens.create('', publisher.getId(), cap, name, symbol)
@ -188,8 +188,8 @@ export class Assets extends Instantiable {
} }
this.logger.log('Storing DDO') this.logger.log('Storing DDO')
observer.next(CreateProgressStep.StoringDdo) observer.next(CreateProgressStep.StoringDdo)
// const storedDdo = await this.ocean.metadatacache.storeDDO(ddo) // const storedDdo = await this.ocean.metadataCache.storeDDO(ddo)
const storeTx = await this.ocean.OnChainMetadataCache.publish( const storeTx = await this.ocean.onChainMetadata.publish(
ddo.id, ddo.id,
ddo, ddo,
publisher.getId() publisher.getId()
@ -207,7 +207,7 @@ export class Assets extends Instantiable {
* @return {Promise<string[]>} List of DIDs. * @return {Promise<string[]>} List of DIDs.
*/ */
public async ownerAssets(owner: string): Promise<QueryResult> { public async ownerAssets(owner: string): Promise<QueryResult> {
return this.ocean.metadatacache.getOwnerAssets(owner) return this.ocean.metadataCache.getOwnerAssets(owner)
} }
/** /**
@ -216,7 +216,7 @@ export class Assets extends Instantiable {
* @return {Promise<DDO>} * @return {Promise<DDO>}
*/ */
public async resolve(did: string): Promise<DDO> { public async resolve(did: string): Promise<DDO> {
return this.ocean.metadatacache.retrieveDDO(did) return this.ocean.metadataCache.retrieveDDO(did)
} }
public async resolveByDTAddress( public async resolveByDTAddress(
@ -236,17 +236,17 @@ export class Assets extends Instantiable {
}, },
text: dtAddress text: dtAddress
} as SearchQuery } as SearchQuery
return (await this.ocean.metadatacache.queryMetadata(searchQuery)).results return (await this.ocean.metadataCache.queryMetadata(searchQuery)).results
} }
/** Metadata updates /** Metadata updates
* Don't forget to call ocean.OnChainMetadataCache.update after using this functions * Don't forget to call ocean.OnChainmetadataCache.update after using this functions
* ie: ocean.OnChainMetadataCache.update(ddo.id,ddo,account.getId()) * ie: ocean.OnChainmetadataCache.update(ddo.id,ddo,account.getId())
*/ */
/** /**
* Edit Metadata for a DID. * Edit Metadata for a DID.
* @param {ddo} DDO if empty, will trigger a retrieve * @param {ddo} DDO
* @param {newMetadata} EditableMetadata Metadata fields & new values. * @param {newMetadata} EditableMetadata Metadata fields & new values.
* @return {Promise<DDO>} the new DDO * @return {Promise<DDO>} the new DDO
*/ */
@ -266,6 +266,19 @@ export class Assets extends Instantiable {
return ddo 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 * Edit Service Timeouts
* @param {ddo} DDO if empty, will trigger a retrieve * @param {ddo} DDO if empty, will trigger a retrieve
@ -312,7 +325,7 @@ export class Assets extends Instantiable {
* @return {Promise<QueryResult>} * @return {Promise<QueryResult>}
*/ */
public async query(query: SearchQuery): Promise<QueryResult> { public async query(query: SearchQuery): Promise<QueryResult> {
return this.ocean.metadatacache.queryMetadata(query) return this.ocean.metadataCache.queryMetadata(query)
} }
/** /**
@ -321,7 +334,7 @@ export class Assets extends Instantiable {
* @return {Promise<QueryResult>} * @return {Promise<QueryResult>}
*/ */
public async search(text: string): Promise<QueryResult> { public async search(text: string): Promise<QueryResult> {
return this.ocean.metadatacache.queryMetadata({ return this.ocean.metadataCache.queryMetadata({
text, text,
page: 1, page: 1,
offset: 100, offset: 100,

View File

@ -3,7 +3,7 @@ import { Assets } from './Assets'
import { Versions } from './Versions' import { Versions } from './Versions'
import { OceanUtils } from './utils/Utils' import { OceanUtils } from './utils/Utils'
import { MetadataCache } from '../metadatacache/MetadataCache' import { MetadataCache } from '../metadatacache/MetadataCache'
import { OnChainMetadataCache } from '../metadatacache/OnChainMetaDataCache' import { OnChainMetadata } from '../metadatacache/OnChainMetaData'
import { Provider } from '../provider/Provider' import { Provider } from '../provider/Provider'
import { DataTokens } from '../datatokens/Datatokens' import { DataTokens } from '../datatokens/Datatokens'
import { Network } from '../datatokens/Network' import { Network } from '../datatokens/Network'
@ -37,7 +37,7 @@ export class Ocean extends Instantiable {
instance.utils = await OceanUtils.getInstance(instanceConfig) instance.utils = await OceanUtils.getInstance(instanceConfig)
instance.provider = new Provider(instanceConfig) instance.provider = new Provider(instanceConfig)
instance.metadatacache = new MetadataCache( instance.metadataCache = new MetadataCache(
instanceConfig.config.metadataCacheUri, instanceConfig.config.metadataCacheUri,
instanceConfig.logger instanceConfig.logger
) )
@ -71,7 +71,7 @@ export class Ocean extends Instantiable {
instance.datatokens, instance.datatokens,
instanceConfig.config.startBlock instanceConfig.config.startBlock
) )
instance.OnChainMetadataCache = new OnChainMetadataCache( instance.onChainMetadata = new OnChainMetadata(
instanceConfig.config.web3Provider, instanceConfig.config.web3Provider,
instanceConfig.logger, instanceConfig.logger,
instanceConfig.config.metadataContractAddress, instanceConfig.config.metadataContractAddress,
@ -104,12 +104,12 @@ export class Ocean extends Instantiable {
* MetadataCache instance. * MetadataCache instance.
* @type {MetadataCache} * @type {MetadataCache}
*/ */
public metadatacache: MetadataCache public metadataCache: MetadataCache
/** /**
* OnChainMetadataCache instance. * OnChainMetadataCache instance.
* @type {OnChainMetadataCache} * @type {OnChainMetadataCache}
*/ */
public OnChainMetadataCache: OnChainMetadataCache public onChainMetadata: OnChainMetadata
/** /**
* Ocean account submodule * Ocean account submodule
* @type {Accounts} * @type {Accounts}

View File

@ -51,7 +51,7 @@ export class Versions extends Instantiable {
// MetadataCache // MetadataCache
try { try {
const { software: name, version } = await this.ocean.metadatacache.getVersionInfo() const { software: name, version } = await this.ocean.metadataCache.getVersionInfo()
versions.metadataCache = { versions.metadataCache = {
name, name,
status: OceanPlatformTechStatus.Working, status: OceanPlatformTechStatus.Working,

View File

@ -474,7 +474,7 @@ describe('Compute flow', () => {
newComputePrivacy newComputePrivacy
) )
assert(newDdo !== null) 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) assert(txid !== null)
await sleep(60000) await sleep(60000)
const metaData = await ocean.assets.getServiceByType(ddo.id, 'compute') const metaData = await ocean.assets.getServiceByType(ddo.id, 'compute')

View File

@ -214,7 +214,7 @@ describe('Marketplace flow', () => {
} }
const newDdo = await ocean.assets.editMetadata(ddo, newMetaData) const newDdo = await ocean.assets.editMetadata(ddo, newMetaData)
assert(newDdo !== null) 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) assert(txid !== null)
await sleep(60000) await sleep(60000)
const metaData = await ocean.assets.getServiceByType(ddo.id, 'metadata') const metaData = await ocean.assets.getServiceByType(ddo.id, 'metadata')
@ -233,7 +233,7 @@ describe('Marketplace flow', () => {
const newTimeout = 123 const newTimeout = 123
const newDdo = await ocean.assets.editServiceTimeout(ddo, serviceIndex, newTimeout) const newDdo = await ocean.assets.editServiceTimeout(ddo, serviceIndex, newTimeout)
assert(newDdo !== null) 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) assert(txid !== null)
await sleep(60000) await sleep(60000)
const metaData = await ocean.assets.getServiceByType(ddo.id, 'access') const metaData = await ocean.assets.getServiceByType(ddo.id, 'access')

View File

@ -16,7 +16,7 @@ describe('MetadataCache', () => {
beforeEach(async () => { beforeEach(async () => {
ocean = await Ocean.getInstance(config) ocean = await Ocean.getInstance(config)
metadataCache = ocean.metadatacache // eslint-disable-line prefer-destructuring metadataCache = ocean.metadataCache // eslint-disable-line prefer-destructuring
}) })
afterEach(() => { afterEach(() => {

View File

@ -15,7 +15,7 @@ describe('Assets', () => {
beforeEach(async () => { beforeEach(async () => {
ocean = await Ocean.getInstance(config) ocean = await Ocean.getInstance(config)
metadataCache = ocean.metadatacache // eslint-disable-line prefer-destructuring metadataCache = ocean.metadataCache // eslint-disable-line prefer-destructuring
}) })
afterEach(() => { afterEach(() => {