NodeJs:通过chainpoint-client JS库“无法发布/哈希”调用Chainpoint API调用异常

时间:2018-08-20 17:40:00

标签: javascript node.js promise async-await blockchain

当我想要时,我无法真正告诉或重现此“例外”,因为这仅在某些时候发生,而其他时候POST /哈希值就可以了。当我重新启动节点应用程序时,它运行良好,散列后返回了我的证明句柄,并进一步检索了链点证明,但有时,我无法告知何时但主要是何时发送新请求而无需重新启动节点应用程序(control + c并重新运行)

chainpoint-client的代码

    /**
 * Submit hash(es) to one or more Nodes, returning an Array of proof handle objects, one for each submitted hash and Node combination.
 * @param {Array<String>} hashes - An Array of String Hashes in Hexadecimal form.
 * @param {Array<String>} uris - An Array of String URI's. Each hash will be submitted to each Node URI provided. If none provided three will be chosen at random using service discovery.
 * @return {Array<{uri: String, hash: String, hashIdNode: String}>} An Array of Objects, each a handle that contains all info needed to retrieve a proof.
 */
function submitHashes (hashes, uris, callback) {
  uris = uris || []
  callback = callback || function () { }
  let nodesPromise

  // Validate callback is a function
  if (!_isFunction(callback)) throw new Error('callback arg must be a function')

  // Validate all hashes provided
  if (!_isArray(hashes)) throw new Error('hashes arg must be an Array')
  if (_isEmpty(hashes)) throw new Error('hashes arg must be a non-empty Array')
  if (hashes.length > 250) throw new Error('hashes arg must be an Array with <= 250 elements')
  let rejects = _reject(hashes, function (h) { return _isHex(h) })
  if (!_isEmpty(rejects)) throw new Error(`hashes arg contains invalid hashes : ${rejects.join(', ')}`)

  // Validate all Node URIs provided
  if (!_isArray(uris)) throw new Error('uris arg must be an Array of String URIs')
  if (uris.length > 5) throw new Error('uris arg must be an Array with <= 5 elements')

  if (_isEmpty(uris)) {
    // get a list of nodes via service discovery
    nodesPromise = getNodes(3)
  } else {
    // eliminate duplicate URIs
    uris = _uniq(uris)

    // non-empty, check that *all* are valid or throw
    let badURIs = _reject(uris, function (h) { return _isValidNodeURI(h) })
    if (!_isEmpty(badURIs)) throw new Error(`uris arg contains invalid URIs : ${badURIs.join(', ')}`)
    // all provided URIs were valid
    nodesPromise = Promise.resolve(uris)
  }

  return new Promise(function (resolve, reject) {
    // Resolve an Array of Nodes from service discovery or the arg provided
    nodesPromise.then((nodes) => {
      // Setup an options Object for each Node we'll submit hashes to.
      // Each Node will then be sent the full Array of hashes.
      let nodesWithPostOpts = _map(nodes, node => {
        let postOptions = {
          method: 'POST',
          uri: node + '/hashes',
          body: {
            hashes: hashes
          },
          headers: {
            'content-type': 'application/json'
          },
          timeout: 10000,
          json: true
        }
        return postOptions
      })

      // All requests succeed in parallel or all fail.
      Promise.map(nodesWithPostOpts, rp, { concurrency: 25 }).then(parsedBody => {
        // Nodes cannot be guaranteed to know what IP address they are reachable
        // at, so we need to amend each result with the Node URI it was submitted
        // to so that proofs may later be retrieved from the appropriate Node(s).
        // This mapping relies on that fact that Promise.map returns results in the
        // same order that options were passed to it so the results can be mapped to
        // the Nodes submitted to.
        _forEach(nodes, (uri, index) => {
          parsedBody[index].meta.submitted_to = uri
        })

        // Map the API response to a form easily consumable by getProofs
        let proofHandles = _mapSubmitHashesRespToProofHandles(parsedBody)

        resolve(proofHandles)
        return callback(null, proofHandles)
      }, function (err) {
        reject(err)
        return callback(err)
      })
    }).catch(err => {
      console.error(err.message)
      throw err
    })
  })
}

当我调用submitHashes时,我的代码(下面的代码在异步函数内部):

let hash = req.body.hash;
                    console.log(hash);

                    //the below line throws the exception, not always but at times
                    let proofHandles =await chp.submitHashes([hash]);   
                    console.log(proofHandles);//this never gets printed, nothing is returned from await chp.submitHashes([hash])

0 个答案:

没有答案