From bdced13230b082b82c981bb06c5fd1672b20df13 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 27 Jan 2022 08:52:12 -0800 Subject: [PATCH 1/2] fix(Electrumx): Added commented code for debugging sorting --- src/electrumx.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/electrumx.js b/src/electrumx.js index af2f00e..defa5d3 100644 --- a/src/electrumx.js +++ b/src/electrumx.js @@ -638,13 +638,24 @@ class ElectrumX { // Sort confirmed Transactions by the block height sortConfTxs (txs, sortingOrder = 'DESCENDING') { try { + // console.log(`sortConfTxs txs: ${JSON.stringify(txs, null, 2)}`) + // Filter out unconfirmed transactions, with a height of 0 or less. txs = txs.filter(elem => elem.height > 0) if (sortingOrder === 'DESCENDING') { - return txs.sort((a, b) => b.height - a.height) + // console.log('Sorting in descending order') + return txs.sort((a, b) => { + // console.log(`descending b.height: ${b.height}, a.height: ${a.height}`) + return b.height - a.height + }) } - return txs.sort((a, b) => a.height - b.height) + + // console.log('Sorting in ascending order') + return txs.sort((a, b) => { + // console.log(`ascending b.height: ${b.height}, a.height: ${a.height}`) + return a.height - b.height + }) } catch (err) { console.log('Error in util.js/sortConfTxs()') throw err @@ -685,6 +696,8 @@ class ElectrumX { // Substitute zero-conf txs with the current block-height + 1 async sortAllTxs (txs, sortingOrder = 'DESCENDING') { try { + // console.log(`sortingOrder: ${sortingOrder}`) + // Calculate the height of the next block const nextBlock = (await this.blockchain.getBlockCount()) + 1 From bd1c3b622d9a3d38251cffdadcc9790e3ec2c0f7 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 27 Jan 2022 17:37:20 -0800 Subject: [PATCH 2/2] feat(chunk100): Adding chunk100 which works just like chunk20 --- src/util.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/util.js b/src/util.js index 724d51a..1390322 100644 --- a/src/util.js +++ b/src/util.js @@ -150,6 +150,56 @@ class Util { } } + /** + * @api Util.chunk100() chunk100() + * @apiName chunk100 + * @apiGroup Util + * @apiDescription chunk up an array into multiple arrays of 100 elements each. + * Input: arrayToSlice - a one-dimensional array of elements. + * Returns a two-dimensional array. An array of 100-element arrays. + * + * @apiExample Example usage: + * (async () => { + * try { + * const bigArray = [0,1,2,3,4,5,6,7,8,9,10,...,148, 149, 150] + * + * const chunked = bchjs.Util.chunk20(bigArray) + * console.log(chunked) + * } catch(error) { + * console.error(error) + * } + * })() + * + * // returns + * [ + * [0,1,2,3,4,5,6,7,8,9,10,11,...,98,99], + * [100,101,102,...,148,149,150] + * ] + */ + chunk100 (arrayToSlice) { + try { + // Validate inputs + if (!Array.isArray(arrayToSlice)) { + throw new Error('input must be an array') + } + + let offset = 0 + const result = [] + + // Loop over the array and slice off chunks of 100 elements. + while (offset < arrayToSlice.length) { + const chunk = arrayToSlice.slice(offset, offset + 100) + result.push(chunk) + offset = offset + 100 + } + + return result + } catch (err) { + console.error('Error in chunk100()') + throw err + } + } + /** * @api Util.sleep() sleep() * @apiName sleep