discover.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. const axios = require('axios')
  2. const debug = require('debug')('discovery::discover')
  3. const stripEndingSlash = require('@joystream/util/stripEndingSlash')
  4. const ipfs = require('ipfs-http-client')('localhost', '5001', { protocol: 'http' })
  5. const BN = require('bn.js')
  6. function inBrowser () {
  7. return typeof window !== 'undefined'
  8. }
  9. var activeDiscoveries = {}
  10. var accountInfoCache = {}
  11. const CACHE_TTL = 60 * 60 * 1000
  12. async function getIpnsIdentity (storageProviderId, runtimeApi) {
  13. storageProviderId = new BN(storageProviderId)
  14. // lookup ipns identity from chain corresponding to storageProviderId
  15. const info = await runtimeApi.discovery.getAccountInfo(storageProviderId)
  16. if (info == null) {
  17. // no identity found on chain for account
  18. return null
  19. } else {
  20. return info.identity.toString()
  21. }
  22. }
  23. async function discover_over_ipfs_http_gateway (storageProviderId, runtimeApi, gateway) {
  24. storageProviderId = new BN(storageProviderId)
  25. let isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
  26. if (!isProvider) {
  27. throw new Error('Cannot discover non storage providers')
  28. }
  29. const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
  30. if (identity == null) {
  31. // dont waste time trying to resolve if no identity was found
  32. throw new Error('no identity to resolve')
  33. }
  34. gateway = gateway || 'http://localhost:8080'
  35. gateway = stripEndingSlash(gateway)
  36. const url = `${gateway}/ipns/${identity}`
  37. const response = await axios.get(url)
  38. return response.data
  39. }
  40. async function discover_over_joystream_discovery_service (storageProviderId, runtimeApi, discoverApiEndpoint) {
  41. storageProviderId = new BN(storageProviderId)
  42. let isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
  43. if (!isProvider) {
  44. throw new Error('Cannot discover non storage providers')
  45. }
  46. const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
  47. if (identity == null) {
  48. // dont waste time trying to resolve if no identity was found
  49. throw new Error('no identity to resolve')
  50. }
  51. if (!discoverApiEndpoint) {
  52. // Use bootstrap nodes
  53. let discoveryBootstrapNodes = await runtimeApi.discovery.getBootstrapEndpoints()
  54. if (discoveryBootstrapNodes.length) {
  55. discoverApiEndpoint = stripEndingSlash(discoveryBootstrapNodes[0].toString())
  56. } else {
  57. throw new Error('No known discovery bootstrap nodes found on network')
  58. }
  59. }
  60. const url = `${discoverApiEndpoint}/discover/v0/${storageProviderId.toNumber()}`
  61. // should have parsed if data was json?
  62. const response = await axios.get(url)
  63. return response.data
  64. }
  65. async function discover_over_local_ipfs_node (storageProviderId, runtimeApi) {
  66. storageProviderId = new BN(storageProviderId)
  67. let isProvider = await runtimeApi.workers.isStorageProvider(storageProviderId)
  68. if (!isProvider) {
  69. throw new Error('Cannot discover non storage providers')
  70. }
  71. const identity = await getIpnsIdentity(storageProviderId, runtimeApi)
  72. if (identity == null) {
  73. // dont waste time trying to resolve if no identity was found
  74. throw new Error('no identity to resolve')
  75. }
  76. const ipns_address = `/ipns/${identity}/`
  77. debug('resolved ipns to ipfs object')
  78. let ipfs_name = await ipfs.name.resolve(ipns_address, {
  79. recursive: false, // there should only be one indirection to service info file
  80. nocache: false
  81. }) // this can hang forever!? can we set a timeout?
  82. debug('getting ipfs object', ipfs_name)
  83. let data = await ipfs.get(ipfs_name) // this can sometimes hang forever!?! can we set a timeout?
  84. // there should only be one file published under the resolved path
  85. let content = data[0].content
  86. // verify information and if 'discovery' service found
  87. // add it to our list of bootstrap nodes
  88. // TODO cache result or flag
  89. return JSON.parse(content)
  90. }
  91. async function discover (storageProviderId, runtimeApi, useCachedValue = false, maxCacheAge = 0) {
  92. storageProviderId = new BN(storageProviderId)
  93. const id = storageProviderId.toNumber()
  94. const cached = accountInfoCache[id]
  95. if (cached && useCachedValue) {
  96. if (maxCacheAge > 0) {
  97. // get latest value
  98. if (Date.now() > (cached.updated + maxCacheAge)) {
  99. return _discover(storageProviderId, runtimeApi)
  100. }
  101. }
  102. // refresh if cache is stale, new value returned on next cached query
  103. if (Date.now() > (cached.updated + CACHE_TTL)) {
  104. _discover(storageProviderId, runtimeApi)
  105. }
  106. // return best known value
  107. return cached.value
  108. } else {
  109. return _discover(storageProviderId, runtimeApi)
  110. }
  111. }
  112. function createExternallyControlledPromise () {
  113. let resolve, reject
  114. const promise = new Promise((_resolve, _reject) => {
  115. resolve = _resolve
  116. reject = _reject
  117. })
  118. return ({ resolve, reject, promise })
  119. }
  120. async function _discover (storageProviderId, runtimeApi) {
  121. storageProviderId = new BN(storageProviderId)
  122. const id = storageProviderId.toNumber()
  123. const discoveryResult = activeDiscoveries[id]
  124. if (discoveryResult) {
  125. debug('discovery in progress waiting for result for', id)
  126. return discoveryResult
  127. }
  128. debug('starting new discovery for', id)
  129. const deferredDiscovery = createExternallyControlledPromise()
  130. activeDiscoveries[id] = deferredDiscovery.promise
  131. let result
  132. try {
  133. if (inBrowser()) {
  134. result = await discover_over_joystream_discovery_service(storageProviderId, runtimeApi)
  135. } else {
  136. result = await discover_over_local_ipfs_node(storageProviderId, runtimeApi)
  137. }
  138. debug(result)
  139. result = JSON.stringify(result)
  140. accountInfoCache[id] = {
  141. value: result,
  142. updated: Date.now()
  143. }
  144. deferredDiscovery.resolve(result)
  145. delete activeDiscoveries[id]
  146. return result
  147. } catch (err) {
  148. // we catch the error so we can update all callers
  149. // and throw again to inform the first caller.
  150. debug(err.message)
  151. delete activeDiscoveries[id]
  152. // deferredDiscovery.reject(err)
  153. deferredDiscovery.resolve(null) // resolve to null until we figure out the issue below
  154. // throw err // <-- throwing but this isn't being
  155. // caught correctly in express server! Is it because there is an uncaught promise somewhere
  156. // in the prior .reject() call ?
  157. // I've only seen this behaviour when error is from ipfs-client
  158. // ... is this unique to errors thrown from ipfs-client?
  159. // Problem is its crashing the node so just return null for now
  160. return null
  161. }
  162. }
  163. module.exports = {
  164. discover,
  165. discover_over_joystream_discovery_service,
  166. discover_over_ipfs_http_gateway,
  167. discover_over_local_ipfs_node
  168. }