Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Feature/manual seal (#44)
Browse files Browse the repository at this point in the history
* Add support in tests for using manual seal

* 0.7.0

* Linting fix

* Update workflows to use correct compose file
  • Loading branch information
mattdean-digicatapult authored Dec 19, 2023
1 parent 5fa4b7a commit 7e251dc
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
run: npm run build
- name: Setup dependencies
if: matrix.test-type != 'unit'
run: docker-compose up -d
run: docker-compose -f ./docker-compose-test.yml up -d
- name: Sleep
if: matrix.test-type != 'unit'
uses: kibertoad/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
run: npm run build
- name: Setup dependencies
if: matrix.test-type != 'unit'
run: docker-compose up -d
run: docker-compose -f ./docker-compose-test.yml up -d
- name: Sleep
if: matrix.test-type != 'unit'
uses: kibertoad/[email protected]
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,29 @@ npx knex migrate:make --knexfile src/lib/db/knexfile.ts attachment-table

## Tests

Integration tests are executed by calling:
Unit tests are executed by calling:

```sh
npm run test
npm run test:unit
```

Unit tests are executed by calling:
Integration tests require the test dependency services be brought up using docker:

```sh
npm run test:unit
# start dependencies
docker compose -f ./docker-compose-test.yml up -d
# install packages
npm ci
# run migrations
npm run db:migrate
# put process flows on-chain
npm run flows
```

Integration tests are then executed by calling:

```sh
npm run test
```

## Process Flows
Expand Down
78 changes: 78 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
version: '3'

services:
postgres-hyproof-api:
image: postgres:16.1-alpine
container_name: postgres-hyproof-api
ports:
- 5432:5432
volumes:
- hyproof-api-storage:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=dscp-hyproof-api

postgres-identity-service:
image: postgres:16.1-alpine
container_name: postgres-identity
ports:
- 5433:5432
volumes:
- identity-storage:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=dscp-identity-service

dscp-identity-service:
image: digicatapult/dscp-identity-service:latest
container_name: identity-service
command: /bin/sh -c "
sleep 30 &&
npx knex migrate:latest &&
node app/index.js"
ports:
- 3002:3002
environment:
- PORT=3002
- API_HOST=dscp-node
- DB_HOST=postgres-identity-service
- DB_PORT=5432
- DB_NAME=dscp-identity-service
- DB_USERNAME=postgres
- DB_PASSWORD=postgres
- SELF_ADDRESS=5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
- AUTH_TYPE=${AUTH_TYPE:-NONE}

dscp-node:
image: digicatapult/dscp-node:latest
container_name: node
command: --base-path /data/
--dev
--manual-seal
--unsafe-ws-external
--unsafe-rpc-external
--ws-max-connections 512
--rpc-cors all
ports:
- 30333:30333
- 9944:9944
- 9933:9933
restart: on-failure

ipfs:
image: ipfs/go-ipfs:v0.24.0
container_name: ipfs
environment:
- |
IPFS_SWARM_KEY=/key/swarm/psk/1.0.0/
/base16/
0000000000000000000000000000000000000000000000000000000000000000
ports:
- 4001:4001
- 8080:8080
- 5001:5001
volumes:
hyproof-api-storage:
identity-storage:
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/dscp-hyproof-api",
"version": "0.6.5",
"version": "0.7.0",
"description": "An OpenAPI API service for DSCP",
"main": "src/index.ts",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/chainNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,8 @@ export default class ChainNode {
roles,
}
}

async sealBlock(createEmpty: boolean = true, finalise: boolean = true) {
return await this.api.rpc.engine.createBlock(createEmpty, finalise)
}
}
44 changes: 22 additions & 22 deletions test/helpers/chainTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,28 @@ import { logger } from '../../src/lib/logger'
import { put } from './routeHelper'
import { mockEnv, notSelfAddress, regulatorAddress, selfAddress } from './mock'
import { processInitiateCert, processIssueCert } from '../../src/lib/payload'
import { SubmittableExtrinsic } from '@polkadot/api/types'
import { SubmittableResult } from '@polkadot/api'

const db = new Database()

