ElecrumX tests are passing

This commit is contained in:
Chris Troutner
2020-04-16 09:53:34 -07:00
parent f4e90a0518
commit d6925164e0
4 changed files with 106 additions and 9 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
"dev": "nodemon ./dist/app.js",
"test": "npm run lint && npm run test-v3",
"lint": "standard --env mocha --fix",
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/",
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/a01-electrumx.js",
"test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/blockchain.js",
"test:integration": "mocha test/v3/integration",
"coverage": "nyc report --reporter=text-lcov | coveralls",
+38 -2
View File
@@ -195,11 +195,14 @@ class Electrum {
})
}
wlogger.debug('Executing electrumx/getUtxos with this address: ', cashAddr)
wlogger.debug(
'Executing electrumx/getUtxos with this address: ',
cashAddr
)
// Get data from ElectrumX server.
const electrumResponse = await _this._utxosFromElectrumx(cashAddr)
// console.log(`electrumResponse: ${JSON.stringify(electrumResponse, null, 2)}`)
// console.log(`_utxosFromElectrumx(): ${JSON.stringify(electrumResponse, null, 2)}`)
// Pass the error message if ElectrumX reports an error.
if (Object.prototype.hasOwnProperty.call(electrumResponse, 'code')) {
@@ -245,6 +248,39 @@ class Electrum {
throw err
}
}
// Returns a promise that resolves to a balance for an address. Expects input
// to be a cash address, and input validation to have already been done by
// parent, calling function.
async _balanceFromElectrumx (address) {
try {
// Convert the address to a scripthash.
const scripthash = _this.addressToScripthash(address)
if (!_this.isReady) {
throw new Error(
'ElectrumX server connection is not ready. Call await connectToServer() first.'
)
}
// Query the address balance from the ElectrumX server.
const electrumResponse = await _this.electrumx.request(
'blockchain.scripthash.get_balance',
scripthash
)
console.log(
`electrumResponse: ${JSON.stringify(electrumResponse, null, 2)}`
)
return electrumResponse
} catch (err) {
// console.log('err1: ', err)
// Write out error to error log.
wlogger.error('Error in elecrumx.js/_utxosFromElectrumx(): ', err)
throw err
}
}
}
module.exports = Electrum
+53 -4
View File
@@ -21,7 +21,6 @@ const assert = chai.assert
const sinon = require('sinon')
const ElecrumxRoute = require('../../src/routes/v3/electrumx')
const electrumxRoute = new ElecrumxRoute()
// Mocking data.
const { mockReq, mockRes } = require('./mocks/express-mocks')
@@ -34,9 +33,11 @@ util.inspect.defaultOptions = { depth: 1 }
describe('#ElectrumX Router', () => {
let req, res
let sandbox
const electrumxRoute = new ElecrumxRoute()
before(async () => {
// console.log(`Testing type is: ${process.env.TEST}`)
if (!process.env.TEST) process.env.TEST = 'unit'
console.log(`Testing type is: ${process.env.TEST}`)
if (!process.env.NETWORK) process.env.NETWORK = 'testnet'
@@ -69,6 +70,8 @@ describe('#ElectrumX Router', () => {
req.query = {}
sandbox = sinon.createSandbox()
// electrumxRoute = new ElecrumxRoute()
})
afterEach(() => {
@@ -103,7 +106,7 @@ describe('#ElectrumX Router', () => {
})
})
describe('#UTXO', () => {
describe('#getUtxos', () => {
it('should throw 400 if address is empty', async () => {
const result = await electrumxRoute.getUtxos(req, res)
// console.log(`result: ${util.inspect(result)}`)
@@ -196,7 +199,7 @@ describe('#ElectrumX Router', () => {
electrumxRoute.isReady = true // Force flag.
sandbox
.stub(electrumxRoute.electrumx, 'request')
.stub(electrumxRoute, '_utxosFromElectrumx')
.resolves(mockData.utxos)
}
@@ -274,4 +277,50 @@ describe('#ElectrumX Router', () => {
assert.property(result[0], 'value')
})
})
describe('#_balanceFromElectrumx', () => {
it('should throw error for invalid address', async () => {
try {
// Address has invalid checksum.
const address = 'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
// Mock unit tests to prevent live network calls.
// if (process.env.TEST === 'unit') {
// electrumxRoute.isReady = true // Force flag.
//
// sandbox
// .stub(electrumxRoute.electrumx, 'request')
// .throws('Invalid Argument: Invalid checksum:')
// }
// Call the details API.
await electrumxRoute._balanceFromElectrumx(address)
assert.equal(true, false, 'Unexpected code path')
} catch (err) {
// console.log('err2: ', err)
assert.include(err.message, 'Invalid checksum')
}
})
it('should get balance for a single address', async () => {
const address = 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
// Mock unit tests to prevent live network calls.
if (process.env.TEST === 'unit') {
electrumxRoute.isReady = true // Force flag.
sandbox
.stub(electrumxRoute.electrumx, 'request')
.resolves(mockData.balance)
}
// Call the details API.
const result = await electrumxRoute._balanceFromElectrumx(address)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.property(result, 'confirmed')
assert.property(result, 'unconfirmed')
})
})
})
+14 -2
View File
@@ -10,9 +10,21 @@ const utxos = [
tx_hash: '7774e449c5a3065144cefbc4c0c21e6b69c987f095856778ef9f45ddd8ae1a41',
tx_pos: 0,
value: 1000
},
{
height: 630834,
tx_hash: '4fe60a51e0d8f5134bfd8e5f872d6e502d7f01b28a6afebb27f4438a4f638d53',
tx_pos: 0,
value: 6000
}
]
module.exports = {
utxos
const balance = {
confirmed: 7000,
unconfirmed: 0
}
module.exports = {
utxos,
balance
}