From e399e050220adc9e478432856a0e243306ebf277 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 11 Oct 2020 09:10:35 -0700 Subject: [PATCH] fix(validateTxid2): Final form. Updated integration tests --- package-lock.json | 13 ++++++++--- package.json | 3 ++- src/routes/v3/slp.js | 46 ++++++++++++-------------------------- test/v3/integration/slp.js | 30 ++++++++++++++++--------- 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f858c4..0b56232 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6476,6 +6476,13 @@ "p-cancelable": "^1.0.0", "to-readable-stream": "^1.0.0", "url-parse-lax": "^3.0.0" + }, + "dependencies": { + "p-cancelable": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz", + "integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw==" + } } }, "graceful-fs": { @@ -13359,9 +13366,9 @@ "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" }, "p-cancelable": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz", - "integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw==" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.0.0.tgz", + "integrity": "sha512-wvPXDmbMmu2ksjkB4Z3nZWTSkJEb9lqVdMaCKpZUGJG9TMiNp9XcbG3fn9fPKjem04fJMJnXoyFPk2FmgiaiNg==" }, "p-each-series": { "version": "2.1.0", diff --git a/package.json b/package.json index 927389f..4478854 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/", "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/slp.js", "test:integration": "mocha test/v3/integration", - "test:integration:slpdb": "mocha --timeout 25000 test/v3/integration/slp*.js", + "test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v3/integration/slp*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v3/", "docs": "./node_modules/.bin/apidoc -i src/routes/v3 -o docs" @@ -50,6 +50,7 @@ "mocha": "^7.1.1", "morgan": "^1.9.1", "mqtt": "^4.0.0", + "p-cancelable": "^2.0.0", "p-timeout": "^3.2.0", "passport": "^0.4.0", "passport-anonymous": "^1.0.1", diff --git a/src/routes/v3/slp.js b/src/routes/v3/slp.js index c14dc1b..5c79d0b 100644 --- a/src/routes/v3/slp.js +++ b/src/routes/v3/slp.js @@ -16,6 +16,7 @@ const ValidatorType1 = slpValidate.ValidatorType1 const RpcClient = require('bitcoin-rpc-promise-retry') const RPC_CONNECTION_STRING = `http://${process.env.RPC_USERNAME}:${process.env.RPC_PASSWORD}@${process.env.RPC_IP}` const pTimeout = require('p-timeout') +const PCancelable = require('p-cancelable') // const strftime = require('strftime') const wlogger = require('../../util/winston-logging') @@ -1162,8 +1163,8 @@ class Slp { * @apiGroup SLP * @apiDescription Validate single SLP transaction by txid, using slp-validate. * Slower, less efficient method of validating an SLP TXID using the slp-validate - * npm library. This method is independent of SLPDB and can be used when - * SLPDB return 'null' values. + * npm library. This method is independent of SLPDB and can be used as a fall-back + * when SLPDB returns 'null' values. * * * @apiExample Example usage: @@ -1186,17 +1187,6 @@ class Slp { txid ) - // Get the raw transaction from the full node. - try { - await _this.slpValidator.getRawTransaction(txid) - } catch (err) { - wlogger.error( - `err in slp.js/validate2Single() with getRawTransaction(${txid}): `, - err - ) - return _this.errorHandler(err, res) - } - // null by default. // Default return value. const result = { @@ -1205,31 +1195,23 @@ class Slp { msg: '' } - // Time in milliseconds when the promise is canceled. - const TIMEOUT = 5000 - - // Validat the TXID. - try { - result.isValid = await _this.pTimeout( - _this.slpValidator.isValidSlpTxid({ txid }), - TIMEOUT, - `Validation took longer than ${TIMEOUT} milliseconds to complete.` - ) - - // isValid = await _this.slpValidator.isValidSlpTxid({ txid }) - // console.log('isValid: ', isValid) - } catch (error) { - console.log(error) - result.isValid = null - result.msg = error.message + // Request options + const opt = { + method: 'get', + baseURL: `${process.env.SLP_API_URL}slp/validate/${txid}`, + timeout: 10000 // Exit after 10 seconds. } + const tokenRes = await _this.axios.request(opt) + // console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`) + // console.log(`tokenRes: `, tokenRes) - // Build result. - // result.valid = isValid + // Overwrite the default value with the result from slp-api. + result.isValid = tokenRes.data.isValid res.status(200) return res.json(result) } catch (err) { + console.log('validate2Single error: ', err) wlogger.error('Error in slp.ts/validate2Single().', err) return _this.errorHandler(err, res) diff --git a/test/v3/integration/slp.js b/test/v3/integration/slp.js index f16c0c8..4de4e88 100644 --- a/test/v3/integration/slp.js +++ b/test/v3/integration/slp.js @@ -239,16 +239,24 @@ describe('#slp', () => { assert.equal(result.isValid, true) }) - it('should cancel if validation takes too long', async () => { - const txid = - '2822f7d21e17ca8c36846613dc52caf0d03e355a13022a6cec066d197e59bb27' - - req.params.txid = txid - const result = await slp.validate2Single(req, res) - console.log(`result: ${JSON.stringify(result, null, 2)}`) - - assert.equal(result.txid, txid) - assert.equal(result.isValid, null) - }) + // CT 10-11-2020: This test is valid, but because of the cacheing built + // into slp-validate, it will not consistently pass or fail. To manually + // Run this test, re-start slp-api or find a token txid with a long DAG. + // it('should cancel if validation takes too long', async () => { + // const txid = + // 'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579' + // // '2822f7d21e17ca8c36846613dc52caf0d03e355a13022a6cec066d197e59bb27' + // + // req.params.txid = txid + // const result = await slp.validate2Single(req, res) + // // console.log(`result: ${JSON.stringify(result, null, 2)}`) + // + // assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.') + // assert.include( + // result.error, + // 'Could not communicate with full node', + // 'Error message expected' + // ) + // }) }) })