From cad2c6378ea44a3fde7324887476d73655f5098a Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 27 Jan 2021 06:51:36 -0800 Subject: [PATCH] fix(sort0ConfTxs): Switching to Descending by default --- src/electrumx.js | 33 +++++++++++++++++---------------- test/integration/electrumx.js | 2 +- test/unit/electrumx.js | 16 ++++++++-------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/electrumx.js b/src/electrumx.js index 8d7a9bf..bbf1a57 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -613,36 +613,37 @@ class ElectrumX { * A simple sort function for the output of Electrum.transactions(). Ignores * unconfirmed transactions. * - * Sorts in 'ASCENDING' order by default, or 'DESCENDING' can be specified. + * Sorts in 'DESCENDING' order by default, or 'ASCENDING' can be specified. + * Descending makes the first element the newest (largest block height). * * @apiExample Example usage: * (async () => { * const txs = await bchjs.Electrumx.transactions('bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v') - * const sortedTxs = bchjs.Electrumx.sortConfTxs(txs.transactions, 'DESCENDING') + * const sortedTxs = bchjs.Electrumx.sortConfTxs(txs.transactions, 'ASCENDING') * console.log(sortedTxs) * })() * * // [ * // { - * // "height": 560534, - * // "tx_hash": "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" - * // }, - * // { * // "height": 560430, * // "tx_hash": "3e1f3e882be9c03897eeb197224bf87f312be556a89f4308fabeeeabcf9bc851" + * // }, + * // { + * // "height": 560534, + * // "tx_hash": "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" * // } * // ] */ // Sort confirmed Transactions by the block height - sortConfTxs (txs, sortingOrder = 'ASCENDING') { + sortConfTxs (txs, sortingOrder = 'DESCENDING') { try { // Filter out unconfirmed transactions, with a height of 0 or less. txs = txs.filter(elem => elem.height > 0) - if (sortingOrder === 'ASCENDING') { - return txs.sort((a, b) => a.height - b.height) + if (sortingOrder === 'DESCENDING') { + return txs.sort((a, b) => b.height - a.height) } - return txs.sort((a, b) => b.height - a.height) + return txs.sort((a, b) => a.height - b.height) } catch (err) { console.log('Error in util.js/sortConfTxs()') throw err @@ -665,23 +666,23 @@ class ElectrumX { * @apiExample Example usage: * (async () => { * const txs = await bchjs.Electrumx.transactions('bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v') - * const sortedTxs = await bchjs.Electrumx.sort0ConfTxs(txs.transactions, 'DESCENDING') + * const sortedTxs = await bchjs.Electrumx.sort0ConfTxs(txs.transactions, 'ASCENDING') * console.log(sortedTxs) * })() * * // [ * // { - * // "height": 560534, - * // "tx_hash": "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" - * // }, - * // { * // "height": 560430, * // "tx_hash": "3e1f3e882be9c03897eeb197224bf87f312be556a89f4308fabeeeabcf9bc851" + * // }, + * // { + * // "height": 560534, + * // "tx_hash": "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" * // } * // ] */ // Substitute zero-conf txs with the current block-height + 1 - async sort0ConfTxs (txs, sortingOrder = 'ASCENDING') { + async sort0ConfTxs (txs, sortingOrder = 'DESCENDING') { try { // Calculate the height of the next block const nextBlock = (await this.blockchain.getBlockCount()) + 1 diff --git a/test/integration/electrumx.js b/test/integration/electrumx.js index 77e6939..50d41ec 100644 --- a/test/integration/electrumx.js +++ b/test/integration/electrumx.js @@ -381,7 +381,7 @@ describe('#ElectrumX', () => { // `sortedTransactions: ${JSON.stringify(sortedTransactions, null, 2)}` // ) - assert.isBelow(sortedTransactions[0].height, sortedTransactions[1].height) + assert.isAbove(sortedTransactions[0].height, sortedTransactions[1].height) }) }) }) diff --git a/test/unit/electrumx.js b/test/unit/electrumx.js index d8a5f1d..ba578d3 100644 --- a/test/unit/electrumx.js +++ b/test/unit/electrumx.js @@ -422,15 +422,15 @@ describe('#ElectrumX', () => { }) describe('#sortConfTxs', () => { - it('should sort in ascending order by default', () => { - const result = bchjs.Electrumx.sortConfTxs(mockData.transaction.transactions) + it('should sort in ascending order', () => { + const result = bchjs.Electrumx.sortConfTxs(mockData.transaction.transactions, 'ASCENDING') // console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.isBelow(result[0].height, result[1].height) }) - it('should sort in descending order', () => { - const result = bchjs.Electrumx.sortConfTxs(mockData.transaction.transactions, 'DESCENDING') + it('should sort in descending order by default', () => { + const result = bchjs.Electrumx.sortConfTxs(mockData.transaction.transactions) // console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.isAbove(result[0].height, result[1].height) @@ -457,21 +457,21 @@ describe('#ElectrumX', () => { // These tests use mocked data that contains unconfirmed transactions. describe('#sort0ConfTxs', () => { - it('should sort in ascending order by default', async () => { + it('should sort in ascending', async () => { // Stub network calls sandbox.stub(bchjs.Electrumx.blockchain, 'getBlockCount').resolves(672141) - const result = await bchjs.Electrumx.sort0ConfTxs(mockData.txHistoryWithUnconfirmed.transactions) + const result = await bchjs.Electrumx.sort0ConfTxs(mockData.txHistoryWithUnconfirmed.transactions, 'ASCENDING') // console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.isBelow(result[0].height, result[1].height) }) - it('should sort in descending order', async () => { + it('should sort in descending order by default', async () => { // Stub network calls sandbox.stub(bchjs.Electrumx.blockchain, 'getBlockCount').resolves(672141) - const result = await bchjs.Electrumx.sort0ConfTxs(mockData.txHistoryWithUnconfirmed.transactions, 'DESCENDING') + const result = await bchjs.Electrumx.sort0ConfTxs(mockData.txHistoryWithUnconfirmed.transactions) // console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.isAbove(result[1].height, result[2].height)