Эх сурвалжийг харах

storage-node: refactor call to storageworkinggoup into workers api module

Mokhtar Naamani 4 жил өмнө
parent
commit
1ebdf8f2ca

+ 5 - 36
storage-node/packages/cli/bin/dev.js

@@ -90,51 +90,20 @@ const init = async (api) => {
 
   // Make alice the storage lead
   debug('Setting Alice as Lead')
-  // prepare set storage lead tx
-  const setLeadTx = api.api.tx.storageWorkingGroup.setLead(aliceMemberId, alice)
-  // make sudo call
-  api.signAndSend(
-    alice,
-    api.api.tx.sudo.sudo(setLeadTx)
-  )
+  api.workers.dev_setLead(alice, aliceMemberId, alice)
 
   // create an openinging, apply, start review, fill opening
   debug(`Making ${ROLE_ACCOUNT_URI} account a storage provider`)
 
-  const openTx = api.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
-    application_rationing_policy: {
-      'max_active_applicants': 1
-    },
-    max_review_period_length: 1000
-    // default values for everything else..
-  }, 'dev-opening')
-
-  const openingId = await api.signAndSendThenGetEventResult(alice, openTx, {
-    eventModule: 'storageWorkingGroup',
-    eventName: 'WorkerOpeningAdded',
-    eventProperty: 'WorkerOpeningId'
-  })
+  const openingId = await api.workers.dev_addWorkerOpening(alice)
   debug(`created new opening id ${openingId}`)
 
-  const applyTx = api.api.tx.storageWorkingGroup.applyOnWorkerOpening(
-    aliceMemberId, openingId, roleAccount, null, null, 'colossus'
-  )
-  const applicationId = await api.signAndSendThenGetEventResult(alice, applyTx, {
-    eventModule: 'storageWorkingGroup',
-    eventName: 'AppliedOnWorkerOpening',
-    eventProperty: 'WorkerApplicationId'
-  })
+  const applicationId = await api.workers.dev_applyOnOpening(openingId, aliceMemberId, alice, roleAccount)
   debug(`created application id ${applicationId}`)
 
-  const reviewTx = api.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
-  api.signAndSend(alice, reviewTx)
+  api.workers.dev_beginOpeningReview(openingId, alice)
 
-  const fillTx = api.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicationId], null)
-  const filledMap = await api.signAndSendThenGetEventResult(alice, fillTx, {
-    eventModule: 'storageWorkingGroup',
-    eventName: 'WorkerOpeningFilled',
-    eventProperty: 'WorkerApplicationIdToWorkerIdMap'
-  })
+  const filledMap = await api.workers.dev_fillOpeningWithSingleApplication(openingId, alice, applicationId)
 
   if (filledMap.size === 0) {
     throw new Error('Expected opening to be filled!')

+ 60 - 0
storage-node/packages/runtime-api/workers.js

@@ -112,6 +112,66 @@ 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
+
+  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_addWorkerOpening(leadAccount) {
+    const openTx = this.base.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
+      application_rationing_policy: {
+        'max_active_applicants': 1
+      },
+      max_review_period_length: 1000
+      // default values for everything else..
+    }, 'dev-opening')
+
+    const openingId = await this.base.signAndSendThenGetEventResult(leadAccount, openTx, {
+      eventModule: 'storageWorkingGroup',
+      eventName: 'WorkerOpeningAdded',
+      eventProperty: 'WorkerOpeningId'
+    })
+
+    return openingId
+  }
+
+  async dev_applyOnOpening(openingId, memberId, memberAccount, roleAccount) {
+    const applyTx = this.base.api.tx.storageWorkingGroup.applyOnWorkerOpening(
+      memberId, openingId, roleAccount, null, null, `colossus-${memberId}`
+    )
+    const applicationId = await this.base.signAndSendThenGetEventResult(memberAccount, applyTx, {
+      eventModule: 'storageWorkingGroup',
+      eventName: 'AppliedOnWorkerOpening',
+      eventProperty: 'WorkerApplicationId'
+    })
+
+    return applicationId
+  }
+
+  async dev_beginOpeningReview(openingId, leadAccount) {
+    const reviewTx = this.base.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
+    return this.base.signAndSend(leadAccount, reviewTx)
+  }
+
+  async dev_fillOpeningWithSingleApplication(openingId, leadAccount, applicationId) {
+    const fillTx = this.base.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicationId], null)
+
+    const filledMap = await this.base.signAndSendThenGetEventResult(leadAccount, fillTx, {
+      eventModule: 'storageWorkingGroup',
+      eventName: 'WorkerOpeningFilled',
+      eventProperty: 'WorkerApplicationIdToWorkerIdMap'
+    })
+
+    return filledMap
+  }
 }
 
 module.exports = {