dev.js 3.8 KB

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