dev.js 3.7 KB

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