Browse Source

add tracker api

add /support/rescan endpoint allowing to rescan a range of blocks
feat-mydojo_upgrade_explorer
kenshin-samourai 5 years ago
parent
commit
31f424ae1e
  1. 4
      docker/my-dojo/.env
  2. 1
      docker/my-dojo/docker-compose.yaml
  3. 5
      docker/my-dojo/nginx/mainnet.conf
  4. 5
      docker/my-dojo/nginx/testnet.conf
  5. 4
      docker/my-dojo/node/keys.index.js
  6. 5
      keys/index-example.js
  7. 16
      tracker/index.js
  8. 79
      tracker/tracker-rest-api.js

4
docker/my-dojo/.env

@ -13,8 +13,8 @@ COMPOSE_CONVERT_WINDOWS_PATHS=1
DOJO_VERSION_TAG=1.3.0
DOJO_DB_VERSION_TAG=1.1.0
DOJO_BITCOIND_VERSION_TAG=1.2.0
DOJO_NODEJS_VERSION_TAG=1.2.0
DOJO_NGINX_VERSION_TAG=1.2.0
DOJO_NODEJS_VERSION_TAG=1.3.0
DOJO_NGINX_VERSION_TAG=1.3.0
DOJO_TOR_VERSION_TAG=1.1.0

1
docker/my-dojo/docker-compose.yaml

@ -36,6 +36,7 @@ services:
expose:
- "8080"
- "8081"
- "8082"
volumes:
- data-nodejs:/data
depends_on:

5
docker/my-dojo/nginx/mainnet.conf

@ -34,6 +34,11 @@ server {
proxy_pass http://node:8081/;
}
# Tracker server is separate, so proxy first
location /v2/tracker/ {
proxy_pass http://node:8082/;
}
# Proxy requests to maintenance tool
location /admin/ {
proxy_pass http://node:8080/static/admin/;

5
docker/my-dojo/nginx/testnet.conf

@ -34,6 +34,11 @@ server {
proxy_pass http://node:8081/;
}
# Tracker server is separate, so proxy first
location /test/v2/tracker/ {
proxy_pass http://node:8082/;
}
# Proxy requests to maintenance tool
location /admin/ {
proxy_pass http://node:8080/static/admin/;

4
docker/my-dojo/node/keys.index.js

@ -70,8 +70,10 @@ module.exports = {
ports: {
// Port used by the API
account: 8080,
// Port used by pushtx
// Port used by the pushtx API
pushtx: 8081,
// Port used by the tracker API
trackerApi: 8082,
// Port used by the tracker for its notifications
tracker: 5555,
// Port used by pushtx for its notifications

5
keys/index-example.js

@ -67,8 +67,10 @@ module.exports = {
ports: {
// Port used by the API
account: 8080,
// Port used by pushtx
// Port used by pushtx API
pushtx: 8081,
// Port used by the tracker API
trackerApi: 8082,
// Port used by the tracker for its notifications
tracker: 5555,
// Port used by pushtx for its notifications
@ -270,6 +272,7 @@ module.exports = {
ports: {
account: 18080,
pushtx: 18081,
trackerApi: 18082,
tracker: 15555,
notifpushtx: 15556,
orchestrator: 15557

16
tracker/index.js

@ -11,7 +11,9 @@
const keys = require('../keys')[network.key]
const db = require('../lib/db/mysql-db-wrapper')
const Logger = require('../lib/logger')
const HttpServer = require('../lib/http-server/http-server')
const Tracker = require('./tracker')
const TrackerRestApi = require('./tracker-rest-api')
Logger.info('Process ID: ' + process.pid)
@ -33,8 +35,20 @@
db.connect(dbConfig)
// Start the tracker
// Initialize the tracker
const tracker = new Tracker()
// Initialize the http server
const port = keys.ports.trackerApi
const httpServer = new HttpServer(port)
// Initialize the rest api endpoints
const trackerRestApi = new TrackerRestApi(httpServer, tracker)
// Start the http server
httpServer.start()
// Start the tracker
tracker.start()
})().catch(err => {

79
tracker/tracker-rest-api.js

@ -0,0 +1,79 @@
/*!
* tracker/tracker-rest-api.js
* Copyright (c) 2016-2019, Samourai Wallet (CC BY-NC-ND 4.0 License).
*/
'use strict'
const qs = require('querystring')
const validator = require('validator')
const bodyParser = require('body-parser')
const Logger = require('../lib/logger')
const errors = require('../lib/errors')
const authMgr = require('../lib/auth/authorizations-manager')
const HttpServer = require('../lib/http-server/http-server')
const network = require('../lib/bitcoin/network')
const keys = require('../keys')[network.key]
/**
* Tracker API endpoints
*/
class TrackerRestApi {
/**
* Constructor
* @param {pushtx.HttpServer} httpServer - HTTP server
* @param {tracker.Tracker} tracker - tracker
*/
constructor(httpServer, tracker) {
this.httpServer = httpServer
this.tracker = tracker
const urlencodedParser = bodyParser.urlencoded({ extended: true })
// Establish routes. Proxy server strips /pushtx
this.httpServer.app.get(
`/${keys.prefixes.support}/rescan`,
authMgr.checkHasAdminProfile.bind(authMgr),
this.getBlocksRescan.bind(this),
HttpServer.sendAuthError
)
}
/**
* Rescan a range of blocks
*/
async getBlocksRescan(req, res) {
// Check request arguments
if (!req.query)
return HttpServer.sendError(res, errors.body.INVDATA)
if (!req.query.fromHeight || !validator.isInt(req.query.fromHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
if (req.query.toHeight && !validator.isInt(req.query.toHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
// Retrieve the request arguments
const fromHeight = parseInt(req.query.fromHeight)
const toHeight = req.query.toHeight ? parseInt(req.query.toHeight) : fromHeight
if (req.query.toHeight && (toHeight < fromHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
try {
await this.tracker.blockchainProcessor.rescanBlocks(fromHeight, toHeight)
const ret = {
status: 'Rescan complete',
fromHeight: fromHeight,
toHeight: toHeight
}
HttpServer.sendRawData(res, JSON.stringify(ret, null, 2))
} catch(e) {
return HttpServer.sendError(res, e)
}
}
}
module.exports = TrackerRestApi
Loading…
Cancel
Save