mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
fix(control/getnetworkinfo): Added getnetworkinfo endpoint
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user