dev.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const debug = require('debug')('joystream:storage-cli:dev')
  2. function aliceKeyPair (api) {
  3. return api.identities.keyring.addFromUri('//Alice', null, 'sr25519')
  4. }
  5. function roleKeyPair (api) {
  6. return api.identities.keyring.addFromUri('//Colossus', null, 'sr25519')
  7. }
  8. // Setup Alice account on a developement chain that was
  9. // just launched as the storage lead, and a storage provider using the same
  10. // key as the role key
  11. const init = async (api) => {
  12. try {
  13. return await check(api)
  14. } catch (err) {
  15. // setup is not correct we can try to run setup
  16. }
  17. const alice = aliceKeyPair(api).address
  18. const roleAccount = roleKeyPair(api).address
  19. const providerId = 0 // first assignable id
  20. console.log(`Checking for dev chain...`)
  21. // make sure alice is sudo - indirectly checking this is a dev chain
  22. const sudo = await api.api.query.sudo.key()
  23. if (!sudo.eq(alice)) {
  24. throw new Error('Setup requires Alice to be sudo. Are you sure you are running a devchain?')
  25. }
  26. console.log('Setting up chain...')
  27. debug('Transfering tokens to storage role account')
  28. // Give role account some tokens to work with
  29. api.balances.transfer(alice, roleAccount, 100000)
  30. debug('Registering Alice as Member')
  31. // register alice as a member
  32. const aliceMemberId = await api.identities.registerMember(alice, {
  33. handle: 'alice'
  34. })
  35. // if (!aliceMemberId.eq(0)) {
  36. // // not first time script is running!
  37. // }
  38. // Make alice the storage lead
  39. debug('Setting Alice as Lead')
  40. // prepare set storage lead tx
  41. const setLeadTx = api.api.tx.storageWorkingGroup.setLead(aliceMemberId, alice)
  42. // make sudo call
  43. api.signAndSend(
  44. alice,
  45. api.api.tx.sudo.sudo(setLeadTx)
  46. )
  47. // create an openinging, apply, start review, fill opening
  48. // Assumption opening id and applicant id, provider id will all be the
  49. // first assignable id == 0
  50. // so we don't await each tx to finalize to get the ids. this allows us to
  51. // batch all the transactions into a single block.
  52. debug('Making //Colossus account a storage provider')
  53. const openTx = api.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
  54. application_rationing_policy: {
  55. 'max_active_applicants': 1
  56. },
  57. max_review_period_length: 1000
  58. // default values for everything else..
  59. }, 'opening0')
  60. api.signAndSend(alice, openTx)
  61. const openingId = 0 // first assignable opening id
  62. const applyTx = api.api.tx.storageWorkingGroup.applyOnWorkerOpening(
  63. aliceMemberId, openingId, roleAccount, null, null, 'colossus'
  64. )
  65. api.signAndSend(alice, applyTx)
  66. const applicantId = 0 // first assignable applicant id
  67. const reviewTx = api.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
  68. api.signAndSend(alice, reviewTx)
  69. const fillTx = api.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicantId], null)
  70. await api.signAndSend(alice, fillTx)
  71. // wait for previous transactions to finalize so we can read correct state
  72. if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccount)) {
  73. console.log('Storage Role setup Completed Successfully')
  74. } else { throw new Error('Setup Failed') }
  75. // set localhost colossus as discovery provider on default port
  76. // assuming pioneer dev server is running on port 3000 we should run
  77. // the storage dev server on port 3001
  78. debug('Setting Local development node as bootstrap endpoint')
  79. await api.discovery.setBootstrapEndpoints(alice, ['http://localhost:3001/'])
  80. }
  81. const check = async (api) => {
  82. const providerId = 0 // the first provider id which would have been assigned in dev-init
  83. const roleAccountId = roleKeyPair(api).address
  84. const alice = aliceKeyPair(api).address
  85. if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccountId)) {
  86. console.log(`
  87. Chain is setup with Alice as a storage provider:
  88. providerId = ${providerId}
  89. roleAccount = "//Colossus"
  90. `)
  91. } else { throw new Error('Alice is not a storage provider') }
  92. const currentLead = await api.api.query.storageWorkingGroup.currentLead()
  93. if (currentLead.isSome && currentLead.unwrap().role_account_id.eq(alice)) {
  94. console.log(`
  95. Alice is correctly setup as the storage lead
  96. `)
  97. } else {
  98. throw new Error('Alice is not the storage lead!')
  99. }
  100. }
  101. module.exports = {
  102. init,
  103. check,
  104. aliceKeyPair,
  105. roleKeyPair
  106. }