Browse Source

storage-node: update nicaea types

Mokhtar Naamani 4 years ago
parent
commit
a40b77a1a0

+ 1 - 2
storage-node/packages/cli/bin/cli.js

@@ -103,7 +103,7 @@ const commands = {
     // Generate content ID
     // FIXME this require path is like this because of
     // https://github.com/Joystream/apps/issues/207
-    const { ContentId } = require('@joystream/types/lib/media')
+    const { ContentId } = require('@joystream/types/media')
     var cid = ContentId.generate()
     cid = cid.encode().toString()
     debug('Generated content ID: ' + chalk.green(cid))
@@ -234,7 +234,6 @@ async function main () {
 
 main()
   .then(() => {
-    debug('Process exiting gracefully.')
     process.exit(0)
   })
   .catch((err) => {

+ 10 - 25
storage-node/packages/cli/bin/dev.js

@@ -89,40 +89,25 @@ const init = async (api) => {
   }
 
   // Make alice the storage lead
-  debug('Setting Alice as Lead')
-  api.workers.dev_setLead(alice, aliceMemberId, alice)
+  debug('Making Alice the storage Lead')
+  const leadOpeningId = await api.workers.dev_addStorageLeadOpening(alice)
+  const leadApplicationId = await api.workers.dev_applyOnOpening(leadOpeningId, aliceMemberId, alice, alice)
+  api.workers.dev_beginLeadOpeningReview(leadOpeningId, alice)
+  api.workers.dev_fillLeadOpening(leadOpeningId, leadApplicationId, alice)
 
-  // create an openinging, apply, start review, fill opening
+  // Create a storage openinging, apply, start review, and fill opening
   debug(`Making ${ROLE_ACCOUNT_URI} account a storage provider`)
 
-  const openingId = await api.workers.dev_addWorkerOpening(alice)
+  const openingId = await api.workers.dev_addStorageOpening(alice)
   debug(`created new opening id ${openingId}`)
 
   const applicationId = await api.workers.dev_applyOnOpening(openingId, aliceMemberId, alice, roleAccount)
   debug(`created application id ${applicationId}`)
 
-  api.workers.dev_beginOpeningReview(openingId, alice)
+  api.workers.dev_beginStorageOpeningReview(openingId, alice)
 
-  const filledMap = await api.workers.dev_fillOpeningWithSingleApplication(openingId, alice, applicationId)
-
-  if (filledMap.size === 0) {
-    throw new Error('Expected opening to be filled!')
-  }
-
-  let ourApplicationIdKey
-
-  for (let key of filledMap.keys()) {
-    if (key.eq(applicationId)) {
-      ourApplicationIdKey = key
-      break
-    }
-  }
-
-  if (!ourApplicationIdKey) {
-    throw new Error('Expected our application id to have been filled!')
-  }
-
-  const providerId = filledMap.get(ourApplicationIdKey)
+  debug(`filling storage opening`)
+  const providerId = await api.workers.dev_fillStorageOpening(openingId, applicationId, alice)
 
   debug(`Assigned storage provider id: ${providerId}`)
 

+ 3 - 0
storage-node/packages/colossus/bin/cli.js

@@ -294,6 +294,9 @@ async function main () {
 }
 
 main()
+  .then(() => {
+    process.exit(0)
+  })
   .catch((err) => {
     console.error(chalk.red(err.stack))
     process.exit(-1)

+ 125 - 37
storage-node/packages/runtime-api/workers.js

@@ -20,7 +20,7 @@
 
 const debug = require('debug')('joystream:runtime:roles')
 const BN = require('bn.js')
-const { Worker } = require('@joystream/types/lib/working-group')
+const { Worker } = require('@joystream/types/working-group')
 
 /*
  * Add worker related functionality to the substrate API.
@@ -62,7 +62,7 @@ class WorkersApi {
    */
   async storageProviderRoleAccount (storageProviderId) {
     const worker = await this.storageWorkerByProviderId(storageProviderId)
-    return worker ? worker.role_account : null
+    return worker ? worker.role_account_id : null
   }
 
   /*
@@ -82,7 +82,7 @@ class WorkersApi {
 
     for (let i = 0; i < ids.length; i++) {
       const id = ids[i]
-      if (providers[id].role_account.eq(roleAccount)) {
+      if (providers[id].role_account_id.eq(roleAccount)) {
         return id
       }
     }
@@ -125,38 +125,57 @@ class WorkersApi {
     return { ids, providers }
   }
 
-  // Helper methods below don't really belong in the colossus api, they are here
-  // mainly to be used by the dev-init command in the cli to setup a development environment
+  // Helper methods below don't really belong in the colossus runtime api library.
+  // They are only used by the dev-init command in the cli to setup a development environment
 
   /*
-   * Sets the storage working group lead
+   * Add a new storage group opening using the lead account. Returns the
+   * new opening id.
    */
-  async dev_setLead(sudo, memberId, roleAccount) {
-    const setLeadTx = this.base.api.tx.storageWorkingGroup.setLead(memberId, roleAccount)
-    // make sudo call
-    return this.base.signAndSend(
-      sudo,
-      this.base.api.tx.sudo.sudo(setLeadTx)
-    )
+  async dev_addStorageOpening(leadAccount) {
+    const openTx = this.dev_makeAddOpeningTx('Worker')
+    return this.dev_submitAddOpeningTx(openTx, leadAccount)
   }
 
   /*
-   * Add a new storage group worker opening using the lead account. Returns the
+   * Add a new storage working group lead opening using sudo account. Returns the
    * new opening id.
    */
-  async dev_addWorkerOpening(leadAccount) {
-    const openTx = this.base.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
-      application_rationing_policy: {
-        'max_active_applicants': 1
+  async dev_addStorageLeadOpening(sudoAccount) {
+    const openTx = this.dev_makeAddOpeningTx('Leader')
+    const sudoTx = this.base.api.tx.sudo.sudo(openTx)
+    return this.dev_submitAddOpeningTx(sudoTx, sudoAccount)
+  }
+
+  /*
+   * Constructs an addOpening tx of openingType
+   */
+  dev_makeAddOpeningTx(openingType) {
+    const openTx = this.base.api.tx.storageWorkingGroup.addOpening(
+      'CurrentBlock',
+      {
+        application_rationing_policy: {
+          'max_active_applicants': 1
+        },
+        max_review_period_length: 1000
+        // default values for everything else..
       },
-      max_review_period_length: 1000
-      // default values for everything else..
-    }, 'dev-opening')
+      'dev-opening',
+      openingType
+    )
+
+    return openTx
+  }
 
-    const openingId = await this.base.signAndSendThenGetEventResult(leadAccount, openTx, {
+  /*
+   * Submits a tx (expecting it to dispatch storageWorkingGroup.addOpening) and returns
+   * the OpeningId from the resulting event.
+   */
+  async dev_submitAddOpeningTx(tx, senderAccount) {
+    const openingId = await this.base.signAndSendThenGetEventResult(senderAccount, tx, {
       eventModule: 'storageWorkingGroup',
-      eventName: 'WorkerOpeningAdded',
-      eventProperty: 'WorkerOpeningId'
+      eventName: 'OpeningAdded',
+      eventProperty: 'OpeningId'
     })
 
     return openingId
@@ -166,42 +185,111 @@ class WorkersApi {
    * Apply on an opening, returns the application id.
    */
   async dev_applyOnOpening(openingId, memberId, memberAccount, roleAccount) {
-    const applyTx = this.base.api.tx.storageWorkingGroup.applyOnWorkerOpening(
+    const applyTx = this.base.api.tx.storageWorkingGroup.applyOnOpening(
       memberId, openingId, roleAccount, null, null, `colossus-${memberId}`
     )
     const applicationId = await this.base.signAndSendThenGetEventResult(memberAccount, applyTx, {
       eventModule: 'storageWorkingGroup',
-      eventName: 'AppliedOnWorkerOpening',
-      eventProperty: 'WorkerApplicationId'
+      eventName: 'AppliedOnOpening',
+      eventProperty: 'ApplicationId'
     })
 
     return applicationId
   }
 
   /*
-   * Move the opening into the review state
+   * Move lead opening to review state using sudo account
    */
-  async dev_beginOpeningReview(openingId, leadAccount) {
-    const reviewTx = this.base.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
-    return this.base.signAndSend(leadAccount, reviewTx)
+  async dev_beginLeadOpeningReview(openingId, sudoAccount) {
+    const beginReviewTx = this.dev_makeBeginOpeningReviewTx(openingId)
+    const sudoTx = this.base.api.tx.sudo.sudo(beginReviewTx)
+    return this.base.signAndSend(sudoAccount, sudoTx)
   }
 
   /*
-   * Fill an opening, returns a map of the application id to their new assigned worker ids.
+   * Move a storage opening to review state using lead account
    */
-  async dev_fillOpeningWithSingleApplication(openingId, leadAccount, applicationId) {
-    const fillTx = this.base.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicationId], null)
+  async dev_beginStorageOpeningReview(openingId, leadAccount) {
+    const beginReviewTx = this.dev_makeBeginOpeningReviewTx(openingId)
+    return this.base.signAndSend(leadAccount, beginReviewTx)
+  }
+
+  /*
+   * Constructs a beingApplicantReview tx for openingId, which puts an opening into the review state
+   */
+  dev_makeBeginOpeningReviewTx(openingId) {
+    return this.base.api.tx.storageWorkingGroup.beginApplicantReview(openingId)
+  }
 
-    const filledMap = await this.base.signAndSendThenGetEventResult(leadAccount, fillTx, {
+  /*
+   * Fill a lead opening, return the assigned worker id, using the sudo account
+   */
+  async dev_fillLeadOpening(openingId, applicationId, sudoAccount) {
+    const fillTx = this.dev_makeFillOpeningTx(openingId, applicationId)
+    const sudoTx = this.base.api.tx.sudo.sudo(fillTx)
+    const filled = await this.dev_submitFillOpeningTx(sudoAccount, sudoTx)
+    return getWorkerIdFromApplicationIdToWorkerIdMap(filled, applicationId)
+  }
+
+  /*
+   * Fill a storage opening, return the assigned worker id, using the lead account
+   */
+  async dev_fillStorageOpening(openingId, applicationId, leadAccount) {
+    const fillTx = this.dev_makeFillOpeningTx(openingId, applicationId)
+    const filled = await this.dev_submitFillOpeningTx(leadAccount, fillTx)
+    return getWorkerIdFromApplicationIdToWorkerIdMap(filled, applicationId)
+  }
+
+  /*
+   * Constructs a FillOpening transaction
+   */
+  dev_makeFillOpeningTx(openingId, applicationId) {
+    const fillTx = this.base.api.tx.storageWorkingGroup.fillOpening(openingId, [applicationId], null)
+    return fillTx
+  }
+
+  /*
+   * Dispatches a fill opening tx and returns a map of the application id to their new assigned worker ids.
+   */
+  async dev_submitFillOpeningTx(senderAccount, tx) {
+    const filledMap = await this.base.signAndSendThenGetEventResult(senderAccount, tx, {
       eventModule: 'storageWorkingGroup',
-      eventName: 'WorkerOpeningFilled',
-      eventProperty: 'WorkerApplicationIdToWorkerIdMap'
+      eventName: 'OpeningFilled',
+      eventProperty: 'ApplicationIdToWorkerIdMap'
     })
 
     return filledMap
   }
 }
 
+/*
+ * Finds assigned worker id corresponding to the application id from the resulting
+ * ApplicationIdToWorkerIdMap map in the OpeningFilled event. Expects map to
+ * contain at least one entry.
+ */
+function getWorkerIdFromApplicationIdToWorkerIdMap (filledMap, applicationId) {
+  if (filledMap.size === 0) {
+    throw new Error('Expected opening to be filled!')
+  }
+
+  let ourApplicationIdKey
+
+  for (let key of filledMap.keys()) {
+    if (key.eq(applicationId)) {
+      ourApplicationIdKey = key
+      break
+    }
+  }
+
+  if (!ourApplicationIdKey) {
+    throw new Error('Expected application id to have been filled!')
+  }
+
+  const workerId = filledMap.get(ourApplicationIdKey)
+
+  return workerId
+}
+
 module.exports = {
   WorkersApi
 }