From 82cd9270fa5ec8a0ba310e295a7d11b24882b547 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 13 Dec 2020 08:51:58 -0800 Subject: [PATCH] fix(tokenUtxoDetails): Updating to handle alternative output from SLPDB --- package.json | 3 +- src/slp/utils.js | 78 +++++++++++++++++++++++++++++------------ test/integration/slp.js | 41 +++++++++++++++++----- 3 files changed, 91 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 4c537ed..449b31b 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "test:integration:local": "RESTURL=http://localhost:3000/v4/ mocha --timeout 30000 test/integration", "test:integration:local:bchn": "RESTURL=http://localhost:3000/v4/ bash test/integration/test-bchn-integration.sh", "test:integration:local:testnet": "RESTURL=http://localhost:4000/v4/ mocha --timeout 30000 test/integration/testnet", - "test:temp": "export RESTURL=http://localhost:3000/v4/ && mocha --timeout 30000 -g '#hydrateUtxos' test/integration/", + "test:temp": "export RESTURL=http://localhost:3000/v4/ && mocha --timeout 30000 -g '#validateTxid2' test/integration/", + "test:temp2": "mocha --timeout 30000 -g 'tokenUtxoDetails' test/unit/", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/", "docs": "./node_modules/.bin/apidoc -i src/ -o docs" diff --git a/src/slp/utils.js b/src/slp/utils.js index d0c3e6a..6cb36d3 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -431,6 +431,10 @@ class Utils { * '00ea27261196a411776f81029c0ebe34362936b4a9847deb1f7a40a02b3a1476', * valid: true } ] */ + // This function has two responses. If SLPDB is working correctly, the output + // will be like the examples above. If SLPDB has fallen behind real-time + // processing, it will return this output: + // [ null ] async validateTxid(txid) { const path = `${this.restURL}slp/validateTxid` @@ -1054,7 +1058,7 @@ class Utils { // utxo list may have duplicate tx_hash, varying tx_pos // only need to call decodeOpReturn once for those const decodeOpReturnCache = {} - const cachedTxValidation = {} + // const cachedTxValidation = {} // Throw error if input is not an array. if (!Array.isArray(utxos)) throw new Error("Input must be an array.") @@ -1290,27 +1294,52 @@ class Utils { // property. i.e. it has been successfully hydrated with SLP // information. + // CT 12/13/2020: Disabling the cache until I get processing of tokens + // to be more stable. // If the value has been cached, use the cached version first. - let isValid = cachedTxValidation[utxo.txid] + // let isValid = cachedTxValidation[utxo.txid] + let isValid = null // There are two possible responses from SLPDB. If SLPDB is functioning - // correctly... + // correctly, then validateTxid() will return this: + // isValid: [ + // { + // "txid": "ff0c0354f8d3ddb34fa36f73494eb58ea24f8b8da6904aa8ed43b7a74886c583", + // "valid": true + // } + // ] + // + // If SLPDB has fallen behind real-time processing, it will return this: + // isValid: [ + // null + // ] + // + // Note: validateTxid3() has the same output as validateTxid(). + // validateTxid2() uses slp-validate, which has a different output format. // If not in the cache, try the general SLPDB. - if (isValid == null) { - console.log(`utxo: ${JSON.stringify(utxo, null, 2)}`) + if (isValid === null) { + // console.log(`utxo: ${JSON.stringify(utxo, null, 2)}`) isValid = await this.validateTxid(utxo.txid) - console.log(`isValid: ${JSON.stringify(isValid, null, 2)}`) + // console.log( + // `validateTxid() isValid: ${JSON.stringify(isValid, null, 2)}` + // ) - if (isValid !== null) { - isValid = isValid[0].valid + // Handle corner case where SLPDB returns an array with a null element. + if (isValid[0] === null) + isValid = [{ txid: utxo.txid, valid: null }] - // Save the result to the local cache. - cachedTxValidation[utxo.txid] = isValid - } + isValid = isValid[0].valid + + // Save the result to the local cache. + // cachedTxValidation[utxo.txid] = isValid } // console.log(`isValid: ${JSON.stringify(isValid, null, 2)}`) + // console.log( + // `pre-validateTxid3() isValid: ${JSON.stringify(isValid, null, 2)}` + // ) + // If still null, check the whitelist SLPDB if (isValid === null) { // console.log( @@ -1344,28 +1373,33 @@ class Utils { // )}` // ) - if (isValid !== null) { - isValid = isValid[0].valid + if (isValid[0] !== null) isValid = isValid[0].valid - // Save the result to the local cache. - cachedTxValidation[utxo.txid] = isValid - } + // Save the result to the local cache. + // cachedTxValidation[utxo.txid] = isValid } } + // console.log( + // `pre-validateTxid2() isValid: ${JSON.stringify(isValid, null, 2)}` + // ) + // If still null, as a last resort, check it against slp-validate if (isValid === null) { - isValid = await this.validateTxid2(utxo.txid) + try { + isValid = await this.validateTxid2(utxo.txid) + } catch (err) { + // Mark as invalid if validateTxid2() throws an error. + isValid = null + } // console.log( // `slp-validate isValid: ${JSON.stringify(isValid, null, 2)}` // ) - if (isValid !== null) { - isValid = isValid.isValid + if (isValid !== null) isValid = isValid.isValid - // Save the result to the local cache. - cachedTxValidation[utxo.txid] = isValid - } + // Save the result to the local cache. + // cachedTxValidation[utxo.txid] = isValid } // console.log(`isValid: ${JSON.stringify(isValid, null, 2)}`) diff --git a/test/integration/slp.js b/test/integration/slp.js index a26fed2..581241e 100644 --- a/test/integration/slp.js +++ b/test/integration/slp.js @@ -472,6 +472,13 @@ describe(`#SLP`, () => { "4b89405c54d1c0bde8aa476a47561a42a6e7a5e927daa2ec69d428810eae3419", tx_pos: 1, value: 546 + }, + { + height: 665577, + tx_hash: + "3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488", + tx_pos: 1, + value: 546 } ] @@ -627,6 +634,13 @@ describe(`#SLP`, () => { "4b89405c54d1c0bde8aa476a47561a42a6e7a5e927daa2ec69d428810eae3419", tx_pos: 1, value: 546 + }, + { + height: 665577, + tx_hash: + "f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a", + tx_pos: 1, + value: 546 } ] @@ -636,6 +650,7 @@ describe(`#SLP`, () => { }) describe("#validateTxid", () => { + /* it("should return null on a known invalid TXID", async () => { const txid = "f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a" @@ -667,15 +682,25 @@ describe(`#SLP`, () => { assert.property(result[0], "valid") assert.equal(result[0].valid, true) }) - +*/ // This test is not necessary. - // it("should handle a null response from SLPDB", async () => { - // const txid = - // "4b89405c54d1c0bde8aa476a47561a42a6e7a5e927daa2ec69d428810eae3419" - // - // const result = await bchjs.SLP.Utils.validateTxid(txid) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) - // }) + it("should handle a null response from SLPDB", async () => { + const txid = + "4b89405c54d1c0bde8aa476a47561a42a6e7a5e927daa2ec69d428810eae3419" + + const result = await bchjs.SLP.Utils.validateTxid(txid) + console.log(`result: ${JSON.stringify(result, null, 2)}`) + }) + + it("should handle a null response from SLPDB", async () => { + const txid = [ + "4b89405c54d1c0bde8aa476a47561a42a6e7a5e927daa2ec69d428810eae3419", + "3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488" + ] + + const result = await bchjs.SLP.Utils.validateTxid(txid) + console.log(`result: ${JSON.stringify(result, null, 2)}`) + }) }) describe("#validateTxid2", () => {