1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

move template registration outside of asset registration again

This commit is contained in:
Sebastian Gerske 2018-11-06 16:13:12 +01:00
parent 7b399a049a
commit 6ab85631ca
15 changed files with 120 additions and 81 deletions

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@oceanprotocol/squid",
"version": "0.1.2",
"version": "0.1.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -120,9 +120,9 @@
}
},
"@oceanprotocol/keeper-contracts": {
"version": "0.3.16",
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.3.16.tgz",
"integrity": "sha512-aEXJcyqj59YhaCg4me9GEyTF9xX6dYAg4413puVE3sBcOpQUsX27W+uVOd9nQe+b95+Yq7I+dmg1kjN0Xtthpg=="
"version": "0.3.17",
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.3.17.tgz",
"integrity": "sha512-2/dXD2DrHEToVf4QBC+LBI1uMvptW2GnFIQCp3Bfir79crTVKcGymYbpKLUnRO11tb/Hp7ZgdInIFGcxDDLnQA=="
},
"@oceanprotocol/secret-store-client": {
"version": "0.0.7",

View File

@ -51,7 +51,7 @@
"node": ">=8 <10"
},
"dependencies": {
"@oceanprotocol/keeper-contracts": "^0.3.16",
"@oceanprotocol/keeper-contracts": "^0.3.17",
"@oceanprotocol/secret-store-client": "0.0.7",
"bignumber.js": "^8.0.1",
"eth-crypto": "^1.2.5",

View File

@ -1,4 +1,7 @@
import Contract from "web3-eth-contract"
import ServiceAgreementTemplate from "../ocean/ServiceAgreements/ServiceAgreementTemplate"
import Access from "../ocean/ServiceAgreements/Templates/Access"
import FitchainCompute from "../ocean/ServiceAgreements/Templates/FitchainCompute"
import Logger from "../utils/Logger"
import Keeper from "./Keeper"
import Web3Provider from "./Web3Provider"
@ -15,13 +18,24 @@ export default class ContractHandler {
}
}
public static async deployContracts() {
Logger.log("Trying to deploy contracts")
public static async prepareContracts() {
const web3 = Web3Provider.getWeb3()
const deployerAddress = (await web3.eth.getAccounts())[0]
// deploy contracts
await ContractHandler.deployContracts(deployerAddress)
// register templates
await new ServiceAgreementTemplate(new Access()).register(deployerAddress)
await new ServiceAgreementTemplate(new FitchainCompute()).register(deployerAddress)
}
private static contracts: Map<string, Contract> = new Map<string, Contract>()
private static async deployContracts(deployerAddress: string) {
Logger.log("Trying to deploy contracts")
// deploy libs
/* not part of trilobite
const dll = await ContractHandler.deployContract("DLL", deployerAddress)
@ -72,8 +86,6 @@ export default class ContractHandler {
})
}
private static contracts: Map<string, Contract> = new Map<string, Contract>()
private static async load(what: string, where: string): Promise<Contract> {
const web3 = Web3Provider.getWeb3()
// Logger.log("Loading", what, "from", where)

View File

@ -24,6 +24,11 @@ export default class ServiceAgreement extends ContractBase {
return this.call("getTemplateStatus", [templateId])
}
public async getTemplateOwner(templateId: string) {
return this.call("getTemplateOwner", [templateId])
}
public async getAgreementStatus(serviceDefinitionId: string) {
return this.call("getAgreementStatus", [serviceDefinitionId])

View File

@ -49,13 +49,11 @@ export default class Ocean {
const assetId: string = IdGenerator.generateId()
const did: string = `did:op:${assetId}`
const serviceAgreementTemplate: ServiceAgreementTemplate =
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(Access.templateName, Access.Methods,
asset.publisher)
const template = new Access()
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
// get condition keys from template
const conditions: Condition[] = serviceAgreementTemplate.getConditions()
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
// create ddo conditions out of the keys
const ddoConditions: DDOCondition[] = conditions.map((condition: Condition): DDOCondition => {
@ -78,7 +76,7 @@ export default class Ocean {
id: did,
service: [
{
type: Access.templateName,
type: template.templateName,
// tslint:disable
serviceEndpoint: "http://mybrizo.org/api/v1/brizo/services/consume?pubKey=${pubKey}&serviceId={serviceId}&url={url}",
purchaseEndpoint: "http://mybrizo.org/api/v1/brizo/services/access/purchase?",

View File

@ -3,53 +3,51 @@ import ServiceAgreement from "../../keeper/contracts/ServiceAgreement"
import Web3Provider from "../../keeper/Web3Provider"
import MethodReflection from "../../models/MethodReflection"
import ValuePair from "../../models/ValuePair"
import Logger from "../../utils/Logger"
import Account from "../Account"
import OceanBase from "../OceanBase"
import Condition from "./Condition"
import Method from "./Method"
import TemplateBase from "./Templates/TemplateBase"
export default class ServiceAgreementTemplate extends OceanBase {
public static async registerServiceAgreementsTemplate(serviceName: string, methods: Method[],
templateOwner: Account)
: Promise<ServiceAgreementTemplate> {
const methodReflections: MethodReflection[] =
await Promise.all(methods.map(async (method: Method) => {
const methodReflection = await
ContractReflector.reflectContractMethod(method.path)
return methodReflection
}))
public async register(templateOwnerAddress: string)
: Promise<boolean> {
const dependencyMatrix: number[] =
await Promise.all(methods.map(async (method: Method) => {
await Promise.all(this.template.Methods.map(async (method: Method) => {
// tslint:disable
return method.dependency | method.timeout
}))
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
const methodReflections = await this.getMethodReflections()
const owner = await this.getOwner()
if (!owner.getId().startsWith("0x0")) {
Logger.error(`Template with id "${this.template.id}" already registered.`)
return false
}
const receipt = await serviceAgreement.setupAgreementTemplate(
methodReflections, dependencyMatrix,
Web3Provider.getWeb3().utils.fromAscii(serviceName),
templateOwner.getId())
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
templateOwnerAddress)
const serviceAgreementTemplateId =
receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId
const templateId = receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId
const conditions: Condition[] = methodReflections.map((methodReflection, i) => {
return {
methodReflection,
timeout: methods[i].timeout,
condtionKey: ServiceAgreementTemplate.generateConditionsKey(serviceAgreementTemplateId,
methodReflection),
} as Condition
})
if (templateId !== this.template.id) {
throw new Error(`TemplateId missmatch on ${this.template.templateName}! Should be "${this.template.id}" but is ${templateId}`)
}
return new ServiceAgreementTemplate(
serviceAgreementTemplateId,
conditions,
templateOwner)
if (receipt.status) {
Logger.error("Registering template failed")
}
return receipt.status
}
private static generateConditionsKey(serviceAgreementTemplateId: string, methodReflection: MethodReflection)
@ -62,9 +60,8 @@ export default class ServiceAgreementTemplate extends OceanBase {
return Web3Provider.getWeb3().utils.soliditySha3(...values).toString("hex")
}
private constructor(serviceAgreementTemplateId, private conditions: Condition[],
private owner: Account) {
super(serviceAgreementTemplateId)
public constructor(private template: TemplateBase) {
super(template.id)
}
/**
@ -75,11 +72,34 @@ export default class ServiceAgreementTemplate extends OceanBase {
return serviceAgreement.getTemplateStatus(this.getId())
}
public getOwner(): Account {
return this.owner
public async getOwner(): Promise<Account> {
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
return new Account(await serviceAgreement.getTemplateOwner(this.id))
}
public getConditions(): Condition[] {
return this.conditions
private async getMethodReflections(): Promise<MethodReflection[]> {
const methodReflections: MethodReflection[] =
await Promise.all(this.template.Methods.map(async (method: Method) => {
const methodReflection = await
ContractReflector.reflectContractMethod(method.path)
return methodReflection
}))
return methodReflections
}
public async getConditions(): Promise<Condition[]> {
const methodReflections = await this.getMethodReflections()
const conditions: Condition[] = methodReflections.map((methodReflection, i) => {
return {
methodReflection,
timeout: this.template.Methods[i].timeout,
condtionKey: ServiceAgreementTemplate.generateConditionsKey(this.getId(),
methodReflection),
} as Condition
})
return conditions
}
}

View File

@ -3,9 +3,9 @@ import TemplateBase from "./TemplateBase"
export default class Access extends TemplateBase {
public static templateName: string = "Access"
public static id: string = "0x00000000000000000000000000000000000000000000000000000000000001"
public static Methods: Method[] = [
public templateName: string = "Access"
public id: string = "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563"
public Methods: Method[] = [
{
path: "PaymentConditions.lockPayment",
dependency: 0,

View File

@ -1,11 +1,11 @@
import Method from "../Method"
import TemplateBase from "./TemplateBase"
export default class Access extends TemplateBase {
export default class FitchainCompute extends TemplateBase {
public static templateName: string = "FitchainCompute"
public static id: string = "0x00000000000000000000000000000000000000000000000000000000000002"
public static Methods: Method[] = [
public templateName: string = "FitchainCompute"
public id: string = "0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"
public Methods: Method[] = [
{
path: "PaymentConditions.lockPayment",
dependency: 0,

View File

@ -1,6 +1,11 @@
export default class TemplateBase {
import Method from "../Method"
protected static templateName: string
protected static id: string = "0x00000000000000000000000000000000000000000000000000000000000000"
export default abstract class TemplateBase {
public Methods: Method[]
public templateName: string
public id: string = "0x00000000000000000000000000000000000000000000000000000000000000"
constructor(id?: string) {
this.id = id
}
}

View File

@ -13,7 +13,7 @@ describe("ContractWrapperBase", () => {
before(async () => {
ConfigProvider.setConfig(config)
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
await wrappedContract.initMock()
const ocean: Ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()

View File

@ -7,7 +7,7 @@ describe("ContractHandler", () => {
before(async () => {
ConfigProvider.setConfig(config)
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
})
describe("#get()", () => {

View File

@ -30,7 +30,7 @@ describe("Asset", () => {
ConfigProvider.setConfig(config)
AquariusProvider.setAquarius(new AquariusMock(config))
SecretStoreProvider.setSecretStore(new SecretStoreMock(config))
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
testPublisher = accounts[0]

View File

@ -26,7 +26,7 @@ describe("Ocean", () => {
ConfigProvider.setConfig(config)
AquariusProvider.setAquarius(new AquariusMock(config))
SecretStoreProvider.setSecretStore(new SecretStoreMock(config))
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()

View File

@ -19,30 +19,26 @@ import AquariusConnectorMock from "../mocks/AquariusConnector.mock"
let ocean: Ocean
let accounts: Account[]
let publisherAccount: Account
let templateOwnerAccount: Account
let consumerAccount: Account
let testServiceAgreementTemplate: ServiceAgreementTemplate
let serviceDefintion
describe("ServiceAgreement", () => {
before(async () => {
ConfigProvider.setConfig(config)
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
templateOwnerAccount = accounts[0]
publisherAccount = accounts[1]
consumerAccount = accounts[2]
testServiceAgreementTemplate =
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(Access.templateName, Access.Methods,
templateOwnerAccount)
const serviceAgreementTemplate: ServiceAgreementTemplate =
new ServiceAgreementTemplate(new Access())
// get condition keys from template
const conditions: Condition[] = testServiceAgreementTemplate.getConditions()
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
// create ddo conditions out of the keys
const ddoConditions: DDOCondition[] = conditions.map((condition): DDOCondition => {
@ -62,7 +58,7 @@ describe("ServiceAgreement", () => {
serviceDefintion = [
{
serviceDefinitionId: IdGenerator.generateId(),
templateId: testServiceAgreementTemplate.getId(),
templateId: serviceAgreementTemplate.getId(),
conditions: ddoConditions,
} as Service,
]

View File

@ -2,6 +2,7 @@ import {assert} from "chai"
import ConfigProvider from "../../src/ConfigProvider"
import ContractHandler from "../../src/keeper/ContractHandler"
import Account from "../../src/ocean/Account"
import IdGenerator from "../../src/ocean/IdGenerator"
import Ocean from "../../src/ocean/Ocean"
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
@ -14,22 +15,23 @@ describe("ServiceAgreementTemplate", () => {
before(async () => {
ConfigProvider.setConfig(config)
await ContractHandler.deployContracts()
await ContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
accounts = await ocean.getAccounts()
})
describe("#registerServiceAgreementsTemplate()", () => {
describe("#register()", () => {
it("should setup an agreement template", async () => {
const templateOwner = accounts[0]
const serviceAgreementTemplate: ServiceAgreementTemplate =
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(Access.templateName, Access.Methods,
templateOwner)
new ServiceAgreementTemplate(new Access(IdGenerator.generateId()))
assert(serviceAgreementTemplate)
await serviceAgreementTemplate.register(templateOwner.getId())
assert(serviceAgreementTemplate.getId())
assert(serviceAgreementTemplate.getOwner().getId() === templateOwner.getId())
assert((await serviceAgreementTemplate.getOwner()).getId() === templateOwner.getId())
})
})
@ -38,10 +40,11 @@ describe("ServiceAgreementTemplate", () => {
const publisherAccount = accounts[0]
const serviceAgreementTemplate: ServiceAgreementTemplate =
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(Access.templateName, Access.Methods,
publisherAccount)
new ServiceAgreementTemplate(new Access(IdGenerator.generateId()))
assert(serviceAgreementTemplate)
await serviceAgreementTemplate.register(publisherAccount.getId())
const templateStatus = await serviceAgreementTemplate.getStatus()
assert(templateStatus === true)
})