dev.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const assert = require('assert')
  2. function aliceKeyPair (api) {
  3. return api.identities.keyring.addFromUri('//Alice', null, 'sr25519')
  4. }
  5. // Setup Alice account on a developement chain that was
  6. // just launched as the storage lead, and a storage provider using the same
  7. // key as the role key
  8. const init = async (api) => {
  9. const alice = aliceKeyPair(api).address
  10. const providerId = 0 // first assignable id
  11. // Check if setup already completed
  12. if (await api.workers.isRoleAccountOfStorageProvider(providerId, alice)) {
  13. console.log('Alice already setup as a storage provider')
  14. return
  15. }
  16. // make sure alice is sudo - indirectly checking this is a dev chain
  17. const sudo = await api.api.query.sudo.key()
  18. if (!sudo.eq(alice)) {
  19. throw new Error('Setup requires Alice to be sudo. Are you sure you are running a devchain?')
  20. }
  21. // register alice as a member
  22. console.log(`Registering Alice as a member`)
  23. const aliceMemberId = await api.identities.registerMember(alice, {
  24. handle: 'alice'
  25. })
  26. // Make alice the storage lead
  27. console.log('Setting Alice as Lead')
  28. // prepare set storage lead tx
  29. const setLeadTx = api.api.tx.storageWorkingGroup.setLead(aliceMemberId, alice)
  30. // make sudo call
  31. api.signAndSend(
  32. alice,
  33. api.api.tx.sudo.sudo(setLeadTx)
  34. )
  35. // create an openinging, apply, start review, fill opening
  36. // Assumption opening id and applicant id, provider id will all be the
  37. // first assignable id == 0
  38. // so we don't await each tx to finalize to get the ids. this allows us to
  39. // batch all the transactions into a single block.
  40. console.log('Making Alice a storage provider')
  41. const openTx = api.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
  42. application_rationing_policy: {
  43. 'max_active_applicants': 1
  44. },
  45. max_review_period_length: 1000
  46. // default values for everything else..
  47. }, 'opening0')
  48. api.signAndSend(alice, openTx)
  49. const openingId = 0 // first assignable opening id
  50. const applyTx = api.api.tx.storageWorkingGroup.applyOnWorkerOpening(
  51. aliceMemberId, openingId, alice, null, null, 'alice'
  52. )
  53. api.signAndSend(alice, applyTx)
  54. const applicantId = 0 // first assignable applicant id
  55. const reviewTx = api.api.tx.storageWorkingGroup.beginWorkerApplicantReview(openingId)
  56. api.signAndSend(alice, reviewTx)
  57. const fillTx = api.api.tx.storageWorkingGroup.fillWorkerOpening(openingId, [applicantId], null)
  58. await api.signAndSend(alice, fillTx)
  59. // const worker = await api.workers.storageWorkerByProviderId(providerId)
  60. if (await api.workers.isRoleAccountOfStorageProvider(providerId, alice)) {
  61. console.log('Setup Alice successfully as storage provider')
  62. } else { throw new Error('Setup Failed') }
  63. // set localhost colossus as discovery provider on default port
  64. // assuming pioneer dev server is running on port 3000 we should run
  65. // the storage dev server on port 3001
  66. await api.discovery.setBootstrapEndpoints(alice, ['http://localhost:3001/'])
  67. }
  68. const check = async (api) => {
  69. const providerId = 0 // the first provider id which would have been assigned in dev-init
  70. const roleAccountId = aliceKeyPair(api).address
  71. if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccountId)) {
  72. console.log('Alice is correctly setup as a storage provider')
  73. } else { throw new Error('Alice is not setup as a storage provider') }
  74. const currentLead = await api.api.query.storageWorkingGroup.currentLead()
  75. if (currentLead.isSome && currentLead.unwrap().role_account_id.eq(roleAccountId)) {
  76. console.log('Alice is correctly setup as the storage lead')
  77. } else { throw new Error('Alice is not the storage lead') }
  78. }
  79. module.exports = {
  80. init,
  81. check,
  82. aliceKeyPair
  83. }