Browse Source

storage node refactoring

Mokhtar Naamani 4 years ago
parent
commit
fff4d37935

+ 2 - 0
storage-node/.gitignore

@@ -25,3 +25,5 @@ node_modules/
 
 # Ignore nvm config file
 .nvmrc
+
+yarn.lock

+ 5 - 5
storage-node/packages/discovery/discover.js

@@ -12,9 +12,9 @@ var activeDiscoveries = {};
 var accountInfoCache = {};
 const CACHE_TTL = 60 * 60 * 1000;
 
-async function getIpnsIdentity (actorAccountId, runtimeApi) {
-    // lookup ipns identity from chain corresponding to actorAccountId
-    const info = await runtimeApi.discovery.getAccountInfo(actorAccountId)
+async function getIpnsIdentity (storageProviderId, runtimeApi) {
+    // lookup ipns identity from chain corresponding to storageProviderId
+    const info = await runtimeApi.discovery.getAccountInfo(storageProviderId)
 
     if (info == null) {
         // no identity found on chain for account
@@ -24,8 +24,8 @@ async function getIpnsIdentity (actorAccountId, runtimeApi) {
     }
 }
 
-async function discover_over_ipfs_http_gateway(actorAccountId, runtimeApi, gateway) {
-    let isActor = await runtimeApi.identities.isActor(actorAccountId)
+async function discover_over_ipfs_http_gateway(storageProviderId, runtimeApi, gateway) {
+    let isActor = await runtimeApi.identities.isActor(storageProviderId)
 
     if (!isActor) {
         throw new Error('Cannot discover non actor account service info')

+ 0 - 39
storage-node/packages/runtime-api/identities.js

@@ -137,16 +137,6 @@ class IdentitiesApi
     return memberIds.length > 0 // true if at least one member id exists for the acccount
   }
 
-  /*
-   * Return true if the account is an actor/role account
-   */
-  // async isActor(accountId) // change to is StorageWorkingGroup worker ?
-  // {
-  //   const decoded = this.keyring.decodeAddress(accountId);
-  //   const actor = await this.base.api.query.actors.actorByAccountId(decoded)
-  //   return actor.isSome
-  // }
-
   /*
    * Return the member IDs of an account
    */
@@ -166,35 +156,6 @@ class IdentitiesApi
     return ids[0]
   }
 
-  /*
-   * Create a new key for the given role *name*. If no name is given,
-   * default to 'storage'.
-   */
-  // async createRoleKey(accountId, role)
-  // {
-  //   role = role || 'storage';
-
-  //   // Generate new key pair
-  //   const keyPair = util_crypto.naclKeypairFromRandom();
-
-  //   // Encode to an address.
-  //   const addr = this.keyring.encodeAddress(keyPair.publicKey);
-  //   debug('Generated new key pair with address', addr);
-
-  //   // Add to key wring. We set the meta to identify the account as
-  //   // a role key.
-  //   const meta = {
-  //     name: `${role} role account for ${accountId}`,
-  //   };
-
-  //   const createPair = require('@polkadot/keyring/pair').default;
-  //   const pair = createPair('ed25519', keyPair, meta);
-
-  //   this.keyring.addPair(pair);
-
-  //   return pair;
-  // }
-
   /*
    * Export a key pair to JSON. Will ask for a passphrase.
    */

+ 31 - 28
storage-node/packages/runtime-api/workers.js

@@ -16,49 +16,52 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-'use strict';
-
-const debug = require('debug')('joystream:runtime:roles');
-
-const { Null, u64 } = require('@polkadot/types');
-
-const { _ } = require('lodash');
+'use strict'
 
+const debug = require('debug')('joystream:runtime:roles')
+const { Null } = require('@polkadot/types')
+const { _ } = require('lodash')
+const { Worker } = require('@joystream/types/working-group')
 /*
  * Add worker related functionality to the substrate API.
  */
-class WorkersApi
-{
-  static async create(base)
-  {
-    const ret = new WorkersApi();
-    ret.base = base;
-    await ret.init();
-    return ret;
+class WorkersApi {
+  static async create (base) {
+    const ret = new WorkersApi()
+    ret.base = base
+    await ret.init()
+    return ret
   }
 
-  async init()
-  {
-    debug('Init');
+  async init () {
+    debug('Init')
   }
 
-
   /*
-   * Check whether the given account and id is an active storage provider
+   * Check whether the given account and id represent an active storage provider
    */
-  async checkForRole(roleAccountId, storageProviderId)
-  {
-    const worker = await this.base.api.query.storageWorkingGroup.workerById(storageProviderId);
+  async isRoleAccountOfStorageProvider (storageProviderId, roleAccountId) {
+    const worker = await this.storageWorkerByProviderId(storageProviderId)
+    return worker && worker.role_account.eq(roleAccountId)
+  }
+
+  async isStorageProvider (storageProviderId) {
+    const worker = await this.storageWorkerByProviderId(storageProviderId)
+    return worker !== null
+  }
 
-    // FIXME: get single linked entry
+  async storageWorkerByProviderId (storageProviderId) {
+    // FIXME: single linked entry
+    const workerEntry = await this.base.api.query.storageWorkingGroup.workerById(storageProviderId)
 
-    if (_.isEqual(worker.raw, new Null())) {
-      return false
+    // use .isEmpty instead ?
+    if (_.isEqual(workerEntry.raw, new Null())) {
+      return null
     }
 
-    return worker.raw.role_account == roleAccountId
+    // return value
+    return new Worker(workerEntry[0])
   }
-
 }
 
 module.exports = {