Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marketplace: load manifest on demand #896

Merged
merged 6 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 144 additions & 133 deletions systemservices/marketplace/mesg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,137 +36,6 @@ tasks:
type: String
manifestProtocol: &manifestProtocol
type: String
manifestData: &manifestData
type: Object
optional: true
object:
version:
type: String
service: &manifestServiceData
type: Object
object:
deployment:
type: Object
object:
type:
type: String
source:
type: String
definition:
type: Object
object:
hash:
type: String
sid:
type: String
name:
type: String
description:
type: String
optional: true
tasks:
type: Object
repeated: true
optional: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
inputs: &manifestServiceDataParameters
type: Object
optional: true
repeated: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
type:
type: String
optional:
type: Boolean
optional: true
repeated:
type: Boolean
optional: true
object:
type: Object
optional: true
outputs:
type: Object
repeated: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
data: *manifestServiceDataParameters
events:
type: Object
repeated: true
optional: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
data: *manifestServiceDataParameters
configuration:
type: Object
optional: true
object: &manifestServiceDataConfiguration
volumes:
type: String
repeated: true
optional: true
volumesFrom:
type: String
repeated: true
optional: true
ports:
type: String
repeated: true
optional: true
args:
type: String
repeated: true
optional: true
command:
type: String
optional: true
dependencies:
type: Object
repeated: true
optional: true
object:
<<: *manifestServiceDataConfiguration
key:
type: String
image:
type: String
repository:
type: String
optional: true
readme:
type: String
optional: true
createTime: *createTime
offers: &offers
type: Object
Expand Down Expand Up @@ -209,9 +78,151 @@ tasks:
inputs:
sid: *sid
outputs:
success:
data: *service
error: *outputError
success:
data:
sid: *sid
createTime: *createTime
owner: *owner
offers: *offers
purchases: *purchases
versions:
type: Object
repeated: true
optional: true
object:
<<: *version
manifestData: &manifestData
type: Object
optional: true
object:
version:
type: String
service: &manifestServiceData
type: Object
object:
deployment:
type: Object
object:
type:
type: String
source:
type: String
definition:
type: Object
object:
hash:
type: String
sid:
type: String
name:
type: String
description:
type: String
optional: true
tasks:
type: Object
repeated: true
optional: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
inputs: &manifestServiceDataParameters
type: Object
optional: true
repeated: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
type:
type: String
optional:
type: Boolean
optional: true
repeated:
type: Boolean
optional: true
object:
type: Object
optional: true
outputs:
type: Object
repeated: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
data: *manifestServiceDataParameters
events:
type: Object
repeated: true
optional: true
object:
key:
type: String
name:
type: String
optional: true
description:
type: String
optional: true
data: *manifestServiceDataParameters
configuration:
type: Object
optional: true
object: &manifestServiceDataConfiguration
volumes:
type: String
repeated: true
optional: true
volumesFrom:
type: String
repeated: true
optional: true
ports:
type: String
repeated: true
optional: true
args:
type: String
repeated: true
optional: true
command:
type: String
optional: true
dependencies:
type: Object
repeated: true
optional: true
object:
<<: *manifestServiceDataConfiguration
key:
type: String
image:
type: String
repository:
type: String
optional: true
readme:
type: String
optional: true
preparePublishServiceVersion:
inputs:
service: *manifestServiceData
Expand Down
9 changes: 0 additions & 9 deletions systemservices/marketplace/src/contracts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import BigNumber from "bignumber.js"
import { Marketplace } from "./Marketplace"
import { Version } from "../types/version";
import { hexToString, parseTimestamp, stringToHex, hexToHash, hashToHex, keccak256 } from "./utils";
import { getManifest } from "./manifest";
import { requireServiceExist } from "./service";
import * as assert from "assert";

Expand All @@ -24,18 +23,10 @@ const getServiceVersionWithIndex = async (contract: Marketplace, sid: string, ve
const getServiceVersion = async (contract: Marketplace, versionHash: string): Promise<Version> => {
assert.ok(await isVersionExist(contract, versionHash), `version does not exist`)
const version = await contract.methods.serviceVersion(hashToHex(versionHash)).call()
let manifestData = null
try {
manifestData = await getManifest(hexToString(version.manifestProtocol), hexToString(version.manifest))
}
catch (error) {
console.warn('error getManifest', error.message)
}
return {
versionHash: versionHash,
manifest: hexToString(version.manifest),
manifestProtocol: hexToString(version.manifestProtocol),
manifestData: manifestData,
createTime: parseTimestamp(version.createTime),
}
}
Expand Down
19 changes: 18 additions & 1 deletion systemservices/marketplace/src/tasks/getService.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import { TaskInputs, TaskOutputs } from "mesg-js/lib/service"
import { Marketplace } from "../contracts/Marketplace"
import { getService } from "../contracts/service";
import { getManifest } from "../contracts/manifest";

export default (contract: Marketplace) => async (inputs: TaskInputs, outputs: TaskOutputs): Promise<void> => {
try {
const service = await getService(contract, inputs.sid)
return outputs.success(service)
const versionsWithManifest = await Promise.all(service.versions.map(async (version) => {
let manifestData = null
try {
manifestData = await getManifest(version.manifestProtocol, version.manifest)
}
catch (error) {
console.warn('error getManifest', error.toString())
}
return {
...version,
manifestData
}
}))
return outputs.success({
...service,
versions: versionsWithManifest,
})
}
catch (error) {
console.error('error in getService', error)
Expand Down
10 changes: 4 additions & 6 deletions systemservices/marketplace/src/tasks/isAuthorized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Marketplace } from "../contracts/Marketplace"
import { getServiceVersion } from "../contracts/version";
import { hexToString, stringToHex, hashToHex, hexToHash } from "../contracts/utils";
import BigNumber from "bignumber.js";
import { getManifest } from "../contracts/manifest";
import { requireServiceExist } from "../contracts/service";

export default (
Expand Down Expand Up @@ -50,15 +51,12 @@ export default (

// get version's manifest data
const version = await getServiceVersion(contract, versionHash)
if (version.manifestData === null) {
throw new Error('could not download manifest of version with hash ' + versionHash)
}

const manifest = await getManifest(version.manifestProtocol, version.manifest)
return outputs.success({
authorized: authorized,
sid: sid,
type: version.manifestData.service.deployment.type,
source: version.manifestData.service.deployment.source,
type: manifest.service.deployment.type,
source: manifest.service.deployment.source,
})
}
catch (error) {
Expand Down
3 changes: 0 additions & 3 deletions systemservices/marketplace/src/types/version.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Manifest } from "./manifest";

export interface Version {
versionHash: string;
manifest: string;
manifestProtocol: string;
manifestData: Manifest|null;
Copy link
Contributor

@ilgooz ilgooz Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about changing this to manifestData?: Manifest; and after this change we can set the manifestData directly into https://github.com/mesg-foundation/core/pull/896/files#diff-1497f2b1650c529c8f58159491cfe107R8 instead of recreating the object

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it's safer to have multiple type. Like this, manifestData doesn't exist in Version and the developer cannot confuse between a not loaded manifestData and an not existing one (when the file is no more accessible from IPFS).

createTime: Date;
}