Skip to content

Commit

Permalink
Merge pull request #128 from Bitcoin-com/stage
Browse files Browse the repository at this point in the history
V2 Blocks
  • Loading branch information
cgcardona authored Nov 19, 2018
2 parents dc58241 + 48d32e4 commit c1b244d
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 315 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ PORT=3000 BITCOINCOM_BASEURL=http://localhost:3000/api/ RPC_BASEURL=http://local
#### View in browser

Finally open `http://localhost:3000/` and confirm you see the GUI

#### Deploy
126 changes: 94 additions & 32 deletions src/routes/v2/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import * as express from "express"
import * as requestUtils from "./services/requestUtils"
import * as bitbox from "./services/bitbox"

const logger = require("./logging.js")
import axios from "axios"

// Used for processing error messages before sending them to the user.
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }

const router: express.Router = express.Router()
const BitboxHTTP = bitbox.getInstance()
//const BitboxHTTP = bitbox.getInstance()

const RateLimit = require("express-rate-limit")

Expand All @@ -23,6 +27,12 @@ const config: IRLConfig = {
blockRateLimit3: undefined
}

// Dynamically set these based on env vars. Allows unit testing.
let BitboxHTTP: any
let username: any
let password: any
let requestConfig: any

let i = 1
while (i < 4) {
config[`blockRateLimit${i}`] = new RateLimit({
Expand All @@ -44,6 +54,7 @@ while (i < 4) {

router.get("/", config.blockRateLimit1, root)
router.get("/detailsByHash/:hash", config.blockRateLimit2, detailsByHash)
router.get("/detailsByHeight/:height", config.blockRateLimit2, detailsByHeight)

function root(
req: express.Request,
Expand All @@ -53,55 +64,106 @@ function root(
return res.json({ status: "block" })
}

// Call the insight server to get block details based on the hash.
async function detailsByHash(
req: express.Request,
res: express.Response,
next: express.NextFunction
) {
try {
const hash = req.params.hash

// Reject if hash is empty
if (!hash || hash === "") {
res.status(400)
return res.json({ error: "hash must not be empty" })
}

const response = await axios.get(
`${process.env.BITCOINCOM_BASEURL}block/${req.params.hash}`
`${process.env.BITCOINCOM_BASEURL}block/${hash}`
)

const parsed = response.data
res.json(parsed)
return res.json(parsed)
} catch (error) {
// Write out error to error log.
//logger.error(`Error in block/detailsByHash: `, error)

if (error.response && error.response.status === 404) {
res.status(404)
return res.json({ error: "Not Found" })
}

res.status(500)
return res.send(error)
return res.json({ error: util.inspect(error) })
}
}

router.get(
"/detailsByHeight/:height",
config.blockRateLimit2,
async (req, res, next) => {
const requestConfig = requestUtils.getRequestConfig("getblockhash", [
parseInt(req.params.height)
])

BitboxHTTP(requestConfig)
.then(async response => {
try {
const rsp = await axios.get(
`${process.env.BITCOINCOM_BASEURL}block/${response.data.result}`
)
const parsed = rsp.data
res.json(parsed)
} catch (error) {
res.status(500)
return res.send(error)
}
})
.catch(error => {
res.status(500)
return res.send(error)
})
// Call the Full Node to get block hash based on height, then call the Insight
// server to get details from that hash.
async function detailsByHeight(
req: express.Request,
res: express.Response,
next: express.NextFunction
) {
try {
const height = req.params.height

// Reject if id is empty
if (!height || height === "") {
res.status(400)
return res.json({ error: "height must not be empty" })
}

setEnvVars()

requestConfig.data.id = "getblockhash"
requestConfig.data.method = "getblockhash"
requestConfig.data.params = [parseInt(height)]

const response = await BitboxHTTP(requestConfig)

const hash = response.data.result
//console.log(`hash: ${hash}`)

// Call detailsByHash now that the hash has been retrieved.
req.params.hash = hash
return detailsByHash(req, res, next)
} catch (error) {
// Write out error to error log.
//logger.error(`Error in control/getInfo: `, error)

res.status(500)
return res.json({ error: util.inspect(error) })
}
)

}

// Dynamically set these based on env vars. Allows unit testing.
function setEnvVars() {
BitboxHTTP = axios.create({
baseURL: process.env.RPC_BASEURL
})
username = process.env.RPC_USERNAME
password = process.env.RPC_PASSWORD

requestConfig = {
method: "post",
auth: {
username: username,
password: password
},
data: {
jsonrpc: "1.0"
}
}
}

module.exports = {
router,
testableComponents: {
root,
detailsByHash
detailsByHash,
detailsByHeight
}
}
21 changes: 10 additions & 11 deletions test/v2/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ const mockData = require("./mocks/address-mock")
const util = require("util")
util.inspect.defaultOptions = { depth: 1 }

function beforeTests() {
originalUrl = process.env.BITCOINCOM_BASEURL
describe("#AddressRouter", () => {
let req, res

// Set default environment variables for unit tests.
if (!process.env.TEST) process.env.TEST = "unit"
if (process.env.TEST === "unit")
process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/"
before(() => {
originalUrl = process.env.BITCOINCOM_BASEURL

console.log(`Testing type is: ${process.env.TEST}`)
}
beforeTests()
// Set default environment variables for unit tests.
if (!process.env.TEST) process.env.TEST = "unit"
if (process.env.TEST === "unit")
process.env.BITCOINCOM_BASEURL = "http://fakeurl/api/"

describe("#AddressRouter", () => {
let req, res
console.log(`Testing type is: ${process.env.TEST}`)
})

// Setup the mocks before each test.
beforeEach(() => {
Expand Down
Loading

0 comments on commit c1b244d

Please sign in to comment.