const submitRunProcessExtrinsicAndSeal = async (
node: ChainNode,
extrinsic: SubmittableExtrinsic<'promise', SubmittableResult>
): Promise<number[]> => {
return new Promise<number[]>((resolve, reject) => {
node
.submitRunProcess(extrinsic, (state, outputs) => {
if (state === 'finalised') {
setTimeout(() => resolve(outputs ? outputs : []), 100)
} else if (state === 'failed') reject()
})
.then(() => {
node.sealBlock()
})
})
}

export const withAppAndIndexer = (context: { app: Express; indexer: Indexer }) => {
before(async function () {
context.app = await createHttpServer()
Expand Down Expand Up @@ -64,13 +83,7 @@ export const withInitialisedCertFromNotSelf = async (context: { app: Express; db
} as CertificateRow)
)

const tokenId = await new Promise<number>((resolve, reject) => {
node.submitRunProcess(extrinsic, (state, outputs) => {
if (state === 'finalised') {
setTimeout(() => resolve(outputs ? outputs[0] : Number.NaN), 100)
} else if (state === 'failed') reject()
})
})
const [tokenId] = await submitRunProcessExtrinsicAndSeal(node, extrinsic)

const [{ id }] = await db.get('certificate', { latest_token_id: tokenId })

Expand Down Expand Up @@ -107,13 +120,7 @@ export const withIssuedCertAsRegulator = async (context: { app: Express; db: Dat
} as CertificateRow)
)

const initTokenId = await new Promise<number>((resolve, reject) => {
heidiNode.submitRunProcess(initExtrinsic, (state, outputs) => {
if (state === 'finalised') {
setTimeout(() => resolve(outputs ? outputs[0] : Number.NaN), 100)
} else if (state === 'failed') reject()
})
})
const [initTokenId] = await submitRunProcessExtrinsicAndSeal(heidiNode, initExtrinsic)

const emmaNode = new ChainNode(
mockEnv({
Expand All @@ -133,14 +140,7 @@ export const withIssuedCertAsRegulator = async (context: { app: Express; db: Dat
} as CertificateRow)
)

const issuedTokenId = await new Promise<number>((resolve, reject) => {
emmaNode.submitRunProcess(issueExtrinsic, (state, outputs) => {
if (state === 'finalised') {
setTimeout(() => resolve(outputs ? outputs[0] : Number.NaN), 100)
} else if (state === 'failed') reject()
})
})

const [issuedTokenId] = await submitRunProcessExtrinsicAndSeal(emmaNode, issueExtrinsic)
const [cert] = await db.get('certificate', { latest_token_id: issuedTokenId })
context.cert = cert
}
4 changes: 4 additions & 0 deletions test/integration/onchain/certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('on-chain', function () {
)
expect(state).to.equal('submitted')

await node.sealBlock()
await pollTransactionState(db, transactionId, 'finalised')

const [cert] = await db.get('certificate', { id: certId })
Expand Down Expand Up @@ -112,6 +113,7 @@ describe('on-chain', function () {
)
expect(state).to.equal('submitted')

await node.sealBlock()
await pollTransactionState(db, transactionId, 'finalised')

const [cert] = await db.get('certificate', { id: context.cert.id })
Expand All @@ -138,6 +140,7 @@ describe('on-chain', function () {
)
expect(state).to.equal('submitted')

await node.sealBlock()
await pollTransactionState(db, transactionId, 'finalised')

const [cert] = await db.get('certificate', { id: context.cert.id })
Expand Down Expand Up @@ -166,6 +169,7 @@ describe('on-chain', function () {
)
expect(state).to.equal('submitted')

await node.sealBlock()
await pollTransactionState(db, transactionId, 'finalised')

const [cert] = await db.get('certificate', { id: context.cert.id })
Expand Down
1 change: 1 addition & 0 deletions test/integration/onchain/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('on-chain', function () {
node.submitRunProcess(extrinsic, (state) => db.update('transaction', { id: transaction.id }, { state }))

// wait for dispatch error
await node.sealBlock()
const failedTransaction = await pollTransactionState(db, transaction.id, 'failed')
expect(failedTransaction.state).to.equal('failed')
})
Expand Down

0 comments on commit 7e251dc

Please sign in to comment.