Prototyped new blockchain library and unit tests

This commit is contained in:
Chris Troutner
2020-01-27 15:46:37 -08:00
parent 725adc1ce1
commit 57f3454e88
5 changed files with 1343 additions and 20 deletions
+1 -14
View File
@@ -56,25 +56,12 @@ function root(req, res, next) {
*/
async function getBestBlockHash(req, res, next) {
try {
// const {
// BitboxHTTP,
// username,
// password,
// requestConfig
// } = routeUtils.setEnvVars()
//
// requestConfig.data.id = "getbestblockhash"
// requestConfig.data.method = "getbestblockhash"
// requestConfig.data.params = []
//
// const response = await BitboxHTTP(requestConfig)
// Axios options
const options = routeUtils.getAxiosOptions()
options.data.id = "getbestblockhash"
options.data.method = "getbestblockhash"
options.data.params = []
// const response = await axios.post(options.baseURL, options)
const response = await axios.request(options)
return res.json(response.data.result)
+80
View File
@@ -0,0 +1,80 @@
/*
A library for interacting with the Full Node
*/
"use strict"
const express = require("express")
const router = express.Router()
// Used to convert error messages to strings, to safely pass to users.
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }
const axios = require("axios")
const routeUtils = require("../route-utils")
const wlogger = require("../../../util/winston-logging")
const BCHJS = require("@chris.troutner/bch-js")
const bchjs = new BCHJS()
let _this
class Blockchain {
constructor() {
_this = this
this.bchjs = bchjs
this.axios = axios
this.router = router
this.router.get("/", this.root)
}
root(req, res, next) {
return res.json({ status: "blockchain" })
}
/**
* @api {get} /blockchain/getBestBlockHash Get best block hash
* @apiName GetBestBlockHash
* @apiGroup Blockchain
* @apiDescription Returns the hash of the best (tip) block in the longest
* block chain.
*
* @apiExample Example usage:
* curl -X GET "https://mainnet.bchjs.cash/v3/blockchain/getBestBlockHash" -H "accept: application/json"
*
* @apiSuccess {String} bestBlockHash 000000000000000002bc884334336d99c9a9c616670a9244c6a8c1fc35aa91a1
*/
async getBestBlockHash(req, res, next) {
try {
// Axios options
const options = routeUtils.getAxiosOptions()
options.data.id = "getbestblockhash"
options.data.method = "getbestblockhash"
options.data.params = []
const response = await this.axios.request(options)
// console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`)
return res.json(response.data.result)
} catch (err) {
// Attempt to decode the error message.
const { msg, status } = routeUtils.decodeError(err)
if (msg) {
res.status(status)
return res.json({ error: msg })
}
// Write out error to error log.
//logger.error(`Error in rawtransactions/decodeRawTransaction: `, err)
wlogger.error(`Error in blockchain.ts/getBestBlockHash().`, err)
res.status(500)
return res.json({ error: util.inspect(err) })
}
}
}
module.exports = Blockchain
+15
View File
@@ -131,6 +131,9 @@ function decodeError(err) {
if (err.response && err.response.data)
return { msg: err.response.data, status: err.response.status }
// console.log(`err.message: ${err.message}`)
// console.log(`err: `, err)
// Attempt to detect a network connection error.
if (err.message && err.message.indexOf("ENOTFOUND") > -1) {
return {
@@ -158,6 +161,18 @@ function decodeError(err) {
}
}
// Axios timeout (aborted) error, or service is down (connection refused).
if (
err.code &&
(err.code === "ECONNABORTED" || err.code === "ECONNREFUSED")
) {
return {
msg:
"Network error: Could not communicate with full node or other external service.",
status: 503
}
}
return { msg: false, status: 500 }
} catch (err) {
wlogger.error(`unhandled error in route-utils.js/decodeError(): `, err)