diff --git a/src/routes/v3/control.js b/src/routes/v3/control.js index 270ca37..e0baecd 100644 --- a/src/routes/v3/control.js +++ b/src/routes/v3/control.js @@ -13,6 +13,7 @@ util.inspect.defaultOptions = { depth: 1 } router.get("/", root) router.get("/getinfo", getInfo) +router.get("/getnetworkinfo", getNetworkInfo) function root(req, res, next) { return res.json({ status: "control" }) @@ -69,6 +70,45 @@ async function getInfo(req, res, next) { } } +/** + * @api {get} /control/getnetworkinfo Get full node info + * @apiName GetNetworkInfo + * @apiGroup Control + * @apiDescription RPC call which gets basic full node information. + * + * @apiExample Example usage: + * curl -X GET "http://localhost:3000/v3/control/getnetworkinfo" -H "accept: application/json" + * + */ +async function getNetworkInfo(req, res, next) { + const { + BitboxHTTP, + username, + password, + requestConfig + } = routeUtils.setEnvVars() + + requestConfig.data.id = "getnetworkinfo" + requestConfig.data.method = "getnetworkinfo" + requestConfig.data.params = [] + + try { + const response = await BitboxHTTP(requestConfig) + + return res.json(response.data.result) + } catch (error) { + wlogger.error(`Error in control.ts/getNetworkInfo().`, error) + + // Write out error to error log. + //logger.error(`Error in control/getInfo: `, error) + + res.status(500) + if (error.response && error.response.data && error.response.data.error) + return res.json({ error: error.response.data.error }) + return res.json({ error: util.inspect(error) }) + } +} + // router.get('/getMemoryInfo', (req, res, next) => { // BitboxHTTP({ // method: 'post', @@ -110,6 +150,7 @@ module.exports = { router, testableComponents: { root, - getInfo + getInfo, + getNetworkInfo } } diff --git a/test/v3/control.js b/test/v3/control.js index fd63e48..c1c79f5 100644 --- a/test/v3/control.js +++ b/test/v3/control.js @@ -137,4 +137,57 @@ describe("#ControlRouter", () => { ]) }) }) + + describe("#GetNetworkInfo", () => { + const getNetworkInfo = controlRoute.testableComponents.getNetworkInfo + + it("should throw 500 when network issues", async () => { + // Save the existing RPC URL. + const savedUrl = process.env.RPC_BASEURL + + // Manipulate the URL to cause a 500 network error. + process.env.RPC_BASEURL = "http://fakeurl/api/" + + const result = await getNetworkInfo(req, res) + //console.log(`result: ${util.inspect(result)}`) + + // Restore the saved URL. + process.env.RPC_BASEURL = savedUrl + + assert.isAbove( + res.statusCode, + 499, + "HTTP status code 500 or greater expected." + ) + //assert.include(result.error, "ENOTFOUND", "Error message expected") + }) + + it("should get info on the full node", async () => { + // Mock the RPC call for unit tests. + if (process.env.TEST === "unit") { + nock(`${process.env.RPC_BASEURL}`) + .post(``) + .reply(200, { result: mockData.mockGetNetworkInfo }) + } + + const result = await getNetworkInfo(req, res) + //console.log(`result: ${util.inspect(result)}`) + + assert.hasAnyKeys(result, [ + "version", + "subversion", + "protocolversion", + "localservices", + "localrelay", + "timeoffset", + "networkactive", + "connections", + "networks", + "relayfee", + "excessutxocharge", + "localaddresses", + "warnings" + ]) + }) + }) }) diff --git a/test/v3/mocks/control-mock.js b/test/v3/mocks/control-mock.js index cca9338..c0dbb5b 100644 --- a/test/v3/mocks/control-mock.js +++ b/test/v3/mocks/control-mock.js @@ -22,6 +22,24 @@ const mockGetInfo = { errors: "Warning: unknown new rules activated (versionbit 28)" } -module.exports = { - mockGetInfo +const mockGetNetworkInfo = { + version: 190700, + subversion: "/Bitcoin ABC:0.19.7(EB32.0)/", + protocolversion: 70015, + localservices: "0000000000000425", + localrelay: true, + timeoffset: 0, + networkactive: true, + connections: 23, + networks: [{}, {}, {}], + relayfee: 0.00001, + excessutxocharge: 0, + localaddresses: [], + warnings: + "Warning: Unknown block versions being mined! It's possible unknown rules are in effect" +} + +module.exports = { + mockGetInfo, + mockGetNetworkInfo }