Skip to content

Commit

Permalink
Replace dscp-api with own runProcess (#50)
Browse files Browse the repository at this point in the history
* wip

* tests passing

* file size limit

* v bump

* removing anys

* replace more anys

* add ipfs 500 test

* add polkadot types package

* cleanup seeds

* add comment

* remove file upload limit

* user uri as constructor arg

* run process error

* ipfs as class

* revert generic demand creation

* use order creation route

* pass IPFS envs to chainnode

* port defaults
  • Loading branch information
jonmattgray authored Apr 12, 2023
1 parent 78a2bcb commit 8e8c9e2
Show file tree
Hide file tree
Showing 21 changed files with 541 additions and 291 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ A `Node.js` typescript template with open api implementation

Use a `.env` at root of the repository to set values for the environment variables defined in `.env` file.

| variable | required | default | description |
| :-------------------- | :------: | :--------------------: | :----------------------------------------------------------------------------------- |
| PORT | N | `3000` | The port for the API to listen on |
| LOG_LEVEL | N | `debug` | Logging level. Valid values are [`trace`, `debug`, `info`, `warn`, `error`, `fatal`] |
| ENVIRONMENT_VAR | N | `example` | An environment specific variable |
| DB_PORT | N | `5432` | The port for the database |
| DB_HOST | Y | - | The database hostname / host |
| DB_NAME | N | `dscp-matchmaker-api ` | The database name |
| DB_USERNAME | Y | - | The database username |
| DB_PASSWORD | Y | - | The database password |
| IDENTITY_SERVICE_HOST | Y | - | Hostname of the `dscp-identity-service` |
| IDENTITY_SERVICE_PORT | Y | - | Port of the `dscp-identity-service` |
| DSCP_API_HOST | Y | - | Hostname of the `dscp-api` |
| DSCP_API_PORT | Y | - | Port of the `dscp-api` |
| variable | required | default | description |
| :-------------------- | :------: | :--------------------: | :------------------------------------------------------------------------------------------- |
| PORT | N | `3000` | The port for the API to listen on |
| LOG_LEVEL | N | `debug` | Logging level. Valid values are [`trace`, `debug`, `info`, `warn`, `error`, `fatal`] |
| ENVIRONMENT_VAR | N | `example` | An environment specific variable |
| DB_PORT | N | `5432` | The port for the database |
| DB_HOST | Y | - | The database hostname / host |
| DB_NAME | N | `dscp-matchmaker-api ` | The database name |
| DB_USERNAME | Y | - | The database username |
| DB_PASSWORD | Y | - | The database password |
| IDENTITY_SERVICE_HOST | Y | - | Hostname of the `dscp-identity-service` |
| IDENTITY_SERVICE_PORT | N | `3000` | Port of the `dscp-identity-service` |
| NODE_HOST | Y | - | The hostname of the `dscp-node` the API should connect to |
| NODE_PORT | N | `9944` | The port of the `dscp-node` the API should connect to |
| LOG_LEVEL | N | `info` | Logging level. Valid values are [`trace`, `debug`, `info`, `warn`, `error`, `fatal`] |
| USER_URI | Y | - | The Substrate `URI` representing the private key to use when making `dscp-node` transactions |
| IPFS_HOST | Y | - | Hostname of the `IPFS` node to use for metadata storage |
| IPFS_PORT | N | `5001` | Port of the `IPFS` node to use for metadata storage |

## Getting started

Expand Down
16 changes: 0 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ services:
- 9933:9933
restart: on-failure

dscp-api:
image: digicatapult/dscp-api:latest
container_name: dscp-api
ports:
- 3001:3001
environment:
- PORT=3001
- API_HOST=dscp-node
- API_PORT=9944
- USER_URI=//Alice
- IPFS_HOST=ipfs
- IPFS_PORT=5001
- LOG_LEVEL=trace
- AUTH_TYPE=${AUTH_TYPE:-NONE}
restart: on-failure

ipfs:
image: ipfs/go-ipfs:v0.18.1
container_name: ipfs
Expand Down
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/dscp-matchmaker-api",
"version": "0.5.11",
"version": "0.6.0",
"description": "An OpenAPI Matchmaking API service for DSCP",
"main": "src/index.ts",
"scripts": {
Expand Down Expand Up @@ -34,6 +34,7 @@
},
"homepage": "https://github.com/digicatapult/dscp-matchmaker-api#readme",
"devDependencies": {
"@polkadot/types": "^10.2.2",
"@types/chai": "^4.3.4",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"dependencies": {
"@polkadot/api": "^10.2.2",
"base-x": "^4.0.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/attachment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class attachment extends Controller {

if (!req.body && !file) throw new BadRequest('nothing to upload')

const [{ id, filename, binary_blob, created_at }]: any[] = await this.db
const [{ id, filename, binary_blob, created_at }] = await this.db
.attachment()
.insert({
filename: file ? file.originalname : 'json',
Expand Down
15 changes: 13 additions & 2 deletions src/controllers/capacity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@ import { BadRequest, NotFound } from '../../lib/error-handler/index'
import { getMemberByAddress, getMemberBySelf } from '../../lib/services/identity'
import { TransactionResponse, TransactionState, TransactionApiType, TransactionType } from '../../models/transaction'
import { DEMAND } from '../../models/tokenType'
import { runProcess } from '../..//lib/services/dscpApi'
import { demandCreate } from '../../lib/payload'
import { observeTokenId } from '../../lib/services/blockchainWatcher'
import ChainNode from '../../lib/chainNode'
import env from '../../env'

@Route('capacity')
@Tags('capacity')
@Security('bearerAuth')
export class CapacityController extends Controller {
log: Logger
db: Database
node: ChainNode

constructor() {
super()
this.log = logger.child({ controller: '/capacity' })
this.db = new Database()
this.node = new ChainNode({
host: env.NODE_HOST,
port: env.NODE_PORT,
logger,
userUri: env.USER_URI,
ipfsHost: env.IPFS_HOST,
ipfsPort: env.IPFS_PORT,
})
}

/**
Expand Down Expand Up @@ -114,7 +125,7 @@ export class CapacityController extends Controller {
})

// temp - until there is a blockchain watcher, need to await runProcess to know token IDs
const [tokenId] = await runProcess(demandCreate(capacity))
const [tokenId] = await this.node.runProcess(demandCreate(capacity))
await this.db.updateTransaction(transaction.id, { state: TransactionState.finalised })

// demand-create returns a single token ID
Expand Down
18 changes: 14 additions & 4 deletions src/controllers/match2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@ import { UUID } from '../../models/uuid'
import { TransactionResponse, TransactionState, TransactionType, TransactionApiType } from '../../models/transaction'
import { MATCH2, DEMAND } from '../../models/tokenType'
import { observeTokenId } from '../../lib/services/blockchainWatcher'
import { runProcess } from '../../lib/services/dscpApi'
import { match2AcceptFinal, match2AcceptFirst, match2Propose } from '../../lib/payload'
import { DemandPayload, DemandState, DemandSubtype } from '../../models/demand'
import ChainNode from '../../lib/chainNode'
import env from '../../env'

@Route('match2')
@Tags('match2')
@Security('bearerAuth')
export class Match2Controller extends Controller {
log: Logger
db: Database
node: ChainNode

constructor() {
super()
this.log = logger.child({ controller: '/match2' })
this.db = new Database()
this.node = new ChainNode({
host: env.NODE_HOST,
port: env.NODE_PORT,
logger,
userUri: env.USER_URI,
ipfsHost: env.IPFS_HOST,
ipfsPort: env.IPFS_PORT,
})
}

/**
Expand Down Expand Up @@ -123,7 +133,7 @@ export class Match2Controller extends Controller {
})

// temp - until there is a blockchain watcher, need to await runProcess to know token IDs
const tokenIds = await runProcess(match2Propose(match2, demandA, demandB))
const tokenIds = await this.node.runProcess(match2Propose(match2, demandA, demandB))
await this.db.updateTransaction(transaction.id, { state: TransactionState.finalised })

// match2-propose returns 3 token IDs
Expand Down Expand Up @@ -205,7 +215,7 @@ export class Match2Controller extends Controller {
const newState = ownsDemandA ? Match2State.acceptedA : Match2State.acceptedB

// temp - until there is a blockchain watcher, need to await runProcess to know token IDs
const [tokenId] = await runProcess(match2AcceptFirst(match2, newState, demandA, demandB))
const [tokenId] = await this.node.runProcess(match2AcceptFirst(match2, newState, demandA, demandB))
await this.db.updateTransaction(transaction.id, { state: TransactionState.finalised })

await observeTokenId(MATCH2, match2.id, newState, tokenId, false)
Expand All @@ -222,7 +232,7 @@ export class Match2Controller extends Controller {
})

// temp - until there is a blockchain watcher, need to await runProcess to know token IDs
const tokenIds = await runProcess(match2AcceptFinal(match2, demandA, demandB))
const tokenIds = await this.node.runProcess(match2AcceptFinal(match2, demandA, demandB))
await this.db.updateTransaction(transaction.id, { state: TransactionState.finalised })

// match2-acceptFinal returns 3 token IDs
Expand Down
14 changes: 12 additions & 2 deletions src/controllers/order/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@ import { logger } from '../../lib/logger'
import { BadRequest, NotFound } from '../../lib/error-handler'
import Database from '../../lib/db'
import { getMemberByAddress, getMemberBySelf } from '../../lib/services/identity'
import { runProcess } from '../..//lib/services/dscpApi'
import { observeTokenId } from '../../lib/services/blockchainWatcher'
import { demandCreate } from '../../lib/payload'
import ChainNode from '../../lib/chainNode'
import env from '../../env'

@Route('order')
@Tags('order')
@Security('bearerAuth')
export class order extends Controller {
log: Logger
db: Database
node: ChainNode

constructor() {
super()
this.log = logger.child({ controller: '/order' })
this.db = new Database()
this.node = new ChainNode({
host: env.NODE_HOST,
port: env.NODE_PORT,
logger,
userUri: env.USER_URI,
ipfsHost: env.IPFS_HOST,
ipfsPort: env.IPFS_PORT,
})
}

/**
Expand Down Expand Up @@ -119,7 +129,7 @@ export class order extends Controller {
state: TransactionState.submitted,
})

const [tokenId] = await runProcess(demandCreate(order))
const [tokenId] = await this.node.runProcess(demandCreate(order))
await this.db.updateTransaction(transaction.id, { state: TransactionState.finalised })

// demand-create returns a single token ID
Expand Down
7 changes: 4 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export default envalid.cleanEnv(process.env, {
DB_PASSWORD: envalid.str({ devDefault: 'postgres' }),
DB_NAME: envalid.str({ default: 'dscp-matchmaker-api' }),
IDENTITY_SERVICE_HOST: envalid.host({ devDefault: 'localhost' }),
IDENTITY_SERVICE_PORT: envalid.port({ devDefault: 3002 }),
DSCP_API_HOST: envalid.host({ devDefault: 'localhost' }),
DSCP_API_PORT: envalid.port({ devDefault: 3001 }),
IDENTITY_SERVICE_PORT: envalid.port({ devDefault: 3002, default: 3000 }),
NODE_HOST: envalid.host({ default: 'localhost' }),
NODE_PORT: envalid.port({ default: 9944 }),
ENABLE_INDEXER: envalid.bool({ default: false }),
USER_URI: envalid.str({ devDefault: '//Alice' }),
IPFS_HOST: envalid.host({ devDefault: 'localhost' }),
IPFS_PORT: envalid.port({ default: 5001 }),
})
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import ChainNode from './lib/chainNode'
host: env.NODE_HOST,
port: env.NODE_PORT,
logger,
userUri: env.USER_URI,
ipfsHost: env.IPFS_HOST,
ipfsPort: env.IPFS_PORT,
})

const handleBlock = () => Promise.resolve({})
Expand Down
Loading

0 comments on commit 8e8c9e2

Please sign in to comment.