Compare commits

...
16 Commits
Author SHA1 Message Date
Chris Troutner 4f0e59ea61 Merge pull request #5 from Permissionless-Software-Foundation/ct-unstable
Syncing with upstream ipfs-service-provider
2021-12-06 12:57:40 -08:00
Chris Troutner a1a910c3b5 Merge branch 'master' of https://github.com/Permissionless-Software-Foundation/ipfs-service-provider into ct-unstable 2021-12-06 12:56:32 -08:00
Chris Troutner 1bf8f756e5 Merge pull request #59 from Permissionless-Software-Foundation/ct-unstable
fix(ipfs): Deleting /blocks dir if it prevents app from starting
2021-12-06 12:48:48 -08:00
Chris Troutner 1e513cc133 fix(ipfs): Deleting /blocks dir if it prevents app from starting 2021-12-06 12:46:22 -08:00
Chris Troutner 2f48f1bb61 Merge pull request #4 from Permissionless-Software-Foundation/ct-unstable
feat(REST API): Adding and upgrading REST API endpoints
2021-12-06 12:17:04 -08:00
Chris Troutner 70c81af88e Added REST endpoint for ipfs-relays 2021-12-05 12:00:24 -08:00
Chris Troutner cdbb8b18b6 Added REST API endpoint for ipfs/peers 2021-12-04 10:08:30 -08:00
Chris Troutner b3c3bb4ae2 Added ipfs-status endpoint 2021-12-04 08:57:44 -08:00
Chris Troutner 4d291f7310 Refining BCH REST API endpoints 2021-12-04 07:52:22 -08:00
Chris Troutner 8641a4100c Updating gitignore 2021-12-02 14:09:43 -08:00
Chris Troutner 3a30dd6b03 fix(mongo): Changing db name 2021-12-02 14:08:33 -08:00
Chris Troutner 762ed8dd43 Merge branch 'ct-unstable' 2021-12-02 10:25:09 -08:00
Chris Troutner 17914f2937 Removing default service-provider in config file 2021-12-02 10:24:57 -08:00
Chris Troutner 74c6bc5737 Merge pull request #58 from Permissionless-Software-Foundation/ct-unstable
fix(garbage collection): Updating IPFS config for more conservative GC
2021-11-27 15:14:30 -08:00
Chris Troutner bac6c8b1e9 Disabling debug code 2021-11-27 15:13:23 -08:00
Chris Troutner 9e7d955bc4 fix(garbage collection): Updating IPFS config for more conservative GC 2021-11-27 15:12:08 -08:00
17 changed files with 402 additions and 23 deletions
+1
View File
@@ -7,6 +7,7 @@
#logs
logs/*.json
*.log
*.log.*
npm-debug.log*
# Runtime data
+2 -1
View File
@@ -82,7 +82,8 @@ module.exports = {
preferredProvider: process.env.PREFERRED_PROVIDER
? process.env.PREFERRED_PROVIDER
: 'QmdBGB8S6sEr19gaAxJYjhRbz5ZmMiH5a1JyyMmaxcRKnY'
: ''
// : 'QmdBGB8S6sEr19gaAxJYjhRbz5ZmMiH5a1JyyMmaxcRKnY'
// : 'QmUTx6KqYKVZbKpKxR7vGDUgZFYVvVVyEWDeCYq4GwBCff'
// Used for development
}
+1 -1
View File
@@ -7,6 +7,6 @@
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/ipfs-service-dev',
database: 'mongodb://localhost:27017/ipfs-bch-consumer-dev',
env: 'dev'
}
+1 -1
View File
@@ -13,6 +13,6 @@ module.exports = {
// database: 'mongodb://172.17.0.1:5555/ipfs-service-prod',
database: process.env.DBURL
? process.env.DBURL
: 'mongodb://172.17.0.1:5555/ipfs-service-prod',
: 'mongodb://172.17.0.1:5555/ipfs-bch-consumer-prod',
env: 'prod'
}
+1 -1
View File
@@ -7,6 +7,6 @@
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/ipfs-service-test',
database: 'mongodb://localhost:27017/ipfs-bch-consumer-test',
env: 'test'
}
+1 -1
View File
@@ -1,6 +1,6 @@
const common = require('./env/common')
const env = process.env.SVC_ENV || 'development'
const env = process.env.CONSUMER_ENV || 'development'
const config = require(`./env/${env}`)
module.exports = Object.assign({}, common, config)
+5 -5
View File
@@ -6,14 +6,14 @@
"scripts": {
"start": "node index.js",
"test": "npm run test:all",
"test:all": "export SVC_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 --recursive test/unit test/e2e/automated/",
"test:unit": "export SVC_ENV=test && mocha --exit --timeout 15000 --recursive test/unit/",
"test:e2e:auto": "export SVC_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/",
"test:temp": "export SVC_ENV=test && mocha --exit --timeout 15000 -g '#rate-limit' test/unit/json-rpc/",
"test:all": "export CONSUMER_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 --recursive test/unit test/e2e/automated/",
"test:unit": "export CONSUMER_ENV=test && mocha --exit --timeout 15000 --recursive test/unit/",
"test:e2e:auto": "export CONSUMER_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/",
"test:temp": "export CONSUMER_ENV=test && mocha --exit --timeout 15000 -g '#rate-limit' test/unit/json-rpc/",
"lint": "standard --env mocha --fix",
"docs": "./node_modules/.bin/apidoc -i src/ -o docs",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coverage:report": "export SVC_ENV=test && nyc --reporter=html mocha --exit --timeout 15000 --recursive test/unit/ test/e2e/automated/"
"coverage:report": "export CONSUMER_ENV=test && nyc --reporter=html mocha --exit --timeout 15000 --recursive test/unit/ test/e2e/automated/"
},
"author": "Chris Troutner <chris.troutner@gmail.com>",
"license": "MIT",
+13
View File
@@ -77,6 +77,19 @@ class BchAdapter {
}
}
// Choose the BCH wallet service to use.
async selectProvider (providerId) {
try {
this.ipfs.ipfsCoordAdapter.config.preferredProvider = providerId
return true
} catch (err) {
// console.log('createUser() error: ', err)
wlogger.error('Error in use-cases/bch.js/getStatus()')
throw err
}
}
// Get the BCH balance for an array of addresses.
async getBalances (addrs) {
try {
+124
View File
@@ -58,6 +58,130 @@ class IPFS {
throw err
}
}
// Get the status of this IPFS node.
getStatus () {
try {
// console.log(
// 'this.ipfsCoordAdapter.ipfsCoord.thisNode: ',
// this.ipfsCoordAdapter.ipfsCoord.thisNode
// )
const statusObj = {
ipfsId: this.ipfsCoordAdapter.ipfsCoord.thisNode.ipfsId,
multiAddrs: this.ipfsCoordAdapter.ipfsCoord.thisNode.ipfsMultiaddrs,
bchAddr: this.ipfsCoordAdapter.ipfsCoord.thisNode.bchAddr,
slpAddr: this.ipfsCoordAdapter.ipfsCoord.thisNode.slpAddr,
pubKey: this.ipfsCoordAdapter.ipfsCoord.thisNode.pubKey,
peers: this.ipfsCoordAdapter.ipfsCoord.thisNode.peerList.length,
relays: this.ipfsCoordAdapter.ipfsCoord.thisNode.relayData.length
}
return statusObj
} catch (err) {
console.error('Error in ipfs-coord.js/getStatus()')
throw err
}
}
// Get details on the other peers this node is connected to.
async getPeers (showAll) {
try {
const peerData = this.ipfsCoordAdapter.ipfsCoord.thisNode.peerData
// console.log(`peerData: ${JSON.stringify(peerData, null, 2)}`)
let ipfsPeers =
await this.ipfsCoordAdapter.ipfsCoord.adapters.ipfs.getPeers()
// console.log('ipfsPeers: ', ipfsPeers)
ipfsPeers = this._removeDuplicatePeers(ipfsPeers)
// console.log('filtered ipfsPeers: ', ipfsPeers)
// Loop through each IPFS peer and hydrate it with data from the peerData.
for (let i = 0; i < ipfsPeers.length; i++) {
const thisPeer = ipfsPeers[i]
if (!showAll) {
// Delete properties that don't contain good info.
delete thisPeer.muxer
delete thisPeer.latency
delete thisPeer.streams
}
// Get the ipfs-coord peer data for this peer.
let thisPeerData = peerData.filter((x) =>
x.from.includes(thisPeer.peer)
)
thisPeerData = thisPeerData[0]
// Skip if peerData for this IPFS peer could not be found.
if (!thisPeerData) continue
try {
// console.log('thisPeerData: ', thisPeerData)
// Add data to the IPFS peer data.
thisPeer.name = thisPeerData.data.jsonLd.name
thisPeer.protocol = thisPeerData.data.jsonLd.protocol
thisPeer.version = thisPeerData.data.jsonLd.version
if (showAll) {
// Add all the peer data.
thisPeer.peerData = thisPeerData
}
} catch (err) {
console.log(
`Error trying to hydrate peer ${thisPeer.peer}: ${err.message}`
)
}
}
return ipfsPeers
} catch (err) {
console.error('Error in getPeers(): ', err)
throw err
}
}
// Get data about the known Circuit Relays. Hydrate with data from peers list.
getRelays () {
try {
const relayData = this.ipfsCoordAdapter.ipfsCoord.thisNode.relayData
const peerData = this.ipfsCoordAdapter.ipfsCoord.thisNode.peerData
// console.log(`relayData: ${JSON.stringify(relayData, null, 2)}`)
// console.log(`peerData: ${JSON.stringify(peerData, null, 2)}`)
for (let i = 0; i < relayData.length; i++) {
const thisRelay = relayData[i]
// Find the peer that corresponds to this relay.
const thisPeer = peerData.filter((x) =>
x.from.includes(thisRelay.ipfsId)
)
// console.log('thisPeer: ', thisPeer)
// If the peer couldn't be found, skip.
if (!thisPeer.length) {
thisRelay.name = ''
continue
}
thisRelay.name = thisPeer[0].data.jsonLd.name
}
return relayData
} catch (err) {
console.error('Error in getRelays(): ', err)
throw err
}
}
// Expects an array of peers and returns an array of peers with duplicates
// removed.
_removeDuplicatePeers (arr) {
// https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects
return arr.filter((v, i, a) => a.findIndex((t) => t.peer === v.peer) === i)
}
}
module.exports = IPFS
-10
View File
@@ -198,14 +198,4 @@ class IpfsCoordAdapter {
}
}
// Create a random number to use in the name of this IPFS n ode.
// const randNum = Math.floor(Math.random() * 10000)
// const announceJsonLd = {
// '@context': 'https://schema.org/',
// '@type': 'Person',
// name: `wallet-consumer-${randNum}`,
// description: 'A consumer of BCH wallet services'
// }
module.exports = IpfsCoordAdapter
+36 -1
View File
@@ -11,10 +11,13 @@
// Global npm libraries
// const IPFS = require('ipfs')
const IPFS = require('@chris.troutner/ipfs')
const fs = require('fs')
// Local libraries
const config = require('../../../config')
const IPFS_DIR = './.ipfsdata/ipfs'
class IpfsAdapter {
constructor (localConfig) {
// Encapsulate dependencies
@@ -23,6 +26,7 @@ class IpfsAdapter {
// Properties of this class instance.
this.isReady = false
this.config = config
this.fs = fs
}
// Start an IPFS node.
@@ -30,7 +34,7 @@ class IpfsAdapter {
try {
// Ipfs Options
const ipfsOptions = {
repo: './.ipfsdata/ipfs',
repo: IPFS_DIR,
start: true,
config: {
relay: {
@@ -51,6 +55,11 @@ class IpfsAdapter {
`/ip4/0.0.0.0/tcp/${this.config.ipfsTcpPort}`,
`/ip4/0.0.0.0/tcp/${this.config.ipfsWsPort}/ws`
]
},
Datastore: {
StorageMax: '2GB',
StorageGCWatermark: 50,
GCPeriod: '15m'
}
}
}
@@ -71,6 +80,12 @@ class IpfsAdapter {
return this.ipfs
} catch (err) {
console.error('Error in ipfs.js/start()')
// If IPFS crashes because the /blocks directory is full, wipe the directory.
if (err.message.includes('No space left on device')) {
this.rmBlocksDir()
}
throw err
}
}
@@ -80,6 +95,26 @@ class IpfsAdapter {
return true
}
// Remove the '/blocks' directory that is used to store IPFS data.
// Dev Note: It's assumed this node is not pinning any data and that
// everything in this directory is transient. This folder will regularly
// fill up and prevent IPFS from starting.
rmBlocksDir () {
try {
const dir = `${IPFS_DIR}/blocks`
console.log(`Deleting ${dir} directory...`)
this.fs.rmdirSync(dir, { recursive: true })
console.log(`${dir} directory is deleted!`)
return true // Signal successful execution.
} catch (err) {
console.log('Error in rmBlocksDir()')
throw err
}
}
}
module.exports = IpfsAdapter
+20 -2
View File
@@ -1,5 +1,5 @@
/*
REST API Controller library for the /user route
REST API Controller library for the /bch route
*/
const { wlogger } = require('../../../adapters/wlogger')
@@ -51,6 +51,24 @@ class BchRESTControllerLib {
}
}
async postProvider (ctx) {
try {
const providerId = ctx.request.body.provider
await this.adapters.bch.selectProvider(providerId)
const body = {
success: true
}
ctx.body = body
} catch (err) {
wlogger.error('Error in bch/controller.js/postProvider(): ')
// ctx.throw(422, err.message)
this.handleError(ctx, err)
}
}
/**
* @api {post} /bch/balance Balance
* @apiName Balance
@@ -196,7 +214,7 @@ class BchRESTControllerLib {
const utxos = await this.adapters.bch.getUtxos(address)
// console.log(`utxos: ${JSON.stringify(utxos, null, 2)}`)
ctx.body = utxos
ctx.body = [utxos]
} catch (err) {
this.handleError(ctx, err)
}
+5
View File
@@ -52,6 +52,7 @@ class BchRouter {
// Define the routes and attach the controller.
this.router.get('/', this.getStatus)
this.router.post('/provider', this.postProvider)
this.router.post('/balance', this.postBalance)
this.router.post('/utxos', this.postUtxos)
this.router.post('/broadcast', this.postBroadcast)
@@ -68,6 +69,10 @@ class BchRouter {
await _this.bchRESTController.getStatus(ctx, next)
}
async postProvider (ctx, next) {
await _this.bchRESTController.postProvider(ctx, next)
}
async postBalance (ctx, next) {
await _this.bchRESTController.balance(ctx, next)
}
+5
View File
@@ -12,6 +12,7 @@ const UserRouter = require('./users')
const ContactRESTController = require('./contact')
const LogsRESTController = require('./logs')
const BchRESTController = require('./bch')
const IpfsRESTController = require('./ipfs')
class RESTControllers {
constructor (localConfig = {}) {
@@ -57,6 +58,10 @@ class RESTControllers {
// Attach the REST API Controllers associated with the /bch route
const bchRESTController = new BchRESTController(dependencies)
bchRESTController.attach(app)
// Attach the REST API Controllers associated with the /ipfs route
const ipfsRESTController = new IpfsRESTController(dependencies)
ipfsRESTController.attach(app)
}
}
@@ -0,0 +1,98 @@
/*
REST API Controller library for the /ipfs route
*/
const { wlogger } = require('../../../adapters/wlogger')
let _this
class IpfsRESTControllerLib {
constructor (localConfig = {}) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating /ipfs REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating /ipfs REST Controller.'
)
}
// Encapsulate dependencies
// this.UserModel = this.adapters.localdb.Users
// this.userUseCases = this.useCases.user
_this = this
}
/**
* @api {get} /ipfs Get status on IPFS infrastructure
* @apiPermission public
* @apiName GetIpfsStatus
* @apiGroup REST BCH
*
* @apiExample Example usage:
* curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs
*
*/
async getStatus (ctx) {
try {
const status = await _this.adapters.ipfs.getStatus()
ctx.body = { status }
} catch (err) {
wlogger.error('Error in ipfs/controller.js/getStatus(): ')
// ctx.throw(422, err.message)
_this.handleError(ctx, err)
}
}
// Return information on IPFS peers this node is connected to.
async getPeers (ctx) {
try {
const showAll = ctx.request.body.showAll
const peers = await _this.adapters.ipfs.getPeers(showAll)
ctx.body = { peers }
} catch (err) {
wlogger.error('Error in ipfs/controller.js/getPeers(): ')
// ctx.throw(422, err.message)
_this.handleError(ctx, err)
}
}
// Get data about the known Circuit Relays. Hydrate with data from peers list.
async getRelays (ctx) {
try {
const relays = await _this.adapters.ipfs.getRelays()
ctx.body = { relays }
} catch (err) {
wlogger.error('Error in ipfs/controller.js/getRelays(): ')
// ctx.throw(422, err.message)
_this.handleError(ctx, err)
}
}
// DRY error handler
handleError (ctx, err) {
// If an HTTP status is specified by the buisiness logic, use that.
if (err.status) {
if (err.message) {
ctx.throw(err.status, err.message)
} else {
ctx.throw(err.status)
}
} else {
// By default use a 422 error if the HTTP status is not specified.
ctx.throw(422, err.message)
}
}
}
module.exports = IpfsRESTControllerLib
+68
View File
@@ -0,0 +1,68 @@
/*
REST API library for the /ipfs route.
*/
// Public npm libraries.
const Router = require('koa-router')
// Local libraries.
const IPFSRESTControllerLib = require('./controller')
const Validators = require('../middleware/validators')
// let _this
class BchRouter {
constructor (localConfig = {}) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating IPFS REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating IPFS REST Controller.'
)
}
const dependencies = {
adapters: this.adapters,
useCases: this.useCases
}
// Encapsulate dependencies.
this.ipfsRESTController = new IPFSRESTControllerLib(dependencies)
this.validators = new Validators()
// Instantiate the router and set the base route.
const baseUrl = '/ipfs'
this.router = new Router({ prefix: baseUrl })
// _this = this
}
attach (app) {
if (!app) {
throw new Error(
'Must pass app object when attaching REST API controllers.'
)
}
// Define the routes and attach the controller.
this.router.get('/', this.ipfsRESTController.getStatus)
this.router.post('/peers', this.ipfsRESTController.getPeers)
this.router.post('/relays', this.ipfsRESTController.getRelays)
// Attach the Controller routes to the Koa app.
app.use(this.router.routes())
app.use(this.router.allowedMethods())
}
// async getStatus (ctx, next) {
// await _this.ipfsRESTController.getStatus(ctx, next)
// }
}
module.exports = BchRouter
+21
View File
@@ -60,4 +60,25 @@ describe('#IPFS-adapter', () => {
assert.equal(result, true)
})
})
describe('#rmBlocksDir', () => {
it('should delete the /blocks directory', () => {
const result = uut.rmBlocksDir()
assert.equal(result, true)
})
it('should catch and throw an error', () => {
try {
// Force an error
sandbox.stub(uut.fs, 'rmdirSync').throws(new Error('test error'))
uut.rmBlocksDir()
assert.fail('Unexpected code path')
} catch (err) {
assert.equal(err.message, 'test error')
}
})
})
})