From d15e11b75e84b2252634d6ff3ad5d65a9dbba2f8 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 9 Jun 2019 15:04:30 -0700 Subject: [PATCH] Finished adding unit and integration tests to blockbook API balance and utxo --- src/routes/v3/bitcore.js | 5 +--- src/routes/v3/blockbook.js | 50 ++++++++++++++------------------- test/v3/blockbook.js | 47 ++++++------------------------- test/v3/mocks/blockbook-mock.js | 19 ++++--------- 4 files changed, 35 insertions(+), 86 deletions(-) diff --git a/src/routes/v3/bitcore.js b/src/routes/v3/bitcore.js index 54965f3..70722d6 100644 --- a/src/routes/v3/bitcore.js +++ b/src/routes/v3/bitcore.js @@ -251,10 +251,7 @@ async function utxosSingle(req, res, next) { }) } - wlogger.debug( - `Executing bitcore/balanceSingle with this address: `, - address - ) + wlogger.debug(`Executing bitcore/utxoSingle with this address: `, address) // Ensure the input is a valid BCH address. try { diff --git a/src/routes/v3/blockbook.js b/src/routes/v3/blockbook.js index 059685b..00d1542 100644 --- a/src/routes/v3/blockbook.js +++ b/src/routes/v3/blockbook.js @@ -1,5 +1,5 @@ /* - Bitcore Node API route + Blockbook API route */ "use strict" @@ -25,15 +25,15 @@ const BITBOX = new BITBOXJS() router.get("/", root) router.get("/balance/:address", balanceSingle) router.post("/balance", balanceBulk) -//router.get("/utxos/:address", utxosSingle) -//router.post("/utxos", utxosBulk) +router.get("/utxos/:address", utxosSingle) +router.post("/utxos", utxosBulk) // Root API endpoint. Simply acknowledges that it exists. function root(req, res, next) { return res.json({ status: "address" }) } -// Query the Bitcore Node API for a balance on a single BCH address. +// Query the Blockbook Node API for a balance on a single BCH address. // Returns a Promise. async function balanceFromBlockbook(thisAddress) { try { @@ -42,13 +42,9 @@ async function balanceFromBlockbook(thisAddress) { // Convert the address to a cashaddr without a prefix. const addr = BITBOX.Address.toCashAddress(thisAddress) - // Determine if we are working with the testnet or mainnet networks. - let network = "mainnet" - if (process.env.NETWORK === "testnet") network = "testnet" - const path = `${process.env.BLOCKBOOK_URL}api/v2/address/${addr}` - // Query the Bitcore Node API. + // Query the Blockbook Node API. const axiosResponse = await axios.get(path) const retData = axiosResponse.data //console.log(`retData: ${util.inspect(retData)}`) @@ -80,7 +76,7 @@ async function balanceSingle(req, res, next) { } wlogger.debug( - `Executing bitcore/balanceSingle with this address: `, + `Executing blockbook/balanceSingle with this address: `, address ) @@ -103,7 +99,7 @@ async function balanceSingle(req, res, next) { }) } - // Query the Bitcore Node API. + // Query the Blockbook Node API. const retData = await balanceFromBlockbook(address) // Return the retrieved address information. @@ -202,7 +198,7 @@ async function balanceBulk(req, res, next) { return res.json({ error: util.inspect(err) }) } } -/* + // Query the Blockbook API for utxos associated with a BCH address. // Returns a Promise. async function utxosFromBlockbook(thisAddress) { @@ -210,13 +206,9 @@ async function utxosFromBlockbook(thisAddress) { //console.log(`BLOCKBOOK_URL: ${BLOCKBOOK_URL}`) // Convert the address to a cashaddr without a prefix. - const addr = BITBOX.Address.toCashAddress(thisAddress, false) + const addr = BITBOX.Address.toCashAddress(thisAddress) - // Determine if we are working with the testnet or mainnet networks. - let network = "mainnet" - if (process.env.NETWORK === "testnet") network = "testnet" - - const path = `${process.env.BLOCKBOOK_URL}api/BCH/${network}/address/${addr}/?unspent=true` + const path = `${process.env.BLOCKBOOK_URL}api/v2/utxo/${addr}` // Query the Blockbook API. const axiosResponse = await axios.get(path) @@ -250,7 +242,7 @@ async function utxosSingle(req, res, next) { } wlogger.debug( - `Executing blockbook/balanceSingle with this address: `, + `Executing blockbook/utxosSingle with this address: `, address ) @@ -273,8 +265,8 @@ async function utxosSingle(req, res, next) { }) } - // Query the Bitcore Node API. - const retData = await utxosFromBitcore(address) + // Query the Blockbook API. + const retData = await utxosFromBlockbook(address) // Return the retrieved address information. res.status(200) @@ -288,7 +280,7 @@ async function utxosSingle(req, res, next) { } // Write out error to error log. - wlogger.error(`Error in bitcore.js/utxosSingle().`, err) + wlogger.error(`Error in blockbook.js/utxosSingle().`, err) res.status(500) return res.json({ error: util.inspect(err) }) @@ -318,7 +310,7 @@ async function utxosBulk(req, res, next) { } wlogger.debug( - `Executing bitcore.js/utxosBulk with these addresses: `, + `Executing blockbook.js/utxosBulk with these addresses: `, addresses ) @@ -349,7 +341,7 @@ async function utxosBulk(req, res, next) { // Loops through each address and creates an array of Promises, querying // Insight API in parallel. addresses = addresses.map(async (address, index) => - utxosFromBitcore(address) + utxosFromBlockbook(address) ) // Wait for all parallel Insight requests to return. @@ -366,20 +358,20 @@ async function utxosBulk(req, res, next) { return res.json({ error: msg }) } - wlogger.error(`Error in bitcore.js/utxosBulk().`, err) + wlogger.error(`Error in blockbook.js/utxosBulk().`, err) res.status(500) return res.json({ error: util.inspect(err) }) } } -*/ + module.exports = { router, testableComponents: { root, balanceSingle, - balanceBulk - //utxosSingle, - //utxosBulk + balanceBulk, + utxosSingle, + utxosBulk } } diff --git a/test/v3/blockbook.js b/test/v3/blockbook.js index 4cabf5c..b127daa 100644 --- a/test/v3/blockbook.js +++ b/test/v3/blockbook.js @@ -336,7 +336,7 @@ describe("#Blockbook Router", () => { assert.equal(result.length, 2, "2 outputs for 2 inputs") }) }) - /* + describe("#UTXOs Single", () => { // details route handler. const utxosSingle = blockbookRoute.testableComponents.utxosSingle @@ -422,18 +422,10 @@ describe("#Blockbook Router", () => { assert.isArray(result) assert.hasAnyKeys(result[0], [ - "_id", - "chain", - "network", - "coinbase", - "mintIndex", - "spentTxid", - "mintTxid", - "mintHeight", - "spentHeight", - "address", - "script", + "txid", + "vout", "value", + "height", "confirmations" ]) }) @@ -561,18 +553,10 @@ describe("#Blockbook Router", () => { assert.isArray(result) assert.isArray(result[0]) assert.hasAnyKeys(result[0][0], [ - "_id", - "chain", - "network", - "coinbase", - "mintIndex", - "spentTxid", - "mintTxid", - "mintHeight", - "spentHeight", - "address", - "script", + "txid", + "vout", "value", + "height", "confirmations" ]) }) @@ -599,22 +583,7 @@ describe("#Blockbook Router", () => { assert.isArray(result) assert.isArray(result[0]) - assert.hasAnyKeys(result[0][0], [ - "_id", - "chain", - "network", - "coinbase", - "mintIndex", - "spentTxid", - "mintTxid", - "mintHeight", - "spentHeight", - "address", - "script", - "value", - "confirmations" - ]) + assert.equal(result.length, 2, "2 outputs for 2 inputs") }) }) - */ }) diff --git a/test/v3/mocks/blockbook-mock.js b/test/v3/mocks/blockbook-mock.js index 2fe2b09..1a3b89c 100644 --- a/test/v3/mocks/blockbook-mock.js +++ b/test/v3/mocks/blockbook-mock.js @@ -20,20 +20,11 @@ const mockBalance = { const mockUtxos = [ { - _id: "5cf2c31a33bd46a95ec7e730", - chain: "BCH", - network: "testnet", - coinbase: false, - mintIndex: 1, - spentTxid: "", - mintTxid: - "5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392", - mintHeight: 1265275, - spentHeight: -2, - address: "qq89kjkeqz9mngp8kl3dpmu43y2wztdjqu500gn4c4", - script: "76a9140e5b4ad9008bb9a027b7e2d0ef958914e12db20788ac", - value: 10000000, - confirmations: -1 + txid: "5fe9b74056319a8c87f45cc745030715a6180758b94938dbf90d639d55652392", + vout: 1, + value: "10000000", + height: 1265275, + confirmations: 42704 } ]