dev.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* eslint-disable no-console */
  2. 'use strict'
  3. const debug = require('debug')('joystream:storage-cli:dev')
  4. const assert = require('assert')
  5. // Derivation path appended to well known development seed used on
  6. // development chains
  7. const ALICE_URI = '//Alice'
  8. const ROLE_ACCOUNT_URI = '//Colossus'
  9. function aliceKeyPair (api) {
  10. return api.identities.keyring.addFromUri(ALICE_URI, null, 'sr25519')
  11. }
  12. function roleKeyPair (api) {
  13. return api.identities.keyring.addFromUri(ROLE_ACCOUNT_URI, null, 'sr25519')
  14. }
  15. function developmentPort () {
  16. return 3001
  17. }
  18. const check = async (api) => {
  19. const roleAccountId = roleKeyPair(api).address
  20. const providerId = await api.workers.findProviderIdByRoleAccount(roleAccountId)
  21. if (providerId === null) {
  22. throw new Error('Dev storage provider not found on chain!')
  23. }
  24. console.log(`
  25. Chain is setup with Dev storage provider:
  26. providerId = ${providerId}
  27. roleAccountId = ${roleAccountId}
  28. roleKey = ${ROLE_ACCOUNT_URI}
  29. `)
  30. return providerId
  31. }
  32. // Setup Alice account on a developement chain as
  33. // a member, storage lead, and a storage provider using a deterministic
  34. // development key for the role account
  35. const init = async (api) => {
  36. try {
  37. await check(api)
  38. return
  39. } catch (err) {
  40. // We didn't find a storage provider with expected role account
  41. }
  42. const alice = aliceKeyPair(api).address
  43. const roleAccount = roleKeyPair(api).address
  44. debug(`Ensuring Alice is sudo`)
  45. // make sure alice is sudo - indirectly checking this is a dev chain
  46. const sudo = await api.identities.getSudoAccount()
  47. if (!sudo.eq(alice)) {
  48. throw new Error('Setup requires Alice to be sudo. Are you sure you are running a devchain?')
  49. }
  50. console.log('Running setup')
  51. // set localhost colossus as discovery provider
  52. // assuming pioneer dev server is running on port 3000 we should run
  53. // the storage dev server on a different port than the default for colossus which is also
  54. // 3000
  55. debug('Setting Local development node as bootstrap endpoint')
  56. await api.discovery.setBootstrapEndpoints(alice, [`http://localhost:${developmentPort()}/`])
  57. debug('Transferring tokens to storage role account')
  58. // Give role account some tokens to work with
  59. api.balances.transfer(alice, roleAccount, 100000)
  60. debug('Ensuring Alice is as member..')
  61. let aliceMemberId = await api.identities.firstMemberIdOf(alice)
  62. if (aliceMemberId === undefined) {
  63. debug('Registering Alice as member..')
  64. aliceMemberId = await api.identities.registerMember(alice, {
  65. handle: 'alice'
  66. })
  67. } else {
  68. debug('Alice is already a member')
  69. }
  70. // Make alice the storage lead
  71. debug('Making Alice the storage Lead')
  72. const leadOpeningId = await api.workers.dev_addStorageLeadOpening()
  73. const leadApplicationId = await api.workers.dev_applyOnOpening(leadOpeningId, aliceMemberId, alice, alice)
  74. api.workers.dev_beginLeadOpeningReview(leadOpeningId)
  75. await api.workers.dev_fillLeadOpening(leadOpeningId, leadApplicationId)
  76. const leadAccount = await api.workers.getLeadRoleAccount()
  77. if (!leadAccount.eq(alice)) {
  78. throw new Error('Setting alice as lead failed')
  79. }
  80. // Create a storage openinging, apply, start review, and fill opening
  81. debug(`Making ${ROLE_ACCOUNT_URI} account a storage provider`)
  82. const openingId = await api.workers.dev_addStorageOpening()
  83. debug(`created new storage opening: ${openingId}`)
  84. const applicationId = await api.workers.dev_applyOnOpening(openingId, aliceMemberId, alice, roleAccount)
  85. debug(`applied with application id: ${applicationId}`)
  86. api.workers.dev_beginStorageOpeningReview(openingId)
  87. debug(`filling storage opening`)
  88. const providerId = await api.workers.dev_fillStorageOpening(openingId, applicationId)
  89. debug(`Assigned storage provider id: ${providerId}`)
  90. return check(api)
  91. }
  92. module.exports = {
  93. init,
  94. check,
  95. aliceKeyPair,
  96. roleKeyPair,
  97. developmentPort
  98. }