workers.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const debug = require('debug')('joystream:runtime:roles')
  20. const BN = require('bn.js')
  21. // const { createType } = require('@polkadot/types')
  22. const { Worker } = require('@joystream/types/lib/working-group')
  23. /*
  24. * Add worker related functionality to the substrate API.
  25. */
  26. class WorkersApi {
  27. static async create (base) {
  28. const ret = new WorkersApi()
  29. ret.base = base
  30. await ret.init()
  31. return ret
  32. }
  33. async init () {
  34. debug('Init')
  35. }
  36. /*
  37. * Check whether the given account and id represent an active storage provider
  38. */
  39. async isRoleAccountOfStorageProvider (storageProviderId, roleAccountId) {
  40. storageProviderId = new BN(storageProviderId)
  41. roleAccountId = this.base.identities.keyring.decodeAddress(roleAccountId)
  42. const account = await this.storageProviderRoleAccount(storageProviderId)
  43. return account && account.eq(roleAccountId)
  44. }
  45. async isStorageProvider (storageProviderId) {
  46. const worker = await this.storageWorkerByProviderId(storageProviderId)
  47. return worker !== null
  48. }
  49. // Returns a provider's role account or null if provider doesn't exist
  50. async storageProviderRoleAccount (storageProviderId) {
  51. const worker = await this.storageWorkerByProviderId(storageProviderId)
  52. return worker ? worker.role_account : null
  53. }
  54. // Returns a Worker instance or null if provider does not exist
  55. async storageWorkerByProviderId (storageProviderId) {
  56. storageProviderId = new BN(storageProviderId)
  57. const { providers } = await this.getAllProviders()
  58. return providers[storageProviderId.toNumber()] || null
  59. }
  60. async getAllProviders () {
  61. // const workerEntries = await this.base.api.query.storageWorkingGroup.workerById()
  62. // can't rely on .isEmpty or isNone property to detect empty map
  63. // return workerEntries.isNone ? [] : workerEntries[0]
  64. // return workerEntries.isEmpty ? [] : workerEntries[0]
  65. // So we iterate over possible ids which may or may not exist, by reading directly
  66. // from storage value
  67. const nextWorkerId = (await this.base.api.query.storageWorkingGroup.nextWorkerId()).toNumber()
  68. let ids = []
  69. let providers = {}
  70. for (let id = 0; id < nextWorkerId; id++) {
  71. // We get back an Option. Will be None if value doesn't exist
  72. let value = await this.base.api.rpc.state.getStorage(
  73. this.base.api.query.storageWorkingGroup.workerById.key(id)
  74. )
  75. if (!value.isNone) {
  76. // no need to read from storage again!
  77. // const worker = (await this.base.api.query.storageWorkingGroup.workerById(id))[0]
  78. value = value.unwrap()
  79. // construct the Worker type from raw data
  80. // const worker = createType('WorkerOf', value)
  81. // const worker = new Worker(value)
  82. ids.push(id)
  83. providers[id] = new Worker(value)
  84. }
  85. }
  86. return { ids, providers }
  87. }
  88. }
  89. module.exports = {
  90. WorkersApi
  91. }