2019-06-08 19:13:19 -07:00
|
|
|
/*
|
|
|
|
|
TESTS FOR THE MINING.TS LIBRARY
|
|
|
|
|
|
|
|
|
|
This test file uses the environment variable TEST to switch between unit
|
|
|
|
|
and integration tests. By default, TEST is set to 'unit'. Set this variable
|
|
|
|
|
to 'integration' to run the tests against BCH mainnet.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
'use strict'
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
const chai = require('chai')
|
2019-06-08 19:13:19 -07:00
|
|
|
const assert = chai.assert
|
2020-03-04 20:33:20 -04:00
|
|
|
const MiningRoute = require('../../src/routes/v3/full-node/mining')
|
|
|
|
|
const uut = new MiningRoute()
|
|
|
|
|
|
|
|
|
|
// const nock = require('nock') // HTTP mocking
|
|
|
|
|
const sinon = require('sinon')
|
2019-06-08 19:13:19 -07:00
|
|
|
|
|
|
|
|
let originalEnvVars // Used during transition from integration to unit tests.
|
|
|
|
|
|
|
|
|
|
// Mocking data.
|
2020-02-16 06:38:25 -08:00
|
|
|
const { mockReq, mockRes } = require('./mocks/express-mocks')
|
|
|
|
|
const mockData = require('./mocks/mining-mocks')
|
2019-06-08 19:13:19 -07:00
|
|
|
|
|
|
|
|
// Used for debugging.
|
2020-02-16 06:38:25 -08:00
|
|
|
const util = require('util')
|
2019-06-08 19:13:19 -07:00
|
|
|
util.inspect.defaultOptions = { depth: 1 }
|
|
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
describe('#Mining', () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
let req, res
|
2020-03-04 20:33:20 -04:00
|
|
|
let sandbox
|
2019-06-08 19:13:19 -07:00
|
|
|
before(() => {
|
|
|
|
|
// Save existing environment variables.
|
|
|
|
|
originalEnvVars = {
|
|
|
|
|
BITCOINCOM_BASEURL: process.env.BITCOINCOM_BASEURL,
|
|
|
|
|
RPC_BASEURL: process.env.RPC_BASEURL,
|
|
|
|
|
RPC_USERNAME: process.env.RPC_USERNAME,
|
|
|
|
|
RPC_PASSWORD: process.env.RPC_PASSWORD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set default environment variables for unit tests.
|
2020-02-16 06:38:25 -08:00
|
|
|
if (!process.env.TEST) process.env.TEST = 'unit'
|
|
|
|
|
if (process.env.TEST === 'unit') {
|
|
|
|
|
process.env.BITCOINCOM_BASEURL = 'http://fakeurl/api/'
|
|
|
|
|
process.env.RPC_BASEURL = 'http://fakeurl/api'
|
|
|
|
|
process.env.RPC_USERNAME = 'fakeusername'
|
|
|
|
|
process.env.RPC_PASSWORD = 'fakepassword'
|
2019-06-08 19:13:19 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Setup the mocks before each test.
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
// Mock the req and res objects used by Express routes.
|
|
|
|
|
req = mockReq
|
|
|
|
|
res = mockRes
|
|
|
|
|
|
|
|
|
|
// Explicitly reset the parmas and body.
|
|
|
|
|
req.params = {}
|
|
|
|
|
req.body = {}
|
|
|
|
|
req.query = {}
|
|
|
|
|
|
2020-03-04 20:33:20 -04:00
|
|
|
sandbox = sinon.createSandbox()
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-03-04 20:33:20 -04:00
|
|
|
// Restore Sandbox
|
|
|
|
|
sandbox.restore()
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
|
// Restore any pre-existing environment variables.
|
|
|
|
|
process.env.BITCOINCOM_BASEURL = originalEnvVars.BITCOINCOM_BASEURL
|
|
|
|
|
process.env.RPC_BASEURL = originalEnvVars.RPC_BASEURL
|
|
|
|
|
process.env.RPC_USERNAME = originalEnvVars.RPC_USERNAME
|
|
|
|
|
process.env.RPC_PASSWORD = originalEnvVars.RPC_PASSWORD
|
|
|
|
|
})
|
|
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
describe('#root', async () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
// root route handler.
|
2020-02-16 06:38:25 -08:00
|
|
|
it('should respond to GET for base route', async () => {
|
2020-03-04 20:33:20 -04:00
|
|
|
const result = uut.root(req, res)
|
2020-02-16 06:38:25 -08:00
|
|
|
// console.log(`result: ${util.inspect(result)}`)
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
assert.equal(result.status, 'mining', 'Returns static string')
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
describe('#getMiningInfo', async () => {
|
|
|
|
|
it('should throw 503 when network issues', async () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
// Save the existing RPC URL.
|
|
|
|
|
const savedUrl2 = process.env.RPC_BASEURL
|
|
|
|
|
|
|
|
|
|
// Manipulate the URL to cause a 500 network error.
|
2020-02-16 06:38:25 -08:00
|
|
|
process.env.RPC_BASEURL = 'http://fakeurl/api/'
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-03-04 20:33:20 -04:00
|
|
|
await uut.getMiningInfo(req, res)
|
2020-02-16 06:38:25 -08:00
|
|
|
// console.log(`result: ${util.inspect(result)}`)
|
2019-06-08 19:13:19 -07:00
|
|
|
|
|
|
|
|
// Restore the saved URL.
|
|
|
|
|
process.env.RPC_BASEURL = savedUrl2
|
|
|
|
|
|
|
|
|
|
assert.isAbove(
|
|
|
|
|
res.statusCode,
|
|
|
|
|
499,
|
2020-02-16 06:38:25 -08:00
|
|
|
'HTTP status code 500 or greater expected.'
|
2019-06-08 19:13:19 -07:00
|
|
|
)
|
2020-02-16 06:38:25 -08:00
|
|
|
// assert.include(result.error,"Network error: Could not communicate with full node","Error message expected")
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
2020-03-04 20:33:20 -04:00
|
|
|
it('returns proper error when downstream service stalls', async () => {
|
|
|
|
|
// Mock the timeout error.
|
|
|
|
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNABORTED' })
|
|
|
|
|
|
|
|
|
|
const result = await uut.getMiningInfo(req, res)
|
|
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
|
|
|
|
assert.include(
|
|
|
|
|
result.error,
|
|
|
|
|
'Could not communicate with full node',
|
|
|
|
|
'Error message expected'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('returns proper error when downstream service is down', async () => {
|
|
|
|
|
// Mock the timeout error.
|
|
|
|
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNREFUSED' })
|
|
|
|
|
|
|
|
|
|
const result = await uut.getMiningInfo(req, res)
|
|
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
|
|
|
|
assert.include(
|
|
|
|
|
result.error,
|
|
|
|
|
'Could not communicate with full node',
|
|
|
|
|
'Error message expected'
|
|
|
|
|
)
|
|
|
|
|
})
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
it('should GET mining information', async () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
// Mock the RPC call for unit tests.
|
2020-02-16 06:38:25 -08:00
|
|
|
if (process.env.TEST === 'unit') {
|
2020-03-04 20:33:20 -04:00
|
|
|
sandbox
|
|
|
|
|
.stub(uut.axios, 'request')
|
|
|
|
|
.resolves({ data: { result: mockData.mockMiningInfo } })
|
2019-06-08 19:13:19 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-04 20:33:20 -04:00
|
|
|
const result = await uut.getMiningInfo(req, res)
|
2020-02-16 06:38:25 -08:00
|
|
|
// console.log(`result: ${util.inspect(result)}`)
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-06-20 11:54:28 -07:00
|
|
|
assert.property(result, 'blocks')
|
|
|
|
|
assert.property(result, 'difficulty')
|
|
|
|
|
assert.property(result, 'networkhashps')
|
|
|
|
|
assert.property(result, 'pooledtx')
|
|
|
|
|
assert.property(result, 'chain')
|
|
|
|
|
assert.property(result, 'warnings')
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
describe('#getNetworkHashPS', async () => {
|
|
|
|
|
it('should throw 503 when network issues', async () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
// Save the existing RPC URL.
|
|
|
|
|
const savedUrl2 = process.env.RPC_BASEURL
|
|
|
|
|
|
|
|
|
|
// Manipulate the URL to cause a 500 network error.
|
2020-02-16 06:38:25 -08:00
|
|
|
process.env.RPC_BASEURL = 'http://fakeurl/api/'
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-03-04 20:33:20 -04:00
|
|
|
await uut.getNetworkHashPS(req, res)
|
2020-02-16 06:38:25 -08:00
|
|
|
// console.log(`result: ${util.inspect(result)}`)
|
2019-06-08 19:13:19 -07:00
|
|
|
|
|
|
|
|
// Restore the saved URL.
|
|
|
|
|
process.env.RPC_BASEURL = savedUrl2
|
|
|
|
|
|
|
|
|
|
assert.isAbove(
|
|
|
|
|
res.statusCode,
|
|
|
|
|
499,
|
2020-02-16 06:38:25 -08:00
|
|
|
'HTTP status code 500 or greater expected.'
|
2019-06-08 19:13:19 -07:00
|
|
|
)
|
2020-02-16 06:38:25 -08:00
|
|
|
// assert.include(result.error,"Network error: Could not communicate with full node","Error message expected")
|
2019-06-08 19:13:19 -07:00
|
|
|
})
|
2020-03-04 20:33:20 -04:00
|
|
|
it('returns proper error when downstream service stalls', async () => {
|
|
|
|
|
// Mock the timeout error.
|
|
|
|
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNABORTED' })
|
|
|
|
|
|
|
|
|
|
const result = await uut.getNetworkHashPS(req, res)
|
|
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
|
|
|
|
assert.include(
|
|
|
|
|
result.error,
|
|
|
|
|
'Could not communicate with full node',
|
|
|
|
|
'Error message expected'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('returns proper error when downstream service is down', async () => {
|
|
|
|
|
// Mock the timeout error.
|
|
|
|
|
sandbox.stub(uut.axios, 'request').throws({ code: 'ECONNREFUSED' })
|
|
|
|
|
|
|
|
|
|
const result = await uut.getNetworkHashPS(req, res)
|
|
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
|
|
|
|
|
|
|
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
|
|
|
|
assert.include(
|
|
|
|
|
result.error,
|
|
|
|
|
'Could not communicate with full node',
|
|
|
|
|
'Error message expected'
|
|
|
|
|
)
|
|
|
|
|
})
|
2019-06-08 19:13:19 -07:00
|
|
|
|
2020-02-16 06:38:25 -08:00
|
|
|
it('should GET Network Hash per second', async () => {
|
2019-06-08 19:13:19 -07:00
|
|
|
// Mock the RPC call for unit tests.
|
2020-02-16 06:38:25 -08:00
|
|
|
if (process.env.TEST === 'unit') {
|
2020-03-04 20:33:20 -04:00
|
|
|
sandbox
|
|
|
|
|
.stub(uut.axios, 'request')
|
|
|
|
|
.resolves({ data: { result: 517604755.6648782 } })
|
2019-06-08 19:13:19 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-04 20:33:20 -04:00
|
|
|
const result = await uut.getNetworkHashPS(req, res)
|
2020-02-16 06:38:25 -08:00
|
|
|
// console.log(`result: ${util.inspect(result)}`)
|
2019-06-08 19:13:19 -07:00
|
|
|
|
|
|
|
|
assert.isNumber(result)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|